Code

Move "exec_check" to nagixsc.py
[nagixsc.git] / nagixsc.py
index 4903509bff8a15528c3e3a1c0527a276c9f0695b..d65aa09591406e85d9fbde3c1e79206433e5859b 100644 (file)
@@ -1,6 +1,9 @@
 import base64
 import datetime
 import libxml2
+import shlex
+import subprocess
+import sys
 
 def debug(level, verb, string):
        if level <= verb:
@@ -27,6 +30,51 @@ 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):
+       if options.url != None:
+               import urllib2
+
+               if options.httpuser and options.httppasswd:
+                       passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
+                       passman.add_password(None, options.url, options.httpuser, options.httppasswd)
+                       authhandler = urllib2.HTTPBasicAuthHandler(passman)
+                       opener = urllib2.build_opener(authhandler)
+                       urllib2.install_opener(opener)
+
+               try:
+                       response = urllib2.urlopen(options.url)
+               except urllib2.HTTPError, error:
+                       print error
+                       sys.exit(0)
+               except urllib2.URLError, error:
+                       print error.reason[1]
+                       sys.exit(0)
+
+               doc = libxml2.parseDoc(response.read())
+               response.close()
+
+       else:
+               doc = libxml2.parseFile(options.file)
+
+       return doc
+
+
 ##############################################################################
 
 def xml_check_version(xmldoc):
@@ -56,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)))
@@ -68,11 +117,34 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
                hostname = decode(xmlhostname.get_content(), xmlhostname.prop('encoding'))
                debug(2, verb, 'Found host "%s"' % hostname)
 
+               # Look for Host check result
+               if host.xpathEval('returncode'):
+                       retcode   = host.xpathEval('returncode')[0].get_content()
+               else:
+                       retcode   = None
+
+               if host.xpathEval('output'):
+                       xmloutput = host.xpathEval('output')[0]
+                       output    = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip()
+               else:
+                       output    = None
+
+               if host.xpathEval('timestamp'):
+                       timestamp = reset_future_timestamp(int(host.xpathEval('timestamp')[0].get_content()), now)
+               else:
+                       timestamp = filetimestamp
+
+               if retcode and output:
+                       checks.append({'host_name':hostname, 'service_description':None, 'returncode':retcode, 'output':output, 'timestamp':timestamp})
+
+
+               # Look for service filter
                if servicefilter:
                        services = host.xpathEval('service[description="%s"] | service[description="%s"]' % (servicefilter, encode(servicefilter)))
                else:
                        services = host.xpathEval('service')
 
+               # Loop over services in host
                for service in services:
                        service_dict = {}
 
@@ -81,10 +153,10 @@ def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
 
                        srvdescr = decode(xmldescr.get_content(), xmldescr.prop('encoding'))
                        retcode  = service.xpathEval('returncode')[0].get_content()
-                       output   = decode(xmloutput.get_content(), xmldescr.prop('encoding')).rstrip()
+                       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
 
@@ -117,14 +189,23 @@ def xml_from_dict(checks, encoding='base64'):
                        xmlhostname = xmlhost.newChild(None, 'name', encode(check['host_name'], encoding))
                        lasthost = check['host_name']
 
-               xmlservice    = xmlhost.newChild(None, 'service', None)
-               xmlname       = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding))
-               xmlname.setProp('encoding', encoding)
-               xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode']))
-               xmloutput     = xmlservice.newChild(None, 'output', encode(check['output'], encoding))
-               xmloutput.setProp('encoding', encoding)
-               if check.has_key('timestamp'):
-                       xmltimestamp  = xmlservice.newChild(None, 'timestamp', str(check['timestamp']))
+               if check['service_description'] == '' or check['service_description'] == None:
+                       # Host check result
+                       xmlreturncode = xmlhost.newChild(None, 'returncode', str(check['returncode']))
+                       xmloutput     = xmlhost.newChild(None, 'output', encode(check['output'], encoding))
+                       xmloutput.setProp('encoding', encoding)
+                       if check.has_key('timestamp'):
+                               xmltimestamp  = xmlhost.newChild(None, 'timestamp', str(check['timestamp']))
+               else:
+                       # Service check result
+                       xmlservice    = xmlhost.newChild(None, 'service', None)
+                       xmlname       = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding))
+                       xmlname.setProp('encoding', encoding)
+                       xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode']))
+                       xmloutput     = xmlservice.newChild(None, 'output', encode(check['output'], encoding))
+                       xmloutput.setProp('encoding', encoding)
+                       if check.has_key('timestamp'):
+                               xmltimestamp  = xmlservice.newChild(None, 'timestamp', str(check['timestamp']))
 
        return xmldoc
 
@@ -137,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
+