This repository has been archived by the owner on May 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
nagmq_notify.py
151 lines (131 loc) · 4.04 KB
/
nagmq_notify.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
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/python26
import json, time, zmq, re, os, pwd, smtplib
from optparse import OptionParser
from email.mime.text import MIMEText
op = OptionParser(usage = 'email state hostname [-d service] [-f failures] [-g hostgroup] "msg"')
op.add_option("-d", "--service", type="string", action="store", dest="service",
help="Service description of the service of this alert")
op.add_option("-f", "--failures", type="int", action="store", dest="failures",
help="Tolerate n number of failures before alerting", default=0)
op.add_option("-g", "--hostgroup", type="string", action="store", dest="hostgroup",
help="Hostgroup to tolerate failures in")
op.add_option("-t", "--duration", type="string", action="store", dest="duration",
help="Time the host/service has been in the current state")
op.add_option("-i", "--notification-id", type="int", action="store", dest="id",
help="Notification ID; will be set as token in email")
op.add_option("-a", "--alternate-schedule", type="string", action="store",
dest="schedule", help="Key-value list of timeperiods and intervals")
(opts, args) = op.parse_args()
if(len(args) < 4):
print "Did not supply any output to send!"
exit(-1)
contact = args.pop()
stateid = args.pop()
host = args.pop()
ctx = zmq.Context()
reqs = ctx.socket(zmq.REQ)
reqs.connect("ipc:///tmp/nagmqreq.sock")
def check_failues(tolerate):
reqp = { }
states = { }
hgo = None
nfailures = 0
if('service' in opts):
reqp['list_services'] = opts.service
reqp['expand_lists'] = True
if('hostgroup' in opts):
reqp['hostgroup_name'] = opts.hostgroup
reqp['include_hosts'] = True
reqp['keys'] = [ 'type', 'current_state', 'members', 'host_name' ]
reqs.send_json(reqp)
answer = json.loads(reqs.recv())
for o in answer:
if(o['type'] == 'service'):
states[o['host_name']] = o['current_state']
if(o['type'] == 'host' and 'service' not in ops):
states[o['host_name']] = o['current_state']
if(o['type'] == 'hostgroup'):
hgo = o
if('hostgroup' in opts):
if(hgo == None):
return False
for m in hgo['members']:
if(m in states and states[m] != 0):
nfailures += 1
else:
for s in states:
if(s != 0):
nfailures += 1
if(nfailures <= tolerate):
return True
def check_schedules(schedule):
tkvl = re.split(r'\s*,\s*', schedule)
keys = [ 'in_timeperiod', 'type', 'last_notification' ]
for tpkv in tkvl:
tperiod, interval = re.split(r'\s*=\s*', tpkv)[:2]
reqs.send_json({ 'timeperiod_name': schedule, 'keys': keys})
tpo = json.loads(reqs.recv())[0]
if(tpo == None):
continue
if(tpo['in_timeperiod'] == False):
continue
if(service in opts):
reqs.send_json({ 'host_name': opts.host,
'service_description': opts.service, 'keys': keys })
else:
reqs.send_json({ 'host_name': opts.host, 'keys': keys })
tpo = json.loads(reqs.recv())[0]
if(tpo['last_notification'] + (interval * 60) >= time.time()):
return True
return False
if(failures in opts and not check_failures(opts.failures)):
exit(0)
if(schedule in opts and not check_schedules(opts.schedule)):
exit(0)
msgtxt = None
state = None
if(service in opts):
if(opts.state == 0):
state = 'OK'
elif(opts.state == 1):
state = 'WARNING'
elif(opts.state == 2):
state = 'CRITICAL'
elif(opts.state == 3):
state = 'UNKNOWN'
summary = args.join('\n')
msgtxt = """
Summary: {0}\n
\n
Host:\t\t{2}\n
Service:\t\t{3}\n
State:\t\t{4}\n
Duration:\t\t{5}\n
Token:\t\t{6}""".format(summary, opts.host, opts.service,
state, opts.duration, opts.id)
else:
if(opts.state < 2):
state = 'UP'
else:
state = 'DOWN'
summary = args.join('\n')
msgtxt = """
Summary: {0}\n
\n
Host:\t\t{2}\n
State:\t\t{3}\n
Duration:\t\t{4}\n
Token:\t\{5}\n""".format(summary, opts.long, opts.host,
opts.service, state, opts.duration, opts.id)
msg = MIMEText(msgtxt)
msg['To'] = opts.contact
msg['From'] = 'symon@columbia.edu'
if(service in opts):
msg['Subject'] = '{0}: {1}@{2}'.format(
state, opts.service, opts.host)
else:
msg['Subject'] = '{0}: {1}'.format(
state, opts.host)
smtp = smtplib.SMTP('localhost')
smtp.sendmail('symon@columbia.edu', opts.contact, msg.as_string())
smtp.quit()