From 669cbe6a577865d0c4646082617a12e587931ad7 Mon Sep 17 00:00:00 2001 From: Jeffrey Forman Date: Wed, 9 Jan 2013 20:09:29 -0500 Subject: [PATCH] Add config parser module to make specifying hosts easy --- m2g.conf.example | 13 +++++++++++ m2g_configparser.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 m2g.conf.example create mode 100755 m2g_configparser.py diff --git a/m2g.conf.example b/m2g.conf.example new file mode 100644 index 0000000..b7f7e99 --- /dev/null +++ b/m2g.conf.example @@ -0,0 +1,13 @@ +[host1.domain1.local] +address: host1.domain1.local + +[mta.domain1.local] +graphitename: smtp + +[www.domain1.local] +address: server1.domain1.local + +[mta2.domain1.local] +graphitename: smtp2 + +[jabber.domain1.local] diff --git a/m2g_configparser.py b/m2g_configparser.py new file mode 100755 index 0000000..1cc7b5e --- /dev/null +++ b/m2g_configparser.py @@ -0,0 +1,53 @@ +#!/usr/bin/env python + +import ConfigParser +import sys + +class M2GConfigParser(): + def __init__(self, config_path): + self._config_path = config_path + self._config_dict = {} # The entire file parsed into a dictionary. + self._graphite_config = {} # The data we'll use for Munin queries. + self._parsed_config = ConfigParser.SafeConfigParser() + self._parsed_config.read(self._config_path) + + def parse_config(self): + """Parse out all the config's data.""" + self.set_config_dict() + self.set_host_list() + + def set_config_dict(self): + """Populate the object's dictionary from the config file.""" + sections = self._parsed_config.sections() + for current_section in sections: + options = self._parsed_config.options(current_section) + self._config_dict[current_section] = {} + for current_option in options: + self._config_dict[current_section][current_option] = self._parsed_config.get(current_section, current_option) + + def get_config_dict(self): + return self._config_dict + + def set_host_list(self): + """From a parsed config, list the computed Graphite host list.""" + for section in self._config_dict: + self._graphite_config[section] = {} + # Set Hostname to query first + if "address" in self._config_dict[section]: + self._graphite_config[section]["address"] = self._config_dict[section]["address"] + else: + self._graphite_config[section]["address"] = section + + # Set graphitename to display in Graphite + if "graphitename" in self._config_dict[section]: + self._graphite_config[section]["graphitename"] = self._config_dict[section]["graphitename"] + else: + self._graphite_config[section]["graphitename"] = section.split(".")[0] + + def get_graphite_config(self): + return self._graphite_config + +if __name__ == "__main__": + myconfig = M2GConfigParser(sys.argv[1]) + myconfig.parse_config() + print "Graphite Config: %r" % myconfig.get_graphite_config()