1830233 Members
1824 Online
109999 Solutions
New Discussion

FileSystemAlert Script

 
SOLVED
Go to solution
Kyle D. Harris
Regular Advisor

FileSystemAlert Script

Gents- The following script i have below for some reason isn't running. I use 60 % in my script just for testing purposes to see if it works since I currently have no file system even 90 % full. Can somebody please help me to see why this isn't running? I have it set in the crontab to run hourly. Thanks

Kyle


------------------------------------------
#!/usr/bin/sh
#Script Name: FileSystemAlert
#Author: Cpl Harris
#Date: July 10, 2003
#Purpose: To send out an email alerting admin when a filesystem is over 97 %


mark=1
while [ $mark = 1 ]
do
df | grep -v Filesystem |
while read line
do
MTP=$(echo "$line" | awk '{print $6}')
USE=$(echo "$line" | awk '{print $5}' | sed 's/.$//g')
if [ "$USE" > "60" ]
then
echo "$MTP $USE " | mail -s "FS alert" harriskd@mcsc.usmc.mil
sleep 100 #or whatever time
else
sleep 100 #or whatever time
fi
done
done
10 REPLIES 10
Claudio Cilloni
Honored Contributor

Re: FileSystemAlert Script

what kind of error do you get from this script?
I think that /usr/bin/sh (first line) isn't the correct path for the shell interpreter. In my system (Redhat 73) I must use /bin/sh.
I made this correction and the script seems to work (I executed it manually, not by cron).

You can check the exact location of sh so:

$ which sh

hth
Claudio
Balaji N
Honored Contributor
Solution

Re: FileSystemAlert Script

#!/bin/bash
mark=1
while [ $mark = 1 ]
do
df | grep -v Filesystem | while read line
do
MTP=$(echo "$line" | awk '{print $6}')
USE=$(echo "$line" | awk '{print $5}' | sed 's/.$//g')
if [ "$USE" -gt "30" ]
then
echo "$MTP $USE "
#echo "$MTP $USE " | mail -s "FS alert" harriskd@mcsc.usmc.mil
#sleep 100 #or whatever time
#else
#sleep 100 #or whatever time
fi
done
sleep 100
done


this script works.

1. use -gt for numeric comparisons. > for string comparisons.

2. dont put the sleep in if /else loops. for every line of ur df outptut the script will sleep. instead have it in the outer loop.

cheers
-balaji
Its Always Important To Know, What People Think Of You. Then, Of Course, You Surprise Them By Giving More.
Claudio Cilloni
Honored Contributor

Re: FileSystemAlert Script

mmmmh...

try this:
----------------------------------------------
#! /bin/sh

df | grep -v Filesystem |
while read line
do
MTP=$(echo "$line" | awk '{print $6}')
USE=$(echo "$line" | awk '{print $5}' | sed 's/.$//g')
if [ "$USE" -gt "60" ]
then
echo "$MTP $USE "
fi
done

------------------------------------------
If you wanna run it hourly in cron, you shouldn't use the external endless while loop, because that loop prevents the script to go to an end, so cron will start a new endless script every hour.

the "$USE" > "60" seems don't work, use -gt.

remember to complete the 'echo' line with the 'mail' command piped (I removed it just to test the script without sending mail).

Ciao
Claudio

John Meissner
Esteemed Contributor

Re: FileSystemAlert Script

In your previous post I went through and posted that i found the problem in my script... I then at the bottom posted the corrections to my script


#!/usr/bin/ksh
mark=1
while [ $mark = 1 ]
do
df | grep -v Filesystem |
while read line
do
MTP=$(echo "$line" | awk '{print $6}')
USE=$(echo "$line" | awk '{print $5}' | sed 's/.$//g')
if [ "$USE" > "60" ]
then
echo "$MTP $USE " | mailx -s "FS alert" sendto@work.com
sleep 100 #or whatever time
else
sleep 100 #or whatever time
fi
done
done
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: FileSystemAlert Script

sorry... didn't read your entire post before i posted my last post...
All paths lead to destiny
John Meissner
Esteemed Contributor

Re: FileSystemAlert Script

ok... ugh... sorry for all these posts... I'm extreemly upset today becuase I'm buying a new car and the dealership is giving me the run around.... AHHHHHHHHHH

sorry...


anyway ... i would use the BASH shell not sh. That's what I originally scripted this in. (I put ksh in one of the last posts becuase I'm losing it today... i need a vacation) try not putting any shell interpreter in the first line or if you do try using

#!/bin/bash

Also ... do you get any error messeges in your code.. also putting this in cron might not be the best way to launch it.... i would put it in your startup scripts when the system starts. Then you don't need to worry about cron

once again.. sorry for all the posts... it's just been a really rough couple of days for me
All paths lead to destiny
Kyle D. Harris
Regular Advisor

Re: FileSystemAlert Script

John, No problem about the messages. Good luck with the car.

To all, i'll try suggestions from your posts in a minute. I wasn't getting any errors, it just wasn't running. I believe one of you said the way i had the script it would create an endless script every hour, which i believe it was when i looked at my processes! I'll respond back in a few. Thanks!
John Meissner
Esteemed Contributor

Re: FileSystemAlert Script

Kyle - yes... this script, as is, if entered into a cron job would start an endless script every time you ran it....

to change that either 1 of 2 things can be done.

1 - enter it into your start up scripts on your box
2 - remove the folloing lines:
mark=1
while [ $mark = 1 ]
do
#and also
sleep 100 #or whatever time
else
sleep 100 #or whatever time
#and also
done #which is the last line which closes the while [$mark = 1 ] line
All paths lead to destiny
Caesar_3
Esteemed Contributor

Re: FileSystemAlert Script

Hello!

You can start the script as he if you will
start it in the boot by adding link to him
to the runlevel that you work in.

ln -s <script path> /sbin/rcX.d/S998diskcheck

Caesar
Caesar_3
Esteemed Contributor

Re: FileSystemAlert Script

Hello!

You can start the script as he if you will
start it in the boot by adding link to him
to the runlevel that you work in.

ln -s <script path>
/etc/rc.d/rcX.d/S998diskcheck

Caesar