On a regular basis I noticed that people forget to monitor the free diskspace of their VMFS volumes. I created a script that can easily be scheduled with crontab and mailed with smtp_send. You can send it as an html based email if you setup MIME correctly. (For more info on how to set this up, check this blog) The script creates an html file with a table in which the necessary info is dumped, if a VMFS volume has less than 10% free space that specific line will be yellow and if it’s less than 5% than that line will be red… So a helpdesk should be able to monitor it and warn you in case it runs or will run out of diskspace.
The script, and you can download it here!:
#!/bin/bash
# Script voor het controleren op het bestaan van Snapshots
LOGLOC="/var/log/"
LOG="${LOGLOC}vdf.html"
MYDATE=$(date +%d-%m-%y)
VMFSYELLOW=90
VMFSRED=95cat /dev/null > "${LOG}"
echo "<html>" >> "${LOG}"
echo "<head>" >> "${LOG}"
echo "<style>" >> "${LOG}"
echo "body {margin: 10px; width: 600px; font-family:arial; font-size: 10px;}" >> "${LOG}"
echo "div#footer {font-size: 9px;}" >> "${LOG}"
echo "</style>" >> "${LOG}"
echo "</head>" >> "${LOG}"
echo "<body>" >> "${LOG}"echo "<table cellspacing="0" cellpadding="2" border="1"; style='border-collapse:collapse; background-color: white; color: black'>" >> "${LOG}"
echo "<tr><th colspan='5'; align='left'; style='background-color: rgb(179,179,179); color: black'>" >> "${LOG}"
echo "VMFS Disk Usage - $MYDATE</th></tr>" >> "${LOG}"
echo "" >> "${LOG}"
echo "<tr>" >> "${LOG}"
echo "<td><b>VMFS Volume</b></td><td><b>Disk Size</b></td><td><b>Used</b></td><td><b>Available</b></td><td><b>Percentage</b></td>" >> "${LOG}"
echo "</tr>" >> "${LOG}"
vdf -h -P | grep -E '^/vmfs/volumes/' | awk '{ print $2 " " $3 " " $4 " " $5 " " $6 }' | while read output ; do
DISKSIZE=$(echo $output | awk '{ print $1 }' )
DISKUSED=$(echo $output | awk '{ print $2 }' )
DISKAVAILABLE=$(echo $output | awk '{ print $3 }' )
PERCENTINUSE=$(echo $output | awk '{ print $4 }' )
VOLNAME=$(echo $output | awk '{ print $5 }' )
CUTPERC=$(echo $PERCENTINUSE | cut -d'%' -f1 )
WARNING="white"
if [ $CUTPERC -ge $VMFSYELLOW ] ; then
WARNING="yellow"
fi
if [ $CUTPERC -ge $VMFSRED ] ; then
WARNING="red"
fi
echo "<tr><td style='background-color: ${WARNING}'>$VOLNAME</td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'>$DISKSIZE</td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'>$DISKUSED</td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'>$DISKAVAILABLE</td>" >> "${LOG}"
echo "<td style='background-color: ${WARNING}'>$PERCENTINUSE</td></tr>" >> "${LOG}"
done
echo "</table>" >> "${LOG}"
echo "<div id='footer'>Created by Ictivity bv.</div>" >> "${LOG}"
echo "</body>" >> "${LOG}"
echo "</html>" >> "${LOG}"
The output will look similar to this:
Matt Lydy says
I really want to try out this script in my environment. Thanks for posting this. Can you describe what you mean by “You can send it as an html based email if you setup MIME correctly.”
Should MIME be configured on the ESX server or is that for IIS or something like that? If you could point me to a link or documentation for getting that configured it would be greatly appreciated!
Duncan Epping says
Matt, I will post a blog tomorrow for you and others that requested this…
Edwin Dass says
Dear Duncan,
I download your script and ran it on Centos machine on our network, which is not a VM. it show and error Line 25:vdf:command not found
How do you point to the Vcenter to pull the data? Can you suggest me where I went wrong. I am not an linux expert.
Thanks,
Edwin Dass
Duncan Epping says
This script is over 4 years old. Can I recommend using a script by Alan (http://www.virtu-al.net , pretty sure his vCheck script does this) or William (http://www.virtuallyghetto.com/p/vghetto-script-repository.html)
Matt Lydy says
Awesome! Thank you!
Clint says
Very nice script. We use BMC patrol in our environment and although it does a good job monitoring many things in ESX it always tends to have some false positives on the VMFS disk size.
So this could be useful!!
Alexander Ebert says
Hello Duncan,
many thanks for this great job! This script is very useful for our VMware Enviroment. Vmware should be integrated into virtual center. The Alarms in Virtual Center are very poor und we need additional features, like your script, to monitor our productive enviroment.
Greetings from Germany
Alex
Kent A says
Could you repost the script without the formatting or as a download? The quotations are problematic, and I am unable to get the vdf -h -P line correct. Thanks.
Duncan Epping says
Kent, check the download!
Kent A says
Thank you – the download (which I must have missed) worked perfectly. Great blog!
Dinny says
Nice script.
I added a ” | sort -k 6″ into the vdf command string, to sort the vmfs vols in alphabetic order.
May be useful to some people – depending on naming conventions?
Dinny
Garp1997 says
Hi Duncan,
I used your script (great job) and it working well but, if I run it from crontab no table is in, only header and footer. Have you any suggestion?
Bye
Duncan Epping says
probably a path related issue. try to use full paths every where.
Garp1997 says
Thanks for answer Duncan, I added /usr/sbin/ before vdf and now it works.
Many thanks.
RiK says
I love this script, it helps a lot! I’m not a scripter, does anyone know how I could add a row at the bottom with total used/available and overall percent? Thanks!
rwu says
Would it be possible for you to try and get this info incorporated into RVTools?
Abid Belghiran says
Hi Duncan,
very great job! This script is very useful.
Thanks a lot !
Greetings
Abid
Abid says
Hello Duncan,
thanks for your very great script. Your Script works very good. I am a beginner in perl but now I would like to modify the script. I would like to be Informed only if the threshold for Warning is reached.
at the bottom of your script,I have add this.
if [ $CUTPERC -ge $VMFSRED ] ; then
/usr/local/bin/smtp_send.pl -t [email protected] -s “Diskspace report of the VMFS volumes” -f [email protected] -m “`cat /var/log/vdf.html`” -r 172.10.10.10
fi
But unfortunately it does not work because the values of $CUTPERC and $VMFSRED are lost after the loop. Could you give me a hint , how I can solve my problem
Many thanks.
timbow says
Hello Duncan,
thanks for the nice script. I’ve got a ESX 3.5 Server and it works great. I modified it so it only sends Emails when one of the Volumes status is “red”. Manually used it works perfect, but when I put it in Crontab as a job nothing happens. In the Crontab log files I can see that the script is executed every hour, thats like i wanted it but theres no email sending… can you help me? Thanks for help greetings from germany
naz says
Hello timbow,
how did you modify the script?
I need the same functionality, I want only Emails when one of the Volumes status is red. could you help me ?
Thank you
Naz
kklo says
Hello,
To be able to send a mail only when there is a YELLOW or a RED warning you should modify the script as follows:
SENDMAIL=0
SENDMAIL=$( /usr/sbin/vdf -h -P | sort -k 6 | grep -E ‘^/vmfs/volumes/’ | awk ‘{ print $2 ” ” $3 ” ” $4 ” ” $5 ” ” $6 }’ | while read output ; do
……
……
if [ $CUTPERC -ge $VMFSYELLOW ] ; then
WARNING=”yellow”
echo -ne 1
fi
……
……
done
)
echo “” >> “${LOG}”
echo “VMFS – Disk Usage Monitor by BitBat, S.L.” >> “${LOG}”
echo “” >> “${LOG}”
echo “” >> “${LOG}”
if [ $SENDMAIL -ge 1 ]; then
MAIL_BODY=`cat ${LOG}`
/usr/bin/perl smtp_send.pl -t $MAIL_TO -f $MAIL_FROM -s “VMFS Disk Usage – $MYDATE” -r $MAIL_SERVER -m “$MAIL_BODY”
fi
best regards,
kklo
kklo says
There is a malfunction in my last post.
if there is o warning yellow or red, the SENDMAIL variable is not defined and this gives a sintaxis error in the eval:
if [ $SENDMAIL -ge 1 ]; then
So the better way to avoid this error is change the test as follows:
if [ ! $SENDMAIL ]; then
# SENDMAIL is not defined, so there is no warning
else
#Put here the sending mail procedure
….
fi
regards
kklo
kcampbell says
fabu -many tx!
John says
Does this work for ESXi?
Duncan says
No more than likely it doesn’t. I would recommend using PowerCLI today. These scripts were developed before PowerCLI existed.
John says
Do you have any examples for this?
Duncan says
Something like this? (needs to be tweaked though)
http://communities.vmware.com/message/1701585
Andrey Vakhitov says
Hello, Duncan. Thank you for your script. I rewrote the script, it now runs from vma
http://vmind.ru/2012/05/04/monitoring-svobodnogo-mesta-na-xranilishhax-esxi5-cherez-vma/
Link to the script is located at the bottom of article.