Skip to content

Instantly share code, notes, and snippets.

@n00rm
Last active February 5, 2024 09:30
Show Gist options
  • Save n00rm/32f1334b1dd2efc40122fee36551ef17 to your computer and use it in GitHub Desktop.
Save n00rm/32f1334b1dd2efc40122fee36551ef17 to your computer and use it in GitHub Desktop.
Discord Checkmk Notification Script
#!/usr/bin/env python3
# Notify via Discord
import os
import requests
import sys
USERNAME = "check_mk"
AVATARURL = "https://checkmk.com/favicon-96x96.png"
COLORS = {
"CRITICAL": "15597568",
"DOWN": "15597568",
"WARNING": "16768256",
"OK": "52224",
"UP": "52224",
"UNKNOWN": "13421772",
"UNREACHABLE": "13421772",
}
def discord_msg(context):
"""Build the message for discord"""
facts = []
if context.get('WHAT', None) == "SERVICE":
state = context["SERVICESTATE"]
color = COLORS.get(state)
subtitle = "Service Notification"
facts.append({"name": "Service:", "value": context["SERVICEDESC"]})
output = context["SERVICEOUTPUT"] if context["SERVICEOUTPUT"] else ""
else:
state = context["HOSTSTATE"]
color = COLORS.get(state)
subtitle = "Host Notification"
output = context["HOSTOUTPUT"] if context["HOSTOUTPUT"] else ""
facts.extend([
{
"name": "Host:",
"value": context["HOSTNAME"]
},
{
"name": "State:",
"value": state
}
])
return {
"username": USERNAME,
"avatar_url": AVATARURL,
"embeds": [
{
"title": subtitle,
"color": color,
"fields": facts,
"footer": {
"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=204):
context = collect_context()
url = context.get("PARAMETERS")
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(discord_msg)
@Glowsome
Copy link

Glowsome commented Aug 30, 2023

Would it be possible to make a switch so notifications become more condensed ?
Meaning the discord message becomes more lean and displays as following:
Service Notification: (carriage return here) Service: service name (carriage return here) Host: Hostname (carriage return here) State: State_here (carriage return here) Detail: detail msg here (carriage return here)

Why : when you have a large amount of hosts it is preferable to see the messages in a more condensed way (saves scrolling).

Hope to hear back from you :)

@Glowsome
Copy link

Also one thing i did change in the script was the value of AVATARURL.
For some reason it never took hold, and got the default 'Wumpus' avatar witth the notifications.

After some digging i changed it to : "https://checkmk.com/android-chrome-192x192.png" - which works and shows the logo of CMK as avatar.

@fschlag
Copy link

fschlag commented Feb 2, 2024

Inspired by your plugin I also extended it a lot for my homelab an pushed all changes to a dedicated repo in case someone else wants to use it: https://github.com/fschlag/cmk_discord

@n00rm
Copy link
Author

n00rm commented Feb 5, 2024

Inspired by your plugin I also extended it a lot for my homelab an pushed all changes to a dedicated repo in case someone else wants to use it: https://github.com/fschlag/cmk_discord

Thanks @fschlag! Looks good and future development should and will be done in your project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment