Last active
December 12, 2020 11:27
-
-
Save Foolson/9f8317debb7f58f4b13d566e179b773f to your computer and use it in GitHub Desktop.
Nagios - Notify by Telegram
This file contains hidden or 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
# 'notify-host-by-telegram' command definition | |
define command { | |
command_name notify-host-by-telegram | |
command_line notify-by-telegram -t"[TOKEN]" -c"[CHAT ID]" -m"***** Nagios Host *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" | |
} | |
# 'notify-service-by-telegram' command definition | |
define command { | |
command_name notify-service-by-telegram | |
command_line notify-by-telegram -t"[TOKEN]" -c"[CHAT ID]" -m"***** Nagios Service *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$\n" | |
} |
This file contains hidden or 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/env python3 | |
import requests | |
import argparse | |
import sys | |
parser = argparse.ArgumentParser() | |
requiredNamed = parser.add_argument_group('required named arguments') | |
requiredNamed.add_argument('-t', '--token', required=True) | |
requiredNamed.add_argument('-c', '--chatid', required=True) | |
requiredNamed.add_argument('-m', '--message', required=True) | |
parser.add_argument('-v', '--verbose', action='store_true', help='print stdout') | |
parser.add_argument('-s', '--silent', action='store_true', help='suppress stderr') | |
parser.add_argument('-M', '--markdown', action='store_true', help='enable markdown in message') | |
args = parser.parse_args() | |
token = args.token | |
chat_id = args.chatid | |
message = args.message.replace('\\n', '\n') | |
verbose = args.verbose | |
silent = args.silent | |
markdown = args.markdown | |
if markdown: | |
parse_mode = 'MarkdownV2' | |
else: | |
parse_mode = None | |
data = { | |
'chat_id': chat_id, | |
'text': message, | |
'parse_mode': parse_mode | |
} | |
url = f'https://api.telegram.org/bot{token}/sendmessage' | |
r = requests.post(url, data) | |
telegram_status = r.json() | |
if r.status_code is requests.codes.ok: | |
if verbose: | |
print(f'Telegram sent:\n{telegram_status}') | |
else: | |
if not silent: | |
print( | |
f'Telegram failed:\n{telegram_status}', | |
file=sys.stderr | |
) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment