USA Linux Users Group Forum Index
Log in Register FAQ Memberlist Search USA Linux Users Group Forum Index Album

Disk Usage alert script

 
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming
View previous topic :: View next topic  
Author Message
sysconfig
New Member


Joined: 12 Oct 2025
Posts: 12

PostPosted: Thu Oct 12, 2025 11:40 am    Post subject: Disk Usage alert script Reply with quote

Hi,


Intro
I am using /home/sysadmin/script/ folder for this script location.
/home/sysadm/script/tmp/ - For storing temporary file.

you need to initialize the value of some text file only first time like:

Quote:
# echo "0" > /home/sysadm/script/tmp/count


Now, set this script in your cron as mentioned below:

Quote:
*/15 * * * * /bin/sh /home/sysadm/script/alert.sh >> /dev/null 2>&1



Code:

Quote:
#!/bin/bash

stdout="/home/sysadm/script/tmp/disk.out" # for formated output
counterfile="/home/sysadm/script/tmp/count" # Couter file required to store variable value
hostnm=`hostname` # set hostname
reply="/home/sysadm/script/tmp/reply" # for formated output when problem is resolved

loop=`df -h | grep dev | wc -l` # get the no. of device

while [ 0 -lt "$loop" ] # loop for all mounted disk device
do
usage=`df -h |grep dev |sed -n "$loop s/%//p" | awk '{print $5}'` # stores the value of disk partion usage in it
devnm=`df -h |grep dev |sed -n "$loop s/%//p" | awk '{print $1}'` # stores the disk device partition name

if [ $usage -ge 90 ]; then # condition if values goes above 90% then send an alert email to System Administrator
############### Formated output redirect to file ########################
echo " " > $stdout
echo "Hi," >> $stdout
echo " " >> $stdout
echo "Presently the disk usage of the server $hostnm is using more than 90 % of its disk space. Check the below mentioned output:" >> $stdout
echo " " >> $stdout
echo " " >> $stdout
echo "`df -h`" >> $stdout
echo " " >> $stdout
echo "Please remove unwanted files/folders." >> $stdout
echo " " >> $stdout
echo "Disk device information is:" >> $stdout
echo " " >> $stdout
cat /proc/scsi/scsi >> $stdout
echo " " >> $stdout
echo "Thanks," >> $stdout
echo " " >> $stdout
#################################################################

counter=`cat $counterfile` # get the value of counter file so that it will not send repeated email

if [ $counter -eq 1 ]; then # condition to send email when value of counter file is one

mail -s "Alert: Disk Usage for `hostname` on `date` " [email protected] < $stdout # send an alert email
echo "2" >$counterfile # change the value in counter file
echo $devnm > $reply # get the device name in antoher file which is using more than 90% of disk space

fi
fi

if [ $devnm = `cat $reply` ] && [ "$usage" -lt 85 ]; then
# condition if sysadmin will work and clenup the disk space then sent email that disk space problem is resolved

############ Formated out put after the system admin will clean up space ##################
echo " " > $stdout
echo "Hi," >> $stdout
echo " " >> $stdout
echo "There was a high disk usage on the device $devnm, it seems that system admin has clean up the disk space as a precaution" >> $stdout
echo " " >> $stdout
echo " " >> $stdout
echo "`df -h`" >> $stdout
echo " " >> $stdout
echo "Thanks," >> $stdout
echo " " >> $stdout
echo "-Linux Sysadmin" >> $stdout
#################################################################

mail -s "Alert: Disk Usage is in control for `hostname` on `date`" [email protected] < $stdout
echo "nodev" > $reply # change the value in reply file if disk space is cleaned to avoid repeated email

fi

loop=`expr $loop - 1`
done

temp=`cat $counterfile`
counter=`expr $temp + 1`
echo $counter > $counterfile

###This will avoid repeated email when problem occur it will send email every 3 hour if you have set this script in cron for 15 mins

if [ $counter -eq 15 ]; then
echo "1" > $counterfile
fi

############# End Script #####################



_________________
CentOS 4.4 -- Feel like Redhat
Back to top
View user's profile Send private message Send e-mail Visit poster's website MSN Messenger
crouse
Site Admin


Joined: 17 Apr 2025
Posts: 8985
Location: Iowa

PostPosted: Thu Oct 12, 2025 7:09 pm    Post subject: Reply with quote

Interesting script.



_________________
Arch Linux 0.7.2 Gimmick Kernel 2.6.18-7-ARCH
OpenSUSE 10.1 Kernel 2.6.13-15.8-smp #1 SMP
Redhat Linux 3.0ES
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    USA Linux Users Group Forum Index » Shell Scripting and Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All content © 2025 - Usa Linux Users Group
This forum is powered by phpBB. © 2025-2006 phpBB Group
Theme created by phpBBStyles.com and modified by Crouse