Code

obsses: Debug output, typo
[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
31         count_lines = 0
33         for line in f:
34                 if line.startswith('LASTSERVICECHECK'):
35                         m = service_analyzer.match(line)
36                         if m:
37                                 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)})
39                 elif line.startswith('LASTHOSTCHECK'):
40                         m = host_analyzer.match(line)
41                         if m:
42                                 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)})
43                 else:
44                         print 'FAIL: ' + line
45                 count_lines += 1
47         print "Read %s lines" % count_lines
48         f.close()
49         return checks
52 ##############################################################################
54 if len(sys.argv) != 4:
55         print 'Please call script as: "%s http://SERVER:PORT/ username password"\n' % sys.argv[0]
56         print '... with "http://SERVER:PORT/" your http2nagios-URL'
57         print '... and "username"/"password" authentication data for it'
58         sys.exit(1)
60 spoolpath_base = '/tmp/nagixsc.spool'
61 spoolpath_new = os.path.join(spoolpath_base, 'new')
62 spoolpath_work = os.path.join(spoolpath_base, 'work')
63 spoolpath_done = os.path.join(spoolpath_base, 'done')
65 for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
66         check_dir(d)
68 # Output XML files to this directory
69 outdir = os.path.join(spoolpath_base, 'xmlout')
70 check_dir(outdir)
72 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
73 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
75 # Prepare
76 checks = []
77 files_done = []
79 # Check if there are old files in "work" - they didn't get sent!
80 print 'Startup...'
81 if os.listdir(spoolpath_work):
82         for filename in os.listdir(spoolpath_work):
83                 spoolfile = os.path.join(spoolpath_work, filename)
84                 checks.extend(read_obsess_file(spoolfile))
85                 files_done.append(filename)
86         print 'Reloaded %d check results form work dir' % len(checks)
89 print 'Main loop...'
90 while True:
91         if os.listdir(spoolpath_new):
92                 for filename in os.listdir(spoolpath_new):
93                         spoolfile = os.path.join(spoolpath_work, filename)
94                         os.rename(os.path.join(spoolpath_new, filename), spoolfile)
96                         # Work with file
97                         checks.extend(read_obsess_file(spoolfile))
98                         files_done.append(filename)
99         print 'Got %d check results for submitting' % len(checks)
101         if len(checks):
102                 xmldoc = xml_from_dict(checks)
104                 # Write to File
105                 outfilename = str(int(time.time())) + '.xml'
106                 write_xml(xmldoc, os.path.join(outdir, outfilename), None, None)
107                 print 'Written ' + outfilename
109                 # Write to http2nagios
110                 try:
111                         write_xml(xmldoc, sys.argv[1], sys.argv[2], sys.argv[3])
112                 except urllib2.URLError, error:
113                         print error[0]
114                 else:
115                         for filename in files_done:
116                                 os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
117                         checks = []
118                         files_done = []
120         time.sleep(5)