X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=nagixsc_conf2xml.py;h=2b5018c723aac08852ba3c00b2d0c4410693e09c;hb=HEAD;hp=d1ae4a8c9cbe351a0eda691d793c7b878f7e2af2;hpb=b3fcf0f48a9df34f5f647d9b93a15992fb6bbc0e;p=nagixsc.git diff --git a/nagixsc_conf2xml.py b/nagixsc_conf2xml.py index d1ae4a8..2b5018c 100755 --- a/nagixsc_conf2xml.py +++ b/nagixsc_conf2xml.py @@ -1,10 +1,26 @@ #!/usr/bin/python +# +# Nag(ix)SC -- nagixsc_conf2xml.py +# +# Copyright (C) 2009-2010 Sven Velt +# +# This program is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by the +# Free Software Foundation; either version 2 of the License, or (at your +# option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA -import ConfigParser import optparse -import shlex -import subprocess import sys +import urllib2 ############################################################################## @@ -12,16 +28,16 @@ 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('-q', '', action='store_true', dest='quiet', help='Be quiet') parser.add_option('-v', '', action='count', dest='verb', help='Verbose output') parser.set_defaults(conffile='nagixsc.conf') @@ -35,82 +51,27 @@ parser.set_defaults(verb=0) ############################################################################## -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) ############################################################################## -def exec_check(host_name, service_descr, cmdline): - try: - cmd = subprocess.Popen(shlex.split(cmdline), stdout=subprocess.PIPE) - output = cmd.communicate()[0].rstrip() - retcode = cmd.returncode - except OSError: - output = 'Could not execute "%s"' % cmdline - retcode = 127 - - return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')} - -############################################################################## - -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 - - - 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 = [] - - 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) - - check = exec_check(host_name, service, cmdline) - checks.append(check) + sys.exit(5) +# Execute checks, build dict +checks = conf2dict(config, options.host, options.service) +# Convert to XML xmldoc = xml_from_dict(checks, options.encoding) -if options.outfile == '-': - xmldoc.saveFormatFile('-', format=1) -else: - xmldoc.saveFile(options.outfile) +# Output +response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd) +if response and not options.quiet: + print response