Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc_live2xml.py
1 #!/usr/bin/python
2 #
3 # Nag(ix)SC -- nagixsc_live2xml.py
4 #
5 # Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; either version 2 of the License, or (at your
10 # option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
21 import optparse
22 import sys
24 ##############################################################################
26 from nagixsc import *
28 ##############################################################################
30 parser = optparse.OptionParser()
32 parser.add_option('-s', '', dest='socket', help='Livestatus socket to connect')
33 parser.add_option('-o', '', dest='outfile', help='Output file name, "-" for STDOUT or HTTP POST URL')
34 parser.add_option('-e', '', dest='encoding', help='Encoding ("%s")' % '", "'.join(available_encodings()) )
35 parser.add_option('-H', '', dest='host', help='Hostname/section to search for in config file')
36 parser.add_option('-D', '', dest='service', help='Service description to search for in config file (needs -H)')
37 parser.add_option('-l', '', dest='httpuser', help='HTTP user name, if outfile is HTTP(S) URL')
38 parser.add_option('-a', '', dest='httppasswd', help='HTTP password, if outfile is HTTP(S) URL')
39 parser.add_option('-q', '', action='store_true', dest='quiet', help='Be quiet')
40 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
42 parser.set_defaults(socket=None)
43 parser.set_defaults(outfile='-')
44 parser.set_defaults(encoding='base64')
45 parser.set_defaults(host=None)
46 parser.set_defaults(service=None)
47 parser.set_defaults(verb=0)
49 (options, args) = parser.parse_args()
51 ##############################################################################
53 if not options.socket:
54         print 'No socket specified!\n'
55         parser.print_help()
56         sys.exit(1)
58 if not check_encoding(options.encoding):
59         print 'Wrong encoding method "%s"!' % options.encoding
60         print 'Could be one of: "%s"' % '", "'.join(available_encodings())
61         sys.exit(1)
63 ##############################################################################
65 # Prepare socket
66 s_opts = prepare_socket(options.socket)
68 # Read informations from livestatus
69 checks = livestatus2dict(s_opts, options.host, options.service)
71 # Convert to XML
72 xmldoc = xml_from_dict(checks, options.encoding)
74 # Output
75 response = write_xml_or_die(xmldoc, options.outfile, options.httpuser, options.httppasswd)
76 if response and not options.quiet:
77         print response