Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc_read_xml.py
1 #!/usr/bin/python
2 #
3 # Nag(ix)SC -- nagixsc_read_xml.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 base64
22 import libxml2
23 import optparse
24 import sys
25 import time
27 parser = optparse.OptionParser()
29 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
30 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
31 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
32 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
33 parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp')
34 parser.add_option('-m', '', action='store_true', dest='markold', help='Mark (Set state) of too old checks as UNKNOWN')
35 parser.add_option('-P', '', action='store_true', dest='pprint', help='Output with Python\'s pprint')
36 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
38 parser.set_defaults(url=None)
39 parser.set_defaults(httpuser=None)
40 parser.set_defaults(httppasswd=None)
41 parser.set_defaults(file='-')
42 parser.set_defaults(seconds=14400)
43 parser.set_defaults(markold=False)
44 parser.set_defaults(pprint=False)
45 parser.set_defaults(verb=0)
47 (options, args) = parser.parse_args()
49 ##############################################################################
51 from nagixsc import *
53 ##############################################################################
55 now = int(time.time())
57 # Get URL or file
58 doc = read_xml(options)
61 # Check XML file basics
62 (status, string) = xml_check_version(doc)
63 debug(1, options.verb, string)
64 if not status:
65         print string
66         sys.exit(127)
69 # Get timestamp and check it
70 filetimestamp = xml_get_timestamp(doc)
71 if not filetimestamp:
72         print 'No timestamp found in XML file, exiting because of invalid XML data...'
73         sys.exit(127)
75 timedelta = int(now) - int(filetimestamp)
76 debug(1, options.verb, 'Age of XML file: %s seconds, max allowed: %s seconds' % (timedelta, options.seconds))
79 # Put XML to Python dict
80 checks = xml_to_dict(doc)
83 if options.pprint:
84         # Print 'em all in one
85         import pprint
86         pprint.pprint(checks)
87 else:
88         # Loop over check results and output them
89         for check in checks:
90                 check = check_mark_outdated(check, now, options.seconds, options.markold)
91                 print 'Host:      %s\nService:   %s\nRetCode:   %s\nOutput:    %r\nTimestamp: %s\n' % (check['host_name'], check['service_description'], check['returncode'], check['output'], check['timestamp'])