diff --git a/nagixsc.py b/nagixsc.py
index 4f834869f236ff6ba35bc2299ffe413cecf9fd0f..be3613d8d57ead1a9907bd52ed4bcb17516d8907 100644 (file)
--- a/nagixsc.py
+++ b/nagixsc.py
+import ConfigParser
import base64
import datetime
import libxml2
+import shlex
+import subprocess
import sys
def debug(level, verb, string):
return ['base64', 'plain',]
+def check_encoding(enc):
+ if enc in available_encodings():
+ return True
+ else:
+ return False
+
+
def decode(data, encoding):
if encoding == 'plain':
return data
return base64.b64encode(data)
+##############################################################################
+
+def read_inifile(inifile):
+ config = ConfigParser.RawConfigParser()
+ config.optionxform = str # We need case-sensitive options
+ ini_list = config.read(inifile)
+
+ if ini_list:
+ return config
+ else:
+ return False
+
+
+##############################################################################
+
+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 conf2dict(config, opt_host=None, opt_service=None):
+ checks = []
+
+ # Sections are Hosts (not 'nagixsc'), options in sections are Services
+ hosts = config.sections()
+ if 'nagixsc' in hosts:
+ hosts.remove('nagixsc')
+
+ # Filter out host/section if it exists
+ if opt_host:
+ if opt_host in hosts:
+ hosts = [opt_host,]
+ else:
+ hosts = []
+
+ for host in hosts:
+ # Overwrite section/host name with '_host_name'
+ if config.has_option(host,'_host_name'):
+ host_name = config.get(host,'_host_name')
+ else:
+ host_name = host
+
+
+ services = config.options(host)
+ # Look for host check
+ if '_host_check' in services and not opt_service:
+ cmdline = config.get(host, '_host_check')
+ check = exec_check(host_name, None, cmdline)
+ checks.append(check)
+
+
+ # Filter out service if given in cmd line options
+ if opt_service:
+ if opt_service in services:
+ services = [opt_service,]
+ else:
+ services = []
+
+ for service in services:
+ # If option starts with '_' it may be a NagixSC option in the future
+ if service[0] != '_':
+ cmdline = config.get(host, service)
+
+ check = exec_check(host_name, service, cmdline)
+ checks.append(check)
+
+ return checks
+
+
##############################################################################
def read_xml(options):
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)))
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
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
check['returncode'] = 3
return check
+
+def reset_future_timestamp(timestamp, now):
+ if timestamp <= now:
+ return timestamp
+ else:
+ return now
+