This file is indexed.

/usr/share/check_mk/web/plugins/views/commands.py is in check-mk-multisite 1.2.8p16-1ubuntu0.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
#!/usr/bin/python
# -*- encoding: utf-8; py-indent-offset: 4 -*-
# +------------------------------------------------------------------+
# |             ____ _               _        __  __ _  __           |
# |            / ___| |__   ___  ___| | __   |  \/  | |/ /           |
# |           | |   | '_ \ / _ \/ __| |/ /   | |\/| | ' /            |
# |           | |___| | | |  __/ (__|   <    | |  | | . \            |
# |            \____|_| |_|\___|\___|_|\_\___|_|  |_|_|\_\           |
# |                                                                  |
# | Copyright Mathias Kettner 2014             mk@mathias-kettner.de |
# +------------------------------------------------------------------+
#
# This file is part of Check_MK.
# The official homepage is at http://mathias-kettner.de/check_mk.
#
# check_mk is free software;  you can redistribute it and/or modify it
# under the  terms of the  GNU General Public License  as published by
# the Free Software Foundation in version 2.  check_mk is  distributed
# in the hope that it will be useful, but WITHOUT ANY WARRANTY;  with-
# out even the implied warranty of  MERCHANTABILITY  or  FITNESS FOR A
# PARTICULAR PURPOSE. See the  GNU General Public License for more de-
# tails. You should have  received  a copy of the  GNU  General Public
# License along with GNU Make; see the file  COPYING.  If  not,  write
# to the Free Software Foundation, Inc., 51 Franklin St,  Fifth Floor,
# Boston, MA 02110-1301 USA.


# Declarations of commands on monitoring objects. This file is
# read in with execfile by views.py.
#
# Each command has the following aspects:
#
# - permission
# - title
# - table ("hostservices", "downtime", "comment")
# - function that outputs the HTML input fields
# - function that creates the nagios command and title


#   .--Reschedule----------------------------------------------------------.
#   |          ____                _              _       _                |
#   |         |  _ \ ___  ___  ___| |__   ___  __| |_   _| | ___           |
#   |         | |_) / _ \/ __|/ __| '_ \ / _ \/ _` | | | | |/ _ \          |
#   |         |  _ <  __/\__ \ (__| | | |  __/ (_| | |_| | |  __/          |
#   |         |_| \_\___||___/\___|_| |_|\___|\__,_|\__,_|_|\___|          |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def command_reschedule(cmdtag, spec, row, row_nr, total_rows):
    if html.var("_resched_checks"):
        spread = saveint(html.var("_resched_spread"))
        text = "<b>" + _("reschedule an immediate check")
        if spread:
            text += _(" spread over %d minutes ") % spread

        text += "</b>" + _("of")

        t = time.time()
        if spread:
            t += spread * 60.0 * row_nr / total_rows

        command = "SCHEDULE_FORCED_" + cmdtag + "_CHECK;%s;%d" % (spec, int(t))
        return command, text

config.declare_permission("action.reschedule",
        _("Reschedule checks"),
        _("Reschedule host and service checks"),
        [ "user", "admin" ])


multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.reschedule",
    "title"       : _("Reschedule active checks"),
    "render"      : lambda: \
        html.button("_resched_checks", _("Reschedule")) == \
        html.write(_("and spread over") + " ") == \
        html.number_input("_resched_spread", 0, size=3) == \
        html.write(" " + _("minutes") + " "),
    "action"      : command_reschedule,
    "row_stats"   : True, # Get information about number of rows and current row nr.
})


#.
#   .--Enable/Disable Notifications----------------------------------------.
#   |           _____          ______  _           _     _                 |
#   |          | ____|_ __    / /  _ \(_)___  __ _| |__ | | ___            |
#   |          |  _| | '_ \  / /| | | | / __|/ _` | '_ \| |/ _ \           |
#   |          | |___| | | |/ / | |_| | \__ \ (_| | |_) | |  __/           |
#   |          |_____|_| |_/_/  |____/|_|___/\__,_|_.__/|_|\___|           |
#   |                                                                      |
#   |       _   _       _   _  __ _           _   _                        |
#   |      | \ | | ___ | |_(_)/ _(_) ___ __ _| |_(_) ___  _ __  ___        |
#   |      |  \| |/ _ \| __| | |_| |/ __/ _` | __| |/ _ \| '_ \/ __|       |
#   |      | |\  | (_) | |_| |  _| | (_| (_| | |_| | (_) | | | \__ \       |
#   |      |_| \_|\___/ \__|_|_| |_|\___\__,_|\__|_|\___/|_| |_|___/       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.notifications",
        _("Enable/disable notifications"),
        _("Enable and disable notifications on hosts and services"),
        [ "admin" ])

