Exemple #1
0
    def action(self, cmdtag, spec, row, row_index, num_rows):
        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.request.var("_acknowledge"):
            comment = html.request.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."))
            non_empty_comment = comment

            sticky = 2 if html.request.var("_ack_sticky") else 0
            sendnot = 1 if html.request.var("_ack_notify") else 0
            perscomm = 1 if html.request.var("_ack_persistent") else 0

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

            def make_command_ack(spec, cmdtag):
                return "ACKNOWLEDGE_" + cmdtag + "_PROBLEM;%s;%d;%d;%d;%s" % (
                    spec, sticky, sendnot, perscomm, config.user.id) + (
                        ";%s" % livestatus.lqencode(non_empty_comment)) + expire_text

            if "aggr_tree" in row:  # BI mode
                commands = [
                    (site, make_command_ack(spec_, cmdtag_)) for site, spec_, cmdtag_ in specs
                ]
            else:
                commands = [make_command_ack(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

        if html.request.var("_remove_ack"):

            def make_command_rem(spec, cmdtag):
                return "REMOVE_" + cmdtag + "_ACKNOWLEDGEMENT;%s" % spec

            if "aggr_tree" in row:  # BI mode
                commands = [(site, make_command_rem(spec, cmdtag)) for site, spec_, cmdtag_ in specs
                           ]
            else:
                commands = [make_command_rem(spec, cmdtag)]
            title = _("<b>remove acknowledgements</b> from")
            return commands, title
Exemple #2
0
def bi_commands(downtime: DowntimeSchedule, node: Any) -> List[Tuple[Any, Any]]:
    """Generate the list of downtime command strings for the BI module"""
    commands_aggr = []
    for site, host, service in bi.find_all_leaves(node):
        if service:
            spec = "%s;%s" % (host, service)
            cmdtag = "SVC"
        else:
            spec = host
            cmdtag = "HOST"
        commands_aggr.append((site, downtime.livestatus_command(spec, cmdtag)))
    return commands_aggr
Exemple #3
0
    def action(self, cmdtag, spec, row, row_index, num_rows):
        down_from = int(time.time())
        down_to = None

        if self._has_recurring_downtimes() and html.get_checkbox(
                "_down_do_recur"):
            from cmk.gui.cee.plugins.wato.cmc import recurring_downtimes_types
            recurring_type = int(html.request.var("_down_recurring"))
            title_start = _("schedule a periodic downtime every %s"
                            ) % recurring_downtimes_types()[recurring_type]
        else:
            title_start = _("schedule an immediate downtime")

        rangebtns = (varname for varname, _value in html.request.itervars(
            prefix="_downrange"))

        def resolve_end(name):
            now = time.localtime(down_from)
            if name == "next_day":
                return time.mktime((now.tm_year, now.tm_mon, now.tm_mday, 23, 59, 59, 0, 0, now.tm_isdst)) + 1, \
                    _("<b>%s until 24:00:00</b> on") % title_start
            elif name == "next_week":
                wday = now.tm_wday
                days_plus = 6 - wday
                res = time.mktime((now.tm_year, now.tm_mon, now.tm_mday, 23,
                                   59, 59, 0, 0, now.tm_isdst)) + 1
                res += days_plus * 24 * 3600
                return res, _("<b>%s until sunday night</b> on") % title_start
            elif name == "next_month":
                new_month = now.tm_mon + 1
                if new_month == 13:
                    new_year = now.tm_year + 1
                    new_month = 1
                else:
                    new_year = now.tm_year
                return time.mktime((new_year, new_month, 1, 0, 0, 0, 0, 0, now.tm_isdst)), \
                    _("<b>%s until end of month</b> on") % title_start
            elif name == "next_year":
                return time.mktime((now.tm_year, 12, 31, 23, 59, 59, 0, 0, now.tm_isdst)) + 1, \
                    _("<b>%s until end of %d</b> on") % (title_start, now.tm_year)
            else:
                duration = int(name)
                return down_from + duration, \
                    _("<b>%s of %s length</b> on") %\
                    (title_start, self._get_duration_human_readable(duration))

        try:
            rangebtn = next(rangebtns)
        except StopIteration:
            rangebtn = None

        if rangebtn:
            _btnname, end = rangebtn.split("__", 1)
            down_to, title = resolve_end(end)
        elif html.request.var("_down_from_now"):
            try:
                minutes = int(html.request.var("_down_minutes", ""))
            except ValueError:
                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.request.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.request.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.request.var("_down_remove") and config.user.may(
                "action.remove_all_downtimes"):
            if html.request.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.request.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.request.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,
                        )) + livestatus.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.request.var("_include_childs"):  # only for hosts
                    specs = [spec] + self._get_child_hosts(
                        row["site"], [spec],
                        recurse=bool(
                            html.request.var("_include_childs_recurse")))
                elif html.request.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