forked from wAmpIre/nagixsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobsess_daemon.py
executable file
·139 lines (113 loc) · 4.44 KB
/
obsess_daemon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python
#
# Nag(ix)SC -- obsess_daemon.py
#
# Copyright (C) 2010 Sven Velt <sv@teamix.net>
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
import os
import re
import sys
import time
##############################################################################
from nagixsc import *
##############################################################################
def check_dir(dirpath):
if not os.path.isdir(dirpath):
# dirpath does not exist, try to create it
try:
os.path.os.mkdir(dirpath, 0777) # FIXME
except OSError:
print 'Could not create directory "%s"!' % dirpath
sys.exit(1)
if not os.access(dirpath,os.W_OK):
# dirpath is not writeable
print 'No write access to directory "%s"!' % dirpath
sys.exit(1)
def read_obsess_file(filename):
checks = []
f = open(filename)
print 'Read ' + filename
count_lines = 0
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
count_lines += 1
print "Read %s lines" % count_lines
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 http2nagios-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')
spoolpath_done = os.path.join(spoolpath_base, 'done')
for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
check_dir(d)
# Output XML files to this directory
outdir = os.path.join(spoolpath_base, 'xmlout')
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):
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
checks.extend(read_obsess_file(spoolfile))
files_done.append(filename)
print 'Got %d check results for submitting' % len(checks)
if len(checks):
xmldoc = xml_from_dict(checks)
# 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)