-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrecord_status
95 lines (85 loc) · 3.38 KB
/
record_status
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
#!/usr/bin/env python
import re
import time
import datetime
import sys
import sqlite3
from optparse import OptionParser
def read_from_a_file(filename, type_r):
f = open(filename, "r")
if type_r == "read":
data = f.read()
else:
data = f.readlines()
f.close()
return data
def get_generic_item_from_match(strip_string, match_string, m):
return [j.replace(strip_string, "") for i in m for j in i.split("\n") if match_string in j]
def create_sql_db(db_name):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute("""create table data(host text, service text, value text, time datetime, unique (host, service, value, time))""")
conn.commit()
cursor.close()
def query_db(db_name, host, service):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
sql_statement = """select host, service, value, time from data where host like ? and service like ?"""
cursor.execute(sql_statement, (host, service) )
result = cursor.fetchall()
cursor.close()
return result
def insert_into_db(db_name, data):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
sql_statement = """insert or ignore into data (host, service, value, time) VALUES (?, ?, ?, ?)"""
for (host, service, perf_num, last_c) in data:
time = datetime.datetime.fromtimestamp(float(last_c))
cursor.execute(sql_statement, (host, service, perf_num, time))
conn.commit()
cursor.close()
def get_services(m):
""" Returns a list of service descriptions for hosts.
If there isn't one then it will be an empty string
and regarded as the host check for a given host.
"""
service_desc_list = []
states = [state for state in m if "\thost_name=" in state]
for i in states:
service_desc = ""
for line in i.split("\n"):
if "\tservice_description" in line:
service_desc = line.replace("\tservice_description=", "")
service_desc = service_desc.replace(" ", "_")
service_desc_list.append(service_desc)
return service_desc_list
def main(data, database_name):
m = [m.group(1) for m in re.finditer(r"[^{]*\{([^}]+)\}", data) if "last_check" in m.group(1)]
host_names = get_generic_item_from_match("\thost_name=", "\thost_name=", m)
last_check = get_generic_item_from_match("\tlast_check=", "\tlast_check=", m)
current_state = get_generic_item_from_match("\tcurrent_state=", "\tcurrent_state=", m)
acked = get_generic_item_from_match("\tproblem_has_been_acknowledged=", "\tproblem_has_been_acknowledged=", m)
services = get_services(m)
perf_data = [j.replace("\tperformance_data=", "").split("\n")[0] for i in m for j in re.findall("\tperformance_data=?[^;]*", i)]
perf_num = []
for i in perf_data:
m = [m.group(0) for m in re.finditer(r"[0-9]?\.?[0-9]+", i)]
if len(m) > 0:
perf_num.append(m[0])
else:
perf_num.append("-1")
assert len (services) == len(last_check), "the number of services listed should equal the number of last_check entries"
insert_into_db(database_name, zip(host_names, services, perf_num, last_check))
if __name__ == "__main__":
database_name = "tempit.db"
try:
create_sql_db(database_name)
except sqlite3.OperationalError, e:
pass
parser = OptionParser()
parser.add_option("-o", "--output", action="store_true", dest="display_query_result", help = "output database query result")
(options, args) = parser.parse_args()
if options.display_query_result:
print query_db(database_name, "%", "%")
data = read_from_a_file("/var/cache/nagios3/status.dat", "read")
main(data, database_name)