2021-11-03 17:02:22 -04:00
|
|
|
from datetime import datetime
|
2021-11-03 00:19:52 -04:00
|
|
|
|
|
|
|
|
2021-11-03 17:02:22 -04:00
|
|
|
def formatter(data, headers):
|
2021-11-03 00:19:52 -04:00
|
|
|
"""Pretty-print a pingdom notification."""
|
2021-11-03 23:42:44 -04:00
|
|
|
# JSON data formatting was obtained from https://www.pingdom.com/resources/webhooks/
|
2021-11-04 00:17:24 -04:00
|
|
|
# these are common to all check types
|
2021-11-03 00:19:52 -04:00
|
|
|
check_id = data["check_id"]
|
|
|
|
check_name = data["check_name"]
|
|
|
|
current_state = data["current_state"]
|
2022-01-12 12:08:53 -05:00
|
|
|
tags = data["tags"]
|
2021-11-03 00:19:52 -04:00
|
|
|
local_time = datetime.fromtimestamp(data["state_changed_timestamp"])
|
|
|
|
|
|
|
|
if data["check_type"].lower() == "http":
|
2021-11-04 00:17:24 -04:00
|
|
|
# http https or http_custom check types
|
2021-11-03 00:19:52 -04:00
|
|
|
try:
|
|
|
|
check_url = data["check_params"]["full_url"]
|
2022-01-12 12:08:53 -05:00
|
|
|
message = ""
|
|
|
|
message += f"###{check_name} is {current_state}\n\n{check_url}"
|
|
|
|
message += f" marked {current_state} at {local_time} ⚬ "
|
|
|
|
message += f"[view details](https://my.pingdom.com/reports/responsetime#check={check_id})"
|
|
|
|
if tags:
|
|
|
|
message += f"\n\nTags: {tags}"
|
|
|
|
data["body"] = message
|
2021-11-03 00:19:52 -04:00
|
|
|
except Exception as error:
|
2021-11-04 00:17:24 -04:00
|
|
|
data["body"] = (
|
|
|
|
f"Error: An attempt to post from pingdom was malformed "
|
|
|
|
"(or I don't know how to handle what was sent).\n\n"
|
|
|
|
f"{repr(error)}"
|
|
|
|
)
|
2021-11-03 00:19:52 -04:00
|
|
|
elif data["check_type"].lower() == "dns":
|
|
|
|
# There are a bunch of values that are blanke when you do a test
|
|
|
|
# so ignore them if value is unset
|
2021-11-03 15:51:05 -04:00
|
|
|
try:
|
2021-11-03 00:19:52 -04:00
|
|
|
first_ip = data["first_probe"]["ip"]
|
|
|
|
except KeyError:
|
|
|
|
first_ip = "unknown"
|
|
|
|
try:
|
|
|
|
second_ip = data["second_probe"]["ip"]
|
|
|
|
except KeyError:
|
|
|
|
second_ip = "unknown"
|
|
|
|
try:
|
2021-11-03 23:42:44 -04:00
|
|
|
first_location = data["first_probe"]["location"]
|
|
|
|
except KeyError:
|
2021-11-03 00:19:52 -04:00
|
|
|
first_location = "unknown"
|
|
|
|
try:
|
|
|
|
second_location = data["second_probe"]["location"]
|
2021-11-03 23:42:44 -04:00
|
|
|
except KeyError:
|
2021-11-03 00:19:52 -04:00
|
|
|
second_location = "unknown"
|
|
|
|
try:
|
|
|
|
expected_ip = data["check_params"]["expected_ip"]
|
|
|
|
data["body"] = (
|
|
|
|
f"###{check_name} is {current_state}\n\n"
|
|
|
|
f"expected {expected_ip} but got:\n\n"
|
|
|
|
f" {first_ip} ({first_location})\n\n"
|
|
|
|
f" {second_ip} ({second_location})\n\n"
|
|
|
|
f" marked {current_state} at {local_time} ⚬ "
|
|
|
|
f"[view details](https://my.pingdom.com/reports/responsetime#check={check_id})"
|
|
|
|
)
|
|
|
|
|
|
|
|
except Exception as error:
|
|
|
|
print(error)
|
|
|
|
|
|
|
|
return data
|