Code

Move "exec_check" to nagixsc.py
[nagixsc.git] / nagixsc.py
index 4f834869f236ff6ba35bc2299ffe413cecf9fd0f..d65aa09591406e85d9fbde3c1e79206433e5859b 100644 (file)
@@ -1,6 +1,8 @@
 import base64
 import datetime
 import libxml2
+import shlex
+import subprocess
 import sys
 
 def debug(level, verb, string):
@@ -28,6 +30,20 @@ def encode(data, encoding=None):
                return base64.b64encode(data)
 
 
+##############################################################################
+
+def exec_check(host_name, service_descr, cmdline):
+       try:
+               cmd     = subprocess.Popen(shlex.split(cmdline), stdout=subprocess.PIPE)
+               output  = cmd.communicate()[0].rstrip()
+               retcode = cmd.returncode
+       except OSError:
+               output  = 'Could not execute "%s"' % cmdline
+               retcode = 127
+
+       return {'host_name':host_name, 'service_description':service_descr, 'returncode':retcode, 'output':output, 'timestamp':datetime.datetime.now().strftime('%s')}
+
+
 ##############################################################################
 
 def read_xml(options):
@@ -88,7 +104,8 @@ def xml_get_timestamp(xmldoc):
 
 def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
        checks = []
-       filetimestamp = xml_get_timestamp(xmldoc)
+       now = int(datetime.datetime.now().strftime('%s'))
+       filetimestamp = reset_future_timestamp(xml_get_timestamp(xmldoc), now)
 
        if hostfilter:
                hosts = xmldoc.xpathNewContext().xpathEval('/nagixsc/host[name="%s"] | /nagixsc/host[name="%s"]' % (hostfilter, encode(hostfilter)))
@@ -113,7 +130,7 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
                        output    = None
 
                if host.xpathEval('timestamp'):
-                       timestamp = int(host.xpathEval('timestamp')[0].get_content())
+                       timestamp = reset_future_timestamp(int(host.xpathEval('timestamp')[0].get_content()), now)
                else:
                        timestamp = filetimestamp
 
@@ -139,7 +156,7 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
                        output   = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip()
 
                        try:
-                               timestamp = int(service.xpathEval('timestamp')[0].get_content())
+                               timestamp = reset_future_timestamp(int(service.xpathEval('timestamp')[0].get_content()), now)
                        except:
                                timestamp = filetimestamp
 
@@ -201,3 +218,10 @@ def check_mark_outdated(check, now, maxtimediff, markold):
                        check['returncode'] = 3
        return check
 
+
+def reset_future_timestamp(timestamp, now):
+       if timestamp <= now:
+               return timestamp
+       else:
+               return now
+