With limited disk space and the huge volume of data accepted and stored by web applications,
web servers can easily run into out-of-space issues. That can even lock you out of the server
if those files are piling up in the system volume (which is usually the case). One solution to
this problem is constant monitoring. Disk utilization is displayed on all hosting control panels.
But what if you have an unmanaged VPS? Sure, you can log in via SSH and issue the command
df -h. But what if you want to let some non-technical client check this? Here the ideal solution will be to provide them with a display that can be accessed via a web browser (ideal in terms of usability and security).
Do you have to install a feature-rich monitoring solution like nagios for that? Not at all! Just write a script to pass the output of the
df command and share the link to that page with the client.
The page can be protected using basic HTTP authentication or easier, the link can be made obscure (after all, there isn't much of a security issue here since the script is display-only, that too dealing with some nonsensitive info).
So, how do you do that? Let's start by assuming you have a working PHP web app hosted on your site. Create a PHP file somewhere under the document root of your working site (ideally with an obscure name) and fill it with the following script:
<?php header("Content-Type: text/plain"); system("df -h"); ?>
Wait, that's it? Yes, that's it! After all, all it does is executing the command
df -h and passing the output to the visitor. The option
-h makes sure the sizes are printed in human-readable units like MB and GB rather than bytes. The output can still look cluttered if there are services like Ubuntu Snap running. So let's filter it out:
<?php header("Content-Type: text/plain"); system("df -h|grep -E '^(Filesystem\s|\/dev\/sd)'"); ?>
The above example passes the output of
df -h through
grep to select the lines that start with Filesystem (the headings) or /dev/sd (the disk partitions). You might want to tweak this based on the output you get.
In Other Environments
The above trick will be straightforward only if you have a working PHP-based site on your server, that too having a structure that allows custom scripts. If you are running some installed package like WordPress or Moodle, it's not a good idea to place your scripts inside it. Instead, place the script somewhere else and make Apache or nginx execute it when the user visits a specific URL. This is usually done with the help of the location directive in the server configuration.
The basic idea of passing the output of
df -h will work with languages other than PHP also. All you have to find is the equivalent of
system() in that language.
Now, if you want to password-protect this page, you can do that with HTTP basic authentication in Apache or nginx, or add some password checking in the script itself.