Code

c58ad1e6e135960486ab180858f293ca082a2646
[nagixsc.git] / nagixsc_conf2xml.py
1 #!/usr/bin/python
3 import ConfigParser
4 import optparse
5 import subprocess
6 import sys
8 ##############################################################################
10 from nagixsc import *
12 ##############################################################################
14 checks = []
17 parser = optparse.OptionParser()
19 parser.add_option('-c', '', dest='conffile', help='Config file')
20 parser.add_option('-o', '', dest='outfile', help='Output file')
21 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
22 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
24 parser.set_defaults(conffile='nagixsc.conf')
25 parser.set_defaults(outfile='-')
26 parser.set_defaults(encoding='base64')
27 parser.set_defaults(verb=0)
29 (options, args) = parser.parse_args()
31 ##############################################################################
33 if options.encoding not in available_encodings():
34         print 'Wrong encoding method "%s"!' % options.encoding
35         print 'Could be one of: %s' % ', '.join(available_encodings)
36         sys.exit(127)
38 ##############################################################################
40 config = ConfigParser.RawConfigParser()
41 config.optionxform = str # We need case-sensitive options
42 conf_list = config.read(options.conffile)
44 if conf_list == []:
45         print 'Config file "%s" could not be read!' % options.conffile
46         sys.exit(127)
48 # Sections are Hosts (not 'nagixsc'), options in sections are Services
49 hosts = config.sections()
50 if 'nagixsc' in hosts:
51         hosts.remove('nagixsc')
53 for host in hosts:
54         if config.has_option(host,'_host_name'):
55                 host_name = config.get(host,'_host_name')
56         else:
57                 host_name = host
59         for service in config.options(host):
60                 # If option starts with '_' it may be a NagixSC option in the future
61                 if service[0] != '_':
62                         cmdline = config.get(host, service)
64                         cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
65                         output = cmd.communicate()[0].rstrip()
66                         retcode = cmd.returncode
68                         checks.append({'host_name':host_name, 'service_description':service, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')})
71 xmldoc = xml_from_dict(checks, options.encoding)
72 if options.outfile == '-':
73         xmldoc.saveFormatFile('-', format=1)
74 else:
75         xmldoc.saveFile(options.outfile)