4903509bff8a15528c3e3a1c0527a276c9f0695b
1 import base64
2 import datetime
3 import libxml2
5 def debug(level, verb, string):
6 if level <= verb:
7 print "%s: %s" % (level, string)
10 ##############################################################################
12 def available_encodings():
13 return ['base64', 'plain',]
16 def decode(data, encoding):
17 if encoding == 'plain':
18 return data
19 else:
20 return base64.b64decode(data)
23 def encode(data, encoding=None):
24 if encoding == 'plain':
25 return data
26 else:
27 return base64.b64encode(data)
30 ##############################################################################
32 def xml_check_version(xmldoc):
33 # FIXME: Check XML structure
34 try:
35 xmlnagixsc = xmldoc.xpathNewContext().xpathEval('/nagixsc')[0]
36 except:
37 return (False, 'Not a Nag(IX)SC XML file!')
39 try:
40 if xmlnagixsc.prop('version') != "1.0":
41 return (False, 'Wrong version (found "%s", need "1.0") of XML file!' % xmlnagixsc.prop('version'))
42 except:
43 return (False, 'No version information found in XML file!')
45 return (True, 'XML seems to be ok')
48 def xml_get_timestamp(xmldoc):
49 try:
50 timestamp = int(xmldoc.xpathNewContext().xpathEval('/nagixsc/timestamp')[0].get_content())
51 except:
52 return False
54 return timestamp
57 def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
58 checks = []
59 filetimestamp = xml_get_timestamp(xmldoc)
61 if hostfilter:
62 hosts = xmldoc.xpathNewContext().xpathEval('/nagixsc/host[name="%s"] | /nagixsc/host[name="%s"]' % (hostfilter, encode(hostfilter)))
63 else:
64 hosts = xmldoc.xpathNewContext().xpathEval('/nagixsc/host')
66 for host in hosts:
67 xmlhostname = host.xpathEval('name')[0]
68 hostname = decode(xmlhostname.get_content(), xmlhostname.prop('encoding'))
69 debug(2, verb, 'Found host "%s"' % hostname)
71 if servicefilter:
72 services = host.xpathEval('service[description="%s"] | service[description="%s"]' % (servicefilter, encode(servicefilter)))
73 else:
74 services = host.xpathEval('service')
76 for service in services:
77 service_dict = {}
79 xmldescr = service.xpathEval('description')[0]
80 xmloutput = service.xpathEval('output')[0]
82 srvdescr = decode(xmldescr.get_content(), xmldescr.prop('encoding'))
83 retcode = service.xpathEval('returncode')[0].get_content()
84 output = decode(xmloutput.get_content(), xmldescr.prop('encoding')).rstrip()
86 try:
87 timestamp = int(service.xpathEval('timestamp')[0].get_content())
88 except:
89 timestamp = filetimestamp
91 debug(2, verb, 'Found service "%s"' % srvdescr)
93 service_dict = {'host_name':hostname, 'service_description':srvdescr, 'returncode':retcode, 'output':output, 'timestamp':timestamp}
94 checks.append(service_dict)
96 debug(1, verb, 'Host: "%s" - Service: "%s" - RetCode: "%s" - Output: "%s"' % (hostname, srvdescr, retcode, output) )
98 return checks
101 def xml_from_dict(checks, encoding='base64'):
102 lasthost = None
104 db = [(check['host_name'], check) for check in checks]
105 db.sort()
107 xmldoc = libxml2.newDoc('1.0')
108 xmlroot = xmldoc.newChild(None, 'nagixsc', None)
109 xmlroot.setProp('version', '1.0')
110 xmltimestamp = xmlroot.newChild(None, 'timestamp', datetime.datetime.now().strftime('%s'))
112 for entry in db:
113 check = entry[1]
115 if check['host_name'] != lasthost:
116 xmlhost = xmlroot.newChild(None, 'host', None)
117 xmlhostname = xmlhost.newChild(None, 'name', encode(check['host_name'], encoding))
118 lasthost = check['host_name']
120 xmlservice = xmlhost.newChild(None, 'service', None)
121 xmlname = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding))
122 xmlname.setProp('encoding', encoding)
123 xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode']))
124 xmloutput = xmlservice.newChild(None, 'output', encode(check['output'], encoding))
125 xmloutput.setProp('encoding', encoding)
126 if check.has_key('timestamp'):
127 xmltimestamp = xmlservice.newChild(None, 'timestamp', str(check['timestamp']))
129 return xmldoc
132 def check_mark_outdated(check, now, maxtimediff, markold):
133 timedelta = now - check['timestamp']
134 if timedelta > maxtimediff:
135 check['output'] = 'Nag(ix)SC: Check result is %s(>%s) seconds old - %s' % (timedelta, maxtimediff, check['output'])
136 if markold:
137 check['returncode'] = 3
138 return check