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; either version 2 of the License, or (at your
10 # option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with this program; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import os
22 import re
23 import sys
24 import time
26 ##############################################################################
27 from nagixsc import *
28 ##############################################################################
30 def check_dir(dirpath):
31 if not os.path.isdir(dirpath):
32 # dirpath does not exist, try to create it
33 try:
34 os.path.os.mkdir(dirpath, 0777) # FIXME
35 except OSError:
36 print 'Could not create directory "%s"!' % dirpath
37 sys.exit(1)
39 if not os.access(dirpath,os.W_OK):
40 # dirpath is not writeable
41 print 'No write access to directory "%s"!' % dirpath
42 sys.exit(1)
45 def read_obsess_file(filename):
46 checks = []
47 f = open(filename)
48 print 'Read ' + filename
49 count_lines = 0
51 for line in f:
52 if line.startswith('LASTSERVICECHECK'):
53 m = service_analyzer.match(line)
54 if m:
55 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)})
57 elif line.startswith('LASTHOSTCHECK'):
58 m = host_analyzer.match(line)
59 if m:
60 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)})
61 else:
62 print 'FAIL: ' + line
63 count_lines += 1
65 print "Read %s lines" % count_lines
66 f.close()
67 return checks
70 ##############################################################################
72 if len(sys.argv) != 4:
73 print 'Please call script as: "%s http://SERVER:PORT/ username password"\n' % sys.argv[0]
74 print '... with "http://SERVER:PORT/" your http2nagios-URL'
75 print '... and "username"/"password" authentication data for it'
76 sys.exit(1)
78 spoolpath_base = '/tmp/nagixsc.spool'
79 spoolpath_new = os.path.join(spoolpath_base, 'new')
80 spoolpath_work = os.path.join(spoolpath_base, 'work')
81 spoolpath_done = os.path.join(spoolpath_base, 'done')
83 for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
84 check_dir(d)
86 # Output XML files to this directory
87 outdir = os.path.join(spoolpath_base, 'xmlout')
88 check_dir(outdir)
90 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
91 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
93 # Prepare
94 checks = []
95 files_done = []
97 # Check if there are old files in "work" - they didn't get sent!
98 print 'Startup...'
99 if os.listdir(spoolpath_work):
100 for filename in os.listdir(spoolpath_work):
101 spoolfile = os.path.join(spoolpath_work, filename)
102 checks.extend(read_obsess_file(spoolfile))
103 files_done.append(filename)
104 print 'Reloaded %d check results form work dir' % len(checks)
107 print 'Main loop...'
108 while True:
109 if os.listdir(spoolpath_new):
110 for filename in os.listdir(spoolpath_new):
111 spoolfile = os.path.join(spoolpath_work, filename)
112 os.rename(os.path.join(spoolpath_new, filename), spoolfile)
114 # Work with file
115 checks.extend(read_obsess_file(spoolfile))
116 files_done.append(filename)
117 print 'Got %d check results for submitting' % len(checks)
119 if len(checks):
120 xmldoc = xml_from_dict(checks)
122 # Write to File
123 outfilename = str(int(time.time())) + '.xml'
124 write_xml(xmldoc, os.path.join(outdir, outfilename), None, None)
125 print 'Written ' + outfilename
127 # Write to http2nagios
128 try:
129 write_xml(xmldoc, sys.argv[1], sys.argv[2], sys.argv[3])
130 except urllib2.URLError, error:
131 print error[0]
132 else:
133 for filename in files_done:
134 os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
135 checks = []
136 files_done = []
138 time.sleep(5)