forked from madflojo/automatron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monitoring.py
238 lines (211 loc) · 8.72 KB
/
monitoring.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
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
'''
Automatron: Monitoring
* Get all Targets and Runbooks
* Schedule Checks
* Execute Checks per schedule
* Notify Check status
* Listen for Runbook and Target changes
* Reschedule Checks
'''
import sys
import signal
import json
import tempfile
import types
import fabric.api
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.triggers.cron import CronTrigger
import core.common
import core.logs
import core.db
import core.fab
def monitor(runbook, target, config, dbc, logger):
''' Execute monitor against target '''
# Clear out APSchedulers default loggin
import logging
logging.getLogger('apscheduler.scheduler').setLevel('WARNING')
logging.getLogger('apscheduler.scheduler').propagate = False
runbook_status = {
'msg_type' : 'runbook_status',
'runbook' : runbook,
'target' : target['hostname'],
'checks' : {}
}
logger.debug("Executing runbook {0} against target {1}".format(runbook, target['hostname']))
# Setup default fabric environment
fabric.api.env = core.fab.set_env(config, fabric.api.env)
fabric.api.env.host_string = target['ip']
for check_name in target['runbooks'][runbook]['checks'].keys():
check = target['runbooks'][runbook]['checks'][check_name]
# Check for Credentials override
if "credentials" in check:
fabric.api.env = core.fab.set_env(config, fabric.api.env, override=check['credentials'])
# Start Execution for plugin type
if "plugin" in check['type']:
plugin_file = check['plugin']
plugin_file = '{0}/checks/{1}'.format(config['plugin_path'], plugin_file)
dest_name = next(tempfile._get_candidate_names())
destination = "{0}/{1}".format(config['monitoring']['upload_path'], dest_name)
with fabric.api.hide('output', 'running', 'warnings'):
try:
if "target" in check["execute_from"]:
logger.debug("Placing plugin script into {0}".format(destination))
fabric.api.put(plugin_file, destination)
fabric.api.run("chmod 700 {0}".format(destination))
cmd = "{0} {1}".format(destination, check['args'])
results = fabric.api.run(cmd)
fabric.api.run("rm {0}".format(destination))
elif "remote" in check["execute_from"]:
cmd = "{0} {1}".format(plugin_file, check['args'])
results = fabric.api.local(cmd, capture=True)
else:
logger.warn('Unknown "execute_from" specified in check')
return False
except Exception as e:
logger.debug("Could not put plugin file {0} on remote host {1}".format(
plugin_file, target['ip']))
# Start Executing command type
else:
cmd = check['cmd']
# Perform Check
with fabric.api.hide('output', 'running', 'warnings'):
try:
if "target" in check["execute_from"]:
results = fabric.api.run(cmd)
elif "remote" in check["execute_from"]:
results = fabric.api.local(cmd, capture=True)
else:
logger.warn('Unknown "execute_from" specified in check')
return False
except Exception as e:
logger.debug("Could not execute command {0}".format(cmd))
# Check results
if results.return_code == 0:
check_return = "OK"
elif results.return_code == 1:
check_return = "WARNING"
elif results.return_code == 2:
check_return = "CRITICAL"
else:
check_return = "UNKNOWN"
runbook_status['checks'][check_name] = check_return
logger.info("Check {0} for target {1} returned {2}".format(
check_name, target['hostname'], check_return))
dbc.notify("check:results", runbook_status)
def schedule(scheduler, runbook, target, config, dbc, logger):
''' Setup schedule for new runbooks and targets '''
# Default schedule (every minute)
task_schedule = {
'second' : 0,
'minute' : '*',
'hour' : '*',
'day' : '*',
'month' : '*',
'day_of_week' : '*'
}
# If schedule is present override default
if 'schedule' in target['runbooks'][runbook].keys():
if type(target['runbooks'][runbook]['schedule']) == types.DictType:
for key in target['runbooks'][runbook]['schedule'].keys():
task_schedule[key] = target['runbooks'][runbook]['schedule'][key]
elif type(target['runbooks'][runbook]['schedule']) == types.StringType:
breakdown = target['runbooks'][runbook]['schedule'].split(" ")
task_schedule = {
'second' : 0,
'minute' : breakdown[0],
'hour' : breakdown[1],
'day' : breakdown[2],
'month' : breakdown[3],
'day_of_week' : breakdown[4]
}
cron = CronTrigger(
second=task_schedule['second'],
minute=task_schedule['minute'],
hour=task_schedule['hour'],
day=task_schedule['day'],
month=task_schedule['month'],
day_of_week=task_schedule['day_of_week'],
)
return scheduler.add_job(
monitor,
trigger=cron,
args=[runbook, target, config, dbc, logger]
)
def listen(scheduler, config, dbc, logger):
''' Listen for new events and schedule runbooks '''
logger.info("Starting subscription to monitors channel")
pubsub = dbc.subscribe("monitors")
for msg in pubsub.listen():
logger.debug("Got message: {0}".format(msg))
try:
item = dbc.process_subscription(msg)
logger.debug("Received {0} notification for {1}".format(
item['msg_type'], item['target']))
target = dbc.get_target(target_id=item['target'])
logger.debug("Found target: {0}".format(json.dumps(target)))
job = schedule(scheduler, item['runbook'], target, config, dbc, logger)
if job:
name = "{0}:{1}".format(
target['runbooks'][item['runbook']]['name'],
target['hostname'])
jobs.update({name : job})
logger.info("Scheduled runbook {0} for target {1}".format(
item['runbook'], item['target']))
except Exception as e:
logger.warn("Unable to process message: {0}".format(e.message))
def initialize(config, dbc, scheduler, logger):
''' Grab existing targets and setup monitors '''
targets = dbc.get_target()
scheduled = 0
jobs = {}
for target in targets.keys():
for runbook in targets[target]['runbooks'].keys():
job = schedule(scheduler, runbook, targets[target], config, dbc, logger)
if job:
name = "{0}:{1}".format(
targets[target]['runbooks'][runbook]['name'],
target)
logger.debug("Scheduled runbook {0} for target {1}".format(runbook, target))
jobs.update({name : job})
scheduled = scheduled + 1
return jobs, scheduled
def shutdown(signum, frame):
''' Shutdown this process '''
dbc.disconnect()
# Remove jobs
for job in jobs:
jobs[job].remove()
if signum == 15 or signum == 2:
logger.info("Received signal {0} shutting down".format(signum))
sys.exit(0)
elif signum == 0:
sys.exit(1)
else:
logger.error("Received signal {0} shutting down".format(signum))
sys.exit(1)
if __name__ == "__main__":
config = core.common.get_config(description="Automatron: Monitoring")
if config is False:
print "Could not get configuration"
sys.exit(1)
# Setup Logging
logs = core.logs.Logger(config=config, proc_name="monitoring")
logger = logs.getLogger()
# Listen for signals
signal.signal(signal.SIGTERM, shutdown)
signal.signal(signal.SIGINT, shutdown)
# Open Datastore Connection
db = core.db.SetupDatastore(config=config)
try:
dbc = db.get_dbc()
except Exception as e:
logger.error("Failed to connect to datastore: {0}".format(e.message))
shutdown(0, None)
# Start Scheduler
scheduler = BackgroundScheduler()
scheduler.start()
logger.info("Grabbing targets for initial scheduling")
jobs, scheduled = initialize(config, dbc, scheduler, logger)
logger.info("Scheduled {0} checks".format(scheduled))
while True:
listen(scheduler, config, dbc, logger)