Skip to content

Instantly share code, notes, and snippets.

@KostyaEsmukov
Last active November 26, 2021 09:02
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save KostyaEsmukov/1d49b04eaed1b8de3c8d378943fc1647 to your computer and use it in GitHub Desktop.
Save KostyaEsmukov/1d49b04eaed1b8de3c8d378943fc1647 to your computer and use it in GitHub Desktop.
Zabbix alerts via Telegram

Zabbix alerts via Telegram

⚠️ Deprecated: Zabbix now includes official support for Telegram media, see https://www.zabbix.com/integrations/telegram.

Replace my_zabbix_bot and My Zabbix Bot below with your own names.

Create and test telegram bot

Add @BotFather contact in Telegram and press 'start',
then type:

/newbot
My Zabbix Bot
my_zabbix_bot

The bot will reply you with a token, which we will refer to as ${TOKEN} below.

Add a @my_zabbix_bot contact to your Telegram, press 'start' and send a random message.

Now we need to retrieve a chat_id for this conversation.

$ curl "https://api.telegram.org/bot${TOKEN}/getUpdates"
{"ok":true,"result":[{"update_id":...,"message":{"message_id":...,"from":{...},"chat":{"id":<CHAT_ID>,...},"date":...,"text":...}}]}

The <CHAT_ID> above is what you need. It's the RESULT.result[0].message.chat.id key in JSON notation.

Test that it actually works:

$ curl -X POST --retry 5 --retry-delay 0 --retry-max-time 60 --data-urlencode "chat_id=${CHAT_ID}" --data-urlencode "text=Hello" "https://api.telegram.org/bot${TOKEN}/sendMessage?disable_web_page_preview=true"

Install zabbix media type

Find out your AlertScriptsPath from the zabbix_server.conf. It's ${datadir}/zabbix/alertscripts by default.

Put the telegram.sh file (attached) to that folder. Don't forget to chmod +x telegram.sh

Open your Zabbix web interface, navigate to 'Administration - Media types - Create',
Put in the form:

  • Name: Telegram
  • Type: Script
  • Script name: telegram.sh
  • Script parameters (Zabbix 3 only):
    • {ALERT.SENDTO}
    • {ALERT.SUBJECT}
    • {ALERT.MESSAGE}

Action

Ensure that you have an action (Configuration - Actions) which sends to the Telegram media type.

User

Navigate to 'Administration - Users - <Your user> - Media', add Telegram, in the 'Send to' field put your ${CHAT_ID} and ${TOKEN}, separated with space, for example: 12345678 123456789:AAaaaaAAAaaa_aAAaaAaA-aaAAa.

Refs:

#!/bin/sh
CHAT_ID=`echo "$1" | cut -d " " -f 1`
TOKEN=`echo "$1" | cut -d " " -f 2`
SUBJECT="$2"
MESSAGE="$3"
NL="
"
curl --silent -X POST --retry 5 --retry-delay 0 --retry-max-time 60 --data-urlencode "chat_id=${CHAT_ID}" --data-urlencode "text=Subject: ${SUBJECT}${NL}${NL}${MESSAGE}" "https://api.telegram.org/bot${TOKEN}/sendMessage?disable_web_page_preview=true" | grep -q '"ok":true'
@KostyaEsmukov
Copy link
Author

Zabbix doesn't create an environment for notification scripts, so if anyone else sits behind a http(s)-proxy, the curl-command fails if you configured the proxy with an environment variable.

@Ediacarium The http proxy env variables can be put right to the shell script. That's not from environment, but it is an easy solution for making it work via http proxy.

@mhikolet
Copy link

mhikolet commented Nov 26, 2021

can you help me I copied the script and put in the zabbix correct path alerts I have this error upon testing?
1
3
-94b6-c6
2
3104038490.JPG)

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