Monitor disk space usage in AWS using Node.js by rfvallina

Posted Feb 27, 2016 in nodejs aws linux

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.

'use strict'

var disk = require('diskusage'),
	nodemailer = require('nodemailer'),
	sendmailTransport = require('nodemailer-sendmail-transport');

var THRESHOLD = 30;

disk.check('/', function(err, info) {
	var availablePercentage = ((info.available / * 100).toFixed(2);
	if(availablePercentage <= THRESHOLD){
		//Setup email data
		var transporter = nodemailer.createTransport(sendmailTransport({
			path: '/usr/sbin/sendmail'
		var mailOptions = {
		    from: 'EC2 Admin <>', // sender address
		    to: ',', // list of receivers
		    subject: 'Disk Usage Warning!!!', // Subject line
		    text: 'Only ' + availablePercentage + '% of disk space is available' // plaintext body
		//Send email
		transporter.sendMail(mailOptions, function(error, info){
		        return console.log(error);
		    console.log('Message sent: ' + info.response);

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.

  "name"        : "disk-usage-alarm",
  "version"     : "0.0.1",
  "private"     : "true",
  "description" : "",
  "author"      : "rfvallina",
  "homepage"    : "",
  "dependencies": {
    "diskusage": "^0.1.4",
    "nodemailer": "^2.0.0",
    "nodemailer-sendmail-transport": "1.0.0"

Cron setup

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
crontab -e
  • Add entry to crontab. Place the below text in crontab.

0 * * * * node /path/to/script/app.js