def get_items(self, host_ids=None): params = {"output": "extend", "selectApplications": ["name"], "monitored": True} if host_ids is not None: params["hostids"] = host_ids res_dict = self.get_response_dict("item.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return items = list() for item in res_dict["result"]: if item["lastclock"] == "0": continue self.expand_item_brief(item) if int(item["lastns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(item["lastns"])) items.append({"itemId": item["itemid"], "hostId": item["hostid"], "brief": item["name"], "lastValueTime": Utils.translate_unix_time_to_hatohol_time(int(item["lastclock"]) + ns), "lastValue": item["lastvalue"], "itemGroupName": get_item_groups(item["applications"]), "unit": item["units"]}) return items
def get_history(self, item_id, begin_time, end_time): begin_time = Utils.translate_hatohol_time_to_unix_time(begin_time) end_time = Utils.translate_hatohol_time_to_unix_time(end_time) params = { "output": "extend", "itemids": item_id, "history": self.get_item_value_type(item_id), "sortfield": "clock", "sortorder": "ASC", "time_from": begin_time, "time_till": end_time } res_dict = self.get_response_dict("history.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return histories = list() for history in res_dict["result"]: if int(history["ns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(history["ns"])) histories.append({ "value": history["value"], "time": Utils.translate_unix_time_to_hatohol_time( int(history["clock"]) + ns) }) return histories
def get_history(self, item_id, begin_time, end_time): begin_time = Utils.translate_hatohol_time_to_unix_time(begin_time) end_time = Utils.translate_hatohol_time_to_unix_time(end_time) params = { "output": "extend", "itemids": item_id, "history": self.get_item_value_type(item_id), "sortfield": "clock", "sortorder": "ASC", "time_from": begin_time, "time_till": end_time } res_dict = self.get_response_dict("history.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return histories = list() def proc(history): time = Utils.translate_unix_time_to_hatohol_time( history["clock"], history["ns"]) histories.append({"value": history["value"], "time": time}) self.__iterate_in_try_block(res_dict["result"], proc) return histories
def get_events(self, event_id_from, event_id_till=None): params = {"output": "extend", "eventid_from": event_id_from, "selectHosts": ["name"]} if event_id_till is not None: params["eventid_till"] = event_id_till res_dict = self.get_response_dict("event.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return events = list() for event in res_dict["result"]: try: if int(event["ns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(event["ns"])) trigger = self.get_select_trigger(event["objectid"]) events.append({"eventId": event["eventid"], "time": Utils.translate_unix_time_to_hatohol_time(int(event["clock"]) + ns), "type": EVENT_TYPE[event["value"]], "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["state"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "hostId": event["hosts"][0]["hostid"], "hostName": event["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": ""}) except KeyError: logging.warning("Get a imperfect event: %s" % event) return events
def collect_triggers_and_put(self, fetch_id=None, host_ids=None): query = self.__socket.services.columns("plugin_output", "description", "last_state_change", "host_alias", "host_name", "state") if host_ids is not None: filter_condition = "host_name ~ " for host_id in enumerate(host_ids): if host_id[0] == 0: filter_condition += host_id[1] else: filter_condition += "|" + host_id[1] query = query.filter(filter_condition) all_triggers_should_send = lambda: fetch_id is None update_type = "ALL" if all_triggers_should_send(): if self.__trigger_last_info is None: self.__trigger_last_info = self.get_last_info("trigger") if len(self.__trigger_last_info): unix_timestamp = Utils.translate_hatohol_time_to_unix_time(self.__trigger_last_info) query = query.filter("last_state_change >= %s" % unix_timestamp) update_type = "UPDATED" result = query.call() triggers = [] for service in result: hapi_status, hapi_severity = \ self.__parse_status_and_severity(service["state"]) last_state_change = datetime.datetime.fromtimestamp(service["last_state_change"]) hapi_time = Utils.conv_to_hapi_time(last_state_change, self.__time_offset) triggers.append({ "triggerId": service["description"], "status": hapi_status, "severity": hapi_severity, "lastChangeTime": hapi_time, "hostId": service["host_name"], "hostName": service["host_alias"], "brief": service["plugin_output"], "extendedInfo": "" }) self.__trigger_last_info = \ Utils.get_biggest_num_of_dict_array(triggers, "lastChangeTime") self.put_triggers(triggers, update_type=update_type, last_info=self.__trigger_last_info, fetch_id=fetch_id)
def get_events(self, event_id_from, event_id_till=None): params = { "output": "extend", "eventid_from": event_id_from, "selectHosts": ["name"] } if event_id_till is not None: params["eventid_till"] = event_id_till res_dict = self.get_response_dict("event.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return events = list() for event in res_dict["result"]: try: if int(event["ns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(event["ns"])) trigger = self.get_select_trigger(event["objectid"]) events.append({ "eventId": event["eventid"], "time": Utils.translate_unix_time_to_hatohol_time( int(event["clock"]) + ns), "type": EVENT_TYPE[event["value"]], "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["state"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "hostId": event["hosts"][0]["hostid"], "hostName": event["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": "" }) except KeyError: logging.warning("Get a imperfect event: %s" % event) return events
def get_triggers(self, requestSince=None, host_ids=None): params = {"output": "extend", "selectHosts": ["name"], "active": True} last_change_since = int() if requestSince: last_change_since = \ Utils.translate_hatohol_time_to_unix_time(requestSince) params["lastChangeSince"] = last_change_since if host_ids is not None: params["hostids"] = host_ids res_dict = self.get_response_dict("trigger.get", params, self.auth_token) expanded_descriptions = \ self.get_trigger_expand_description(last_change_since, host_ids) self.result = check_response(res_dict) if not self.result: return triggers = list() for num, trigger in enumerate(res_dict["result"]): try: description = [ ed["description"] for ed in expanded_descriptions["result"] if ed["triggerid"] == trigger["triggerid"] ][0] triggers.append({ "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["state"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "lastChangeTime": Utils.translate_unix_time_to_hatohol_time( int(trigger["lastchange"])), "hostId": trigger["hosts"][0]["hostid"], "hostName": trigger["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": description }) except KeyError: logging.warning("Get a imperfect trigger: %s" % trigger) return triggers
def get_items(self, host_ids=None): params = { "output": "extend", "selectApplications": ["name"], "monitored": True } if host_ids is not None: params["hostids"] = host_ids res_dict = self.get_response_dict("item.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return items = list() for item in res_dict["result"]: if item["lastclock"] == "0": continue self.expand_item_brief(item) if int(item["lastns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(item["lastns"])) items.append({ "itemId": item["itemid"], "hostId": item["hostid"], "brief": item["name"], "lastValueTime": Utils.translate_unix_time_to_hatohol_time( int(item["lastclock"]) + ns), "lastValue": item["lastvalue"], "itemGroupName": get_item_groups(item["applications"]), "unit": item["units"] }) return items
def proc(trigger): description = find_description(trigger["triggerid"]) lastchange = trigger["lastchange"] time = Utils.translate_unix_time_to_hatohol_time(lastchange) triggers.append({ "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["value"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "lastChangeTime": time, "hostId": trigger["hosts"][0]["hostid"], "hostName": trigger["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": description })
def get_triggers(self, requestSince=None, host_ids=None): params = {"output": "extend", "selectHosts": ["name"], "active": True} last_change_since = int() if requestSince: last_change_since = \ Utils.translate_hatohol_time_to_unix_time(requestSince) params["lastChangeSince"] = last_change_since if host_ids is not None: params["hostids"] = host_ids res_dict = self.get_response_dict("trigger.get", params, self.auth_token) expanded_descriptions = \ self.get_trigger_expand_description(last_change_since, host_ids) self.result = check_response(res_dict) if not self.result: return triggers = list() for num, trigger in enumerate(res_dict["result"]): try: description = [ed["description"] for ed in expanded_descriptions["result"] if ed["triggerid"] == trigger["triggerid"]][0] triggers.append({"triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["state"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "lastChangeTime": Utils.translate_unix_time_to_hatohol_time(int(trigger["lastchange"])), "hostId": trigger["hosts"][0]["hostid"], "hostName": trigger["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": description}) except KeyError: logging.warning("Get a imperfect trigger: %s" % trigger) return triggers
def get_history(self, item_id, begin_time, end_time): begin_time = Utils.translate_hatohol_time_to_unix_time(begin_time) end_time = Utils.translate_hatohol_time_to_unix_time(end_time) params = {"output": "extend", "itemids": item_id, "history": self.get_item_value_type(item_id), "sortfield": "clock", "sortorder": "ASC", "time_from": begin_time, "time_till": end_time} res_dict = self.get_response_dict("history.get", params, self.auth_token) self.result = check_response(res_dict) if not self.result: return histories = list() for history in res_dict["result"]: if int(history["ns"]) == 0: ns = 0 else: ns = Utils.translate_int_to_decimal(int(history["ns"])) histories.append({"value": history["value"], "time": Utils.translate_unix_time_to_hatohol_time(int(history["clock"]) + ns)}) return histories
def update_triggers(self, host_ids=None, fetch_id=None): if self.__trigger_last_info is None: self.__trigger_last_info = self.get_last_info("trigger") triggers = self.__api.get_triggers(self.__trigger_last_info, host_ids) if not len(triggers): return self.__trigger_last_info = \ Utils.get_biggest_num_of_dict_array(triggers, "lastChangeTime") update_type = "ALL" if fetch_id is not None else "UPDATED" self.put_triggers(triggers, update_type=update_type, last_info=self.__trigger_last_info, fetch_id=fetch_id)
def proc(event): trigger = self.get_select_trigger(event["objectid"]) time = Utils.translate_unix_time_to_hatohol_time( event["clock"], event["ns"]) events.append({ "eventId": event["eventid"], "time": time, "type": EVENT_TYPE[event["value"]], "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["value"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "hostId": trigger["hosts"][0]["hostid"], "hostName": trigger["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": "" })
def proc(item): if item["lastclock"] == "0": return self.expand_item_brief(item) time = Utils.translate_unix_time_to_hatohol_time( item["lastclock"], item["lastns"]) items.append({ "itemId": item["itemid"], "hostId": item["hostid"], "brief": item["name"], "lastValueTime": time, "lastValue": item["lastvalue"], "itemGroupName": get_item_groups(item["applications"]), "unit": item["units"] })
def get_triggers(self, requestSince=None, host_ids=None): params = {"output": "extend", "selectHosts": ["name"], "active": True} last_change_since = int() if requestSince: last_change_since = \ Utils.translate_hatohol_time_to_unix_time(requestSince) params["lastChangeSince"] = last_change_since if host_ids is not None: params["hostids"] = host_ids res_dict = self.get_response_dict("trigger.get", params, self.auth_token) expanded_descriptions = \ self.get_trigger_expand_description(last_change_since, host_ids) self.result = check_response(res_dict) if not self.result: return triggers = list() def find_description(triggerid): for ex_descr in expanded_descriptions["result"]: if ex_descr["triggerid"] == triggerid: return ex_descr["description"] logger.warning("Not found description: triggerid: %s" % triggerid) return "" def proc(trigger): description = find_description(trigger["triggerid"]) lastchange = trigger["lastchange"] time = Utils.translate_unix_time_to_hatohol_time(lastchange) triggers.append({ "triggerId": trigger["triggerid"], "status": TRIGGER_STATUS[trigger["value"]], "severity": TRIGGER_SEVERITY[trigger["priority"]], "lastChangeTime": time, "hostId": trigger["hosts"][0]["hostid"], "hostName": trigger["hosts"][0]["name"], "brief": trigger["description"], "extendedInfo": description }) self.__iterate_in_try_block(res_dict["result"], proc) return triggers
def proc(history): time = Utils.translate_unix_time_to_hatohol_time( history["clock"], history["ns"]) histories.append({"value": history["value"], "time": time})
def collect_events_and_put(self, fetch_id=None, last_info=None, count=None, direction="ASC"): query = self.__socket.statehist.columns("log_output", "state", "time", "current_host_name", "current_host_alias", "service_description") if last_info is None: last_info = self.get_cached_event_last_info() if last_info: last_info = json.loads(last_info) if not last_info: pass elif direction == "ASC": unix_timestamp = \ Utils.translate_hatohol_time_to_unix_time(last_info["time"]) query = query.filter("time >= %s" % unix_timestamp) elif direction == "DESC": unix_timestamp = \ Utils.translate_hatohol_time_to_unix_time(last_info["time"]) query = query.filter("time <= %s" % unix_timestamp) result = query.call() logger.debug(query) try: latest_state_index = result.index(last_info) result = result[:latest_state_index] except ValueError: pass events = [] for event in result: if not len(event["current_host_name"]): continue hapi_event_type = self.EVENT_TYPE_MAP.get(event["state"]) if hapi_event_type is None: log.warning("Unknown status: " + event["state"]) hapi_event_type = "UNKNOWN" hapi_status, hapi_severity = \ self.__parse_status_and_severity(event["state"]) event_time = datetime.datetime.fromtimestamp(event["time"]) hapi_time = Utils.conv_to_hapi_time(event_time, self.__time_offset) events.append({ "eventId": str(uuid.uuid1()), "time": hapi_time, "type": hapi_event_type, "triggerId": event["service_description"], "status": hapi_status, "severity": hapi_severity, "hostId": event["current_host_name"], "hostName": event["current_host_alias"], "brief": event["log_output"], "extendedInfo": "" }) if len(result): # livestatus return a sorted list. # result[0] is latest statehist. self.__latest_statehist = json.dumps(result[0]) self.put_events(events, fetch_id=fetch_id, last_info_generator=self.return_latest_statehist)