From cf9c34812219c3417d9e4d8af4cafaedd1e8988c Mon Sep 17 00:00:00 2001 From: Christos Triantafyllidis Date: Tue, 12 Jun 2012 00:49:17 +0300 Subject: Initial GIT import --- DNC.py | 31 ++++++++++++++++++++++++++ DNC.yml | 12 ++++++++++ dyn_module.py | 26 +++++++++++++++++++++ modules/datacenter_definitions.py | 10 +++++++++ modules/datacenter_definitions/file.py | 12 ++++++++++ modules/host_definitions.py | 10 +++++++++ modules/host_definitions/fedora_infra_hosts.py | 14 ++++++++++++ modules/host_definitions/file.py | 12 ++++++++++ modules/nagios_config.py | 31 ++++++++++++++++++++++++++ sample_configs/datacenter_definitions.yml | 2 ++ sample_configs/hosts_definitions.yml | 2 ++ sample_configs/infra-hosts/README | 4 ++++ templates/host | 7 ++++++ 13 files changed, 173 insertions(+) create mode 100755 DNC.py create mode 100644 DNC.yml create mode 100644 dyn_module.py create mode 100644 modules/datacenter_definitions.py create mode 100644 modules/datacenter_definitions/file.py create mode 100644 modules/host_definitions.py create mode 100644 modules/host_definitions/fedora_infra_hosts.py create mode 100644 modules/host_definitions/file.py create mode 100644 modules/nagios_config.py create mode 100644 sample_configs/datacenter_definitions.yml create mode 100644 sample_configs/hosts_definitions.yml create mode 100644 sample_configs/infra-hosts/README create mode 100644 templates/host diff --git a/DNC.py b/DNC.py new file mode 100755 index 0000000..eb61acd --- /dev/null +++ b/DNC.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +import yaml +import optparse +import sys + +import dyn_module + +host_definitions = dyn_module.load_module("modules/host_definitions.py") + +if __name__=="__main__": + parser = optparse.OptionParser("usage: %prog [options]") + parser.add_option("-c", "--config", dest="config_file", + default="DNC.yml", type="string", + help="specify the main configuration file") + parser.add_option("-o", "--output", dest="output_folder", + default="output", type="string", + help="specify where results will be stored") + + (options, args) = parser.parse_args() + config_file = options.config_file + + +config_stream = file(config_file, 'r') +config = yaml.load(config_stream) + +defaults = config["modules"]["nagios_config"] + +hosts = dyn_module.load_module("modules/host_definitions.py").get(config["modules"]["host_definitions"]) +datacenters = dyn_module.load_module("modules/datacenter_definitions.py").get(config["modules"]["datacenter_definitions"]) + +dyn_module.load_module("modules/nagios_config.py").generate(defaults,hosts, datacenters) diff --git a/DNC.yml b/DNC.yml new file mode 100644 index 0000000..f4b456d --- /dev/null +++ b/DNC.yml @@ -0,0 +1,12 @@ +modules: + nagios_config: + default_host_use: defaulttemplate + datacenter_definitions: + file: + file: ./sample_configs/datacenter_definitions.yml + host_definitions: + file: + file: ./sample_configs/hosts_definitions.yml + fedora_infra_hosts: + path: ./sample_configs/infra-hosts + \ No newline at end of file diff --git a/dyn_module.py b/dyn_module.py new file mode 100644 index 0000000..e12d744 --- /dev/null +++ b/dyn_module.py @@ -0,0 +1,26 @@ +#!/usr/bin/env python +import md5 +import os.path +import imp +import traceback +import sys + +def load_module(code_path): + try: + try: + code_dir = os.path.dirname(code_path) + code_file = os.path.basename(code_path) + + fin = open(code_path, 'rb') + + return imp.load_source(md5.new(code_path).hexdigest(), code_path, fin) + finally: + try: fin.close() + except: pass + except ImportError, x: + traceback.print_exc(file = sys.stderr) + raise + except: + traceback.print_exc(file = sys.stderr) + raise + diff --git a/modules/datacenter_definitions.py b/modules/datacenter_definitions.py new file mode 100644 index 0000000..a71ecfd --- /dev/null +++ b/modules/datacenter_definitions.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import dyn_module + +def get(config): + result = dict() + for dc_def_module in config: + dc_def_mod = dyn_module.load_module("modules/datacenter_definitions/%s.py" % dc_def_module) + dc_def_mod.get(config[dc_def_module],result) + + return result diff --git a/modules/datacenter_definitions/file.py b/modules/datacenter_definitions/file.py new file mode 100644 index 0000000..d66b49d --- /dev/null +++ b/modules/datacenter_definitions/file.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +import yaml + +def get(config,result): + dc_config = yaml.load(file(config["file"], 'r')) + for dc in dc_config: + if result.has_key(dc): + result[dc].update(dc_config[dc]) + else: + result[dc] = dc_config[dc] + + return result \ No newline at end of file diff --git a/modules/host_definitions.py b/modules/host_definitions.py new file mode 100644 index 0000000..3026986 --- /dev/null +++ b/modules/host_definitions.py @@ -0,0 +1,10 @@ +#!/usr/bin/env python +import dyn_module + +def get(config): + result = dict() + for host_def_module in config: + host_def_mod = dyn_module.load_module("modules/host_definitions/%s.py" % host_def_module) + host_def_mod.get(config[host_def_module],result) + + return result diff --git a/modules/host_definitions/fedora_infra_hosts.py b/modules/host_definitions/fedora_infra_hosts.py new file mode 100644 index 0000000..6ac1878 --- /dev/null +++ b/modules/host_definitions/fedora_infra_hosts.py @@ -0,0 +1,14 @@ +#!/usr/bin/env python +import os + +def get(config,result): + for host in [ name for name in os.listdir(config["path"]) if os.path.isdir(os.path.join(config["path"], name)) ]: + if not result.has_key(host): + result[host] = dict() + path = os.path.join(config["path"], host, "autoinfo") + if os.path.exists(os.path.join(path, "datacenter")): + result[host]["datacenter"] = open(os.path.join(path, "datacenter"),"r").read() + if os.path.exists(os.path.join(path, "distro")): + result[host]["distro"] = open(os.path.join(path, "distro"),"r").read() + + return result diff --git a/modules/host_definitions/file.py b/modules/host_definitions/file.py new file mode 100644 index 0000000..3466db3 --- /dev/null +++ b/modules/host_definitions/file.py @@ -0,0 +1,12 @@ +#!/usr/bin/env python +import yaml + +def get(config,result): + hosts_config = yaml.load(file(config["file"], 'r')) + for host in hosts_config: + if result.has_key(host): + result[host].update(hosts_config[host]) + else: + result[host] = hosts_config[host] + + return result \ No newline at end of file diff --git a/modules/nagios_config.py b/modules/nagios_config.py new file mode 100644 index 0000000..d7668f9 --- /dev/null +++ b/modules/nagios_config.py @@ -0,0 +1,31 @@ +#!/usr/bin/env python +from string import Template + +def generate(defaults,hosts,datacenters): + host_template = Template(file("templates/host", 'r').read()) + for host in hosts: + hostdict = {"hostname": host} + if hosts[host].has_key("parents"): + hostdict["parents"] = 'parents ' + hosts[host]["parents"] + elif hosts[host].has_key("datacenter") and datacenters.has_key(hosts[host]["datacenter"]): + hostdict["parents"] = 'parents ' + datacenters[hosts[host]["datacenter"]]["gateway"] + else: + hostdict["parents"] = '' + + if hosts[host].has_key("use"): + hostdict["use"] = hosts[host]["use"] + else: + hostdict["use"] = defaults["default_host_use"] + + if hosts[host].has_key("alias"): + hostdict["host_alias"] = hosts[host]["alias"] + else: + hostdict["host_alias"] = host + + if hosts[host].has_key("address"): + hostdict["host_address"] = hosts[host]["address"] + else: + hostdict["host_address"] = host + + print host_template.substitute(hostdict) + \ No newline at end of file diff --git a/sample_configs/datacenter_definitions.yml b/sample_configs/datacenter_definitions.yml new file mode 100644 index 0000000..7a06bb1 --- /dev/null +++ b/sample_configs/datacenter_definitions.yml @@ -0,0 +1,2 @@ +phx: + gateway: phx-gw diff --git a/sample_configs/hosts_definitions.yml b/sample_configs/hosts_definitions.yml new file mode 100644 index 0000000..c7c142f --- /dev/null +++ b/sample_configs/hosts_definitions.yml @@ -0,0 +1,2 @@ +proxy02.fedoraproject.org: + use: testtemplate diff --git a/sample_configs/infra-hosts/README b/sample_configs/infra-hosts/README new file mode 100644 index 0000000..d557e62 --- /dev/null +++ b/sample_configs/infra-hosts/README @@ -0,0 +1,4 @@ +This folder should contain an export of the infra-hosts repository of fedora infrastructure. + +A public export can be found at: +http://infrastructure.fedoraproject.org/infra/hosts/ \ No newline at end of file diff --git a/templates/host b/templates/host new file mode 100644 index 0000000..5b39318 --- /dev/null +++ b/templates/host @@ -0,0 +1,7 @@ +define host{ + host_name $hostname + alias $host_alias + address $host_address + use $use + $parents +} \ No newline at end of file -- cgit