forked from madflojo/RunbookWraps
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreactor.py
More file actions
executable file
·240 lines (198 loc) · 7.93 KB
/
Copy pathreactor.py
File metadata and controls
executable file
·240 lines (198 loc) · 7.93 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#!/usr/bin/env python
#####################################################################
#### reactor.py | Runbook Local Reactor
#### -------------------------
#### This wrapper can be used to execute reactions locally when monitors fail. The agent
#### will query a Runbook webhook endpoint for the defined monitors. If those monitors
#### are in a "failed" status the wrapper will execute the scripts defined in the YAML
#### config file.
#### -------------------------
#### Sample Config:
####
#### You can find a sample configurations in config/
#### -------------------------
#### Benjamin Cane | @madflojo
#####################################################################
#### Modules
#########################
## Base
import json
import sys
import getopt
import subprocess
import os
## Non-Base but needed
try:
import requests
except:
print "This application requires the requests python module. Please install it to continue"
sys.exit(2)
try:
import yaml
except:
print "This application requires the pyYAML python module. Please install it to continue"
sys.exit(2)
#### Core Functions
#########################
def usage(cmd):
''' Print usage summary '''
print("Usage: %s -c config_file [--debug]") % cmd
## Insecure option is not presented as it should never be used
def main(arguments):
''' Grab command line args and assign them properly '''
## Grab args from sys.argv
try:
opts, args = getopt.getopt(arguments[1:], "c:di", ["config=", "debug", "insecure"])
except getopt.GetoptError as err:
usage(arguments[0])
sys.exit(2)
## Define defaults
config = None
debug = False
insecure = False
## Go through opts and set given args
for opt, arg in opts:
if opt == "-c" or opt == "--config":
config = arg
elif opt == "-d" or opt == "--debug":
debug = True
elif opt == "-i" or opt == "--insecure":
insecure = True
else:
usage(arguments[0])
sys.exit(2)
## Verify we got a config or exit
if config:
return config, debug, insecure
else:
usage(sys.argv[0])
sys.exit(2)
#### Main Functions
#########################
def crAPI(url, check_key):
''' Call Runbook API to change or update the monitors status '''
## Create json data
data = { 'check_key': check_key,
'action': "status" }
payload = json.dumps(data)
## Set headers
headers = {'content-type': 'application/json'}
## If insecure set verify to false
if insecure:
verify = False
else:
verify = True
if debug:
print "-" * 25
print("Sending %s to %s") % (payload, url)
## Perform request
try:
req = requests.post(url, data=payload, headers=headers, verify=verify)
except requests.exceptions.SSLError:
print("[Reactor Wrapper] Error communicating with Runbook, got SSL certificate error")
print("Exiting...")
sys.exit(2)
## If debug print the status code
if debug:
print "-" * 25
print("[Reactor Wrapper Debug] Sent Request to Runbook and got return code: %r") % req.status_code
print "-" * 25
print(req.text)
print "-" * 25
## Verify we got a 200 return code
if req.status_code == 200:
## Verify we got a success message
if "success" in req.text:
## Put reply into reply dict
reply = json.loads(req.text)
## Check for status key
if "status" in reply:
## return status key and failure count
return reply['status'], reply['failcount']
else:
print("[Reactor Wrapper] Didn't get a status from url: %s") % url
return False
else:
print("[Reactor Wrapper] Error checking status from Runbook for url: %s") % url
return False
else:
print("[Reactor Wrapper] Error checking status from Runbook for url: %s") % url
return False
#### Main Execution
#########################
if __name__ == '__main__':
## Grab the provided command line arguments and get what we need
configfile, debug, insecure = main(sys.argv)
## Check if config is a file and load configurations
if os.path.isfile(configfile):
fh = open(configfile, "r")
config = yaml.load(fh.read())
fh.close()
else:
print("Error: File not found - %s") % configfile
usage(sys.argv[0])
sys.exit(1)
## Loop through monitors
for key in config['monitors'].keys():
## Set monitor to monitor object
monitor = config['monitors'][key]
print("[Reactor Wrapper] Starting to check monitor: %s") % key
## Verify Needed Configs are Here
run = True
## url is required
if monitor['url'] is None:
run = False
print("[Reactor Wrapper] Skipping monitor as it doesn't have a url defined: %s") % key
## check_key is required
if monitor['check_key'] is None:
run = False
print("[Reactor Wrapper] Skipping monitor as it doesn't have a check_key defined: %s") % key
## If url and check_key are present make request
if run:
## Perform API call
status, failcount = crAPI(monitor['url'], monitor['check_key'])
print("[Reactor Wrapper] Checked Monitor %s and got status %s") % (key, status)
if "reactions" in monitor:
for rkey in monitor['reactions'].keys():
reaction = monitor['reactions'][rkey]
print("[Reactor Wrapper] Checking if I should run reaction %r for check %r") % (rkey, key)
## Verify necessary reaction configs are available
runreaction = True
## cmd is required
if "cmd" in reaction:
cmd = reaction['cmd']
else:
runreaction = False
print("[Reactor Wrapper] Skipping reaction as it doesn't have a cmd defined: %s") % rkey
## args are optional
if "args" in reaction:
args = " " + reaction['args']
else:
args = ""
## callon is required and should be either healthy or failed
if reaction['callon'] != status:
runreaction = False
if debug:
print("[Reactor Wrapper Debug] Skipping as this reaction is defined for %s") % reaction['callon']
## trigger is required and should be less than failcount to run
if reaction['trigger'] <= failcount:
print("[Reactor Wrapper] failcount %d is greater than or equal to trigger %d") % (failcount, reaction['trigger'])
else:
runreaction = False
print("[Reactor Wrapper] Skipping reaction as we have not reached an appropriate failcount: %d") % failcount
## If everything checks out run the reaction
if runreaction:
try:
print "-" * 25
print "Execution Output:"
code = subprocess.call(cmd + args, shell=True)
print "-" * 25
if debug:
print("[Reactor Wrapper Debug] Execution exited with %d") % code
except OSError as e:
print("[Reactor Wrapper] Execution Error: %s") % e
else:
print("[Reactor Wrapper] Skipping monitor %s as there are no reactions defined") % key
## Print fancy formatting at the end of each monitor
print "#" * 50
print("")