#!/usr/bin/python # # Arnaud Gomes 2009 # # $Id: gen-host,v 1.12 2009/08/25 09:44:54 gomes Exp $ import ldap import sys server = "nsldap.ircam.fr" basedn = "dc=ircam,dc=fr" url = "ldap://" + server #################################################################### # Templates def host_cfg(name, ip, alias=None): c = """define host{ host_name """ + name + "\n" if alias: c = c + " alias " + alias + "\n" c = c + " address " + ip + """ check_command check-host-alive max_check_attempts 1 check_period 24x7 contact_groups admins notification_interval 30 notification_period 24x7 notification_options u,r,f } """ return c def srv_cfg(name, command, descr=None): c = """define service{ host_name """ + name + "\n" if descr: c = c + " service_description " + descr + "\n" c = c + " check_command " + command + """ max_check_attempts 1 normal_check_interval 5 retry_check_interval 1 check_period 24x7 notification_interval 60 notification_period 24x7 notification_options w,c,r,f contact_groups admins } """ return c #################################################################### l = ldap.initialize(url) # Service definitions r = l.search_s(basedn, ldap.SCOPE_SUBTREE, '(objectClass=nagiosServiceType)', ["cn", "command", "description"]) srv = {} for dn, entry in r: cn = entry['cn'][0] command = entry['command'][0] srv[cn] = {"command": command} if entry.has_key('description'): srv[cn]['descr'] = entry['description'][0] # Hosts and services r = l.search_s(basedn, ldap.SCOPE_SUBTREE, '(objectClass=nagiosHost)', ["cn", "ipHostNumber", "nagiosService", "nagiosName"]) h = open("hosts.cfg", "w") s = open("services.cfg", "w") for f in [h, s]: f.write("#### This file was generated by " + sys.argv[0] + ".\n#### Do not edit.\n\n") for dn, entry in r: name = entry['cn'][0] ip = entry['ipHostNumber'][0] if entry.has_key('nagiosName'): alias = entry['nagiosName'][0] else: alias = None h.write(host_cfg(name, ip, alias)) if entry.has_key('nagiosService'): for ns in entry['nagiosService']: if srv[ns].has_key('descr'): descr = srv[ns]['descr'] else: descr = None s.write(srv_cfg(name, srv[ns]['command'], descr)) for f in [h, s]: f.write("### EOF\n") h.close() s.close()