I am aware of the notify.log located at /opt/omd/sites/servername/var/log/notify.log however this provides very little information about what is actually happening when my custom python script runs. I am using the python script below but it is not triggering the webhook as expected.
Does anyone know of a better logging file or another way to troubleshoot this? I have confirmed that the native teams webhook is working fine so it shouldn't be a networking issue preventing the webhook from going out.
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# Jenkins
import os
import requests
import sys
COLORS = {
"CRITICAL": "#EE0000",
"DOWN": "#EE0000",
"WARNING": "#FFDD00",
"OK": "#00CC00",
"UP": "#00CC00",
"UNKNOWN": "#CCCCCC",
"UNREACHABLE": "#CCCCCC",
}
def teams_msg(context):
facts = []
type = context.get("WHAT", None)
if not type:
sys.stderr.write("Unable to determine notification type")
sys.exit(2)
state = context.get("{type}STATE".format(type=type), "UNKNOWN")
color = COLORS.get(state)
subtitle = "{type} Notification".format(type=type.capitalize())
output = context.get("{type}OUTPUT".format(type=type))
if type == "SERVICE":
facts.append({"name": "Service:", "value": context.get("SERVICEDESC")})
facts.extend(
[
{"name": "Host:", "value": context.get("HOSTNAME")},
{"name": "State:", "value": state},
{"name": "Timestamp:", "value": context.get("LONGDATETIME")},
]
)
facts = list(filter(lambda x: x["value"], facts))
return {
"@type": "MessageCard",
"@context": "https://schema.org/extensions",
"summary": subtitle,
"themeColor": color,
"sections": [
{
"activityTitle": "CheckMK",
"activitySubtitle": subtitle,
"activityImage": "https://checkmk.com/favicon-32x32.png",
"facts": facts,
"text": output
}
]
}
def collect_context():
return {
var[7:]: value
for (var, value) in os.environ.items()
if var.startswith("NOTIFY_")
}
def post_request(message_constructor, success_code=200):
context = collect_context()
url = context.get("PARAMETER_1")
r = requests.post(url=url, json=message_constructor(context))
if r.status_code == success_code:
sys.exit(0)
else:
sys.stderr.write(
"Failed to send notification. Status: %i, Response: %s\n"
% (r.status_code, r.text)
)
sys.exit(2)
if __name__ == "__main__":
post_request(teams_msg)