Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RundesBalli
Last active January 19, 2024 12:29
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save RundesBalli/4bdcf555c78c17a7b917de31cd7b4df0 to your computer and use it in GitHub Desktop.
Save RundesBalli/4bdcf555c78c17a7b917de31cd7b4df0 to your computer and use it in GitHub Desktop.
Server online-status check with reporting to telegram

Server online-status check with reporting to telegram

This simple script uses netcat to check if - for instance - a webserver is running on a server. If the service is not running, it utilizes the telegram bot API for sending a message to inform the operator of the server.

How to

1. Register the bot

Go to the BotFather bot-account and create a bot by writing /newbot. Then, copy the token.

2. Find your chat-id

Send a message to you newly created bot (you can find the link to your bot in the success message sent by BotFather).

Visit https://api.telegram.org/bot{token}/getUpdates to get your chat id: result => 0 => message => chat => id
Please note that the token must be directly behind bot in the URL. If your token starts with AAAA the URL looks like /botAAAA and so on.

3. Configure the script

Fill in the four variables server, port (SSL is on 443 by default), botToken and chatId.

4. Make the script executable

Execute chmod +x serverOnline.sh to make the script executable.

5. Test the script

To test the script, enter an unused port and execute it via ./serverOnline.sh. Don't forget to change the port number after testing.

6. Cron

Use crontab -e to append the following line to the cronjob list:
* * * * * /path/to/serverOnline.sh
Make sure you leave an empty line at the end of the file!

That's it!

And to make it absolutely and undoubtedly clear:
The script is supposed to be run on a different server, than the server being checked.

MIT License
Copyright (c) 2021 RundesBalli
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
#!/bin/sh
# Server online-status check with reporting to telegram
# https://RundesBalli.com
# https://GitHub.com/RundesBalli
# https://gist.github.com/RundesBalli/4bdcf555c78c17a7b917de31cd7b4df0
# Server variables
server="example.org"
port=443
# Telegram variables
botToken=""
chatId=""
# Use netcat to check if a daemon on $server:$port is running.
# If not, send telegram message.
if ! nc -w 2 -z $server $port > /dev/null 2>&1
then
curl \
-X POST \
-s \
--data "chat_id=${chatId}" \
--data "disable_web_page_preview=true" \
--data "text=Server ${server}:${port} down!%0A$(date +"%a, %d. %B %Y, %H:%M:%S")" \
--connect-timeout 30 \
--max-time 45 \
"https://api.telegram.org/bot${botToken}/sendMessage" \
> /dev/null
fi
@RundesBalli
Copy link
Author

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