Script to Check Disk Space and Telegram Alerts

GateOmega
4 min readJul 18, 2022

If your system has low disk space, it can slow down your system and programs in it. This script monitors your system and alerts you if your system has low disk space. In this article you will find how to create a script to check disk space, and alerts if your system disk space is under a specific threshold you choose.

Contents:

  1. Creating a Shell Script.
  2. Creating Telegram Alerts.
  3. Adding variables to the script.
  4. Make Script Executable.
  5. Test the script.
  6. Automation — Crontab.

Create Shell Script

Open your shell and create a script file with the below command.

Add the following script into this file.

To save the file: : CTRL + O and press Enter. To exit from file: CTRL + X

Creating Telegram Alerts

  1. Create a bot using Telegram App.
  2. Create a channel in Telegram and make your bot admin.
  3. Finding your group/channel id.

How to create a bot using Telegram App?
Search for botfather Telegram App. It must have a blue tick on the right side. Click on botfather. The command you need is:

/newbot which leads to the steps to create your unique bot.

  • Choose your bot name and username.
  • Username must be unique and must end with “bot”.
  • You will get a message that contains your access token. This is also your <telegram bot id> will be used in your script. Save your access token in a safe place. Don’t share it and don’t lose it.

Telegram Bot ID example: 5497182788:AAGXS-nArOMZaER_6xqfeOLW-VD-frlSfDM

How to create a channel in Telegram and add your bot?

Create a new channel to get alerts. (1–2)
To add your newly created bot into your channel, click on the Subscribers and add subscribers. (3–4)
Search for your bot with “username” that you chosen for it and make it admin. (5–6)

How to find your chat ID? (channel ID)

We need a chat id to be able to get alerts from our server. Every chat room has an ID number attached to it. It doesn’t matter if it’s private or public.

Adding variables to the script

There are some sections and variables you need to change in the script that you created. Open your script again with the following command.

Threshold
Replace THRESHOLD value with the threshold value of your choice. If your system disk space has equal or above this THRESHOLD percentage you choose, you will get a MESSAGE to your telegram channel.
THRESHOLD=90(the threshold value of your choice)

Message
You may want to change the beginning of the message with your project name, if you particularly work on different and independent servers or projects.
MESSAGE=”Your server root partition remaining free space is critically low. Used: $CURRENT%”

Telegram Chat ID
Replace <YOUR TELEGRAM CHAT ID> section with your channel ID.
Example : -1001567031322

Telegram Bot ID
Replace <YOUR TELEGRAM BOT ID> section with your bot ID
Example : bot
5497182788:AAGXS-nArOMZaER_6xqfeOLW-VD-frlSfDM

To save the file: CTRL + O and press Enter
To exit from file: CTRL + X

Make Script Executable

Run the following command to make your script executable.

Testing the Script

You can test this script by setting a lower disk space THRESHOLD than the system actually has. Change the THRESHOLD value to 1 temporarily in the disk-space script file. Run the following command to check if you get an alert to your telegram channel.

You will get an output like that if everything is ok.

{"ok":true,"result":{"message_id":6,"sender_chat":{"id":-1001567031322,"title":"ServerAlertChannel","type":"channel"},"chat":{"id":-1001567031322,"title":"ServerAlertChannel","type":"channel"},"date":1658068451,"text":"YOUR SYSTEM root partition remaining free space is critically low. Used: 2%"}}

And you will also see a message on your telegram channel like this.

Example of Telegram Bot Alert

Automation

You may want to run this script hourly or in specific periods you need. So you need to create a cron job for it.

If you run this command for the first time in your system or server, you may have the following output. Then, choose 1 to continue.

Save and Exit: CTRL + O , Enter, CTRL + X

To understand crontab timing strings check below and play with https://crontab.guru

That’s all. Now your script will send the automated message if your system is running out of disk space.

--

--

GateOmega

Gate Omega is a new start-up company based in Turkey. We are a team of 6 and currently focusing on disruptive and innovative crypto projects .