Monit Alerts to Telegram Without a Bot Token

Monit Alerts to Telegram Without a Bot Token

push.tg

Monit can run a program when a check fails and again when it recovers. That's all you need for Telegram alerts: a three-line script with curl, and push.tg delivers the message to your chat. No BotFather, no bot token, no chat ID, and no mail server to set up.

Step 1: get your endpoint (30 seconds)

Open @trafficRobot in Telegram and press Start. You get an address like k3x9q@push.tg. Your endpoint is the part before the @: https://push.tg/k3x9q. Every example below uses k3x9q, so swap in your own key.

There's no bot to create, no chat_id to look up and no token to keep safe. Add @trafficRobot to a group and the group gets its own address, so everyone on call sees the messages.

Step 2: a tiny alert script

Save this as /usr/local/bin/pushtg-monit and make it executable (chmod +x):

#!/bin/sh
curl -s -m 10 --data-urlencode "data=Monit on $MONIT_HOST: $MONIT_SERVICE - $MONIT_EVENT
$MONIT_DESCRIPTION" https://push.tg/k3x9q

Monit sets these environment variables for programs it runs from an exec action: MONIT_SERVICE (the check name), MONIT_EVENT (what happened), MONIT_DESCRIPTION (the error text) and MONIT_HOST. --data-urlencode "data=..." sends the text in the form field push.tg reads, and keeps it intact if the description contains & or =.

Step 3: call it from your checks

Use exec as the failure action and else if succeeded then exec for the recovery message:

check host website with address example.com
  if failed port 443 protocol https for 2 cycles
    then exec "/usr/local/bin/pushtg-monit"
    else if succeeded then exec "/usr/local/bin/pushtg-monit"

check filesystem rootfs with path /
  if space usage > 90% then exec "/usr/local/bin/pushtg-monit"
    else if succeeded then exec "/usr/local/bin/pushtg-monit"

check system $HOST
  if memory usage > 85% for 5 cycles then exec "/usr/local/bin/pushtg-monit"

By default Monit runs the program once when the test starts failing. If you want a reminder while it stays broken, append repeat every 30 cycles to the exec action.

Then check the syntax and reload:

sudo monit -t && sudo monit reload

Step 4: test it

Point one check at a port that is closed, or run the script by hand with fake values:

MONIT_HOST=$(hostname) MONIT_SERVICE=test MONIT_EVENT='Connection failed' MONIT_DESCRIPTION='manual test' /usr/local/bin/pushtg-monit
curl -d "test from $(hostname)" https://push.tg/k3x9q

Prefer Monit's own email alerts?

Your push.tg address is also a real email address. If Monit already has a set mailserver line, adding set alert k3x9q@push.tg sends Monit's standard alert emails to the same Telegram chat. The exec route above just saves you the mail server.

Each Telegram chat or group gets its own push.tg address, so you can send the web checks to the on-call group and the disk warnings to yourself.

Check that the message really arrived. push.tg answers HTTP 200 even when the key is wrong; the body then says {"error":"no such user"}. Send one test by hand and look at Telegram before you trust it:

Official reference: Monit documentation (sections Action, Environment variables for the exec action, Setting an alert recipient).

Open @trafficRobot · push.tg

More copy-paste guides

Report Page