Saturday, August 24, 2013

Diskfree Watcher script for Linux (Walkthough)

Problem: VMWare does not report Guest OS freespace, and as such there are no alarms triggered and emailed.

Solution: For Linux, script hourly "df" testing within the OS.

We will script the use of “df” to watch disk free space and perform sendmail if the usage is greater-than or equal-to a specified percentage.

Prerequisite: “df”, “sendmail”, “grep”, “awk”, “cut” -- please install them if not installed.
Prerequisite: You will need a valid mail-relay target or mail server target.

Assuming root account.

note: You will alter the script for your specific “df” output.
note: Your drive device will have to be determined manually.
note: “sendmail” my be in some other path than “/usr/sbin”
note: This was all performed on CentOS, a RedHat clone.  Other distributions may be slightly different.

Let’s first run the “df -h” command (diskfree -human_readable)

In my case, this is:
df -h
Filesystem            Size  Used Avail Use% Mounted on
                      90G   64G   22G  75% /
tmpfs                 939M     0  939M   0% /dev/shm
/dev/sda1             485M  115M  346M  25% /boot
                     4.9G  160M  4.5G   4% /home

So I have a choice, I can use “df” against any of the listed file-systems. Each one has a Mount point (alias)

I’m interested in my root drive, so I’ll choose “df -h /dev/mapper/vg_netflow-lv_root”.  I could have as easily chose “df -h /” which would be the same in this case.
df -h /dev/mapper/vg_netflow-lv_root
Filesystem            Size  Used Avail Use% Mounted on
                      90G   64G   22G  75% /

Now what I’m interested in is the Use% (75% here).  I can “grep” for “% /” so that only that specific line is printed.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/"
                      90G   64G   22G  75% /

Now I’m still only interested in the 75%, which is the 4th item.  Let’s “awk” that.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }'

Now I only see 75%.  But I can’t ‘If-Then’ a number containing the %-sign, so I’ll “cut” it.
df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }' | cut -d% -f1

This is the result I want to perform an action on -- so now a script can be written.  Here is mine:
cat /root/scripts/
used=`df -h /dev/mapper/vg_netflow-lv_root | grep "\% \/" | awk '{ print $4 }' | cut -d% -f1`
echo "diskspace used: $used%";
if [ $used -ge 93 ] ;
/usr/sbin/sendmail "" << EOF
Subject: Alert: netflow diskspace $used% used.
netflow diskspace $used% used.

This script prints the usage to screen and also sends mail if the usage if greater-than_or_equal-to 93%.

Note the “used”-variable assignment -- The command is single-back-quoted.  The ` character on the ~ (tilde) key.
Change the trigger-limit (93 here) as you see fit. And also, all your email information as it pertains to you and your server.  Make sure to mark your script executable, in my case
chmod +x /root/scripts/

You will need to configure your OS for sendmail.  As, stated above, you will need a valid email server or relay target.  This write-up does not go into such.

Now configure sendmail, In my case, I used postfix sendmail, so I added
relayhost =
to the file “/etc/postfix/” then restarted the postfix service
service postfix restart

You should test sendmail by typing the part of the script between “then” and “fi” directly in the command-line.

If it works, you are ready to cron-job the script.
crontab -e
0 * * * * /root/scripts/
for hourly execution.

Good Luck!