Sometimes, if you are a server administrator or you simply are an application owner but you have full rights on the server, you could be interested in being informed when the disk space is reaching the maximum capacity.
In my case I had to deal with this while setting up different AWS environments. In AWS there’s no built-in alarm to monitor disk usage. Depending on the case, AWS could provide scripts to create custom metrics. Such is the case with disk usage metrics. AWS provides a set of scripts to report memory, swap, and disk space utilization metrics for a Linux instance.
- You have to install these scripts manually
- AWS charges you for having custom metrics
A cheaper solution is to develop your own tool to report disk usage. I created a simple Node script which sends an email notification in case the available disk percentage falls below a certain threshold.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
As you see, the script requires to use three third-party node modules. diskusage is responsible to check the disk space, while nodemailer and nodemailer-sendmail-transport are responsible for sending the email notification. It assumes that sendmail is installed in the system to ensure the message reaches its destination.
Below is the package.json used to setup the script. You have to type npm install to have all the necessary modules installed. And type node app.js to run the script.
1 2 3 4 5 6 7 8 9 10 11 12 13
If you want the script to run on a regular basis, let cron daemon do it. Below are the steps to add it to crontab.
- Edit crontab
- Add entry to crontab. Place the below text in crontab.
0 * * * * node /path/to/script/app.js