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('-v', '', action='count', dest='verb', help='Verbose output')
24 parser.set_defaults(conffile='nagixsc.conf')
25 parser.set_defaults(outfile='-')
26 parser.set_defaults(encoding='base64')
27 parser.set_defaults(verb=0)
29 (options, args) = parser.parse_args()
31 ##############################################################################
33 if options.encoding not in available_encodings():
34 print 'Wrong encoding method "%s"!' % options.encoding
35 print 'Could be one of: %s' % ', '.join(available_encodings)
36 sys.exit(127)
38 ##############################################################################
40 def exec_check(host_name, service_descr, cmdline):
41 try:
42 cmd = subprocess.Popen(cmdline.split(' '), stdout=subprocess.PIPE)
43 output = cmd.communicate()[0].rstrip()
44 retcode = cmd.returncode
45 except OSError:
46 output = 'Could not execute "%s"' % cmdline
47 retcode = 127
49 return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}
51 ##############################################################################
53 config = ConfigParser.RawConfigParser()
54 config.optionxform = str # We need case-sensitive options
55 conf_list = config.read(options.conffile)
57 if conf_list == []:
58 print 'Config file "%s" could not be read!' % options.conffile
59 sys.exit(127)
61 # Sections are Hosts (not 'nagixsc'), options in sections are Services
62 hosts = config.sections()
63 if 'nagixsc' in hosts:
64 hosts.remove('nagixsc')
66 for host in hosts:
67 # Overwrite section/host name with '_host_name'
68 if config.has_option(host,'_host_name'):
69 host_name = config.get(host,'_host_name')
70 else:
71 host_name = host
74 # Look for host check
75 if config.has_option(host,'_host_check'):
76 cmdline = config.get(host, '_host_check')
77 check = exec_check(host_name, None, cmdline)
78 checks.append(check)
81 for service in config.options(host):
82 # If option starts with '_' it may be a NagixSC option in the future
83 if service[0] != '_':
84 cmdline = config.get(host, service)
86 check = exec_check(host_name, service, cmdline)
87 checks.append(check)
90 xmldoc = xml_from_dict(checks, options.encoding)
91 if options.outfile == '-':
92 xmldoc.saveFormatFile('-', format=1)
93 else:
94 xmldoc.saveFile(options.outfile)