Code

WIP: obsess_daemon.py with additional HTTP support
authorSven Velt <sven@velt.de>
Thu, 23 Sep 2010 14:13:20 +0000 (16:13 +0200)
committerSven Velt <sven@velt.de>
Thu, 23 Sep 2010 14:13:20 +0000 (16:13 +0200)
Please give "URL user pass" as command line options

Signed-off-by: Sven Velt <sven@velt.de>
obsess_daemon.py

index 1712c4e1e7acdb674e4d4a1b7b38f00a589b37cd..01021a4ce8a736f0c3e15f4c252a164cf3dd3a6a 100755 (executable)
@@ -2,6 +2,7 @@
 
 import os
 import re
+import sys
 import time
 
 ##############################################################################
@@ -22,8 +23,37 @@ def check_dir(dirpath):
                print 'No write access to directory "%s"!' % dirpath
                sys.exit(1)
 
+
+def read_obsess_file(filename):
+       checks = []
+       f = open(filename)
+       print 'Read ' + filename
+
+       for line in f:
+               if line.startswith('LASTSERVICECHECK'):
+                       m = service_analyzer.match(line)
+                       if m:
+                               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)})
+
+               elif line.startswith('LASTHOSTCHECK'):
+                       m = host_analyzer.match(line)
+                       if m:
+                               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)})
+               else:
+                       print 'FAIL: ' + line
+
+       f.close()
+       return checks
+
+
 ##############################################################################
 
+if len(sys.argv) != 4:
+       print 'Please call script as: "%s http://SERVER:PORT/ username password"\n' % sys.argv[0]
+       print '... with "http://SERVER:PORT/" your conf2http-URL'
+       print '... and "username"/"password" authentication data for it'
+       sys.exit(1)
+
 spoolpath_base = '/tmp/nagixsc.spool'
 spoolpath_new = os.path.join(spoolpath_base, 'new')
 spoolpath_work = os.path.join(spoolpath_base, 'work')
@@ -39,39 +69,50 @@ check_dir(outdir)
 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
 
+# Prepare
+checks = []
+files_done = []
+
+# Check if there are old files in "work" - they didn't get sent!
+print 'Startup...'
+if os.listdir(spoolpath_work):
+       for filename in os.listdir(spoolpath_work):
+               spoolfile = os.path.join(spoolpath_work, filename)
+               checks.extend(read_obsess_file(spoolfile))
+               files_done.append(filename)
+       print 'Reloaded %d check results form work dir' % len(checks)
+
 
+print 'Main loop...'
 while True:
        if os.listdir(spoolpath_new):
-               checks = []
-               files_done = []
                for filename in os.listdir(spoolpath_new):
                        spoolfile = os.path.join(spoolpath_work, filename)
                        os.rename(os.path.join(spoolpath_new, filename), spoolfile)
 
                        # Work with file
-                       f = open(spoolfile)
-
-                       print 'Read ' + spoolfile
-                       for line in f:
-                               if line.startswith('LASTSERVICECHECK'):
-                                       m = service_analyzer.match(line)
-                                       if m:
-                                               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)})
-
-                               elif line.startswith('LASTHOSTCHECK'):
-                                       m = host_analyzer.match(line)
-                                       if m:
-                                               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)})
-
-                       f.close()
+                       checks.extend(read_obsess_file(spoolfile))
                        files_done.append(filename)
+       print 'Got %d check results for submitting' % len(checks)
 
-               outfilename = str(int(time.time())) + '.xml'
+       if len(checks):
                xmldoc = xml_from_dict(checks)
-               xmldoc.saveFile(os.path.join(outdir, outfilename)) # FIXME
-               for filename in files_done:
-                       os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
+
+               # Write to File
+               outfilename = str(int(time.time())) + '.xml'
+               write_xml(xmldoc, os.path.join(outdir, outfilename), None, None)
                print 'Written ' + outfilename
 
+               # Write to http2nagios
+               try:
+                       write_xml(xmldoc, sys.argv[1], sys.argv[2], sys.argv[3])
+               except urllib2.URLError, error:
+                       print error[0]
+               else:
+                       for filename in files_done:
+                               os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
+                       checks = []
+                       files_done = []
+
        time.sleep(5)