Code

Changed license to GPL-2+ (from GPL-2only).
[nagixsc.git] / nagixsc_read_xml.py
index 1cd436306e414f6141f6eb01af475be7823a1402..e479d5f48c794520ffe60e9c1d160ce190ea5a75 100755 (executable)
@@ -1,10 +1,28 @@
 #!/usr/bin/python
+#
+# Nag(ix)SC -- nagixsc_read_xml.py
+#
+# Copyright (C) 2009-2010 Sven Velt <sv@teamix.net>
+#
+# 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 base64
-import datetime
 import libxml2
 import optparse
 import sys
+import time
 
 parser = optparse.OptionParser()
 
@@ -14,14 +32,16 @@ parser.add_option('-a', '', dest='httppasswd', help='HTTP password')
 parser.add_option('-f', '', dest='file', help='(Path and) file name of status file')
 parser.add_option('-s', '', dest='seconds', type='int', help='Maximum age in seconds of xml timestamp')
 parser.add_option('-m', '', action='store_true', dest='markold', help='Mark (Set state) of too old checks as UNKNOWN')
+parser.add_option('-P', '', action='store_true', dest='pprint', help='Output with Python\'s pprint')
 parser.add_option('-v', '', action='count', dest='verb', help='Verbose output')
 
 parser.set_defaults(url=None)
 parser.set_defaults(httpuser=None)
 parser.set_defaults(httppasswd=None)
-parser.set_defaults(file='nagixsc.xml')
+parser.set_defaults(file='-')
 parser.set_defaults(seconds=14400)
 parser.set_defaults(markold=False)
+parser.set_defaults(pprint=False)
 parser.set_defaults(verb=0)
 
 (options, args) = parser.parse_args()
@@ -32,7 +52,7 @@ from nagixsc import *
 
 ##############################################################################
 
-now = int(datetime.datetime.now().strftime('%s'))
+now = int(time.time())
 
 # Get URL or file
 doc = read_xml(options)
@@ -60,10 +80,13 @@ debug(1, options.verb, 'Age of XML file: %s seconds, max allowed: %s seconds' %
 checks = xml_to_dict(doc)
 
 
-# Loop over check results and output them
-for check in checks:
-       check = check_mark_outdated(check, now, options.seconds, options.markold)
-       print 'Host:      %s\nService:   %s\nRetCode:   %s\nOutput:    %r\nTimestamp: %s\n' % (check['host_name'], check['service_description'], check['returncode'], check['output'], check['timestamp'])
+if options.pprint:
+       # Print 'em all in one
+       import pprint
+       pprint.pprint(checks)
+else:
+       # Loop over check results and output them
+       for check in checks:
+               check = check_mark_outdated(check, now, options.seconds, options.markold)
+               print 'Host:      %s\nService:   %s\nRetCode:   %s\nOutput:    %r\nTimestamp: %s\n' % (check['host_name'], check['service_description'], check['returncode'], check['output'], check['timestamp'])
 
-import pprint
-pprint.pprint(checks)