Code

WIP: obsess_daemon.py with additional HTTP support
[nagixsc.git] / obsess_daemon.py
1 #!/usr/bin/python
3 import os
4 import re
5 import sys
6 import time
8 ##############################################################################
9 from nagixsc import *
10 ##############################################################################
12 def check_dir(dirpath):
13         if not os.path.isdir(dirpath):
14                 # dirpath does not exist, try to create it
15                 try:
16                         os.path.os.mkdir(dirpath, 0777) # FIXME
17                 except OSError:
18                         print 'Could not create directory "%s"!' % dirpath
19                         sys.exit(1)
21         if not os.access(dirpath,os.W_OK):
22                 # dirpath is not writeable
23                 print 'No write access to directory "%s"!' % dirpath
24                 sys.exit(1)
27 def read_obsess_file(filename):
28         checks = []
29         f = open(filename)
30         print 'Read ' + filename
32         for line in f:
33                 if line.startswith('LASTSERVICECHECK'):
34                         m = service_analyzer.match(line)
35                         if m:
36                                 checks.append({'host_name':m.group(2), 'service_description':m.group(3), 'returncode':m.group(4), 'output':'\n'.join(m.group(5,6)), 'timestamp':m.group(1)})
38                 elif line.startswith('LASTHOSTCHECK'):
39                         m = host_analyzer.match(line)
40                         if m:
41                                 checks.append({'host_name':m.group(2), 'service_description':None, 'returncode':m.group(3), 'output':'\n'.join(m.group(4,5)), 'timestamp':m.group(1)})
42                 else:
43                         print 'FAIL: ' + line
45         f.close()
46         return checks
49 ##############################################################################
51 if len(sys.argv) != 4:
52         print 'Please call script as: "%s http://SERVER:PORT/ username password"\n' % sys.argv[0]
53         print '... with "http://SERVER:PORT/" your conf2http-URL'
54         print '... and "username"/"password" authentication data for it'
55         sys.exit(1)
57 spoolpath_base = '/tmp/nagixsc.spool'
58 spoolpath_new = os.path.join(spoolpath_base, 'new')
59 spoolpath_work = os.path.join(spoolpath_base, 'work')
60 spoolpath_done = os.path.join(spoolpath_base, 'done')
62 for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
63         check_dir(d)
65 # Output XML files to this directory
66 outdir = os.path.join(spoolpath_base, 'xmlout')
67 check_dir(outdir)
69 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
70 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
72 # Prepare
73 checks = []
74 files_done = []
76 # Check if there are old files in "work" - they didn't get sent!
77 print 'Startup...'
78 if os.listdir(spoolpath_work):
79         for filename in os.listdir(spoolpath_work):
80                 spoolfile = os.path.join(spoolpath_work, filename)
81                 checks.extend(read_obsess_file(spoolfile))
82                 files_done.append(filename)
83         print 'Reloaded %d check results form work dir' % len(checks)
86 print 'Main loop...'
87 while True:
88         if os.listdir(spoolpath_new):
89                 for filename in os.listdir(spoolpath_new):
90                         spoolfile = os.path.join(spoolpath_work, filename)
91                         os.rename(os.path.join(spoolpath_new, filename), spoolfile)
93                         # Work with file
94                         checks.extend(read_obsess_file(spoolfile))
95                         files_done.append(filename)
96         print 'Got %d check results for submitting' % len(checks)
98         if len(checks):
99                 xmldoc = xml_from_dict(checks)
101                 # Write to File
102                 outfilename = str(int(time.time())) + '.xml'
103                 write_xml(xmldoc, os.path.join(outdir, outfilename), None, None)
104                 print 'Written ' + outfilename
106                 # Write to http2nagios
107                 try:
108                         write_xml(xmldoc, sys.argv[1], sys.argv[2], sys.argv[3])
109                 except urllib2.URLError, error:
110                         print error[0]
111                 else:
112                         for filename in files_done:
113                                 os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
114                         checks = []
115                         files_done = []
117         time.sleep(5)