12
12
import requests
13
13
try :
14
14
from urlparse import urlparse
15
- except :
15
+ except ImportError :
16
16
from urllib .parse import urlparse
17
17
from utils import *
18
18
19
+
19
20
class SensuHandler (object ):
20
21
def __init__ (self ):
21
22
# Parse the stdin into a global event object
@@ -29,7 +30,7 @@ def __init__(self):
29
30
# Filter (deprecated) and handle
30
31
self .filter ()
31
32
self .handle ()
32
-
33
+
33
34
def read_event (self , check_result ):
34
35
'''
35
36
Convert the piped check result (json) into a global 'event' dict
@@ -52,31 +53,31 @@ def handle(self):
52
53
def filter (self ):
53
54
'''
54
55
Filters exit the proccess if the event should not be handled.
55
-
56
56
Filtering events is deprecated and will be removed in a future release.
57
57
'''
58
58
59
59
if self .deprecated_filtering_enabled ():
60
- print ('warning: event filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin' )
60
+ print ('warning: event filtering in sensu-plugin is deprecated,' +
61
+ 'see http://bit.ly/sensu-plugin' )
61
62
self .filter_disabled ()
62
63
self .filter_silenced ()
63
64
self .filter_dependencies ()
64
65
65
66
if self .deprecated_occurrence_filtering_enabled ():
66
- print ('warning: occurrence filtering in sensu-plugin is deprecated, see http://bit.ly/sensu-plugin' )
67
+ print ('warning: occurrence filtering in sensu-plugin is' +
68
+ 'deprecated, see http://bit.ly/sensu-plugin' )
67
69
self .filter_repeated ()
68
70
69
71
def deprecated_filtering_enabled (self ):
70
72
'''
71
73
Evaluates whether the event should be processed by any of the
72
74
filter methods in this library. Defaults to true,
73
75
i.e. deprecated filters are run by default.
74
-
76
+
75
77
returns bool
76
78
'''
77
79
return self .event ['check' ].get ('enable_deprecated_filtering' , False )
78
80
79
-
80
81
def deprecated_occurrence_filtering_enabled (self ):
81
82
'''
82
83
Evaluates whether the event should be processed by the
@@ -86,7 +87,8 @@ def deprecated_occurrence_filtering_enabled(self):
86
87
returns bool
87
88
'''
88
89
89
- return self .event ['check' ].get ('enable_deprecated_occurrence_filtering' , False )
90
+ return self .event ['check' ].get (
91
+ 'enable_deprecated_occurrence_filtering' , False )
90
92
91
93
def bail (self , msg ):
92
94
'''
@@ -99,27 +101,29 @@ def bail(self, msg):
99
101
100
102
def get_api_settings (self ):
101
103
'''
102
- Return a hash of API settings derived first from ENV['SENSU_API_URL'] if set,
103
- then Sensu config `api` scope if configured, and finally falling back to
104
- to ipv4 localhost address on default API port.
105
-
104
+ Return a hash of API settings derived first from ENV['SENSU_API_URL']
105
+ if set, then Sensu config `api` scope if configured, and finally
106
+ falling back to to ipv4 localhost address on default API port.
107
+
106
108
return dict
107
109
'''
108
110
109
111
SENSU_API_URL = os .environ .get ('SENSU_API_URL' )
110
112
if SENSU_API_URL :
111
113
uri = urlparse (SENSU_API_URL )
112
114
self .api_settings = {
113
- 'host' : '{0}//{1}' .format (uri .scheme ,uri .hostname ),
114
- 'port' : uri .port ,
115
- 'user' : uri .username ,
116
- 'password' : uri .password
115
+ 'host' : '{0}//{1}' .format (uri .scheme , uri .hostname ),
116
+ 'port' : uri .port ,
117
+ 'user' : uri .username ,
118
+ 'password' : uri .password
117
119
}
118
120
else :
119
- self .api_settings = self .settings .get ('api' ,{})
120
- self .api_settings ['host' ] = self .api_settings .get ('host' , '127.0.0.1' )
121
- self .api_settings ['port' ] = self .api_settings .get ('port' , 4567 )
122
-
121
+ self .api_settings = self .settings .get ('api' , {})
122
+ self .api_settings ['host' ] = self .api_settings .get (
123
+ 'host' , '127.0.0.1' )
124
+ self .api_settings ['port' ] = self .api_settings .get (
125
+ 'port' , 4567 )
126
+
123
127
# API requests
124
128
def api_request (method , path , blk ):
125
129
if not hasattr (self , 'api_settings' ):
@@ -129,7 +133,7 @@ def api_request(method, path, blk):
129
133
_request = requests .get
130
134
elif method .lower () == 'post' :
131
135
_request = requests .post
132
-
136
+
133
137
domain = self .api_settings ['host' ]
134
138
# TODO: http/https
135
139
uri = 'http://{}:{}{}' .format (domain , self .api_settings ['port' ], path )
@@ -139,33 +143,37 @@ def api_request(method, path, blk):
139
143
auth = ()
140
144
req = _request (uri , auth = auth )
141
145
return req
142
-
146
+
143
147
def stash_exists (self , path ):
144
148
return self .api_request ('get' , '/stash' + path ).status_code == 200
145
149
146
150
def event_exists (self , client , check ):
147
- return self .api_request ('get' , '/events/' + client + '/' + check ).status_code == 200
151
+ return self .api_request ('get' ,
152
+ '/events/{}/{}' .format (client , check )
153
+ ).status_code == 200
148
154
149
155
# Filters
150
156
def filter_disabled (self ):
151
- if self .event ['check' ]['alert' ] == False :
157
+ if self .event ['check' ]['alert' ] is False :
152
158
bail ('alert disabled' )
153
159
154
160
def filter_silenced (self ):
155
161
stashes = [
156
- ('client' , '/silence/' + self .event ['client' ]['name' ]),
157
- ('check' , '/silence/' + self .event ['client' ]['name' ] + '/' + self .event ['check' ]['name' ]),
158
- ('check' , '/silence/all/' + self .event ['check' ]['name' ])
162
+ ('client' , '/silence/{}' .format (self .event ['client' ]['name' ])),
163
+ ('check' , '/silence/{}/{}' .format (
164
+ self .event ['client' ]['name' ],
165
+ self .event ['check' ]['name' ])),
166
+ ('check' , '/silence/all/{}' .format (self .event ['check' ]['name' ]))
159
167
]
160
168
for scope , path in stashes :
161
169
if stash_exists (path ):
162
170
bail (scope + ' alerts silenced' )
163
171
# TODO: Timeout for querying Sensu API?
164
- # More appropriate in the api_request method?
172
+ # More appropriate in the api_request method?
165
173
166
174
def filter_dependencies (self ):
167
175
dependencies = self .event ['check' ].get ('dependencies' , None )
168
- if dependencies == None or not isinstance (dependencies , list ):
176
+ if dependencies is None or not isinstance (dependencies , list ):
169
177
return
170
178
for dependency in self .event ['check' ]['dependencies' ]:
171
179
if len (str (dependency )) == 0 :
@@ -174,38 +182,42 @@ def filter_dependencies(self):
174
182
# If there's a dependency on a check from another client, then use
175
183
# that client name, otherwise assume same client.
176
184
if len (dependency_split ) == 2 :
177
- client ,check = dependency_split
185
+ client , check = dependency_split
178
186
else :
179
187
client = self .event ['client' ]['name' ]
180
188
check = dependency_split [0 ]
181
189
if self .event_exists (client , check ):
182
190
bail ('check dependency event exists' )
183
191
184
-
185
192
def filter_repeated (self ):
186
193
defaults = {
187
194
'occurrences' : 1 ,
188
195
'interval' : 30 ,
189
196
'refresh' : 1800
190
197
}
191
198
192
- # Override defaults with anything defined in the settings
199
+ # Override defaults with anything defined in the settings
193
200
if isinstance (self .settings ['sensu_plugin' ], dict ):
194
201
defaults .update (settings ['sensu_plugin' ])
195
202
end
196
203
197
- occurrences = int (self .event ['check' ].get ('occurrences' , defaults ['occurrences' ]))
198
- interval = int (self .event ['check' ].get ('interval' , defaults ['interval' ]))
199
- refresh = int (self .event ['check' ].get ('refresh' , defaults ['refresh' ]))
204
+ occurrences = int (self .event ['check' ].get (
205
+ 'occurrences' , defaults ['occurrences' ]))
206
+ interval = int (self .event ['check' ].get (
207
+ 'interval' , defaults ['interval' ]))
208
+ refresh = int (self .event ['check' ].get (
209
+ 'refresh' , defaults ['refresh' ]))
200
210
201
211
if self .event ['occurrences' ] < occurrences :
202
212
bail ('not enough occurrences' )
203
-
204
- if self .event ['occurrences' ] > occurrences and self .event ['action' ] == 'create' :
213
+
214
+ if (self .event ['occurrences' ] > occurrences and
215
+ self .event ['action' ] == 'create' ):
205
216
return
206
-
217
+
207
218
number = int (refresh / interval )
208
- if (number == 0 ) or ((self .event ['occurrences' ] - occurrences ) % number == 0 ):
219
+ if (number == 0 or
220
+ (self .event ['occurrences' ] - occurrences ) % number == 0 ):
209
221
return
210
222
211
223
bail ('only handling every ' + str (number ) + ' occurrences' )
0 commit comments