Code

Correct timestamps of passive service
[nagixsc.git] / nagixsc / __init__.py
1 import BaseHTTPServer
2 import ConfigParser
3 import SocketServer
4 import base64
5 import datetime
6 import libxml2
7 import mimetools
8 import os
9 import random
10 import shlex
11 import signal
12 import socket
13 import string
14 import subprocess
15 import sys
16 import urllib2
18 def debug(level, verb, string):
19         if level <= verb:
20                 print "%s: %s" % (level, string)
23 ##############################################################################
25 class ExecTimeoutError(Exception):
26         pass
28 ##############################################################################
30 def available_encodings():
31         return ['base64', 'plain',]
34 def check_encoding(enc):
35         if enc in available_encodings():
36                 return True
37         else:
38                 return False
41 def decode(data, encoding):
42         if encoding == 'plain':
43                 return data
44         else:
45                 return base64.b64decode(data)
48 def encode(data, encoding=None):
49         if encoding == 'plain':
50                 return data
51         else:
52                 return base64.b64encode(data)
55 ##############################################################################
57 def read_inifile(inifile):
58         config = ConfigParser.RawConfigParser()
59         config.optionxform = str # We need case-sensitive options
60         ini_list = config.read(inifile)
62         if ini_list:
63                 return config
64         else:
65                 return False
68 ##############################################################################
70 def exec_timeout_handler(signum, frame):
71         raise ExecTimeoutError
73 def exec_check(host_name, service_descr, cmdline, timeout=None, timeout_returncode=2):
74         cmdarray = shlex.split(cmdline)
76         check = {}
77         check['host_name'] = host_name
78         check['service_description'] = service_descr
80         if len(cmdarray) == 0:
81                 check['output'] = 'No command line specified!'
82                 check['returncode'] = 127
83                 return check
85         if timeout:
86                 signal.signal(signal.SIGALRM, exec_timeout_handler)
87                 signal.alarm(timeout)
89         try:
90                 cmd = subprocess.Popen(cmdarray, stdout=subprocess.PIPE)
91                 check['output'] = cmd.communicate()[0].rstrip()
92                 check['returncode'] = cmd.returncode
93         except OSError:
94                 check['output'] = 'Could not execute "%s"' % cmdline
95                 check['returncode'] = 127
96         except ExecTimeoutError:
97                 check['output'] = 'Plugin timed out after %s seconds' % timeout
98                 check['returncode'] = timeout_returncode
100         if timeout:
101                 signal.alarm(0)
102                 try:
103                         if sys.version_info >= (2, 6):
104                                 cmd.terminate()
105                         else:
106                                 os.kill(cmd.pid, 15)
107                 except OSError:
108                         pass
110         check['timestamp'] = datetime.datetime.now().strftime('%s')
111         return check
114 ##############################################################################
116 def conf2dict(config, opt_host=None, opt_service=None):
117         checks = []
119         # Read "plugin_timeout" from "[nagixsc]", default "None" (no timeout)
120         try:
121                 timeout = config.getint('nagixsc','plugin_timeout')
122         except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
123                 timeout = None
125         # Read "plugin_timeout_returncode" from "[nagixsc]", default "2" (CRITICAL)
126         try:
127                 timeout_returncode = config.getint('nagixsc','plugin_timeout_returncode')
128         except (ConfigParser.NoSectionError, ConfigParser.NoOptionError):
129                 timeout_returncode = 2
131         # Sections are Hosts (not 'nagixsc'), options in sections are Services
132         hosts = config.sections()
133         if 'nagixsc' in hosts:
134                 hosts.remove('nagixsc')
136         # Filter out host/section if it exists
137         if opt_host:
138                 if opt_host in hosts:
139                         hosts = [opt_host,]
140                 else:
141                         hosts = []
143         for host in hosts:
144                 # Overwrite section/host name with '_host_name'
145                 if config.has_option(host,'_host_name'):
146                         host_name = config.get(host,'_host_name')
147                 else:
148                         host_name = host
151                 services = config.options(host)
152                 # Look for host check
153                 if '_host_check' in services and not opt_service:
154                         cmdline = config.get(host, '_host_check')
155                         check = exec_check(host_name, None, cmdline, timeout, timeout_returncode)
156                         checks.append(check)
159                 # Filter out service if given in cmd line options
160                 if opt_service:
161                         if opt_service in services:
162                                 services = [opt_service,]
163                         else:
164                                 services = []
166                 for service in services:
167                         # If option starts with '_' it may be a NagixSC option in the future
168                         if service[0] != '_':
169                                 cmdline = config.get(host, service)
171                                 check = exec_check(host_name, service, cmdline, timeout, timeout_returncode)
172                                 checks.append(check)
174         return checks
177 ##############################################################################
179 def dict2out_passive(checks, xmltimestamp, opt_pipe, opt_verb=0):
180         FORMAT_HOST = '[%s] PROCESS_HOST_CHECK_RESULT;%s;%s;%s'
181         FORMAT_SERVICE = '[%s] PROCESS_SERVICE_CHECK_RESULT;%s;%s;%s;%s'
182         count_services = 0
183         now = datetime.datetime.now().strftime('%s')
185         # Prepare
186         if opt_verb <= 2:
187                 pipe = open(opt_pipe, "w")
188         else:
189                 pipe = None
191         # Output
192         for check in checks:
193                 count_services += 1
194                 if check.has_key('timestamp'):
195                         timestamp = check['timestamp']
196                 else:
197                         timestamp = xmltimestamp
199                 if check['service_description'] == None or check['service_description'] == '':
200                         # Host check
201                         line = FORMAT_HOST % (timestamp, check['host_name'], check['returncode'], check['output'].replace('\n', '\\n'))
202                 else:
203                         # Service check
204                         line =  FORMAT_SERVICE % (timestamp, check['host_name'], check['service_description'], check['returncode'], check['output'].replace('\n', '\\n'))
206                 if pipe:
207                         pipe.write(line + '\n')
208                 debug(2, opt_verb, line)
210         # Close
211         if pipe:
212                 pipe.close()
213         else:
214                 print "Passive check results NOT written to Nagios pipe due to -vvv!"
216         return count_services
219 def dict2out_checkresult(checks, xmltimestamp, opt_checkresultdir, opt_verb=0):
220         count_services = 0
221         count_failed = 0
222         list_failed = []
223         chars = string.letters + string.digits
224         ctimestamp = datetime.datetime.now().ctime()
225         random.seed()
227         for check in checks:
228                 count_services += 1
229                 if check.has_key('timestamp'):
230                         timestamp = check['timestamp']
231                 else:
232                         timestamp = xmltimestamp
234                 filename = os.path.join(opt_checkresultdir, 'c' + ''.join([random.choice(chars) for i in range(6)]))
235                 try:
236                         crfile = open(filename, "w")
237                         if check['service_description'] == None or check['service_description'] == '':
238                                 # Host check
239                                 crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, ctimestamp, check['host_name'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
240                         else:
241                                 # Service check
242                                 crfile.write('### Active Check Result File ###\nfile_time=%s\n\n### Nagios Service Check Result ###\n# Time: %s\nhost_name=%s\nservice_description=%s\ncheck_type=0\ncheck_options=0\nscheduled_check=1\nreschedule_check=1\nlatency=0.0\nstart_time=%s.00\nfinish_time=%s.05\nearly_timeout=0\nexited_ok=1\nreturn_code=%s\noutput=%s\n' % (timestamp, ctimestamp, check['host_name'], check['service_description'], timestamp, timestamp, check['returncode'], check['output'].replace('\n', '\\n') ) )
243                         crfile.close()
245                         # Create OK file
246                         open(filename + '.ok', 'w').close()
247                 except:
248                         count_failed += 1
249                         list_failed.append([filename, check['host_name'], check['service_description']])
251         return (count_services, count_failed, list_failed)
254 ##############################################################################
256 def read_xml(options):
257         if options.url != None:
259                 if options.httpuser and options.httppasswd:
260                         passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
261                         passman.add_password(None, options.url, options.httpuser, options.httppasswd)
262                         authhandler = urllib2.HTTPBasicAuthHandler(passman)
263                         opener = urllib2.build_opener(authhandler)
264                         urllib2.install_opener(opener)
266                 try:
267                         response = urllib2.urlopen(options.url)
268                 except urllib2.HTTPError, error:
269                         print error
270                         sys.exit(0)
271                 except urllib2.URLError, error:
272                         print error.reason[1]
273                         sys.exit(0)
275                 doc = libxml2.parseDoc(response.read())
276                 response.close()
278         else:
279                 doc = libxml2.parseFile(options.file)
281         return doc
284 def read_xml_from_string(content):
285         return libxml2.parseDoc(content)
288 def write_xml(xmldoc, outfile, httpuser=None, httppasswd=None):
289         if outfile.startswith('http'):
290                 (headers, body) = encode_multipart(xmldoc, httpuser, httppasswd)
291                 response = urllib2.urlopen(urllib2.Request(outfile, body, headers)).read()
292                 return response
294         elif outfile == '-':
295                 xmldoc.saveFormatFile('-', format=1)
296                 return None
298         else:
299                 xmldoc.saveFile(outfile)
300                 return None
303 def write_xml_or_die(xmldoc, outfile, httpuser=None, httppasswd=None):
304         try:
305                 response = write_xml(xmldoc, outfile, httpuser, httppasswd)
306         except urllib2.HTTPError, error:
307                 print error
308                 sys.exit(11)
309         except urllib2.URLError, error:
310                 print error.reason[1]
311                 sys.exit(12)
313         return response
316 ##############################################################################
318 def xml_check_version(xmldoc):
319         # FIXME: Check XML structure
320         try:
321                 xmlnagixsc = xmldoc.xpathNewContext().xpathEval('/nagixsc')[0]
322         except:
323                 return (False, 'Not a Nag(IX)SC XML file!')
325         try:
326                 if xmlnagixsc.prop('version') != "1.0":
327                         return (False, 'Wrong version (found "%s", need "1.0") of XML file!' % xmlnagixsc.prop('version'))
328         except:
329                 return (False, 'No version information found in XML file!')
331         return (True, 'XML seems to be ok')
334 def xml_get_timestamp(xmldoc):
335         try:
336                 timestamp = int(xmldoc.xpathNewContext().xpathEval('/nagixsc/timestamp')[0].get_content())
337         except:
338                 return False
340         return timestamp
343 def xml_to_dict(xmldoc, verb=0, hostfilter=None, servicefilter=None):
344         checks = []
345         now = int(datetime.datetime.now().strftime('%s'))
346         filetimestamp = reset_future_timestamp(xml_get_timestamp(xmldoc), now)
348         if hostfilter:
349                 hosts = xmldoc.xpathNewContext().xpathEval('/nagixsc/host[name="%s"] | /nagixsc/host[name="%s"]' % (hostfilter, encode(hostfilter)))
350         else:
351                 hosts = xmldoc.xpathNewContext().xpathEval('/nagixsc/host')
353         for host in hosts:
354                 xmlhostname = host.xpathEval('name')[0]
355                 hostname = decode(xmlhostname.get_content(), xmlhostname.prop('encoding'))
356                 debug(2, verb, 'Found host "%s"' % hostname)
358                 # Look for Host check result
359                 if host.xpathEval('returncode'):
360                         retcode   = host.xpathEval('returncode')[0].get_content()
361                 else:
362                         retcode   = None
364                 if host.xpathEval('output'):
365                         xmloutput = host.xpathEval('output')[0]
366                         output    = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip()
367                 else:
368                         output    = None
370                 if host.xpathEval('timestamp'):
371                         timestamp = reset_future_timestamp(int(host.xpathEval('timestamp')[0].get_content()), now)
372                 else:
373                         timestamp = filetimestamp
375                 # Append only if no service filter
376                 if not servicefilter and retcode and output:
377                         checks.append({'host_name':hostname, 'service_description':None, 'returncode':retcode, 'output':output, 'timestamp':timestamp})
380                 # Look for service filter
381                 if servicefilter:
382                         services = host.xpathEval('service[description="%s"] | service[description="%s"]' % (servicefilter, encode(servicefilter)))
383                 else:
384                         services = host.xpathEval('service')
386                 # Loop over services in host
387                 for service in services:
388                         service_dict = {}
390                         xmldescr  = service.xpathEval('description')[0]
391                         xmloutput = service.xpathEval('output')[0]
393                         srvdescr = decode(xmldescr.get_content(), xmldescr.prop('encoding'))
394                         retcode  = service.xpathEval('returncode')[0].get_content()
395                         output   = decode(xmloutput.get_content(), xmloutput.prop('encoding')).rstrip()
397                         try:
398                                 timestamp = reset_future_timestamp(int(service.xpathEval('timestamp')[0].get_content()), now)
399                         except:
400                                 timestamp = filetimestamp
402                         debug(2, verb, 'Found service "%s"' % srvdescr)
404                         service_dict = {'host_name':hostname, 'service_description':srvdescr, 'returncode':retcode, 'output':output, 'timestamp':timestamp}
405                         checks.append(service_dict)
407                         debug(1, verb, 'Host: "%s" - Service: "%s" - RetCode: "%s" - Output: "%s"' % (hostname, srvdescr, retcode, output) )
409         return checks
412 def xml_from_dict(checks, encoding='base64'):
413         lasthost = None
415         db = [(check['host_name'], check) for check in checks]
416         db.sort()
418         xmldoc = libxml2.newDoc('1.0')
419         xmlroot = xmldoc.newChild(None, 'nagixsc', None)
420         xmlroot.setProp('version', '1.0')
421         xmltimestamp = xmlroot.newChild(None, 'timestamp', datetime.datetime.now().strftime('%s'))
423         for entry in db:
424                 check = entry[1]
426                 if check['host_name'] != lasthost:
427                         xmlhost = xmlroot.newChild(None, 'host', None)
428                         xmlhostname = xmlhost.newChild(None, 'name', encode(check['host_name'], encoding))
429                         lasthost = check['host_name']
431                 if check['service_description'] == '' or check['service_description'] == None:
432                         # Host check result
433                         xmlreturncode = xmlhost.newChild(None, 'returncode', str(check['returncode']))
434                         xmloutput     = xmlhost.newChild(None, 'output', encode(check['output'], encoding))
435                         xmloutput.setProp('encoding', encoding)
436                         if check.has_key('timestamp'):
437                                 xmltimestamp  = xmlhost.newChild(None, 'timestamp', str(check['timestamp']))
438                 else:
439                         # Service check result
440                         xmlservice    = xmlhost.newChild(None, 'service', None)
441                         xmlname       = xmlservice.newChild(None, 'description', encode(check['service_description'], encoding))
442                         xmlname.setProp('encoding', encoding)
443                         xmlreturncode = xmlservice.newChild(None, 'returncode', str(check['returncode']))
444                         xmloutput     = xmlservice.newChild(None, 'output', encode(check['output'], encoding))
445                         xmloutput.setProp('encoding', encoding)
446                         if check.has_key('timestamp'):
447                                 xmltimestamp  = xmlservice.newChild(None, 'timestamp', str(check['timestamp']))
449         return xmldoc
452 def xml_merge(xmldocs):
453         checks = []
454         for xmldoc in xmldocs:
455                 checks.extend(xml_to_dict(xmldoc))
456         newxmldoc = xml_from_dict(checks)
457         return newxmldoc
460 def check_mark_outdated(check, now, maxtimediff, markold):
461         timedelta = now - check['timestamp']
462         if timedelta > maxtimediff:
463                 check['output'] = 'Nag(ix)SC: Check result is %s(>%s) seconds old - %s' % (timedelta, maxtimediff, check['output'])
464                 if markold:
465                         check['returncode'] = 3
466         return check
469 def reset_future_timestamp(timestamp, now):
470         if timestamp <= now:
471                 return timestamp
472         else:
473                 return now
475 ##############################################################################
477 def encode_multipart(xmldoc, httpuser=None, httppasswd=None):
478         BOUNDARY = mimetools.choose_boundary()
479         CRLF = '\r\n'
480         L = []
481         L.append('--' + BOUNDARY)
482         L.append('Content-Disposition: form-data; name="xmlfile"; filename="xmlfile"')
483         L.append('Content-Type: application/xml')
484         L.append('')
485         L.append(xmldoc.serialize())
486         L.append('--' + BOUNDARY + '--')
487         L.append('')
488         body = CRLF.join(L)
489         content_type = 'multipart/form-data; boundary=%s' % BOUNDARY
490         headers = {'Content-Type': content_type, 'Content-Length': str(len(body))}
492         if httpuser and httppasswd:
493                 headers['Authorization'] = 'Basic %s' % base64.b64encode(':'.join([httpuser, httppasswd]))
495         return (headers, body)
497 ##############################################################################
499 def daemonize(pidfile=None, stdin='/dev/null', stdout='/dev/null', stderr='/dev/null'):
500         # 1st fork
501         try:
502                 pid = os.fork()
503                 if pid > 0:
504                         sys.exit(0)
505         except OSError, e:
506                 sys.stderr.write("1st fork failed: (%d) %s\n" % (e.errno, e.strerror))
507                 sys.exit(1)
508         # Prepare 2nd fork
509         os.chdir("/")
510         os.umask(0)
511         os.setsid( )
512         # 2nd fork
513         try:
514                 pid = os.fork()
515                 if pid > 0:
516                         sys.exit(0)
517         except OSError, e:
518                 sys.stderr.write("2nd fork failed: (%d) %s\n" % (e.errno, e.strerror))
519                 sys.exit(1)
521         # Try to write PID file
522         if pidfile:
523                 pid = str(os.getpid())
524                 try:
525                         file(pidfile, 'w+').write('%s\n' % pid)
526                 except IOError:
527                         sys.stderr.write("Could not write PID file, exiting...\n")
528                         sys.exit(1)
530         # Redirect stdin, stdout, stderr
531         sys.stdout.flush()
532         sys.stderr.flush()
533         si = file(stdin, 'r')
534         so = file(stdout, 'a+')
535         se = file(stderr, 'a+', 0)
536         os.dup2(si.fileno(), sys.stdin.fileno())
537         os.dup2(so.fileno(), sys.stdout.fileno())
538         os.dup2(se.fileno(), sys.stderr.fileno())
540         return
542 ##############################################################################
544 class MyHTTPServer(BaseHTTPServer.HTTPServer):
545         def __init__(self, server_address, HandlerClass, ssl=False, sslpemfile=None):
546                 if ssl:
547                         # FIXME: SSL is in Py2.6
548                         try:
549                                 from OpenSSL import SSL
550                         except:
551                                 print 'No Python OpenSSL wrapper/bindings found!'
552                                 sys.exit(127)
554                         SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
555                         context = SSL.Context(SSL.SSLv23_METHOD)
556                         context.use_privatekey_file (sslpemfile)
557                         context.use_certificate_file(sslpemfile)
558                         self.socket = SSL.Connection(context, socket.socket(self.address_family, self.socket_type))
559                 else:
560                         SocketServer.BaseServer.__init__(self, server_address, HandlerClass)
561                         self.socket = socket.socket(self.address_family, self.socket_type)
563                 self.server_bind()
564                 self.server_activate()
567 class MyHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
568         def setup(self):
569                 self.connection = self.request
570                 self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
571                 self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
573 ##############################################################################
575 def prepare_socket(socket_path):
576         try:
577                 if socket_path.startswith('/'):
578                         s_family=socket.AF_UNIX
579                         s_sockaddr = socket_path
580                 elif socket_path.startswith('unix:'):
581                         s_family=socket.AF_UNIX
582                         s_sockaddr = socket_path[5:]
583                 elif socket_path.find(':') >= 0:
584                         s_port = socket_path.split(':')[-1]
585                         s_host = ':'.join(socket_path.split(':')[:-1])
586                         if s_host.startswith('[') and s_host.endswith(']'):
587                                 s_host = s_host[1:-1]
588                         (s_family, s_socktype, s_proto, s_canonname, s_sockaddr) = socket.getaddrinfo(s_host, s_port, 0, socket.SOCK_STREAM)[0]
589                 else:
590                         return None
591         except:
592                 return None
594         return (s_family, s_sockaddr)
597 def read_socket(s_opts, commands):
598         # print '%20s => %s %s' % (sock, s_family, s_sockaddr)
600         s = socket.socket(s_opts[0], socket.SOCK_STREAM)
601         s.connect(s_opts[1])
602         for line in commands:
603                 if not line.endswith('\n'):
604                         line += '\n'
605                 s.send(line)
606         s.shutdown(socket.SHUT_WR)
608         answer = ''
609         try:
610                 while True:
611                         s.settimeout(10)
612                         data = s.recv(32768)
613                         if data:
614                                 answer += data
615                         else:
616                                 break
617         except socket.timeout:
618                 return ''
620         return answer
623 def livestatus2dict(s_opts, host=None, service=None):
624         checks = []
626         # Get host information only if NO service specified
627         if not service:
628                 commands = []
629                 commands.append('GET hosts\n')
630                 commands.append('Columns: name state plugin_output long_plugin_output last_check\n')
631                 if host:
632                         commands.append('Filter: name = %s' % host)
633                 answer = read_socket(s_opts, commands)
635                 for line in answer.split('\n')[:-1]:
636                         line = line.split(';')
637                         checks.append({'host_name':line[0], 'service_description':None, 'returncode':line[1], 'output':'\n'.join([line[2], line[3]]).rstrip(), 'timestamp':str(line[4])})
639         # Get service information(s)
640         commands = []
641         commands.append('GET services\n')
642         commands.append('Columns: host_name description state plugin_output long_plugin_output last_check\n')
643         if host:
644                 commands.append('Filter: host_name = %s' % host)
645         if service:
646                 commands.append('Filter: description = %s' % service)
648         answer = read_socket(s_opts, commands)
650         for line in answer.split('\n')[:-1]:
651                 line = line.split(';')
652                 checks.append({'host_name':line[0], 'service_description':line[1], 'returncode':line[2], 'output':'\n'.join([line[3], line[4]]).rstrip(), 'timestamp':str(line[5])})
653                                 
655         return checks
656 ##############################################################################