Code

Fix filename
[nagixsc.git] / nagixsc_conf2xml.py
index c58ad1e6e135960486ab180858f293ca082a2646..3105730351fd09073b363356dc0f8b0b3c58ae07 100755 (executable)
@@ -1,8 +1,6 @@
 #!/usr/bin/python
 
-import ConfigParser
 import optparse
-import subprocess
 import sys
 
 ##############################################################################
@@ -11,62 +9,40 @@ from nagixsc import *
 
 ##############################################################################
 
-checks = []
-
-
 parser = optparse.OptionParser()
 
 parser.add_option('-c', '', dest='conffile', help='Config file')
 parser.add_option('-o', '', dest='outfile', help='Output file')
 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
+parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
+parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(conffile='nagixsc.conf')
 parser.set_defaults(outfile='-')
 parser.set_defaults(encoding='base64')
+parser.set_defaults(host=None)
+parser.set_defaults(service=None)
 parser.set_defaults(verb=0)
 
 (options, args) = parser.parse_args()
 
 ##############################################################################
 
-if options.encoding not in available_encodings():
+if not check_encoding(options.encoding):
        print 'Wrong encoding method "%s"!' % options.encoding
-       print 'Could be one of: %s' % ', '.join(available_encodings)
+       print 'Could be one of: "%s"' % '", "'.join(available_encodings())
        sys.exit(127)
 
 ##############################################################################
 
-config = ConfigParser.RawConfigParser()
-config.optionxform = str # We need case-sensitive options
-conf_list = config.read(options.conffile)
+config = read_inifile(options.conffile)
 
-if conf_list == []:
+if not config:
        print 'Config file "%s" could not be read!' % options.conffile
-       sys.exit(127)
-
-# Sections are Hosts (not 'nagixsc'), options in sections are Services
-hosts = config.sections()
-if 'nagixsc' in hosts:
-       hosts.remove('nagixsc')
-
-for host in hosts:
-       if config.has_option(host,'_host_name'):
-               host_name = config.get(host,'_host_name')
-       else:
-               host_name = host
-
-       for service in config.options(host):
-               # If option starts with '_' it may be a NagixSC option in the future
-               if service[0] != '_':
-                       cmdline = config.get(host, service)
-
-                       cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
-                       output = cmd.communicate()[0].rstrip()
-                       retcode = cmd.returncode
-
-                       checks.append({'host_name':host_name, 'service_description':service, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')})
+       sys.exit(5)
 
+checks = conf2dict(config, options.host, options.service)
 
 xmldoc = xml_from_dict(checks, options.encoding)
 if options.outfile == '-':