Example #1
0
    def get_triggers(self, only_hm=True, db=None):
        if db:
            return db.get_hm_triggers(only_hm)

        method = "trigger.get"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        tmp = dict()

        def _get_trigger_info(k):
            request_body = dict(jsonrpc="2.0",
                                method=method,
                                params=dict(output=[k]),
                                id=self.get_request_id(),
                                auth=self.get_token())
            response = self.do_request(request_body)
            triggers = response.get("result", [])
            for trigger in triggers:
                if trigger["triggerid"] in tmp:
                    tmp[trigger["triggerid"]][k] = trigger[k]
                else:
                    tmp[trigger["triggerid"]] = {k: trigger[k]}

        # I can't get all trigger infos in one request(i don't know why, but
        # Zabbix just give no response to me). So here let's divide request
        # into some small parts
        _get_trigger_info("description")
        _get_trigger_info("comments")
        _get_trigger_info("priority")

        triggers = tmp.values()

        if only_hm is False:
            result = triggers
        else:
            result = [t for t in triggers if
                        t["description"].upper().startswith(
                constants.TRIGGER_PREFIX
            ) and " " not in t["description"]]

        # TODO(tianhuan) Remove duplicated triggers, necessary?
        r = []
        for t in result:
            if t not in r:
                r.append(t)
        r.sort()

        cache.set_cached_content(method, r, 10800)
        return r
Example #2
0
    def get_triggers_name(self, only_hm=True, db=None):
        method = "triggers_name"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        triggers = self.get_triggers(only_hm, db)
        triggers_name = []
        for trigger in triggers:
            name = trigger.get("description", None)
            if name and name not in triggers_name:
                triggers_name.append(name.strip())
        triggers_name.sort()
        cache.set_cached_content(method, triggers_name, 1800)
        return triggers_name
Example #3
0
    def get_triggers(self, only_hm=True, db=None):
        if db:
            return db.get_hm_triggers(only_hm)

        method = "trigger.get"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        tmp = dict()

        def _get_trigger_info(k):
            request_body = dict(jsonrpc="2.0",
                                method=method,
                                params=dict(output=[k]),
                                id=self.get_request_id(),
                                auth=self.get_token())
            response = self.do_request(request_body)
            triggers = response.get("result", [])
            for trigger in triggers:
                if trigger["triggerid"] in tmp:
                    tmp[trigger["triggerid"]][k] = trigger[k]
                else:
                    tmp[trigger["triggerid"]] = {k: trigger[k]}

        # I can't get all trigger infos in one request(i don't know why, but
        # Zabbix just give no response to me). So here let's divide request
        # into some small parts
        _get_trigger_info("description")
        _get_trigger_info("comments")
        _get_trigger_info("priority")

        triggers = tmp.values()

        if only_hm is False:
            result = triggers
        else:
            result = [
                t for t in triggers if t["description"].upper().startswith(
                    constants.TRIGGER_PREFIX) and " " not in t["description"]
            ]

        # TODO(tianhuan) Remove duplicated triggers, necessary?
        r = []
        for t in result:
            if t not in r:
                r.append(t)
        r.sort()

        cache.set_cached_content(method, r, 10800)
        return r
Example #4
0
    def get_triggers_name(self, only_hm=True, db=None):
        method = "triggers_name"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        triggers = self.get_triggers(only_hm, db)
        triggers_name = []
        for trigger in triggers:
            name = trigger.get("description", None)
            if name and name not in triggers_name:
                triggers_name.append(name.strip())
        triggers_name.sort()
        cache.set_cached_content(method, triggers_name, 1800)
        return triggers_name
Example #5
0
    def get_triggers_info(self, only_hm=True, db=None):
        method = "triggers_info"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        triggers = self.get_triggers(only_hm, db)
        triggers_info = {}
        for trigger in triggers:
            name = trigger.get("description", None)
            priority = trigger.get("priority", 0)
            comments = trigger.get("comments", "")
            if name:
                triggers_info[name.strip()] = dict(name=name.strip(),
                                                   priority=priority,
                                                   comments=comments.strip())
        cache.set_cached_content(method, triggers_info, 1800)
        return triggers_info
Example #6
0
    def get_triggers_info(self, only_hm=True, db=None):
        method = "triggers_info"
        if cache.get_cached_content(method):
            return cache.get_cached_content(method)

        triggers = self.get_triggers(only_hm, db)
        triggers_info = {}
        for trigger in triggers:
            name = trigger.get("description", None)
            priority = trigger.get("priority", 0)
            comments = trigger.get("comments", "")
            if name:
                triggers_info[name.strip()] = dict(name=name.strip(),
                                                   priority=priority,
                                                   comments=comments.strip())
        cache.set_cached_content(method, triggers_info, 1800)
        return triggers_info