1 #!/usr/bin/python
3 #import base64
4 import datetime
5 import libxml2
6 import optparse
7 import socket
8 import sys
10 parser = optparse.OptionParser()
12 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
13 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
14 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
15 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
16 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
17 parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
18 parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
19 parser.add_option('', '--host-template', dest='tmpl_host', help='Filename of host template')
20 parser.add_option('', '--service-template', dest='tmpl_service', help='Filename of service template')
21 parser.add_option('-O', '', dest='output', help='Output "hosts", "services" or "both" (default)')
22 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
24 parser.set_defaults(url=None)
25 parser.set_defaults(httpuser=None)
26 parser.set_defaults(httppasswd=None)
27 parser.set_defaults(file='nagixsc.xml')
28 parser.set_defaults(schemacheck='')
29 parser.set_defaults(host=None)
30 parser.set_defaults(service=None)
31 parser.set_defaults(output=None)
32 parser.set_defaults(tmpl_host=None)
33 parser.set_defaults(tmpl_service=None)
34 parser.set_defaults(verb=0)
36 (options, args) = parser.parse_args()
38 # Hard coded default for host template
39 HOSTTEMPL='''define host {
40 use templ_host_default
42 host_name %(host_name)s
43 address %(address)s
44 }
45 '''
47 # Hard coded default for service template
48 SERVICETEMPL='''define service {
49 use templ_service_passive
51 host_name %(host_name)s
52 service_description %(service_description)s
53 check_command check_passive
54 }
55 '''
57 ##############################################################################
59 from nagixsc import *
61 ##############################################################################
63 # Output
64 if not options.output in [None, 'both', 'hosts', 'services']:
65 print 'Unknown output mode "%s"!' % options.output
66 sys.exit(1)
68 if options.output in [None, 'both']:
69 options.output = ['hosts', 'services']
70 else:
71 options.output = [options.output,]
73 # Read host and/or service template
74 if options.tmpl_host and 'hosts' in options.output:
75 HOSTTEMPL = open(options.tmpl_host).read()
76 if options.tmpl_service and 'services' in options.output:
77 SERVICETEMPL = open(options.tmpl_service).read()
79 # Get URL or file
80 doc = read_xml(options)
82 # Check XML against DTD
83 if options.schemacheck:
84 dtd = libxml2.parseDTD(None, options.schemacheck)
85 ctxt = libxml2.newValidCtxt()
86 ret = doc.validateDtd(ctxt, dtd)
87 if ret != 1:
88 print "error doing DTD validation"
89 sys.exit(1)
90 dtd.freeDtd()
91 del dtd
92 del ctxt
95 # Check XML file basics
96 (status, statusstring) = xml_check_version(doc)
97 debug(1, options.verb, statusstring)
98 if not status:
99 print statusstring
100 sys.exit(127)
103 # Put XML to Python dict
104 checks = xml_to_dict(doc, options.verb, options.host, options.service)
107 # Set default socket options
108 if hasattr(socket, 'setdefaulttimeout'):
109 socket.setdefaulttimeout(2)
111 # Loop over check results and search for new hosts and new services
112 foundhosts = []
114 for check in checks:
115 if not check['host_name'] in foundhosts:
116 foundhosts.append(check['host_name'])
118 if 'hosts' in options.output:
119 try:
120 check['address'] = socket.gethostbyname(check['host_name'])
121 except socket.gaierror:
122 check['address'] = '127.0.0.1'
123 print HOSTTEMPL % check
125 if check['service_description'] and 'services' in options.output:
126 print SERVICETEMPL % check