Disk usage check via PHP

Just a few days ago when I was checking one of my production servers I saw that one of them had almost run of space and I wasn't notified about it. So I fixed the space problem, but I needed to find a fast solution in order to be notified if they reach a certain amount of disk usage.

I know the solutions like installing all kind of monitoring systems like Nagios, Cacti or/and installing something similar. In most cases that's the right solution, but in this case this server is pretty constant and it stays live only for legacy purposes and it will be terminated in a year.

In this machine I have PHP, Nginx and that's pretty much it. So here is my fast solution.

I created a php file in a publicly accessible directory with the following content.
<?php
$filesystem = 'Filesystem name'; // put your Filesystem that you'd like to monitor.

$out = trim(shell_exec("df -h | grep '".$filesystem."' | awk '{print $5}'"));
$out = (int) str_replace('%', '', $out);

if ($out >= 90) {
    http_response_code(500);
} elseif ($out == 0) { //maybe something is wrong and you don't really check the right thing
    http_response_code(501);
}

Then I went to uptimerobot.com and created an endpoint check to this file. And that's all. If my disk usage reaches >= 90% uptimerobot.com will create an alert and send me an email.

Enjoy your day and be happy :)