Code

Move encoding check to "nagixsc.py"
[nagixsc.git] / nagixsc_conf2xml.py
1 #!/usr/bin/python
3 import ConfigParser
4 import optparse
5 import sys
7 ##############################################################################
9 from nagixsc import *
11 ##############################################################################
13 checks = []
16 parser = optparse.OptionParser()
18 parser.add_option('-c', '', dest='conffile', help='Config file')
19 parser.add_option('-o', '', dest='outfile', help='Output file')
20 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
21 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
22 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
23 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
25 parser.set_defaults(conffile='nagixsc.conf')
26 parser.set_defaults(outfile='-')
27 parser.set_defaults(encoding='base64')
28 parser.set_defaults(host=None)
29 parser.set_defaults(service=None)
30 parser.set_defaults(verb=0)
32 (options, args) = parser.parse_args()
34 ##############################################################################
36 if not check_encoding(options.encoding):
37         print 'Wrong encoding method "%s"!' % options.encoding
38         print 'Could be one of: "%s"' % '", "'.join(available_encodings())
39         sys.exit(127)
41 ##############################################################################
43 config = ConfigParser.RawConfigParser()
44 config.optionxform = str # We need case-sensitive options
45 conf_list = config.read(options.conffile)
47 if conf_list == []:
48         print 'Config file "%s" could not be read!' % options.conffile
49         sys.exit(127)
51 # Sections are Hosts (not 'nagixsc'), options in sections are Services
52 hosts = config.sections()
53 if 'nagixsc' in hosts:
54         hosts.remove('nagixsc')
56 # Filter out host/section if it exists
57 if options.host:
58         if options.host in hosts:
59                 hosts = [options.host,]
60         else:
61                 hosts = []
63 for host in hosts:
64         # Overwrite section/host name with '_host_name'
65         if config.has_option(host,'_host_name'):
66                 host_name = config.get(host,'_host_name')
67         else:
68                 host_name = host
71         services = config.options(host)
72         # Look for host check
73         if '_host_check' in services and not options.service:
74                 cmdline = config.get(host, '_host_check')
75                 check = exec_check(host_name, None, cmdline)
76                 checks.append(check)
79         # Filter out service if it exists
80         if options.service:
81                 if options.service in services:
82                         services = [options.service,]
83                 else:
84                         services = []
86         for service in services:
87                 # If option starts with '_' it may be a NagixSC option in the future
88                 if service[0] != '_':
89                         cmdline = config.get(host, service)
91                         check = exec_check(host_name, service, cmdline)
92                         checks.append(check)
95 xmldoc = xml_from_dict(checks, options.encoding)
96 if options.outfile == '-':
97         xmldoc.saveFormatFile('-', format=1)
98 else:
99         xmldoc.saveFile(options.outfile)