Script to Check Disk Space and Telegram Alerts

  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.

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.
  • 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.

Adding variables to the script

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.

{"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%"}}
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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
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 .