Code

Added English translations for QUICKSTART.txt and obsess.README.
[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; only version 2 of the License is applicable.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20 #import base64
21 import libxml2
22 import optparse
23 import sys
24 import time
26 parser = optparse.OptionParser()
28 parser.add_option('-u', '', dest='url', help='URL of status file (xml)')
29 parser.add_option('-l', '', dest='httpuser', help='HTTP user name')
30 parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
31 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
32 parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp')
33 parser.add_option('-m', '', action='store_true', dest='markold', help='Mark (Set state) of too old checks as UNKNOWN')
34 parser.add_option('-P', '', action='store_true', dest='pprint', help='Output with Python\'s pprint')
35 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
37 parser.set_defaults(url=None)
38 parser.set_defaults(httpuser=None)
39 parser.set_defaults(httppasswd=None)
40 parser.set_defaults(file='-')
41 parser.set_defaults(seconds=14400)
42 parser.set_defaults(markold=False)
43 parser.set_defaults(pprint=False)
44 parser.set_defaults(verb=0)
46 (options, args) = parser.parse_args()
48 ##############################################################################
50 from nagixsc import *
52 ##############################################################################
54 now = int(time.time())
56 # Get URL or file
57 doc = read_xml(options)
60 # Check XML file basics
61 (status, string) = xml_check_version(doc)
62 debug(1, options.verb, string)
63 if not status:
64         print string
65         sys.exit(127)
68 # Get timestamp and check it
69 filetimestamp = xml_get_timestamp(doc)
70 if not filetimestamp:
71         print 'No timestamp found in XML file, exiting because of invalid XML data...'
72         sys.exit(127)
74 timedelta = int(now) - int(filetimestamp)
75 debug(1, options.verb, 'Age of XML file: %s seconds, max allowed: %s seconds' % (timedelta, options.seconds))
78 # Put XML to Python dict
79 checks = xml_to_dict(doc)
82 if options.pprint:
83         # Print 'em all in one
84         import pprint
85         pprint.pprint(checks)
86 else:
87         # Loop over check results and output them
88         for check in checks:
89                 check = check_mark_outdated(check, now, options.seconds, options.markold)
90                 print 'Host:      %s\nService:   %s\nRetCode:   %s\nOutput:    %r\nTimestamp: %s\n' % (check['host_name'], check['service_description'], check['returncode'], check['output'], check['timestamp'])