Created
February 19, 2020 16:22
-
-
Save fr34kyn01535/eac3888a3b05f82fb00341c2c7dfec6e to your computer and use it in GitHub Desktop.
Icinga Teams Connector (Via webhook)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object NotificationCommand "notify-teams" { | |
import "plugin-notification-command" | |
command = [ PluginDir + "/teams_connector.py" ] | |
arguments += { | |
"--h" = "$hostn$" | |
"--hn" = { | |
description = "Host" | |
value = "$host_name$" | |
} | |
"--ho" = { | |
description = "Host output" | |
value = "$host_output$" | |
} | |
"--hsn" = { | |
description = "Host state new" | |
required = true | |
value = "$host_state_new$" | |
} | |
"--hso" = { | |
description = "Host state old" | |
required = true | |
value = "$host_state_old$" | |
} | |
"--s" = "$servicen$" | |
"--sn" = "$service_name$" | |
"--so" = { | |
description = "Service output" | |
value = "$state_output$" | |
} | |
"--ssn" = { | |
description = "Service state new" | |
value = "$service_state_new$" | |
} | |
"--sso" = { | |
description = "Service state old" | |
value = "$service_state_old$" | |
} | |
} | |
vars.host = "$host.name$" | |
vars.host_name = "$host.display_name$" | |
vars.host_output = "$host.output$" | |
vars.host_state_new = "$host.state$" | |
vars.host_state_old = "$host.last_state$" | |
vars.hostn = "$host.name$" | |
vars.notif_name = "$host.name$" | |
vars.notif_state = "$host.last_hard_state$" | |
vars.service = "$service.name$" | |
vars.service_name = "$service.display_name$" | |
vars.service_state_new = "$service.state$" | |
vars.service_state_old = "$service.last_state$" | |
vars.servicen = "$service.name$" | |
vars.state_output = "$service.output$" | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
apply Notification "Teams Notifcations (Hosts)" to Host { | |
import "Teams Notifications" | |
assign where host.zone | |
users = [ "teams-bot" ] | |
} | |
apply Notification "Teams Notifcations (Services)" to Service { | |
import "Teams Notifications" | |
assign where host.zone | |
users = [ "teams-bot" ] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template Notification "Teams Notifications" { | |
command = "notify-teams" | |
interval = 0s | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import requests | |
import argparse | |
import json | |
import os | |
def main(): | |
url = 'https://outlook.office.com/webhook/....' | |
baseUrl = "https://myicinga.com" | |
parser = argparse.ArgumentParser(description='Send a message.') | |
parser.add_argument('--h', dest='h', type=str, required=True) | |
parser.add_argument('--hn', dest='hn', type=str, required=False) | |
parser.add_argument('--ho', dest='ho', type=str, required=False) | |
parser.add_argument('--hsn', dest='hsn', type=str, required=False) | |
parser.add_argument('--hso', dest='hso', type=str, required=False) | |
parser.add_argument('--s', dest='s', type=str, required=False) | |
parser.add_argument('--sn', dest='sn', type=str, required=False) | |
parser.add_argument('--so', dest='so', type=str, required=False) | |
parser.add_argument('--ssn', dest='ssn', type=str, required=False) | |
parser.add_argument('--sso', dest='sso', type=str, required=False) | |
args = vars(parser.parse_args()) | |
if args['hso'] == args['hsn'] and args['sso'] == args['ssn']: | |
return | |
color = "dc3545" | |
if not args['s']: | |
message = "Host down" | |
if args['hsn'] == "Up": | |
color = "28a745" | |
message = "Host back online" | |
requests.post(url, json={ | |
"@type": "MessageCard", | |
"@context": "https://schema.org/extensions", | |
"summary": message, | |
"title": message, | |
"themeColor": color, | |
"sections": [ | |
{ | |
"facts": [ | |
{ | |
"name": "Host:", | |
"value": "[" + args['hn'] + "]("+baseUrl+"/monitoring/host/show?host="+args["h"] + ")" | |
}, | |
{ | |
"name": "State:", | |
"value": args['hso']+" ➠ " +args['hsn'] | |
} | |
], | |
"text": ">"+args['ho'] | |
} | |
] | |
}) | |
else: | |
if args['ssn'] == "OK": | |
color = "28a745" | |
if args['ssn'] == "Warning": | |
color = "ffc107" | |
requests.post(url, json={ | |
"@type": "MessageCard", | |
"@context": "https://schema.org/extensions", | |
"summary": "Service state changed", | |
"title": "Service state changed", | |
"themeColor": color, | |
"sections": [ | |
{ | |
"startGroup": True, | |
"facts": [ | |
{ | |
"name": "Host:", | |
"value": "[" + args['hn'] + "]("+baseUrl+"/monitoring/host/show?host="+args["h"] + ")" | |
}, | |
{ | |
"name": "Service:", | |
"value": "[" + args['sn'] + "]("+baseUrl+"/monitoring/host/show?host="+args["h"]+"&service="+args["s"] + ")" | |
}, | |
{ | |
"name": "State:", | |
"value": "test" + args['sso']+" ➠ " +args['ssn'] | |
} | |
], | |
"text": ">"+args['so'] | |
} | |
] | |
}) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment