|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import logging |
| 4 | +import sys |
| 5 | + |
| 6 | +import requests |
| 7 | + |
| 8 | +parser = argparse.ArgumentParser() |
| 9 | +parser.add_argument('-payload', '--payload', help='Payload from queue', required=True) |
| 10 | +parser.add_argument('-apiKey', '--apiKey', help='The apiKey of the integration', required=True) |
| 11 | +parser.add_argument('-jsmUrl', '--jsmUrl', help='The url', required=True) |
| 12 | +parser.add_argument('-logLevel', '--logLevel', help='Level of log', required=True) |
| 13 | +parser.add_argument('-url', '--url', help='LibreNms Server Url', required=False) |
| 14 | +parser.add_argument('-apiToken', '--apiToken', help='Api Token', required=False) |
| 15 | +parser.add_argument('-timeout', '--timeout', help='Timeout', required=False) |
| 16 | + |
| 17 | +args = vars(parser.parse_args()) |
| 18 | + |
| 19 | +logging.basicConfig(stream=sys.stdout, level=args['logLevel']) |
| 20 | + |
| 21 | +queue_message_string = args['payload'] |
| 22 | +queue_message = json.loads(queue_message_string) |
| 23 | + |
| 24 | +alert_id = queue_message["alert"]["alertId"] |
| 25 | +mapped_action = queue_message["mappedActionV2"]["name"] |
| 26 | + |
| 27 | +LOG_PREFIX = "[" + mapped_action + "]:" |
| 28 | +logging.info(LOG_PREFIX + " Will execute " + mapped_action + " for alertId " + alert_id) |
| 29 | + |
| 30 | + |
| 31 | +def parse_field(key, mandatory): |
| 32 | + variable = queue_message.get(key) |
| 33 | + if not variable: |
| 34 | + variable = args.get(key) |
| 35 | + if mandatory and not variable: |
| 36 | + logging.error(LOG_PREFIX + " Skipping action, Mandatory conf item '" + key + |
| 37 | + "' is missing. Check your configuration file.") |
| 38 | + raise ValueError(LOG_PREFIX + " Skipping action, Mandatory conf item '" + key + |
| 39 | + "' is missing. Check your configuration file.") |
| 40 | + return variable |
| 41 | + |
| 42 | + |
| 43 | +url = parse_field('url', True) |
| 44 | +if url.endswith("/") and len(url) >= 2: |
| 45 | + url = url[0:len(url) - 2] |
| 46 | + |
| 47 | +api_token = parse_field('apiToken', True) |
| 48 | +rule = int(queue_message["rule"]) |
| 49 | +device_id = int(queue_message["deviceId"]) |
| 50 | +timestamp = queue_message["timestamp"] |
| 51 | +timeout = args.get('timeout') |
| 52 | +if timeout is None: |
| 53 | + timeout = 30000 |
| 54 | +else: |
| 55 | + timeout = int(timeout) |
| 56 | + |
| 57 | +logging.debug("Url: " + str(url)) |
| 58 | +logging.debug("ApiToken " + str(api_token)) |
| 59 | +logging.debug("Rule from JSM Alert Details: " + str(rule)) |
| 60 | +logging.debug("Device ID from JSM Alert Details: " + str(device_id)) |
| 61 | +logging.debug("Timestamp from JSM Alert Details: " + str(timestamp)) |
| 62 | + |
| 63 | +list_rules_endpoint = url + "/api/v0/rules" |
| 64 | + |
| 65 | +logging.debug("Sending GET request to " + str(list_rules_endpoint)) |
| 66 | + |
| 67 | +list_rules_response = requests.get(list_rules_endpoint, None, headers={"X-Auth-Token": api_token}, timeout=timeout) |
| 68 | + |
| 69 | +logging.debug("Response from " + str(list_rules_endpoint) + ": " + str(list_rules_response.text) + "Status Code: " |
| 70 | + + str(list_rules_response.status_code)) |
| 71 | + |
| 72 | +if list_rules_response.status_code < 400: |
| 73 | + rules = list_rules_response.json()["rules"] |
| 74 | + rule_id = None |
| 75 | + |
| 76 | + rule_list = [x["id"] for x in rules if x["id"] == rule] |
| 77 | + for x in rule_list: |
| 78 | + logging.debug(x) |
| 79 | + |
| 80 | + if len(rule_list) > 0: |
| 81 | + rule_id = rule_list[0] |
| 82 | + logging.debug("Rule Id from LibreNMS: " + str(rule_id)) |
| 83 | + |
| 84 | + list_alerts_endpoint = url + "/api/v0/alerts" |
| 85 | + list_alerts_response = None |
| 86 | + |
| 87 | + if mapped_action == "ackAlert": |
| 88 | + query_params = {"state": "1"} |
| 89 | + logging.debug("Sending GET request to " + str(list_alerts_endpoint) + "with parameters: " |
| 90 | + + json.dumps(query_params)) |
| 91 | + list_alerts_response = requests.get(list_alerts_endpoint, query_params, headers={"X-Auth-Token": api_token}, |
| 92 | + timeout=timeout) |
| 93 | + |
| 94 | + elif mapped_action == "unmuteAlert": |
| 95 | + query_params = {"state": "2"} |
| 96 | + logging.debug("Sending GET request to " + str(list_alerts_endpoint) + "with parameters: " |
| 97 | + + json.dumps(query_params)) |
| 98 | + list_alerts_response = requests.get(list_alerts_endpoint, query_params, headers={"X-Auth-Token": api_token}, |
| 99 | + timeout=timeout) |
| 100 | + |
| 101 | + logging.debug( |
| 102 | + "Response from " + str(list_alerts_endpoint) + ": " + str(list_alerts_response.content) + "Status Code: " |
| 103 | + + str(list_alerts_response.status_code)) |
| 104 | + |
| 105 | + if list_alerts_response.status_code < 400: |
| 106 | + alerts = list_alerts_response.json()['alerts'] |
| 107 | + alert_id = None |
| 108 | + |
| 109 | + if len(alerts) > 0: |
| 110 | + alert_list = [x['id'] for x in alerts if (x['rule_id'] == rule and x['device_id'] == device_id and |
| 111 | + x['timestamp'] == timestamp)] |
| 112 | + if len(alert_list) > 0: |
| 113 | + alert_id = alert_list[0] |
| 114 | + logging.debug("Alert ID: " + str(alert_id)) |
| 115 | + logging.debug( |
| 116 | + "Found alert that matches the timestamp from JSM alert, using that alert's alert id.") |
| 117 | + else: |
| 118 | + alert_list = [x['id'] for x in alerts if (x['rule_id'] == rule and x['device_id'] == device_id)] |
| 119 | + logging.debug("Timestamp did not match the timestamp retrieved from JSM alert," |
| 120 | + + " using that alert ID of the first alert matches the rule and the device id.") |
| 121 | + alert_id = alert_list[0] |
| 122 | + logging.debug("Alert ID: " + str(alert_id)) |
| 123 | + else: |
| 124 | + logging.error( |
| 125 | + LOG_PREFIX + " Could not obtain alerts list from the list alerts response from LibreNMS API or found no matching alerts.") |
| 126 | + |
| 127 | + logging.debug("Alert Id from LibreNMS: " + str(alert_id)) |
| 128 | + if alert_id is not None: |
| 129 | + if mapped_action == "ackAlert": |
| 130 | + url = url + "/api/v0/alerts/" + str(alert_id) |
| 131 | + elif mapped_action == "unmuteAlert": |
| 132 | + url = url + "/api/v0/alerts/unmute/" + str(alert_id) |
| 133 | + |
| 134 | + logging.debug("Sending PUT request to " + str(url)) |
| 135 | + |
| 136 | + response = requests.put(url, None, headers={"X-Auth-Token": api_token}, timeout=timeout) |
| 137 | + |
| 138 | + logging.debug("Response from " + url + ": " + str(response.content) + "Status Code: " |
| 139 | + + str(response.status_code)) |
| 140 | + |
| 141 | + if response.status_code < 400: |
| 142 | + logging.info(LOG_PREFIX + ' Succesfully executed at LibreNMS.') |
| 143 | + logging.debug(LOG_PREFIX + " LibreNMS response:" + str(response.content)) |
| 144 | + else: |
| 145 | + logging.error( |
| 146 | + LOG_PREFIX + " Could not execute at LibreNMS; response: " + str( |
| 147 | + response.status_code) + ' ' + str(response.text)) |
| 148 | + else: |
| 149 | + logging.error(LOG_PREFIX + " Alert Id from the LibreNMS API was null.") |
| 150 | + else: |
| 151 | + logging.error( |
| 152 | + LOG_PREFIX + " Could not get alert list from LibreNMS; response: " + str( |
| 153 | + list_alerts_response.status_code) + ' ' + str(list_alerts_response.text)) |
| 154 | + else: |
| 155 | + logging.error(LOG_PREFIX + " Rule Id from the LibreNMS API was null.") |
| 156 | +else: |
| 157 | + logging.error( |
| 158 | + LOG_PREFIX + " Could not get rules list from LibreNMS; response: " + str( |
| 159 | + list_rules_response.status_code) + ' ' + str(list_rules_response.text)) |
| 160 | + |
0 commit comments