Code

obsses: Debug output, typo
[nagixsc.git] / nagixsc_live2xml.py
1 #!/usr/bin/python
3 import optparse
4 import sys
6 ##############################################################################
8 from nagixsc import *
10 ##############################################################################
12 parser = optparse.OptionParser()
14 parser.add_option('-s', '', dest='socket', help='Livestatus socket to connect')
15 parser.add_option('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
16 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
17 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
18 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
19 parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
20 parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
21 parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
22 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
24 parser.set_defaults(socket=None)
25 parser.set_defaults(outfile='-')
26 parser.set_defaults(encoding='base64')
27 parser.set_defaults(host=None)
28 parser.set_defaults(service=None)
29 parser.set_defaults(verb=0)
31 (options, args) = parser.parse_args()
33 ##############################################################################
35 if not options.socket:
36         print 'No socket specified!\n'
37         parser.print_help()
38         sys.exit(1)
40 if not check_encoding(options.encoding):
41         print 'Wrong encoding method "%s"!' % options.encoding
42         print 'Could be one of: "%s"' % '", "'.join(available_encodings())
43         sys.exit(1)
45 ##############################################################################
47 # Prepare socket
48 s_opts = prepare_socket(options.socket)
50 # Read informations from livestatus
51 checks = livestatus2dict(s_opts, options.host, options.service)
53 # Convert to XML
54 xmldoc = xml_from_dict(checks, options.encoding)
56 # Output
57 response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
58 if response and not options.quiet:
59         print response