Code

Reorder daemonizing (creation of PID file)
[nagixsc.git] / nagixsc_conf2xml.py
index a5f3f1374e5f117abd3d075a70fc1d550398eeb3..5d5da3c04cc9a7b031d3f6908992d9876ff5590c 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/python
 
-import ConfigParser
 import optparse
 import sys
+import urllib2
 
 ##############################################################################
 
@@ -10,16 +10,15 @@ 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('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
 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('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
+parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(conffile='nagixsc.conf')
@@ -40,62 +39,33 @@ if not check_encoding(options.encoding):
 
 ##############################################################################
 
-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')
-
-# Filter out host/section if it exists
-if options.host:
-       if options.host in hosts:
-               hosts = [options.host,]
-       else:
-               hosts = []
-
-for host in hosts:
-       # Overwrite section/host name with '_host_name'
-       if config.has_option(host,'_host_name'):
-               host_name = config.get(host,'_host_name')
-       else:
-               host_name = host
+       sys.exit(5)
 
+checks = conf2dict(config, options.host, options.service)
 
-       services = config.options(host)
-       # Look for host check
-       if '_host_check' in services and not options.service:
-               cmdline = config.get(host, '_host_check')
-               check = exec_check(host_name, None, cmdline)
-               checks.append(check)
-
-
-       # Filter out service if it exists
-       if options.service:
-               if options.service in services:
-                       services = [options.service,]
-               else:
-                       services = []
+xmldoc = xml_from_dict(checks, options.encoding)
 
-       for service in services:
-               # If option starts with '_' it may be a NagixSC option in the future
-               if service[0] != '_':
-                       cmdline = config.get(host, service)
+if options.outfile.startswith('http'):
+       (headers, body) = encode_multipart(xmldoc, options.httpuser, options.httppasswd)
 
-                       check = exec_check(host_name, service, cmdline)
-                       checks.append(check)
+       try:
+               response = urllib2.urlopen(urllib2.Request(options.outfile, body, headers)).read()
+       except urllib2.HTTPError, error:
+               print error
+               sys.exit(6)
+       except urllib2.URLError, error:
+               print error.reason[1]
+               sys.exit(7)
 
+       print response
 
-xmldoc = xml_from_dict(checks, options.encoding)
-if options.outfile == '-':
+elif options.outfile == '-':
        xmldoc.saveFormatFile('-', format=1)
+
 else:
        xmldoc.saveFile(options.outfile)
 
-