How to monitor the size of a directory
Use Monibot to monitor the size of a directory.
A data folder
For the purpose of this guide, let's say you have an application that stores data files in /opt/data. The data stored in that directory could be Postgres/MySQL database files, json files, or any other kind of persistent storage files.
To show the directory size, you would normally run the following command:
$ du -s /opt/data
9095456 /opt/data
In the example above, the directory with all its files and subdirectories, occupies 9095456 kilobytes of disk space, that's ca. 9 gigabytes.
Create a Monibot metric
For monitoring numeric values, Monibot provides metrics. Let's create one:
- Log into your Monibot account. If you don't have one, register now, it's free.
- In the main menu, click 'Metrics'. You'll see a 'New Metric' button.
- Click the 'New Metric' button. Enter a name for the metric, e.g. 'data size'. You can chose any name here, it's used only for display.
- As metric type, select 'Gauge'. A gauge is a numeric value that can go up and down, which is what we want for our directory size.
- Click 'Save'. Monibot creates the metric and assigns a unique id to it, the metric id. You will need that later.
Send metric values to Monibot
To send values for the 'data size' metric, you can use the curl
command
or our moni
command line tool
(see https://github.com/cvilsmeier/moni).
We will describe the procedure using moni
.
Create a 'datasize.sh' script with the following content:
#!/bin/sh
# initialize api key and metric id - please replace with your values
MONIBOT_API_KEY=0000000000
METRIC_ID=7777777777
# get directory size and store it in SIZE variable
SIZE=$(du -s /opt/data | cut -f1)
# use moni 'set' command to upload value to Monibot
moni -apiKey $MONIBOT_API_KEY set $METRIC_ID $SIZE
Now invoke this script manually from the command line:
$ ./datasize.sh
In Monibot, click the 'data size' metric. The chart should be updated and in the 'Values' tab, you should see the current directory size (you may have to reload the page once to see new values).
Now put the script in your crontab, so that it is executed automatically, e.g. every hour:
$ crontab -l
# m h dom mon dow command
0 * * * * /home/user/datasize.sh
Summary
In this guide we've described how to easily monitor the size of a data folder.