forked from Checkmk/checkmk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_notify_count
executable file
·110 lines (87 loc) · 2.88 KB
/
check_notify_count
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
#!/usr/bin/env python3
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.
import getopt
import os
import sys
import time
import livestatus
def usage():
sys.stderr.write(
"""check_notify_count
USAGE: check_notify_count
check_notify_count -h
OPTIONS:
-h, --help Show this help message and exit
-l PATH Path to livestatus socket (Autodetected in OMD)
-r MINUTES Timerange in the past to observe, given in minutes.
Default is set to 60 minutes.
-w NUM Minumum number of notifications per contact in
timerange to raise a WARNING state.
Disabled by default.
-c NUM Minumum number of notifications per contact in
timerange to raise a CRITICAL state.
Disabled by default.
"""
)
short_options = "hr:w:c:l:"
try:
opts, args = getopt.getopt(sys.argv[1:], short_options)
except getopt.GetoptError as err:
sys.stderr.write("%s\n" % err)
sys.exit(1)
socket_path = ""
timerange = 60
warn, crit = None, None
for o, a in opts:
if o == "-h":
usage()
sys.exit(0)
elif o == "-r":
timerange = int(a)
elif o == "-w":
warn = int(a)
elif o == "-c":
crit = int(a)
elif o == "-l":
socket_path = a
if len(args) == 1:
sys.stderr.write("ERROR: No service pattern given.\n")
sys.exit(1)
if not socket_path and "OMD_ROOT" in os.environ:
socket_path = os.environ["OMD_ROOT"] + "/tmp/run/live"
if not os.path.exists(socket_path):
sys.stderr.write("ERROR: Livestatus socket (%s) does not exist\n" % socket_path)
sys.exit(1)
query = (
"GET log\n"
"Columns: log_contact_name\n"
"Filter: log_time >= %d\n"
"Filter: class = 3\n"
"Stats: state != 999\n"
) % (int(time.time() - (timerange * 60)))
c = livestatus.SingleSiteConnection("unix:" + socket_path)
total_num = 0
perfdata = []
state = 0
details = []
num_users = 0
raw_notifications = 0
for contact_name, num in c.query_table(query):
if contact_name == "check-mk-notify":
raw_notifications += num
else:
total_num += num
if crit is not None and num > crit:
state = max(state, 2)
details.append("%s got %d (> %d)(!!)" % (contact_name, num, crit))
elif warn is not None and num > warn:
state = max(state, 1)
details.append("%s got %d (> %d)(!)" % (contact_name, num, warn))
perfdata.append("%s_num=%d" % (contact_name, num))
detail_txt = " (%s)" % ", ".join(details) if details else ""
sys.stdout.write(
"%d raw notifications, %d user notifications within last %d minutes%s | %s\n"
% (raw_notifications, total_num, timerange, detail_txt, " ".join(perfdata))
)