def command_notifications(cmdtag, spec, row):
    if html.var("_enable_notifications"):
        return ("ENABLE_" + cmdtag + "_NOTIFICATIONS;%s" % spec,
                _("<b>enable notifications</b> for"))
    elif html.var("_disable_notifications"):
        return ("DISABLE_" + cmdtag + "_NOTIFICATIONS;%s" % spec,
                _("<b>disable notifications</b> for"))

multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.notifications",
    "title"       : _("Notifications"),
    "render"      : lambda: \
       html.button("_enable_notifications", _("Enable")) == \
       html.button("_disable_notifications", _("Disable")),
    "action"      : command_notifications,
})


#.
#   .--Enable/Disable Active Checks----------------------------------------.
#   |           _____          ______  _           _     _                 |
#   |          | ____|_ __    / /  _ \(_)___  __ _| |__ | | ___            |
#   |          |  _| | '_ \  / /| | | | / __|/ _` | '_ \| |/ _ \           |
#   |          | |___| | | |/ / | |_| | \__ \ (_| | |_) | |  __/           |
#   |          |_____|_| |_/_/  |____/|_|___/\__,_|_.__/|_|\___|           |
#   |                                                                      |
#   |       _        _   _              ____ _               _             |
#   |      / \   ___| |_(_)_   _____   / ___| |__   ___  ___| | _____      |
#   |     / _ \ / __| __| \ \ / / _ \ | |   | '_ \ / _ \/ __| |/ / __|     |
#   |    / ___ \ (__| |_| |\ V /  __/ | |___| | | |  __/ (__|   <\__ \     |
#   |   /_/   \_\___|\__|_| \_/ \___|  \____|_| |_|\___|\___|_|\_\___/     |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.enablechecks",
        _("Enable/disable checks"),
        _("Enable and disable active or passive checks on hosts and services"),
        [ "admin" ])

def command_enable_active(cmdtag, spec, row):
    if html.var("_enable_checks"):
        return ("ENABLE_" + cmdtag + "_CHECK;%s" % spec,
                _("<b>enable active checks</b> for"))
    elif html.var("_disable_checks"):
        return ("DISABLE_" + cmdtag + "_CHECK;%s" % spec,
                _("<b>disable active checks</b> for"))

multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.enablechecks",
    "title"       : _("Active checks"),
    "render"      : lambda: \
       html.button("_enable_checks", _("Enable")) == \
       html.button("_disable_checks", _("Disable")),
    "action"      : command_enable_active,
})


#.
#   .--Enable/Disable Passive Checks---------------------------------------.
#   |           _____          ______  _           _     _                 |
#   |          | ____|_ __    / /  _ \(_)___  __ _| |__ | | ___            |
#   |          |  _| | '_ \  / /| | | | / __|/ _` | '_ \| |/ _ \           |
#   |          | |___| | | |/ / | |_| | \__ \ (_| | |_) | |  __/           |
#   |          |_____|_| |_/_/  |____/|_|___/\__,_|_.__/|_|\___|           |
#   |                                                                      |
#   |   ____               _              ____ _               _           |
#   |  |  _ \ __ _ ___ ___(_)_   _____   / ___| |__   ___  ___| | _____    |
#   |  | |_) / _` / __/ __| \ \ / / _ \ | |   | '_ \ / _ \/ __| |/ / __|   |
#   |  |  __/ (_| \__ \__ \ |\ V /  __/ | |___| | | |  __/ (__|   <\__ \   |
#   |  |_|   \__,_|___/___/_| \_/ \___|  \____|_| |_|\___|\___|_|\_\___/   |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def command_enable_passive(cmdtag, spec, row):
    if html.var("_enable_passive_checks"):
        return ("ENABLE_PASSIVE_" + cmdtag + "_CHECKS;%s" % spec,
                _("<b>enable passive checks</b> for"))
    elif html.var("_disable_passive_checks"):
        return ("DISABLE_PASSIVE_" + cmdtag + "_CHECKS;%s" % spec,
                _("<b>disable passive checks</b> for"))

multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.enablechecks",
    "title"       : _("Passive checks"),
    "render"      : lambda: \
       html.button("_enable_passive_checks", _("Enable")) == \
       html.button("_disable_passive_checks", _("Disable")),
    "action"      : command_enable_passive,
})



#.
#   .--Clear Modified Attributes-------------------------------------------.
#   |            ____ _                   __  __           _               |
#   |           / ___| | ___  __ _ _ __  |  \/  | ___   __| |              |
#   |          | |   | |/ _ \/ _` | '__| | |\/| |/ _ \ / _` |              |
#   |          | |___| |  __/ (_| | |    | |  | | (_) | (_| |_             |
#   |           \____|_|\___|\__,_|_|    |_|  |_|\___/ \__,_(_)            |
#   |                                                                      |
#   |              _   _   _        _ _           _                        |
#   |             / \ | |_| |_ _ __(_) |__  _   _| |_ ___  ___             |
#   |            / _ \| __| __| '__| | '_ \| | | | __/ _ \/ __|            |
#   |           / ___ \ |_| |_| |  | | |_) | |_| | ||  __/\__ \            |
#   |          /_/   \_\__|\__|_|  |_|_.__/ \__,_|\__\___||___/            |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.clearmodattr",
        _("Reset modified attributes"),
        _("Reset all manually modified attributes of a host or service (like disabled notifications)"),
        [ "admin" ])

multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.clearmodattr",
    "title"       : _("Modified attributes"),
    "render"      : lambda: \
       html.button("_clear_modattr", _('Clear modified attributes')),
    "action"      : lambda cmdtag, spec, row: (
        html.var("_clear_modattr") and (
        "CHANGE_" + cmdtag + "_MODATTR;%s;0" % spec,
         _("<b>clear the modified attributes</b> of"))),
})


#.
#   .--Fake Checks---------------------------------------------------------.
#   |         _____     _           ____ _               _                 |
#   |        |  ___|_ _| | _____   / ___| |__   ___  ___| | _____          |
#   |        | |_ / _` | |/ / _ \ | |   | '_ \ / _ \/ __| |/ / __|         |
#   |        |  _| (_| |   <  __/ | |___| | | |  __/ (__|   <\__ \         |
#   |        |_|  \__,_|_|\_\___|  \____|_| |_|\___|\___|_|\_\___/         |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.fakechecks",
        _("Fake check results"),
        _("Manually submit check results for host and service checks"),
        [ "admin" ])

def command_fake_checks(cmdtag, spec, row):
    for s in [0, 1, 2, 3]:
        statename = html.var("_fake_%d" % s)
        if statename:
            pluginoutput = html.get_unicode_input("_fake_output").strip()
            if not pluginoutput:
                pluginoutput = _("Manually set to %s by %s") % (html.attrencode(statename), config.user_id)
            perfdata = html.var("_fake_perfdata")
            if perfdata:
                pluginoutput += "|" + perfdata
            if cmdtag == "SVC":
                cmdtag = "SERVICE"
            command = "PROCESS_%s_CHECK_RESULT;%s;%s;%s" % (cmdtag, spec, s, lqencode(pluginoutput))
            title = _("<b>manually set check results to %s</b> for") % html.attrencode(statename)
            return command, title


def render_fake_form(what):
    html.write("<table><tr><td>")
    html.write("%s: " % _("Plugin output"))
    html.write("</td><td>")
    html.text_input("_fake_output", "", size=50)
    html.write("</td></tr><tr><td>")
    html.write("%s: " % _("Performance data"))
    html.write("</td><td>")
    html.text_input("_fake_perfdata", "", size=50)
    html.write("</td></tr><tr><td>")
    html.write(_("Result:"))
    html.write("</td><td>")
    if what == "host":
        html.button("_fake_0", _("Up"))
        html.button("_fake_2", _("Down"))
    else:
        html.button("_fake_0", _("OK"))
        html.button("_fake_1", _("Warning"))
        html.button("_fake_2", _("Critical"))
        html.button("_fake_3", _("Unknown"))
    html.write("</td></tr></table>")

multisite_commands.append({
    "tables"      : [ "host" ],
    "permission"  : "action.fakechecks",
    "title"       : _("Fake check results"),
    "group"       : _("Fake check results"),
    "render"      : lambda: render_fake_form("host"),
    "action"      : command_fake_checks,
})

