Check_MK's public GIT repository

This is the public GIT repository of Check_MK, a unique Addon for Nagios developed by Mathias Kettner.
This also contains MK Livestatus, a new Nagios event broker module that is a very fast and efficient replacement for NDO.

You can create your local clone of Check_MK with:
git clone http://git.mathias-kettner.de/check_mk.git
FIX: Fixed encoding problem in "custom notification" message
[check_mk.git] / web / plugins / views / commands.py
1 #!/usr/bin/python
2 # -*- encoding: utf-8; py-indent-offset: 4 -*-
3 # +------------------------------------------------------------------+
4 # | ____ _ _ __ __ _ __ |
5 # | / ___| |__ ___ ___| | __ | \/ | |/ / |
6 # | | | | '_ \ / _ \/ __| |/ / | |\/| | ' / |
7 # | | |___| | | | __/ (__| < | | | | . \ |
8 # | \____|_| |_|\___|\___|_|\_\___|_| |_|_|\_\ |
9 # | |
10 # | Copyright Mathias Kettner 2012 mk@mathias-kettner.de |
11 # +------------------------------------------------------------------+
12 #
13 # This file is part of Check_MK.
14 # The official homepage is at http://mathias-kettner.de/check_mk.
15 #
16 # check_mk is free software; you can redistribute it and/or modify it
17 # under the terms of the GNU General Public License as published by
18 # the Free Software Foundation in version 2. check_mk is distributed
19 # in the hope that it will be useful, but WITHOUT ANY WARRANTY; with-
20 # out even the implied warranty of MERCHANTABILITY or FITNESS FOR A
21 # PARTICULAR PURPOSE. See the GNU General Public License for more de-
22 # ails. You should have received a copy of the GNU General Public
23 # License along with GNU Make; see the file COPYING. If not, write
24 # to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
25 # Boston, MA 02110-1301 USA.
26
27
28 # Declarations of commands on monitoring objects. This file is
29 # read in with execfile by views.py.
30 #
31 # Each command has the following aspects:
32 #
33 # - permission
34 # - title
35 # - table ("hostservices", "downtime", "comment")
36 # - function that outputs the HTML input fields
37 # - function that creates the nagios command and title
38
39 #import datetime, traceback
40 #file('/tmp/1', 'a').write('%s %s\n%s' % (datetime.datetime.now(), current_language, ''.join(traceback.format_stack())))
41
42 # RESCHEDULE ACTIVE CHECKS
43 config.declare_permission("action.reschedule",
44 _("Reschedule checks"),
45 _("Reschedule host and service checks"),
46 [ "user", "admin" ])
47
48 multisite_commands.append({
49 "tables" : [ "host", "service" ],
50 "permission" : "action.reschedule",
51 "title" : _("Reschedule"),
52 "render" : lambda: \
53 html.button("_resched_checks", _("Reschedule active checks")),
54 "action" : lambda cmdtag, spec, row:
55 html.var("_resched_checks") and (
56 "SCHEDULE_FORCED_" + cmdtag + "_CHECK;%s;%d" % (spec, int(time.time())),
57 _("<b>reschedule an immediate check</b> of"))
58 })
59
60
61 # ENABLE/DISABLE NOTIFICATIONS
62 config.declare_permission("action.notifications",
63 _("Enable/disable notifications"),
64 _("Enable and disable notifications on hosts and services"),
65 [ "admin" ])
66
67 def command_notifications(cmdtag, spec, row):
68 if html.var("_enable_notifications"):
69 return ("ENABLE_" + cmdtag + "_NOTIFICATIONS;%s" % spec,
70 _("<b>enable notifications</b> for"))
71 elif html.var("_disable_notifications"):
72 return ("DISABLE_" + cmdtag + "_NOTIFICATIONS;%s" % spec,
73 _("<b>disable notifications</b> for"))
74
75 multisite_commands.append({
76 "tables" : [ "host", "service" ],
77 "permission" : "action.notifications",
78 "title" : _("Notifications"),
79 "render" : lambda: \
80 html.button("_enable_notifications", _("Enable")) == \
81 html.button("_disable_notifications", _("Disable")),
82 "action" : command_notifications,
83 })
84
85
86 # ENABLE/DISABLE ACTIVE CHECKS
87 config.declare_permission("action.enablechecks",
88 _("Enable/disable checks"),
89 _("Enable and disable active or passive checks on hosts and services"),
90 [ "admin" ])
91
92 def command_enable_active(cmdtag, spec, row):
93 if html.var("_enable_checks"):
94 return ("ENABLE_" + cmdtag + "_CHECK;%s" % spec,
95 _("<b>enable active checks</b> for"))
96 elif html.var("_disable_checks"):
97 return ("DISABLE_" + cmdtag + "_CHECK;%s" % spec,
98 _("<b>disable active checks</b> for"))
99
100 multisite_commands.append({
101 "tables" : [ "host", "service" ],
102 "permission" : "action.enablechecks",
103 "title" : _("Active checks"),
104 "render" : lambda: \
105 html.button("_enable_checks", _("Enable")) == \
106 html.button("_disable_checks", _("Disable")),
107 "action" : command_enable_active,
108 })
109
110
111 # ENABLE/DISABLE PASSIVE CHECKS
112 def command_enable_passive(cmdtag, spec, row):
113 if html.var("_enable_passive_checks"):
114 return ("ENABLE_PASSIVE_" + cmdtag + "_CHECKS;%s" % spec,
115 _("<b>enable passive checks</b> for"))
116 elif html.var("_disable_passive_checks"):
117 return ("DISABLE_PASSIVE_" + cmdtag + "_CHECKS;%s" % spec,
118 _("<b>disable passive checks</b> for"))
119
120 multisite_commands.append({
121 "tables" : [ "host", "service" ],
122 "permission" : "action.enablechecks",
123 "title" : _("Passive checks"),
124 "render" : lambda: \
125 html.button("_enable_passive_checks", _("Enable")) == \
126 html.button("_disable_passive_checks", _("Disable")),
127 "action" : command_enable_passive,
128 })
129
130
131
132 # CLEAR MODIFIED ATTRIBUTES
133 config.declare_permission("action.clearmodattr",
134 _("Clear modified attributes"),
135 _("Remove the information that an attribute (like check enabling) has been changed"),
136 [ "admin" ])
137
138 multisite_commands.append({
139 "tables" : [ "host", "service" ],
140 "permission" : "action.clearmodattr",
141 "title" : _("Modified attributes"),
142 "render" : lambda: \
143 html.button("_clear_modattr", _('Clear information about modified attributes')),
144 "action" : lambda cmdtag, spec, row: (
145 html.var("_clear_modattr") and (
146 "CHANGE_" + cmdtag + "_MODATTR;%s;0" % spec,
147 _("<b>clear the information about modified attributes</b> of"))),
148 })
149
150 # FAKE CHECKS
151 config.declare_permission("action.fakechecks",
152 _("Fake check results"),
153 _("Manually submit check results for host and service checks"),
154 [ "admin" ])
155
156 def command_fake_checks(cmdtag, spec, row):
157 for s in [0,1,2,3]:
158 statename = html.var("_fake_%d" % s)
159 if statename:
160 pluginoutput = _("Manually set to %s by %s") % (statename, config.user_id)
161 if cmdtag == "SVC":
162 cmdtag = "SERVICE"
163 command = "PROCESS_%s_CHECK_RESULT;%s;%s;%s" % (cmdtag, spec, s, pluginoutput)
164 title = _("<b>manually set check results to %s</b> for") % statename
165 return command, title
166
167
168 multisite_commands.append({
169 "tables" : [ "host" ],
170 "permission" : "action.fakechecks",
171 "title" : _("Fake check results"),
172 "render" : lambda: \
173 html.button("_fake_0", _("Up")) == \
174 html.button("_fake_1", _("Down")) == \
175 html.button("_fake_2", _("Unreachable")),
176 "action" : command_fake_checks,
177 })
178
179 multisite_commands.append({
180 "tables" : [ "service" ],
181 "permission" : "action.fakechecks",
182 "title" : _("Fake check results"),
183 "render" : lambda: \
184 html.button("_fake_0", _("OK")) == \
185 html.button("_fake_1", _("Warning")) == \
186 html.button("_fake_2", _("Critical")) == \
187 html.button("_fake_3", _("Unknown")),
188 "action" : command_fake_checks,
189 })
190
191 # SEND CUSTOM NOTIFICATION
192 config.declare_permission("action.customnotification",
193 _("Send custom notification"),
194 _("Manually let the core send a notification to a host or service in order "
195 "to test if notifications are setup correctly"),
196 [ "user", "admin" ])
197
198 def command_custom_notification(cmdtag, spec, row):
199 if html.var("_customnotification"):
200 comment = html.var_utf8("_cusnot_comment")
201 broadcast = html.get_checkbox("_cusnot_broadcast") and 1 or 0
202 forced = html.get_checkbox("_cusnot_forced") and 2 or 0
203 command = "SEND_CUSTOM_%s_NOTIFICATION;%s;%s;%s;%s" % \
204 ( cmdtag, spec, broadcast + forced, config.user_id, comment)
205 title = _("<b>send a custom notification</b> regarding")
206 return command, title
207
208
209 multisite_commands.append({
210 "tables" : [ "host", "service" ],
211 "permission" : "action.customnotification",
212 "title" : _("Custom notification"),
213 "render" : lambda: \
214 html.write(_('Comment') + ": ") == \
215 html.text_input("_cusnot_comment", "TEST", size=20) == \
216 html.write(" &nbsp; ") == \
217 html.checkbox("_cusnot_forced", False, label=_("forced")) == \
218 html.checkbox("_cusnot_broadcast", False, label=_("broadcast")) == \
219 html.write(" &nbsp; ") == \
220 html.button("_customnotification", _('Send')),
221 "action" : command_custom_notification,
222 })
223
224 # ACKNOWLEDGE
225 config.declare_permission("action.acknowledge",
226 _("Acknowledge"),
227 _("Acknowledge host and service problems and remove acknowledgements"),
228 [ "user", "admin" ])
229
230 def command_acknowledgement(cmdtag, spec, row):
231 if html.var("_acknowledge"):
232 comment = html.var_utf8("_ack_comment")
233 if not comment:
234 raise MKUserError("_ack_comment", _("You need to supply a comment."))
235 sticky = html.var("_ack_sticky") and 2 or 0
236 sendnot = html.var("_ack_notify") and 1 or 0
237 perscomm = html.var("_ack_persistent") and 1 or 0
238 command = "ACKNOWLEDGE_" + cmdtag + "_PROBLEM;%s;%d;%d;%d;%s" % \
239 (spec, sticky, sendnot, perscomm, config.user_id) + (";%s" % comment)
240 title = _("<b>acknowledge the problems</b> of")
241 return command, title
242
243 elif html.var("_remove_ack"):
244 command = "REMOVE_" + cmdtag + "_ACKNOWLEDGEMENT;%s" % spec
245 title = _("<b>remove acknowledgements</b> from")
246 return command, title
247
248
249 multisite_commands.append({
250 "tables" : [ "host", "service" ],
251 "permission" : "action.acknowledge",
252 "title" : _("Acknowledge"),
253 "render" : lambda: \
254 html.button("_acknowledge", _("Acknowledge")) == \
255 html.button("_remove_ack", _("Remove Acknowledge")) == \
256 html.write("<hr>") == \
257 html.checkbox("_ack_sticky", True, label=_("sticky")) == \
258 html.checkbox("_ack_notify", True, label=_("send notification")) == \
259 html.checkbox("_ack_persistent", False, label=_('persistent comment')) == \
260 html.write("<hr>") == \
261 html.write(_("Comment") + ": ") == \
262 html.text_input("_ack_comment", size=48),
263 "action" : command_acknowledgement,
264 })
265
266
267 # COMMENTS
268 config.declare_permission("action.addcomment",
269 _("Add comments"),
270 _("Add comments to hosts or services, and remove comments"),
271 [ "user", "admin" ])
272
273 def command_comment(cmdtag, spec, row):
274 if html.var("_add_comment"):
275 comment = html.var_utf8("_comment")
276 if not comment:
277 raise MKUserError("_comment", _("You need to supply a comment."))
278 command = "ADD_" + cmdtag + "_COMMENT;%s;1;%s" % \
279 (spec, config.user_id) + (";%s" % comment)
280 title = _("<b>add a comment to</b>")
281 return command, title
282
283 multisite_commands.append({
284 "tables" : [ "host", "service" ],
285 "permission" : "action.addcomment",
286 "title" : _("Add comment"),
287 "render" : lambda: \
288 html.write(_('Comment')+": ") == \
289 html.text_input("_comment", size=33) == \
290 html.write(" &nbsp; ") == \
291 html.button("_add_comment", _("Add comment")),
292 "action" : command_comment,
293 })
294
295 # DOWNTIMES
296 config.declare_permission("action.downtimes",
297 _("Set/Remove Downtimes"),
298 _("Schedule and remove downtimes on hosts and services"),
299 [ "user", "admin" ])
300
301 def command_downtime(cmdtag, spec, row):
302 down_from = int(time.time())
303 down_to = None
304
305 if html.var("_down_2h"):
306 down_to = down_from + 7200
307 title = _("<b>schedule an immediate 2-hour downtime</b> on")
308
309 elif html.var("_down_today"):
310 br = time.localtime(down_from)
311 down_to = time.mktime((br.tm_year, br.tm_mon, br.tm_mday, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
312 title = _("<b>schedule an immediate downtime until 24:00:00</b> on")
313
314 elif html.var("_down_week"):
315 br = time.localtime(down_from)
316 wday = br.tm_wday
317 days_plus = 6 - wday
318 down_to = time.mktime((br.tm_year, br.tm_mon, br.tm_mday, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
319 down_to += days_plus * 24 * 3600
320 title = _("<b>schedule an immediate downtime until sunday night</b> on")
321
322 elif html.var("_down_month"):
323 br = time.localtime(down_from)
324 new_month = br.tm_mon + 1
325 if new_month == 13:
326 new_year = br.tm_year + 1
327 new_month = 1
328 else:
329 new_year = br.tm_year
330 down_to = time.mktime((new_year, new_month, 1, 0, 0, 0, 0, 0, br.tm_isdst))
331 title = _("<b>schedule an immediate downtime until end of month</b> on")
332
333 elif html.var("_down_year"):
334 br = time.localtime(down_from)
335 down_to = time.mktime((br.tm_year, 12, 31, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
336 title = _("<b>schedule an immediate downtime until end of %d</b> on") % br.tm_year
337
338 elif html.var("_down_custom"):
339 down_from = html.get_datetime_input("_down_from")
340 down_to = html.get_datetime_input("_down_to")
341 if down_to < time.time():
342 raise MKUserError("_down_to", _("You cannot set a downtime that ends in the past. "
343 "This incident will be reported."))
344
345 title = _("<b>schedule a downtime from %s to %s</b> on ") % (
346 time.asctime(time.localtime(down_from)),
347 time.asctime(time.localtime(down_to)))
348
349 elif html.var("_down_remove"):
350 downtime_ids = []
351 if cmdtag == "HOST":
352 prefix = "host_"
353 else:
354 prefix = "service_"
355 for id in row[prefix + "downtimes"]:
356 if id != "":
357 downtime_ids.append(int(id))
358 commands = []
359 for dtid in downtime_ids:
360 commands.append("[%d] DEL_%s_DOWNTIME;%d\n" % (int(time.time()), cmdtag, dtid))
361 title = _("<b>remove all scheduled downtimes</b> of ")
362 return commands, title
363
364 if down_to:
365 comment = html.var_utf8("_down_comment")
366 if not comment:
367 raise MKUserError("_down_comment", _("You need to supply a comment for your downtime."))
368 if html.var("_down_flexible"):
369 fixed = 0
370 duration = html.get_time_input("_down_duration", _("the duration"))
371 else:
372 fixed = 1
373 duration = 0
374 command = (("SCHEDULE_" + cmdtag + "_DOWNTIME;%s;" % spec) \
375 + ("%d;%d;%d;0;%d;%s;" % (down_from, down_to, fixed, duration, config.user_id)) \
376 + comment)
377 return command, title
378
379
380 multisite_commands.append({
381 "tables" : [ "host", "service" ],
382 "permission" : "action.downtimes",
383 "title" : _("Schedule downtimes"),
384 "render" : lambda: \
385 html.button("_down_2h", _("2 hours")) == \
386 html.button("_down_today", _("Today")) == \
387 html.button("_down_week", _("This week")) == \
388 html.button("_down_month", _("This month")) == \
389 html.button("_down_year", _("This year")) == \
390 html.write(" &nbsp; - &nbsp;") == \
391 html.button("_down_remove", _("Remove all")) == \
392 html.write("<hr>") == \
393 html.button("_down_custom", _("Custom time range")) == \
394 html.datetime_input("_down_from", time.time()) == \
395 html.write("&nbsp; "+_('to')+" &nbsp;") == \
396 html.datetime_input("_down_to", time.time() + 7200) == \
397 html.write("<hr>") == \
398 html.checkbox("_down_flexible", False, label=_('flexible with max. duration')+" ") == \
399 html.time_input("_down_duration", 2, 0) == \
400 html.write(" "+_('(HH:MM)')) == \
401 html.write("<hr>") == \
402 html.write(_('Comment')+": ") == \
403 html.text_input("_down_comment", size=48),
404 "action" : command_downtime,
405 })
406
407 # REMOVE DOWNTIMES (table downtimes)
408 multisite_commands.append({
409 "tables" : [ "downtime" ],
410 "permission" : "action.downtimes",
411 "title" : _("Downtimes"),
412 "render" : lambda: \
413 html.button("_remove_downtimes", _("Remove")),
414 "action" : lambda cmdtag, spec, row: \
415 html.has_var("_remove_downtimes") and \
416 ( "DEL_%s_DOWNTIME;%d" % (cmdtag, spec),
417 _("remove the following"))
418 })
419
420 # REMOVE COMMENTS (table comments)
421 multisite_commands.append({
422 "tables" : [ "comment" ],
423 "permission" : "action.addcomment",
424 "title" : _("Comments"),
425 "render" : lambda: \
426 html.button("_remove_comments", _("Remove")),
427 "action" : lambda cmdtag, spec, row: \
428 html.has_var("_remove_comments") and \
429 ( "DEL_%s_COMMENT;%d" % (cmdtag, spec),
430 _("remove the following"))
431 })