Code

Output service only if description is set
[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('-H', '', dest='host', help='Hostname/section to search for in config file')
23 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
24 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
26 parser.set_defaults(conffile='nagixsc.conf')
27 parser.set_defaults(outfile='-')
28 parser.set_defaults(encoding='base64')
29 parser.set_defaults(host=None)
30 parser.set_defaults(service=None)
31 parser.set_defaults(verb=0)
33 (options, args) = parser.parse_args()
35 ##############################################################################
37 if options.encoding not in available_encodings():
38         print 'Wrong encoding method "%s"!' % options.encoding
39         print 'Could be one of: %s' % ', '.join(available_encodings)
40         sys.exit(127)
42 ##############################################################################
44 def exec_check(host_name, service_descr, cmdline):
45         try:
46                 cmd     = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
47                 output  = cmd.communicate()[0].rstrip()
48                 retcode = cmd.returncode
49         except OSError:
50                 output  = 'Could not execute "%s"' % cmdline
51                 retcode = 127
53         return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}
55 ##############################################################################
57 config = ConfigParser.RawConfigParser()
58 config.optionxform = str # We need case-sensitive options
59 conf_list = config.read(options.conffile)
61 if conf_list == []:
62         print 'Config file "%s" could not be read!' % options.conffile
63         sys.exit(127)
65 # Sections are Hosts (not 'nagixsc'), options in sections are Services
66 hosts = config.sections()
67 if 'nagixsc' in hosts:
68         hosts.remove('nagixsc')
70 # Filter out host/section if it exists
71 if options.host:
72         if options.host in hosts:
73                 hosts = [options.host,]
74         else:
75                 hosts = []
77 for host in hosts:
78         # Overwrite section/host name with '_host_name'
79         if config.has_option(host,'_host_name'):
80                 host_name = config.get(host,'_host_name')
81         else:
82                 host_name = host
85         services = config.options(host)
86         # Look for host check
87         if '_host_check' in services and not options.service:
88                 cmdline = config.get(host, '_host_check')
89                 check = exec_check(host_name, None, cmdline)
90                 checks.append(check)
93         # Filter out service if it exists
94         if options.service:
95                 if options.service in services:
96                         services = [options.service,]
97                 else:
98                         services = []
100         for service in services:
101                 # If option starts with '_' it may be a NagixSC option in the future
102                 if service[0] != '_':
103                         cmdline = config.get(host, service)
105                         check = exec_check(host_name, service, cmdline)
106                         checks.append(check)
109 xmldoc = xml_from_dict(checks, options.encoding)
110 if options.outfile == '-':
111         xmldoc.saveFormatFile('-', format=1)
112 else:
113         xmldoc.saveFile(options.outfile)