-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent_handler.py
More file actions
130 lines (113 loc) · 4.33 KB
/
event_handler.py
File metadata and controls
130 lines (113 loc) · 4.33 KB
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
#!/usr/bin/python
__author__ = 'anson'
import optparse
import json
from lib_monitor.event_handler_default import api_setting, gen_node_event, gen_disk_event, gen_network_event
from requests.exceptions import ConnectionError
class handle_event:
def __init__(self, api):
self.api = api
def node(self, ip, state_id, event_input):
for event in event_input:
json_body, api_url = gen_node_event(ip, state_id, event)
# If both is None, it means this event does not need to be sent.
if json_body is None and api_url is None:
continue
try:
res = self.api.post(api_url, data=json.dumps(json_body))
if res.status_code != 200:
return False
except ConnectionError:
return False
# If nagios state is ok, only send normal event once.
if state_id == 0:
break
return True
def disk(self, ip, event_input):
for event in event_input:
if event == "none":
continue
json_body, api_url = gen_disk_event(ip, event)
# If both is None, it means this event does not need to be sent.
if json_body is None and api_url is None:
continue
try:
res = self.api.post(api_url, data=json.dumps(json_body))
if res.status_code != 200:
return False
except ConnectionError:
return False
return True
def network(self, ip, state_id, event_input):
for event in event_input:
json_body, api_url = gen_network_event(ip, event)
# If both is None, it means this event does not need to be sent.
if json_body is None and api_url is None:
continue
try:
res = self.api.post(api_url, data=json.dumps(json_body))
if res.status_code != 200:
return False
except ConnectionError:
return False
return True
def foo_callback(option, opt, value, parser):
setattr(parser.values, option.dest, value.split(','))
def main():
"""
event_handler.py [--type] [--input]
"""
parser = optparse.OptionParser(
usage="%prog [options] [--parameter]",
description="Event handler for nagios state change"
)
parser.add_option("--ip",
dest="ip",
help="Node ip",
type="string",
default="127.0.0.1"
)
parser.add_option("-t", "--type",
dest="type",
help="Node, disk or network event",
type="string",
default="node"
)
parser.add_option("--input",
dest="input",
help="Get output from monitor",
type="string",
action='callback',
callback=foo_callback
)
parser.add_option("--long_input",
dest="longinput",
help="Get long_output from monitor",
type="string",
action='callback',
callback=foo_callback
)
parser.add_option("-s", "--state",
dest="stateID",
help="host state ID (default: 0)",
type="int",
default=0
)
parser.add_option("--hoststatetype",
dest="statetype",
help="host state type (default: none)",
default=None
)
(options, args) = parser.parse_args()
api = api_setting()
event = handle_event(api.manager())
if options.statetype == 'HARD' and options.type == 'node':
event.node(options.ip, options.stateID, options.input)
elif options.statetype == 'HARD' and options.type == 'disk' and options.stateID == 0:
event.disk(options.ip, options.longinput)
elif options.statetype == 'HARD' and options.type == 'disk' and options.stateID == 3:
event.disk(options.ip, options.input)
elif options.statetype == 'HARD' and options.type == 'network':
event.network(options.ip, options.stateID, options.input)
if __name__ == '__main__':
main()