Last active
October 10, 2018 12:27
-
-
Save naumvd95/27cbc224b95e2a215da56d5240aed5f9 to your computer and use it in GitHub Desktop.
[ProtocolSweetHome][Telegram] Send alert message in case of shutdown linux machine
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
#!/bin/bash | |
# text to @botFather | |
# get botID, chatID | |
# set env vars | |
echo "export ALARM_CHAT=YOUR_ID" >> .bashrc | |
echo "export ALARM_BOT_TOKEN=YOUR_TOKEN >> .bashrc | |
source .bashrc | |
################################################### | |
root@vnaumov-dev:~# cat /etc/systemd/system/start_and_stop.service | |
[Unit] | |
Description=UP/DOWN telegram alerts | |
After=network.target networking.service network-online.target nss-lookup.target systemd-resolved | |
Requires=network.target networking.service network-online.target nss-lookup.target systemd-resolved | |
[Service] | |
Type=oneshot | |
RemainAfterExit=true | |
ExecStart=/root/.alert_commands startup | |
ExecStop=/root/.alert_commands shutdown | |
[Install] | |
WantedBy=multi-user.target | |
################################################### | |
################################################### | |
root@vnaumov-dev:~# cat .alert_commands | |
#!/bin/bash | |
# Initialize our own variables: | |
ALARM_BOT_TOKEN= | |
ALARM_CHAT= | |
shutdown_header="_I am going to shutdown.._" | |
startup_header="_I am alive_" | |
header="Check manually" | |
case "$1" in | |
shutdown) | |
header=$shutdown_header | |
;; | |
startup) | |
header=$startup_header | |
;; | |
esac | |
echo $header | |
msg="\ | |
$header | |
\`\`\`bash | |
name: $(hostname) | |
-------------------------------------------------- | |
addresses: $(hostname -I) | |
-------------------------------------------------- | |
RAM: $(free -h) | |
-------------------------------------------------- | |
Space: $(df -h) | |
-------------------------------------------------- | |
Visitors: | |
$(who -a) | |
-------------------------------------------------- | |
alert log saved in /root/telegram_monitor.log | |
-------------------------------------------------- | |
\`\`\` | |
" | |
echo -e "$(date)--------------------------------------------\n" >> /root/telegram_monitor.log | |
curl -X POST \ | |
-H 'Content-Type: application/json' \ | |
-d "{\"chat_id\": $ALARM_CHAT, \"text\": \"$msg\", \"disable_notification\": true, \"parse_mode\": \"Markdown\"}" \ | |
https://api.telegram.org/bot$ALARM_BOT_TOKEN/sendMessage -vvv >> /root/telegram_monitor.log | |
echo -e "--------------------------------------------\n" >> /root/telegram_monitor.log | |
########################################################################################################################### |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment