Overview
The purpose of this script is to send notification of
current disk space utilization and also to
monitor disk usage every 12 hours and find
out disk usage difference in usage from last 12 hours in bytes for each
partition.
The script will generate disk space usage report and send
notification to the recipients.
This report will help administrators whether the report has
to be shared with appropriate application owners to clean up disk(s).
Also when the threshold limit or 90% and above is reached,
disk partition usage % is shade in “RED”,
signifying that it’s a “Critical Alert”.
Applies To
·
Tested on CentOS 7
Pre-requisites
Bash
Shell Script – Snippet
Copy and paste the script into a file, change script
permission and run it.
#!/bin/bash
#
# To debug uncomment the below line
#set -x
#
# Set Environment Variables
#
TODAY="at $(date '+%H:%M on %d-%b-%y')"
OutputFilename=$(date +"%b_%d_%Y".html)
LastReport=/tmp/LastReport.txt
NowReport=/tmp/NowReport.txt
CurDate=`date +%D\ %T\ %Z`
#
# Set Alert Type according to Percentage
#
CriticalPercentage=90
WarningPercentage=80
NormalPercentage=70
#
# Get IP Address set from hosts file
#
IPADDRESS=`head -n 1 /etc/hosts | awk
' { print $1 }'`
#
# Remove Output File, if it
already exists
#
if [ -f /tmp/${OutputFilename} ]; then
rm -f /tmp/${OutputFilename}
fi
if [ -f ${NowReport} ]; then
mv ${NowReport}
${LastReport}
cp /dev/null ${NowReport}
fi
#
# Find out Difference Previous and Current Report for
each partition
#
df -Pl | grep -vE "^Filesystem|tmpfs|cdrom"
| awk 'NR>0 && NF==6' | awk '{print $3}' > ${NowReport}
if [ -f ${LastReport} ]; then
DiffValue=(`awk '{ getline
getdifference < "/tmp/LastReport.txt"; $1 -= getdifference; print
}' /tmp/NowReport.txt`)
fi
#
# Defining HTML Table Format & Fields
#
(
echo
'<HTML><HEAD><TITLE>Disk Usage
Statistics</TITLE></HEAD>'
echo '<BODY>'
echo '<H3>Disk Usage
Report for server - '$(uname -n) for ${IPADDRESS}'</H3>'
echo '<P>Report Generated
'${TODAY}'</P>'
echo '<TABLE BORDER=3 CELLSPACING=2
CELLPADDING=0>'
echo '<TR
BGCOLOR=\"#BBFFFF\"> <TH>Filesystem</TH>
<TH>Total</TH> <TH>Disk Used</TH>
<TH>Available</TH> <TH>Percentage Info</TH> <TH>Mounted
On</TH> <TH>Critical Alert</TH> <TH>Report
Date</TH> <TH>12 Hrs Difference </TH><//TR>'
#
# Extract Disk Usage Information
# Suppress Listing of “FileSystem, tmpfs and cdrom” Information
#
ArryCount=0
df -Pl | grep -vE "^Filesystem|tmpfs|cdrom" | awk 'NR>0
&& NF==6'|sort|while read FileSystem Size DiskUsed DiskFree
DiskPercentUsed MountPoint
do
PERCENT=${DiskPercentUsed%%%}
#
# Calculate the Difference between previous run and
current run
#
TDiffValue=(`awk '{ getline
getdifference < "/tmp/LastReport.txt"; $1 -= getdifference; print
}' /tmp/NowReport.txt`)
#
# Verify if disk usage is greater equal to than set
threshold limit - 90%
#
if [[ ${PERCENT} -ge
${CriticalPercentage} ]];
then
COLOR=red
CRITICALALERT="Yes, Notify"
elif [ ${PERCENT} -ge
${WarningPercentage} ] && [ ${PERCENT} -le 80 ];
then
COLOR=orange
CRITICALALERT=No
else
COLOR=green
CRITICALALERT=NA
fi
echo
'<TR><TD>'$FileSystem'</TD><TD
ALIGN=RIGHT>'$Size'</TD>'
echo '<TD
ALIGN=RIGHT>'$DiskUsed'</TD><TD ALIGN=RIGHT>'$DiskFree'</TD>'
echo '<TD><TABLE
BORDER=0 CELLSPACING=3 CELLPADDING=0>'
echo '<TR><TD
WIDTH='$((2 * $PERCENT))' BGCOLOR="'$COLOR'"></TD>'
echo '<TD WIDTH='$((2 *
(100 - $PERCENT)))' BGCOLOR="gray"></TD>'
echo '<TD><FONT FONT-WEIGHT="bold" SIZE=-1 COLOR="'$COLOR'">'$DiskPercentUsed'</FONT></TD>'
echo
'<TR></TABLE><TD>'$MountPoint'</TD>'
echo '<TD><FONT
font-weight="bold">'$CRITICALALERT'</TD></FONT>'
echo '<TD><FONT
font-weight="bold">'`date`'</TD></FONT>'
echo '<TD><FONT
font-weight="bold">'${TDiffValue[ArryCount]} \(in
bytes\)'</TD></FONT></TR>'
echo $DiskUsed >>
`hostname`.usage.txt
ArryCount=$ArryCount+1
done
echo '</TABLE>'
echo '</P><BR>'
echo '<TABLE BORDER=1
CELLSPACING=3 CELLPADDING=0>'
echo '<TR><TH FONT
font-weight="bold">Legend Information</TH></TR>'
echo '<TR><TD FONT
color="white" BGCOLOR="RED">Critical
Alert</TD></TR>'
echo '<TR><TD FONT
color="white" BGCOLOR="ORANGE">Warning
Alert</TD></TR>'
echo '<TR><TD FONT
color="white" BGCOLOR="GREEN">No Action
Alert</TD></TR>'
echo '</TABLE>'
echo
'<BODY></HTML>'
echo '<P><FONT
font-weight="bold">Report Generated by IT Team</P>'
) | tee
`hostname`_${0##*/}.html
#
# Sending E-Mail Notification
#
(
echo To: ToRecipient.Account@domainname.com
echo From:
FromRecipient.Account@domainname.com
echo "Content-Type:
text/html; "
echo Subject: Disk Usage Report
for server `hostname` 'for' $IPADDRESS
echo
cat `hostname`_${0##*/}.html
) | sendmail -t
echo -e "Report Generation is Completed... \n\a"
Run Disk Usage Report – Shell Script
To run the script, ensure you change the file permission to
“executable” and then run it;
chmod +x ./DiskUsageReport.sh
./DiskUsageReport.sh
Disk Usage E-Mail Report
A sample email report is shown below;
No comments:
Post a Comment