Code

Reorder daemonizing (creation of PID file)
[nagixsc.git] / nagixsc_xml2cfg.py
1 #!/usr/bin/python
3 #import base64
4 import datetime
5 import libxml2
6 import optparse
7 import sys
9 parser = optparse.OptionParser()
11 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
12 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
13 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
14 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
15 parser.add_option('-S', '', dest='schemacheck', help='Check XML against DTD')
16 parser.add_option('-H', '', dest='host', help='Hostname to search for in XML file')
17 parser.add_option('-D', '', dest='service', help='Service description to search for in XML file')
18 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
20 parser.set_defaults(url=None)
21 parser.set_defaults(httpuser=None)
22 parser.set_defaults(httppasswd=None)
23 parser.set_defaults(file='nagixsc.xml')
24 parser.set_defaults(schemacheck='')
25 parser.set_defaults(host=None)
26 parser.set_defaults(service=None)
27 parser.set_defaults(verb=0)
29 (options, args) = parser.parse_args()
31 HOSTTEMPL='''define host {
32         use             templ_host_default
34         host_name       %(host_name)s
35         address         127.0.0.1
36 }
37 '''
39 SERVICETEMPL='''define service {
40         use                     templ_service_passive
42         host_name               %(host_name)s
43         service_description     %(service_description)s
44         check_command           check_passive
45 }
46 '''
48 ##############################################################################
50 from nagixsc import *
52 ##############################################################################
54 # Get URL or file
55 doc = read_xml(options)
57 # Check XML against DTD
58 if options.schemacheck:
59         dtd = libxml2.parseDTD(None, options.schemacheck)
60         ctxt = libxml2.newValidCtxt()
61         ret = doc.validateDtd(ctxt, dtd)
62         if ret != 1:
63                 print "error doing DTD validation"
64                 sys.exit(1)
65         dtd.freeDtd()
66         del dtd
67         del ctxt
70 # Check XML file basics
71 (status, statusstring) = xml_check_version(doc)
72 debug(1, options.verb, statusstring)
73 if not status:
74         print statusstring
75         sys.exit(127)
78 # Put XML to Python dict
79 checks = xml_to_dict(doc, options.verb, options.host, options.service)
82 # Loop over check results and search for new hosts and new services
83 foundhosts = []
85 for check in checks:
86         if not check['host_name'] in foundhosts:
87                 foundhosts.append(check['host_name'])
89                 print HOSTTEMPL % check
91         if check['service_description']:
92                 print SERVICETEMPL % check