1712c4e1e7acdb674e4d4a1b7b38f00a589b37cd
1 #!/usr/bin/python
3 import os
4 import re
5 import time
7 ##############################################################################
8 from nagixsc import *
9 ##############################################################################
11 def check_dir(dirpath):
12 if not os.path.isdir(dirpath):
13 # dirpath does not exist, try to create it
14 try:
15 os.path.os.mkdir(dirpath, 0777) # FIXME
16 except OSError:
17 print 'Could not create directory "%s"!' % dirpath
18 sys.exit(1)
20 if not os.access(dirpath,os.W_OK):
21 # dirpath is not writeable
22 print 'No write access to directory "%s"!' % dirpath
23 sys.exit(1)
25 ##############################################################################
27 spoolpath_base = '/tmp/nagixsc.spool'
28 spoolpath_new = os.path.join(spoolpath_base, 'new')
29 spoolpath_work = os.path.join(spoolpath_base, 'work')
30 spoolpath_done = os.path.join(spoolpath_base, 'done')
32 for d in [spoolpath_base, spoolpath_new, spoolpath_work, spoolpath_done]:
33 check_dir(d)
35 # Output XML files to this directory
36 outdir = os.path.join(spoolpath_base, 'xmlout')
37 check_dir(outdir)
39 service_analyzer = re.compile("^LASTSERVICECHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+SERVICEDESC::'?([^']+)'?\s+SERVICESTATEID::'?(\d+)'?\s+SERVICEOUTPUT::'?([^']*)'?\s+LONGSERVICEOUTPUT::'?([^']*)'?$")
40 host_analyzer = re.compile("LASTHOSTCHECK::'?(\d+)'?\s+HOSTNAME::'?([^']+)'?\s+HOSTSTATEID::'?(\d+)'?\s+HOSTOUTPUT::'?([^']*)'?\s+LONGHOSTOUTPUT::'?([^']*)'?$")
43 while True:
44 if os.listdir(spoolpath_new):
45 checks = []
46 files_done = []
47 for filename in os.listdir(spoolpath_new):
48 spoolfile = os.path.join(spoolpath_work, filename)
49 os.rename(os.path.join(spoolpath_new, filename), spoolfile)
51 # Work with file
52 f = open(spoolfile)
54 print 'Read ' + spoolfile
55 for line in f:
56 if line.startswith('LASTSERVICECHECK'):
57 m = service_analyzer.match(line)
58 if m:
59 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)})
61 elif line.startswith('LASTHOSTCHECK'):
62 m = host_analyzer.match(line)
63 if m:
64 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)})
66 f.close()
67 files_done.append(filename)
69 outfilename = str(int(time.time())) + '.xml'
70 xmldoc = xml_from_dict(checks)
71 xmldoc.saveFile(os.path.join(outdir, outfilename)) # FIXME
72 for filename in files_done:
73 os.rename(os.path.join(spoolpath_work, filename), os.path.join(spoolpath_done, filename))
74 print 'Written ' + outfilename
76 time.sleep(5)