This repository has been archived by the owner on Jun 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sensuutils.py
executable file
·102 lines (75 loc) · 2.29 KB
/
sensuutils.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
#! /usr/bin/env python
import re
import yaml
def yamlconfig(filename='/etc/sensu/conf.d/settings.yaml'):
'''reads config from yaml file in
same directory as this file
expected format (must start with settings: ) :
---
settings:
key: value
key2: value2
etc.
example:
---
settings:
e_from: "sensu@mailer.com"
e_to: "alerts@mailer.com"
e_smtp_host: "127.0.0.1"
e_smtp_port: "25"
keys/values depend on requirement of each individual
module - e.g. sensu-mailer.py should specify what
file to read and what to expect in the config file
'''
try:
with open(filename) as f:
config = yaml.load(f.read())
except Exception as e:
print('Exception: ' + str(e))
if type(config) is dict:
if 'settings' not in config.keys():
print('Error: config file ' + filename + ' does not contain "settings: "')
else:
return {}
return config
def sanitize(data):
'''santize data/characters from alerts,etc.
that cannot be parsed as json'''
replace = {
'\n': ''
}
for key, val in replace.items():
data = re.sub(key, val, data)
return data
def checkeventstate(event):
'''check state of the alert, occurrences, interval,
refreshes, stashes, etc'''
defaults = {
'occurrences': 1,
'interval': 30,
'refresh': 600
}
if 'occurrences' in event.keys():
occurrences = event['occurrences']
else:
occurrences = defaults['occurrences']
if 'interval' in event.keys():
interval = event['check']['interval']
else:
interval = defaults['interval']
if 'refresh' in event.keys():
refresh = event['check']['refresh']
else:
refresh = defaults['refresh']
if occurrences > defaults['occurrences'] and event['action'] == 'create':
number = refresh / interval
if int(number) == 0 or event['occurrences'] % number == 0:
return True
else:
print('INFO: Only handling every __'
+ str(number) + '__ occurrences')
return False
def checkstashes(event):
'''checks sensu stashes, will return False if a stash
was found so that no alert will be sent'''
return True