multisite_commands.append({
    "tables"      : [ "service" ],
    "permission"  : "action.fakechecks",
    "title"       : _("Fake check results"),
    "group"       : _("Fake check results"),
    "render"      : lambda: render_fake_form("service"),
    "action"      : command_fake_checks,
})


#.
#   .--Custom Notifications------------------------------------------------.
#   |                   ____          _                                    |
#   |                  / ___|   _ ___| |_ ___  _ __ ___                    |
#   |                 | |  | | | / __| __/ _ \| '_ ` _ \                   |
#   |                 | |__| |_| \__ \ || (_) | | | | | |                  |
#   |                  \____\__,_|___/\__\___/|_| |_| |_|                  |
#   |                                                                      |
#   |       _   _       _   _  __ _           _   _                        |
#   |      | \ | | ___ | |_(_)/ _(_) ___ __ _| |_(_) ___  _ __  ___        |
#   |      |  \| |/ _ \| __| | |_| |/ __/ _` | __| |/ _ \| '_ \/ __|       |
#   |      | |\  | (_) | |_| |  _| | (_| (_| | |_| | (_) | | | \__ \       |
#   |      |_| \_|\___/ \__|_|_| |_|\___\__,_|\__|_|\___/|_| |_|___/       |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.customnotification",
        _("Send custom notification"),
        _("Manually let the core send a notification to a host or service in order "
          "to test if notifications are setup correctly"),
        [ "user", "admin" ])

def command_custom_notification(cmdtag, spec, row):
    if html.var("_customnotification"):
        comment = html.get_unicode_input("_cusnot_comment")
        broadcast = html.get_checkbox("_cusnot_broadcast") and 1 or 0
        forced = html.get_checkbox("_cusnot_forced") and 2 or 0
        command = "SEND_CUSTOM_%s_NOTIFICATION;%s;%s;%s;%s" % \
                ( cmdtag, spec, broadcast + forced, config.user_id, lqencode(comment))
        title = _("<b>send a custom notification</b> regarding")
        return command, title


multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.customnotification",
    "title"       : _("Custom notification"),
    "render"      : lambda: \
        html.write(_('Comment') + ": ") == \
        html.text_input("_cusnot_comment", "TEST", size=20, submit="_customnotification") == \
        html.write(" &nbsp; ") == \
        html.checkbox("_cusnot_forced", False, label=_("forced")) == \
        html.checkbox("_cusnot_broadcast", False, label=_("broadcast")) == \
        html.write(" &nbsp; ") == \
        html.button("_customnotification", _('Send')),
    "action"      : command_custom_notification,
})


#.
#   .--Acknowledge---------------------------------------------------------.
#   |       _        _                        _          _                 |
#   |      / \   ___| | ___ __   _____      _| | ___  __| | __ _  ___      |
#   |     / _ \ / __| |/ / '_ \ / _ \ \ /\ / / |/ _ \/ _` |/ _` |/ _ \     |
#   |    / ___ \ (__|   <| | | | (_) \ V  V /| |  __/ (_| | (_| |  __/     |
#   |   /_/   \_\___|_|\_\_| |_|\___/ \_/\_/ |_|\___|\__,_|\__, |\___|     |
#   |                                                      |___/           |
#   '----------------------------------------------------------------------'

config.declare_permission("action.acknowledge",
        _("Acknowledge"),
        _("Acknowledge host and service problems and remove acknowledgements"),
        [ "user", "admin" ])

