1 #!/usr/bin/python
2 #
3 # Nag(ix)SC -- obsess_daemon.py
4 #
5 # Copyright (C) 2010 Sven Velt <sv@teamix.net>
6 #
7 # This program is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by the
9 # Free Software Foundation; only version 2 of the License is applicable.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with this program; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 import os
21 import re
22 import sys
23 import time
25 ##############################################################################
26 from nagixsc import *
27 ##############################################################################
29 def check_dir(dirpath):
30 if not os.path.isdir(dirpath):
31 # dirpath does not exist, try to create it
32 try:
33 os.path.os.mkdir(dirpath, 0777) # FIXME
34 except OSError:
35 print 'Could not create directory "%s"!' % dirpath
36 sys.exit(1)
38 if not os.access(dirpath,os.W_OK):
39 # dirpath is not writeable
40 print 'No write access to directory "%s"!' % dirpath
41 sys.exit(1)
44 def read_obsess_file(filename):
45 checks = []
46 f = open(filename)
47 print 'Read ' + filename
48 count_lines = 0
50 for line in f:
51 if line.startswith('LASTSERVICECHECK'):
52 m = service_analyzer.match(line)
53 if m:
54 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)})
56 elif line.startswith('LASTHOSTCHECK'):
57 m = host_analyzer.match(line)
58 if m:
59 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)})
60 else:
61 print 'FAIL: ' + line
62 count_lines += 1
64 print "Read %s lines" % count_lines
65 f.close()
66 return checks
69 ##############################################################################
71 if len(sys.argv) != 4:
72 print 'Please call script as: "%s http://SERVER:PORT/ username password"\n' % sys.argv[0]
73 print '... with "http://SERVER:PORT/" your http2nagios-URL'
74 print '... and "username"/"password" authentication data for it'
75 sys.exit(1)
77 spoolpath_base = '/tmp/nagixsc.spool'
78 spoolpath_new = os.path.join(spoolpath_base, 'new')
79 spoolpath_work = os.path.join(spoolpath_base, 'work')
80 spoolpath_done = os.path.join(spoolpath_base, 'done')
82 for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
83 check_dir(d)
85 # Output XML files to this directory
86 outdir = os.path.join(spoolpath_base, 'xmlout')
87 check_dir(outdir)
89 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
90 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
92 # Prepare
93 checks = []
94 files_done = []
96 # Check if there are old files in "work" - they didn't get sent!
97 print 'Startup...'
98 if os.listdir(spoolpath_work):
99 for filename in os.listdir(spoolpath_work):
100 spoolfile = os.path.join(spoolpath_work, filename)
101 checks.extend(read_obsess_file(spoolfile))
102 files_done.append(filename)
103 print 'Reloaded %d check results form work dir' % len(checks)
106 print 'Main loop...'
107 while True:
108 if os.listdir(spoolpath_new):
109 for filename in os.listdir(spoolpath_new):
110 spoolfile = os.path.join(spoolpath_work, filename)
111 os.rename(os.path.join(spoolpath_new, filename), spoolfile)
113 # Work with file
114 checks.extend(read_obsess_file(spoolfile))
115 files_done.append(filename)
116 print 'Got %d check results for submitting' % len(checks)
118 if len(checks):
119 xmldoc = xml_from_dict(checks)
121 # Write to File
122 outfilename = str(int(time.time())) + '.xml'
123 write_xml(xmldoc, os.path.join(outdir, outfilename), None, None)
124 print 'Written ' + outfilename
126 # Write to http2nagios
127 try:
128 write_xml(xmldoc, sys.argv[1], sys.argv[2], sys.argv[3])
129 except urllib2.URLError, error:
130 print error[0]
131 else:
132 for filename in files_done:
133 os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
134 checks = []
135 files_done = []
137 time.sleep(5)