Code

Allow command lines with quotes
[nagixsc.git] / nagixsc_conf2xml.py
1 #!/usr/bin/python
3 import ConfigParser
4 import optparse
5 import shlex
6 import subprocess
7 import sys
9 ##############################################################################
11 from nagixsc import *
13 ##############################################################################
15 checks = []
18 parser = optparse.OptionParser()
20 parser.add_option('-c', '', dest='conffile', help='Config file')
21 parser.add_option('-o', '', dest='outfile', help='Output file')
22 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
23 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
24 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
25 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
27 parser.set_defaults(conffile='nagixsc.conf')
28 parser.set_defaults(outfile='-')
29 parser.set_defaults(encoding='base64')
30 parser.set_defaults(host=None)
31 parser.set_defaults(service=None)
32 parser.set_defaults(verb=0)
34 (options, args) = parser.parse_args()
36 ##############################################################################
38 if options.encoding not in available_encodings():
39         print 'Wrong encoding method "%s"!' % options.encoding
40         print 'Could be one of: %s' % ', '.join(available_encodings)
41         sys.exit(127)
43 ##############################################################################
45 def exec_check(host_name, service_descr, cmdline):
46         try:
47                 cmd     = subprocess.Popen(shlex.split(cmdline), stdout=subprocess.PIPE)
48                 output  = cmd.communicate()[0].rstrip()
49                 retcode = cmd.returncode
50         except OSError:
51                 output  = 'Could not execute "%s"' % cmdline
52                 retcode = 127
54         return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}
56 ##############################################################################
58 config = ConfigParser.RawConfigParser()
59 config.optionxform = str # We need case-sensitive options
60 conf_list = config.read(options.conffile)
62 if conf_list == []:
63         print 'Config file "%s" could not be read!' % options.conffile
64         sys.exit(127)
66 # Sections are Hosts (not 'nagixsc'), options in sections are Services
67 hosts = config.sections()
68 if 'nagixsc' in hosts:
69         hosts.remove('nagixsc')
71 # Filter out host/section if it exists
72 if options.host:
73         if options.host in hosts:
74                 hosts = [options.host,]
75         else:
76                 hosts = []
78 for host in hosts:
79         # Overwrite section/host name with '_host_name'
80         if config.has_option(host,'_host_name'):
81                 host_name = config.get(host,'_host_name')
82         else:
83                 host_name = host
86         services = config.options(host)
87         # Look for host check
88         if '_host_check' in services and not options.service:
89                 cmdline = config.get(host, '_host_check')
90                 check = exec_check(host_name, None, cmdline)
91                 checks.append(check)
94         # Filter out service if it exists
95         if options.service:
96                 if options.service in services:
97                         services = [options.service,]
98                 else:
99                         services = []
101         for service in services:
102                 # If option starts with '_' it may be a NagixSC option in the future
103                 if service[0] != '_':
104                         cmdline = config.get(host, service)
106                         check = exec_check(host_name, service, cmdline)
107                         checks.append(check)
110 xmldoc = xml_from_dict(checks, options.encoding)
111 if options.outfile == '-':
112         xmldoc.saveFormatFile('-', format=1)
113 else:
114         xmldoc.saveFile(options.outfile)