Code

Add function to merge multiple XML files
[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('-f', '', dest='file', help='(Path and) file name of status file')
13 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
15 parser.set_defaults(url=None)
16 parser.set_defaults(file='nagixsc.xml')
17 parser.set_defaults(verb=0)
19 (options, args) = parser.parse_args()
21 HOSTTEMPL='''define host {
22         use             templ_host_default
24         host_name       %(host_name)s
25         address         127.0.0.1
26 }
27 '''
29 SERVICETEMPL='''define service {
30         use                     templ_service_passive
32         host_name               %(host_name)s
33         service_description     %(service_description)s
34         check_command           check_passive
35 }
36 '''
38 ##############################################################################
40 from nagixsc import *
42 ##############################################################################
44 # Get URL or file
45 if options.url != None:
46         import urllib2
48         response = urllib2.urlopen(options.url)
49         doc = libxml2.parseDoc(response.read())
50         response.close()
51 else:
52         doc = libxml2.parseFile(options.file)
55 # Check XML file basics
56 (status, string) = xml_check_version(doc)
57 debug(1, options.verb, string)
58 if not status:
59         print string
60         sys.exit(127)
63 # Put XML to Python dict
64 checks = xml_to_dict(doc)
67 # Loop over check results and search for new hosts and new services
68 foundhosts = []
70 for check in checks:
71         if not check['host_name'] in foundhosts:
72                 foundhosts.append(check['host_name'])
74                 print HOSTTEMPL % check
76         if check['service_description']:
77                 print SERVICETEMPL % check