This script monitors system metrics such as uptime, CPU load, RAM usage, disk availability, and active connections on a Linux server. It then sends the data to a specified Telegram chat through a bot.
- Reports system uptime, CPU load, RAM usage, disk availability, and active network connections.
- Sends the data to a Telegram chat via a bot.
- Can be run periodically using
crontab.
Before using this script, ensure you have:
- curl installed on your server:
sudo apt-get install curl # On Debian/Ubuntu sudo yum install curl # On CentOS/RedHat
- A Telegram Bot:
-
Clone the repository:
git clone git@github.com:DyanGalih/telegram-bot-server-monitoring.git cd telegram-bot-server-monitoring -
Edit the script: Open
tg_server_monitoring.shand replace the placeholders in the script:[CHAT_ID]: Replace this with your Telegramchat_id.[TELEGRAM_BOT_TOKEN]: Replace this with your Telegram bot token.[MESSAGE_ID]: Replace this with your Telegram message_id.[TZ]: Replace this with your Timezone.
-
Make the script executable:
chmod +x tg_server_monitoring.sh
You can schedule this script to run at regular intervals using crontab. This setup will run the script every minute, sending system metrics to your Telegram chat.
-
Open the crontab editor:
crontab -e
-
Add the following line to run the script every minute:
* * * * * /path/to/your/tg_server_monitoring.sh
Replace
/path/to/your/tg_server_monitoring.shwith the actual path to your script. -
Save and exit. The script will now run every minute and send system information to the specified Telegram chat.
- Intervals: To change the frequency of the monitoring, adjust the time in the
crontabentry:- Every 5 minutes:
*/5 * * * * - Every hour:
0 * * * *
- Every 5 minutes:
Since crontab doesn't natively support intervals less than 1 minute, to run a script every 30 seconds, you can achieve it by adding two crontab entries.
crontab does not natively support intervals shorter than 1 minute, but you can work around this by running the script twice within each minute (at the 0-second and 30-second marks).
-
Open the crontab editor:
crontab -e
-
Add the following lines to run the script every 30 seconds:
* * * * * /path/to/your/tg_server_monitoring.sh * * * * * sleep 30 && /path/to/your/tg_server_monitoring.sh
Replace
/path/to/your/tg_server_monitoring.shwith the actual path to your script. -
Save and exit. This setup will run the script at the start of every minute and then again 30 seconds later.
The bot will send a message to the Telegram chat with system information like this:
Hostname = your-server [12:34]
Uptime Server = up 2 days, 4 hours
Ram Free = 1.2G
Disk Available = 50G
Connections = 10
CPU Load = 23%
- Find your bot in Telegram by searching for its username.
- Click Start or send
/startto initiate a conversation with your bot. - You can now send messages directly to the bot.
- In Telegram, create a new group.
- Add the bot to the group by searching for its username.
- Start interacting with the group and the bot.
After starting the conversation (either direct chat or in a group), you can retrieve the chat_id by using the getUpdates method.
-
Open a terminal and run the following
curlcommand to retrieve updates from your bot:curl -X GET "https://api.telegram.org/bot[telegram_bot_token]/getUpdates"Replace
[telegram_bot_token]with your actual bot token. -
You’ll receive a JSON response. Look for the
chatobject inside themessagesection, which contains thechat_id.Example of a response snippet:
{ "update_id": 123456789, "message": { "message_id": 1, "from": { "id": 987654321, "is_bot": false, "first_name": "John", "username": "john_doe", "language_code": "en" }, "chat": { "id": 987654321, "first_name": "John", "username": "john_doe", "type": "private" }, "date": 1629985262, "text": "/start" } }The
idinside thechatobject is yourchat_id. In this case, thechat_idis987654321.
Once you have the chat_id, you can send a message to your chat using the sendMessage method of the Telegram Bot API.
-
Use the following
curlcommand to send a message to your chat:curl -X POST "https://api.telegram.org/bot[telegram_bot_token]/sendMessage" \ -d chat_id=[chat_id] \ -d text="Initialized"
Replace:
[telegram_bot_token]with your actual bot token.[chat_id]with thechat_idyou retrieved."Initialized"with your desired message.
-
After running the command, your bot will send the message to the specified chat.
-
You will get the return from telegram like this:
{"ok":true,"result":{"message_id":12,"from":{"id":12345,"is_bot":true,"first_name":"first_name","username":"bot_username"},"chat":{"id":-364434626,"title":"Server Monitoring","type":"group","all_members_are_administrators":true},"date":1726458968,"text":"Initialized"}}
Replace the MESSAGE_ID in the bash file to message_id from the result sendMessage telegram API
If the bot is added to a group, the steps are the same. You’ll get the chat_id from the getUpdates response, and it will represent the group where the bot was added. You can use the same sendMessage command with the group chat_id to send a message to the group.
With these instructions, you should be able to set up your bot, retrieve the chat_id, and send messages from your server to Telegram chats using curl.
This project is licensed under the MIT License. See the LICENSE file for details.