-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriggers.py
executable file
·97 lines (85 loc) · 3.27 KB
/
triggers.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
#!/usr/bin/env python3
import os
import time
import weakref
import psycopg2
from prettytable import PrettyTable
from dataclasses import dataclass
@dataclass
class O2ZTriggerRow:
host: str
description: str
priority: int
count: int
class O2ZTriggers:
def __init__(self):
db_params = {
"host": os.getenv("P2Z_PGSQL_HOST"),
"database": os.getenv("P2Z_PGSQL_DB"),
"user": os.getenv("P2Z_PGSQL_UNAME"),
"password": os.getenv("P2Z_PGSQL_PWORD"),
"port": "5432",
}
self.conn = psycopg2.connect(**db_params)
self._finalizer = weakref.finalize(self, self._cleanup_conn, self.conn)
self.trigger_list = None
@staticmethod
def _cleanup_conn(conn):
conn.close()
# Queries the DB directly for loudest triggers
# https://www.zabbix.com/forum/zabbix-troubleshooting-and-problems/26057-reporting-top-10-triggers
# Not that we need it, but this is the source code for the top triggers page.
# It has a few queries we could use
# https://git.zabbix.com/projects/ZT/repos/rsm-scripts/browse/ui/toptriggers.php#26
def get_noisiest_triggers(self, group_id, days_ago, limit):
cursor = self.conn.cursor()
current_time = time.time()
timestamp = current_time - (
days_ago * 24 * 60 * 60
) # 7 days * 24 hours * 60 minutes * 60 seconds
query = f"""
SELECT h.name, t.description, t.priority, COUNT(DISTINCT e.eventid) AS cnt_event
FROM triggers t, events e, functions f, items i, hosts h, hosts_groups hg
WHERE t.triggerid = e.objectid
AND e.source = 0
AND e.object = 0
AND e.clock > {timestamp}
AND t.flags IN ('0', '4')
AND t.priority >= 3
AND f.triggerid = t.triggerid
AND i.itemid = f.itemid
AND i.hostid = h.hostid
AND h.hostid = hg.hostid
AND hg.groupid = {group_id}
GROUP BY h.name, t.description, t.priority -- Added h.name to GROUP BY
ORDER BY cnt_event DESC
LIMIT {limit} OFFSET 0;
"""
cursor.execute(query)
result = cursor.fetchall()
cursor.close()
result.sort(key=lambda tup: tup[3], reverse=True)
self.trigger_list = []
for r in result:
self.trigger_list.append(O2ZTriggerRow(r[0], r[1], r[2], r[3]))
return result
def pretty_print(self):
if self.trigger_list is not None:
t = PrettyTable()
# This is how the table title works:
# Pull the environment variable into title.
# Bail if that fails.
# Capitalize all the words using title()
# Split the string into an array by its commas
# Strip away the final blank item in the array using filter()
title = os.getenv("P2Z_CSV_TITLE")
if title is None:
raise ValueError(
"P2Z_CSV_TITLE is not set. Please set a title for this data!"
)
t.field_names = filter(None, title.title().split(","))
# Dump triggers into this table.
for r in self.trigger_list:
t.add_row([r.host, r.description, r.priority, r.count])
return t
return None