def command_acknowledgement(cmdtag, spec, row):
    if "aggr_tree" in row: # BI mode
        specs = []
        for site, host, service in bi.find_all_leaves(row["aggr_tree"]):
            if service:
                spec = "%s;%s" % (host, service)
                cmdtag = "SVC"
            else:
                spec = host
                cmdtag = "HOST"
            specs.append((site, spec, cmdtag))

    if html.var("_acknowledge"):
        comment = html.get_unicode_input("_ack_comment")
        if not comment:
            raise MKUserError("_ack_comment", _("You need to supply a comment."))
        if ";" in comment:
            raise MKUserError("_ack_comment", _("The comment must not contain semicolons."))
        sticky = html.var("_ack_sticky") and 2 or 0
        sendnot = html.var("_ack_notify") and 1 or 0
        perscomm = html.var("_ack_persistent") and 1 or 0

        expire_secs = Age().from_html_vars("_ack_expire")
        if expire_secs:
            expire = int(time.time()) + expire_secs
            expire_text = ";%d" % expire
        else:
            expire_text = ""

        def make_command(spec, cmdtag):
            return "ACKNOWLEDGE_" + cmdtag + "_PROBLEM;%s;%d;%d;%d;%s" % \
                          (spec, sticky, sendnot, perscomm, config.user_id) + (";%s" % lqencode(comment)) \
                          + expire_text

        if "aggr_tree" in row: # BI mode
            commands = [(site, make_command(spec, cmdtag)) for (site, spec, cmdtag) in specs ]
        else:
            commands = [ make_command(spec, cmdtag) ]

        title = _("<b>acknowledge the problems%s</b> of") % \
                    (expire_text and (_(" for a period of %s") % Age().value_to_text(expire_secs)) or "")
        return commands, title

    elif html.var("_remove_ack"):
        def make_command(spec, cmdtag):
            return "REMOVE_" + cmdtag + "_ACKNOWLEDGEMENT;%s" % spec

        if "aggr_tree" in row: # BI mode
            commands = [(site, make_command(spec, cmdtag)) for (site, spec, cmdtag) in specs ]
        else:
            commands = [ make_command(spec, cmdtag) ]
        title = _("<b>remove acknowledgements</b> from")
        return commands, title


multisite_commands.append({
    "tables"      : [ "host", "service", "aggr" ],
    "permission"  : "action.acknowledge",
    "title"       : _("Acknowledge Problems"),
    "render"      : lambda: \
        html.button("_acknowledge", _("Acknowledge")) == \
        html.button("_remove_ack", _("Remove Acknowledgement")) == \
        html.write("<hr>") == \
        html.checkbox("_ack_sticky", True, label=_("sticky")) == \
        html.checkbox("_ack_notify", True, label=_("send notification")) == \
        html.checkbox("_ack_persistent", False, label=_('persistent comment')) == \
        html.write("<hr>") == \
        Age(display=["days", "hours", "minutes"], label=_("Expire acknowledgement after")).render_input("_ack_expire", 0) == \
        html.help(_("Note: Expiration of acknowledgements only works when using the Check_MK Micro Core.")) == \
        html.write("<hr>") == \
        html.write(_("Comment") + ": ") == \
        html.text_input("_ack_comment", size=48, submit="_acknowledge"),
    "action"      : command_acknowledgement,
    "group"       : _("Acknowledge"),
})


#.
#   .--Comments------------------------------------------------------------.
#   |           ____                                     _                 |
#   |          / ___|___  _ __ ___  _ __ ___   ___ _ __ | |_ ___           |
#   |         | |   / _ \| '_ ` _ \| '_ ` _ \ / _ \ '_ \| __/ __|          |
#   |         | |__| (_) | | | | | | | | | | |  __/ | | | |_\__ \          |
#   |          \____\___/|_| |_| |_|_| |_| |_|\___|_| |_|\__|___/          |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.addcomment",
        _("Add comments"),
        _("Add comments to hosts or services, and remove comments"),
        [ "user", "admin" ])

def command_comment(cmdtag, spec, row):
    if html.var("_add_comment"):
        comment = html.get_unicode_input("_comment")
        if not comment:
            raise MKUserError("_comment", _("You need to supply a comment."))
        command = "ADD_" + cmdtag + "_COMMENT;%s;1;%s" % \
                  (spec, config.user_id) + (";%s" % lqencode(comment))
        title = _("<b>add a comment to</b>")
        return command, title

multisite_commands.append({
    "tables"      : [ "host", "service" ],
    "permission"  : "action.addcomment",
    "title"       : _("Add comment"),
    "render"      : lambda: \
        html.write(_('Comment')+": ") == \
        html.text_input("_comment", size=33, submit="_add_comment") == \
        html.write(" &nbsp; ") == \
        html.button("_add_comment", _("Add comment")),
    "action"      : command_comment,
})


#.
#   .--Downtimes-----------------------------------------------------------.
#   |         ____                      _   _                              |
#   |        |  _ \  _____      ___ __ | |_(_)_ __ ___   ___  ___          |
#   |        | | | |/ _ \ \ /\ / / '_ \| __| | '_ ` _ \ / _ \/ __|         |
#   |        | |_| | (_) \ V  V /| | | | |_| | | | | | |  __/\__ \         |
#   |        |____/ \___/ \_/\_/ |_| |_|\__|_|_| |_| |_|\___||___/         |
#   |                                                                      |
#   '----------------------------------------------------------------------'

config.declare_permission("action.downtimes",
        _("Set/Remove Downtimes"),
        _("Schedule and remove downtimes on hosts and services"),
        [ "user", "admin" ])

def command_downtime(cmdtag, spec, row):
    down_from = int(time.time())
    down_to = None

    if has_recurring_downtimes() and html.get_checkbox("_down_do_recur"):
        recurring_type = int(html.var("_down_recurring"))
        title_start = _("schedule a periodic downtime every %s") % wato.recurring_downtimes_types[recurring_type]
    else:
        title_start = _("schedule an immediate downtime")

    if html.var("_down_2h"):
        down_to = down_from + 7200
        title = _("<b>%s of 2 hours length</b> on") % title_start

    elif html.var("_down_today"):
        br = time.localtime(down_from)
        down_to = time.mktime((br.tm_year, br.tm_mon, br.tm_mday, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
        title = _("<b>%s until 24:00:00</b> on") % title_start

    elif html.var("_down_week"):
        br = time.localtime(down_from)
        wday = br.tm_wday
        days_plus = 6 - wday
        down_to = time.mktime((br.tm_year, br.tm_mon, br.tm_mday, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
        down_to += days_plus * 24 * 3600
        title = _("<b>%s until sunday night</b> on") % title_start

    elif html.var("_down_month"):
        br = time.localtime(down_from)
        new_month = br.tm_mon + 1
        if new_month == 13:
            new_year = br.tm_year + 1
            new_month = 1
        else:
            new_year = br.tm_year
        down_to = time.mktime((new_year, new_month, 1, 0, 0, 0, 0, 0, br.tm_isdst))
        title = _("<b>%s until end of month</b> on") % title_start

    elif html.var("_down_year"):
        br = time.localtime(down_from)
        down_to = time.mktime((br.tm_year, 12, 31, 23, 59, 59, 0, 0, br.tm_isdst)) + 1
        title = _("<b>%s until end of %d</b> on") % (title_start, br.tm_year)

    elif html.var("_down_from_now"):
        try:
            minutes = int(html.var("_down_minutes"))
        except:
            minutes = 0

        if minutes <= 0:
            raise MKUserError("_down_minutes", _("Please enter a positive number of minutes."))

        down_to = time.time() + minutes * 60
        title = _("<b>%s for the next %d minutes</b> on" % (title_start, minutes))

    elif html.var("_down_adhoc"):
        minutes = config.adhoc_downtime.get("duration",0)
        down_to = time.time() + minutes * 60
        title = _("<b>%s for the next %d minutes</b> on" % (title_start, minutes))

    elif html.var("_down_custom"):
        down_from = html.get_datetime_input("_down_from")
        down_to   = html.get_datetime_input("_down_to")
        if down_to < time.time():
            raise MKUserError("_down_to", _("You cannot set a downtime that ends in the past. "
                         "This incident will be reported."))

        if down_to < down_from:
            raise MKUserError("_down_to", _("Your end date is before your start date."))

        title = _("<b>schedule a downtime from %s to %s</b> on ") % (
            time.asctime(time.localtime(down_from)),
            time.asctime(time.localtime(down_to)))

    elif html.var("_down_remove"):
        if html.var("_on_hosts"):
            raise MKUserError("_on_hosts", _("The checkbox for setting host downtimes does not work when removing downtimes."))

        downtime_ids = []
        if cmdtag == "HOST":
            prefix = "host_"
        else:
            prefix = "service_"
        for id in row[prefix + "downtimes"]:
            if id != "":
                downtime_ids.append(int(id))

        commands = []
        for dtid in downtime_ids:
            commands.append("DEL_%s_DOWNTIME;%d\n" % (cmdtag, dtid))
        title = _("<b>remove all scheduled downtimes</b> of ")
        return commands, title

    if down_to:
        if html.var("_down_adhoc"):
            comment = config.adhoc_downtime.get("comment","")
        else:
            comment = html.get_unicode_input("_down_comment")
        if not comment:
            raise MKUserError("_down_comment", _("You need to supply a comment for your downtime."))
        if html.var("_down_flexible"):
            fixed = 0
            duration = html.get_time_input("_down_duration", _("the duration"))
        else:
            fixed = 1
            duration = 0

        if html.get_checkbox("_down_do_recur"):
            fixed_and_recurring = recurring_type * 2 + fixed
        else:
            fixed_and_recurring = fixed

        def make_command(spec, cmdtag):
            return ("SCHEDULE_" + cmdtag + "_DOWNTIME;%s;" % spec ) \
                   + ("%d;%d;%d;0;%d;%s;" % (down_from, down_to, fixed_and_recurring, duration, config.user_id)) \
                   + lqencode(comment)

        if "aggr_tree" in row: # BI mode
            commands = []
            for site, host, service in bi.find_all_leaves(row["aggr_tree"]):
                if service:
                    spec = "%s;%s" % (host, service)
                    cmdtag = "SVC"
                else:
                    spec = host
                    cmdtag = "HOST"
                commands.append((site, make_command(spec, cmdtag)))
        else:
            if html.var("_include_childs"): # only for hosts
                specs = [ spec ] + get_child_hosts(row["site"], [spec], recurse = not not html.var("_include_childs_recurse"))
            elif html.var("_on_hosts"): # set on hosts instead of services
                specs = [ spec.split(";")[0] ]
                title += " the hosts of"
                cmdtag = "HOST"
            else:
                specs = [ spec ]

            commands = [ make_command(spec, cmdtag) for spec in  specs ]

        return commands, title


def get_child_hosts(site, hosts, recurse):
    hosts = set(hosts)
    html.live.set_only_sites([site])
    query = "GET hosts\nColumns: name\n"
    for h in hosts:
        query += "Filter: parents >= %s\n" % h
    query += "Or: %d\n" % len(hosts)
    childs = html.live.query_column(query)
    html.live.set_only_sites(None)
    # Recursion, but try to avoid duplicate work
    childs = set(childs)
    new_childs = childs.difference(hosts)
    if new_childs and recurse:
        rec_childs = get_child_hosts(site, new_childs, True)
        new_childs.update(rec_childs)
    return list(new_childs)


def paint_downtime_buttons(what):
    html.write(_('Downtime Comment')+": ")
    html.text_input("_down_comment", "", size=60, submit="")
    html.write("<hr>")
    html.button("_down_from_now", _("From now for"))
    html.write("&nbsp;")
    html.number_input("_down_minutes", 60, size=4, submit="_down_from_now")
    html.write("&nbsp; " + _("minutes"))
    html.write("<hr>")
    html.button("_down_2h", _("2 hours"))
    html.button("_down_today", _("Today"))
    html.button("_down_week", _("This week"))
    html.button("_down_month", _("This month"))
    html.button("_down_year", _("This year"))
    if what != "aggr":
        html.write(" &nbsp; - &nbsp;")
        html.button("_down_remove", _("Remove all"))
    html.write("<hr>")
    if config.adhoc_downtime and config.adhoc_downtime.get("duration"):
        adhoc_duration = config.adhoc_downtime.get("duration")
        adhoc_comment  = config.adhoc_downtime.get("comment", "")
        html.button("_down_adhoc", _("Adhoc for %d minutes") % adhoc_duration)
        html.write("&nbsp;")
        html.write(_('with comment')+": ")
        html.write(adhoc_comment)
        html.write("<hr>")

    html.button("_down_custom", _("Custom time range"))
    html.datetime_input("_down_from", time.time(), submit="_down_custom")
    html.write("&nbsp; "+_('to')+" &nbsp;")
    html.datetime_input("_down_to", time.time() + 7200, submit="_down_custom")
    html.write("<hr>")
    html.checkbox("_down_flexible", False, label=_('flexible with duration')+" ")
    html.time_input("_down_duration", 2, 0)
    html.write(" "+_('(HH:MM)'))
    if what == "host":
        html.write("<hr>")
        html.checkbox("_include_childs", False, label=_('Also set downtime on child hosts'))
        html.write("  ")
        html.checkbox("_include_childs_recurse", False, label=_('Do this recursively'))
    elif what == "service":
        html.write("<hr>")
        html.checkbox("_on_hosts", False, label=_('Schedule downtimes on the affected '
                                                  '<b>hosts</b> instead of on the individual '
                                                  'services'))

    if has_recurring_downtimes():
        html.write("<hr>")
        html.checkbox("_down_do_recur", False,
                      label=_("Repeat this downtime on a regular base every"))
        html.write(" ")
        recurring_selections = [ (str(k), v) for (k,v) in
                                 sorted(wato.recurring_downtimes_types.items())]
        html.select("_down_recurring", recurring_selections, "3")
        html.write(_("(This only works when using CMC)"))


def has_recurring_downtimes():
    try:
        wato.recurring_downtimes_types # Check if this exists
        return True
    except AttributeError:
        return False
    except NameError:
        return False


multisite_commands.append({
    "tables"      : [ "host" ],
    "permission"  : "action.downtimes",
    "title"       : _("Schedule downtimes"),
    "render"      : lambda: paint_downtime_buttons("host"),
    "action"      : command_downtime,
    "group"       : _("Downtimes"),
})

multisite_commands.append({
    "tables"      : [ "service" ],
    "permission"  : "action.downtimes",
    "title"       : _("Schedule downtimes"),
    "render"      : lambda: paint_downtime_buttons("service"),
    "action"      : command_downtime,
    "group"       : _("Downtimes"),
})

multisite_commands.append({
    "tables"      : [ "aggr" ],
    "permission"  : "action.downtimes",
    "title"       : _("Schedule downtimes"),
    "render"      : lambda: paint_downtime_buttons("aggr"),
    "action"      : command_downtime,
    "group"       : _("Downtimes"),
})

# REMOVE DOWNTIMES (table downtimes)
multisite_commands.append({
    "tables"      : [ "downtime" ],
    "permission"  : "action.downtimes",
    "title"       : _("Remove downtimes"),
    "render"      : lambda: html.button("_remove_downtimes", _("Remove")),
    "action"      : lambda cmdtag, spec, row: html.has_var("_remove_downtimes") and \
                           ( "DEL_%s_DOWNTIME;%d" % (cmdtag, spec), _("remove"))
})

# REMOVE COMMENTS (table comments)

def remove_comments(cmdtag, spec, row):
    if html.has_var("_remove_comments"):
        commands = [("DEL_%s_COMMENT;%d" % (cmdtag, spec))]
        if row.get("comment_entry_type") == 4:
            if row.get("service_description"):
                commands.append(("REMOVE_%s_ACKNOWLEDGEMENT;%s;%s" %\
                                (cmdtag, row["host_name"], row["service_description"])))
            else:
                commands.append(("REMOVE_%s_ACKNOWLEDGEMENT;%s" %\
                                (cmdtag, row["host_name"])))

        return commands, _("remove")

multisite_commands.append({
    "tables"      : [ "comment" ],
    "permission"  : "action.addcomment",
    "title"       : _("Remove comments"),
    "render"      : lambda: html.button("_remove_comments", _("Remove")),
    "action"      : remove_comments
})

#.
#   .--Stars * (Favorites)-------------------------------------------------.
#   |                   ____  _                                            |
#   |                  / ___|| |_ __ _ _ __ ___  __/\__                    |
#   |                  \___ \| __/ _` | '__/ __| \    /                    |
#   |                   ___) | || (_| | |  \__ \ /_  _\                    |
#   |                  |____/ \__\__,_|_|  |___/   \/                      |
#   |                                                                      |
#   '----------------------------------------------------------------------'

def command_star(cmdtag, spec, row):
    if html.var("_star") or html.var("_unstar"):
        star = html.var("_star") and 1 or 0
        if star:
            title = _("<b>add to you favorites</b>")
        else:
            title = _("<b>remove from your favorites</b>")
        return "STAR;%d;%s" % (star, spec), title


def command_executor_star(command, site):
    foo, star, spec = command.split(";", 2)
    stars = config.load_stars()
    if star == "0" and spec in stars:
        stars.remove(spec)
    elif star == "1":
        stars.add(spec)
    config.save_stars(stars)

config.declare_permission("action.star",
    _("Use favorites"),
    _("This permission allows a user to make certain host and services "
      "his personal favorites. Favorites can be used for a having a fast "
      "access to items that are needed on a regular base."),
    [ "user", "admin" ])

multisite_commands.append({
    "tables"         : [ "host", "service" ],
    "permission"     : "action.star",
    "title"          : _("Favorites"),
    "render"         : lambda: \
       html.button("_star",   _("Add to Favorites")) == \
       html.button("_unstar", _("Remove from Favorites")),
    "action"         : command_star,
    "executor"       : command_executor_star,
})