08-25-2008
Filesystem Monitoring script problems
Hi there all,
Code :
#!/usr/bin/ksh
Set -x
MIN_MB_FREE="100MB" # Min. MB of Free FS Space
MAX_PERCENT="85%" # Max. FS percentage value
FSTRIGGER="1000MB" # Trigger to switch from % Used to MB Free
WORKFILE="/tmp/df.work" # Holds filesystem data
>$WORKFILE # Initialize to empty
OUTFILE="/tmp/df.outfile" # Output display file
>$OUTFILE # Initialize to empty
EXCEPTIONS="/usr/local/bin/exceptions" # Override data file
DATA_EXCEPTIONS="/tmp/dfdata.out" # Exceptions file w/o # rows
EXCEPT_FILE="N" # Assume no $EXCEPTIONS FILE
THISHOST=`hostname` # Hostname of this machine
###### FORMAT VARIABLES HERE ######
# Both of these variables need to be multiplied by 1024 blocks
(( MIN_MB_FREE = $(echo $MIN_MB_FREE | sed s/MB//g) * 1024 ))
(( FSTRIGGER = $(echo $FSTRIGGER | sed s/MB//g) * 1024 ))
###### SHELL SPECIFIC VARIABLES ######
# Set the correct usage for the echo command for found $SHELL
case $SHELL in
*/bin/bash) ECHO="echo -e"
;;
*/bin/ksh) ECHO="echo"
;;
*/bin/sh) ECHO="echo -e"
;;
*) ECHO="echo"
;;
esac
##########################################################
############### DEFINE FUNCTIONS HERE ####################
##########################################################
function get_OS_info
{
# For a few commands it is necessary to know the OS and its level
# to execute the proper command syntax. This will always return
# the OS in UPPERCASE
typeset -u OS # Use the UPPERCASE values for the OS variable
OS=`uname` # Grab the Operating system, i.e. AIX, HP-UX
print $OS # Send back the UPPERCASE value
}
####################################
function check_exceptions
{
# set -x
while read FSNAME FSLIMIT
do
IN_FILE="N"
# Do an NFS sanity check and get rid of any .:..
# If this is found it is actually an error entry
# but we will try to resolve it. It will only
# work if it is an NFS cross mount to the same
# mount point on both machines.
$ECHO $FSNAME | grep ':' >/dev/null \
&& FSNAME=$($ECHO $FSNAME | cut -d ':' -f2)
# Check for empty and null variable
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
then
if [[ $FSNAME = $FSMOUNT ]] # Found it!
then
# Check for "MB" Characters...Set IN_FILE=MB
$ECHO $FSLIMIT | grep MB >/dev/null && IN_FILE="MB" \
&& (( FSLIMIT = $($ECHO $FSLIMIT \
| sed s/MB//g) * 1024 ))
# check for '%' Character...Set IN_FILE=PC, for %
$ECHO $FSLIMIT | grep '%' >/dev/null && IN_FILE="PC" \
&& FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g)
case $IN_FILE in
MB) # Use MB of Free Space Method
# Up-case the characters, if they exist
FSLIMIT=$($ECHO $FSLIMIT | tr '[a-z]' '[A-Z]')
# Get rid of the 'MB' if it exists
FSLIMIT=$($ECHO $FSLIMIT | sed s/MB//g)
# Test for blank and null values
if [[ ! -z $FSLIMIT && $FSLIMIT != '' ]]
then
# Test for a valid filesystem 'MB' limit
if (( FSLIMIT >= 0 && FSLIMIT < FSSIZE ))
then
if (( FSMB_FREE < FSLIMIT ))
then
return 1 # Found out of limit
# using MB Free method
else
return 3 # Found OK
fi
else
$ECHO "\nERROR: Invalid filesystem MAX for\
$FSMOUNT - $FSLIMIT"
$ECHO " Exceptions file value must be less\
than or"
$ECHO " equal to the size of the filesystem\
measured"
$ECHO " in 1024 bytes\n"
fi
else
$ECHO "\nERROR: Null value specified in exceptions\
file"
$ECHO " for the $FSMOUNT mount point.\n"
fi
;;
PC) # Use Filesystem %Used Method
# Strip out the % sign if it exists
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
# Test for blank and null values
if [[ ! -z "$FSLIMIT" && "$FSLIMIT" != '' ]]
then
# Test for a valid percentage, i.e. 0-100
if (( FSLIMIT >= 0 && FSLIMIT <= 100 ))
then
if (( $PC_USED > $FSLIMIT ))
then
return 2 # Found exceeded by % Used method
else
return 3 # Found OK
fi
else
$ECHO "\nERROR: Invalid percentage for $FSMOUNT -\
$FSLIMIT"
$ECHO " Exceptions file values must be"
$ECHO " between 0 and 100%\n"
fi
else
$ECHO "\nERROR: Null value specified in exceptions\
file"
$ECHO " for the $FSMOUNT mount point.\n"
fi
;;
N) # Method Not Specified - Use Script Defaults
if (( FSSIZE >= FSTRIGGER ))
then # This is a "large" filesystem
if (( FSMB_FREE < MIN_MB_FREE ))
then
return 1 # Found out of limit
# using MB Free method
else
return 3 # Found OK
fi
else # This is a standard filesystem
PC_USED=$($ECHO $PC_USED | sed s/\%//g) # Remove %
FSLIMIT=$($ECHO $FSLIMIT | sed s/\%//g) # Remove %
if (( PC_USED > FSLIMIT ))
then
return 2 # Found exceeded by % Used method
else
return 3 # Found OK
fi
fi
;;
esac
fi
fi
done < $DATA_EXCEPTIONS # Feed the loop from the bottom!!!
return 4 # Not found in $EXCEPTIONS file
}
####################################
function display_output
{
if [[ -s $OUTFILE ]]
then
$ECHO "\nFull Filesystem(s) on $THISHOST\n"
cat $OUTFILE
print
fi
}
####################################
function load_EXCEPTIONS_data
{
# Ignore any line that begins with a pound sign, #
# and omit all blank lines
cat $EXCEPTIONS | grep -v "^#" | sed /^$/d > $DATA_EXCEPTIONS
}
####################################
function load_AIX_FS_data
{
df -k | tail +2 | egrep -v "/dev/cd[0-9]|/proc" \
| awk '{print $1, $2, $3, $4, $7}' > $WORKFILE
}
####################################
function load_HP_UX_FS_data
{
bdf | tail +2 | egrep -v "/cdrom" \
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
}
####################################
function load_LINUX_FS_data
{
df -k | tail -n +2 | egrep -v "/cdrom"\
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
}
####################################
function load_OpenBSD_FS_data
{
df -k | tail +2 | egrep -v "/mnt/cdrom"\
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
}
####################################
function load_Solaris_FS_data
{
df -k | tail +2 | egrep -v "/dev/fd|/etc/mnttab|/proc"\
| awk '{print $1, $2, $4, $5, $6}' > $WORKFILE
}
####################################
######### START OF MAIN ############
####################################
# Query the operating system to find the Unix flavor, then
# load the correct filesystem data for the resident OS
case $(get_OS_info) in
AIX) # Load filesystem data for AIX
load_AIX_FS_data
;;
HP-UX) # Load filesystem data for HP-UX
load_HP_UX_FS_data
;;
LINUX) # Load filesystem data for Linux
load_LINUX_FS_data
;;
OPENBSD) # Load filesystem data for OpenBSD
load_OpenBSD_FS_data
;;
SUNOS) # Load filesystem data for Solaris
load_Solaris_FS_data
;;
*) # Unsupported in script
$ECHO "\nUnsupported Operating System for this Script...EXITING\n"
exit 1
esac
# Do we have a nonzero size $EXCEPTIONS file?
if [[ -s $EXCEPTIONS ]]
then # Found a nonempty $EXCEPTIONS file
load_EXCEPTIONS_data
EXCEP_FILE="Y"
fi
while read FSDEVICE FSSIZE FSMB_FREE PC_USED FSMOUNT
do
if [[ $EXCEP_FILE = "Y" ]]
then
check_exceptions
CE_RC="$?" # Check Exceptions Return Code (CE_RC)
case $CE_RC in
1) # Found exceeded in exceptions file by MB Method
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
$ECHO "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" >> $OUTFILE
;;
2) # Found exceeded in exceptions file by %Used method
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
>> $OUTFILE
;;
3) # Found OK in exceptions file
: # NO-OP Do Nothing. A ':' is a no-op!
;;
4) # Not found in exceptions file - Use Default Triggers
if (( FSSIZE >= FSTRIGGER ))
then # This is a "large" filesystem
FSMB_FREE=$($ECHO $FSMB_FREE | sed s/MB//g) # Remove the "MB"
if (( FSMB_FREE < MIN_MB_FREE ))
then
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
$ECHO "$FSDEVICE mounted on $FSMOUNT has {FS_FREE_OUT}MB Free" >> $OUTFILE
fi
else # This is a standard filesystem
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
MAX_PERCENT=$($ECHO $MAX_PERCENT | sed s/\%//g)
if (( PC_USED > MAX_PERCENT ))
then
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
>> $OUTFILE
fi
fi
;;
esac
else # NO $EXCEPTIONS FILE USE DEFAULT TRIGGER VALUES
if (( FSSIZE >= FSTRIGGER ))
then # This is a "large" filesystem - Use MB Free Method
FSMB_FREE=$($ECHO $FSMB_FREE | sed s/MB//g) # Remove the 'MB'
if (( FSMB_FREE < MIN_MB_FREE ))
then
(( FS_FREE_OUT = FSMB_FREE / 1000 ))
$ECHO "$FSDEVICE mounted on $FSMOUNT has ${FS_FREE_OUT}MB Free" \
>> $OUTFILE
fi
else # This is a standard filesystem - Use % Used Method
PC_USED=$($ECHO $PC_USED | sed s/\%//g)
MAX_PERCENT=$($ECHO $MAX_PERCENT | sed s/\%//g)
if (( PC_USED > MAX_PERCENT ))
then
$ECHO "$FSDEVICE mount on $FSMOUNT is ${PC_USED}%" \
>> $OUTFILE
fi
fi
fi
done < $WORKFILE # Feed the while loop from the bottom!!!!!
display_output
Last edited by draco; 08-26-2008 at 05:57 AM ..
10 More Discussions You Might Find Interesting
1. UNIX for Advanced & Expert Users
Hi,
I've a Unixware 7.1.4, the machine has 3 disks, the second disk is not mounted, he said the message "WARNING: msgcnt 2 vxfs: mesg 021: vx_fs_init - /dev/dsk/c0b0t1d0s1 file system validation failure".
I use the fsck command to check and repair, during the pocess apears messages to... (3 Replies)
Discussion started by: By_Jam
3 Replies
2. AIX
I'd like to create a cron script that checks filesystems. For example if it reaches 95% USED, I'd like it to send me an email. Can this be possible for up to say 4 filesystems using the df -k command? Any samples to get me started would be much appreciated. (7 Replies)
Discussion started by: NycUnxer
7 Replies
3. Shell Programming and Scripting
Hi all,
I got an error when running this script (from BigAdmin community)
the error is
test: argument expected
my server version is
SunOS XXX 5.8 Generic_117350-46 sun4u sparc SUNW,Sun-Fire-V890
any idea on it?
#! /usr/bin/ksh
###
### This script can be run from cron to... (9 Replies)
Discussion started by: SmartAntz
9 Replies
4. Shell Programming and Scripting
Gentlemen
I want to write a small script and run as cronjob sothat the script runs every four hours
The script should be capable of finding any hardware issues,panics,coredumps
if anyissues found it should sent mail.If no issues it shouldnt sent mail
Deeply appreciated if any help... (2 Replies)
Discussion started by: saidiya
2 Replies
5. AIX
As I am new to the Unix field, I would like to get the clarification regarding the Filesystem.
The scenario is.. The filesystem (/drbackup) is getting monitored and if it exceeds the threshold, we will receive an alert from it. The issue is that we receive an alert with the description of... (2 Replies)
Discussion started by: A.Srenivasan
2 Replies
6. UNIX for Advanced & Expert Users
Hi
I do a very simple monitoring of our OpenLDAP (runs in cronjob and generate alerts if unsuccessfull)
$ ldapsearch -h hostname.domain -D "cn=monitor_user,ou=People,dc=organisation" -w "password" -b "dc=organisation" -x "(&(cn=monitor_user)(ou=People))" dn | grep -v version
dn:... (4 Replies)
Discussion started by: slashdotweenie
4 Replies
7. UNIX for Dummies Questions & Answers
Hi,
I am new to shell scripting in unix HP-UX.
I need to determine how much a perticular file system is full and if it goes over 80% it should notify through mail about which are the files which are greater than 1 GB size.
I used df -k command it is showing the alloocated KB and the %used... (14 Replies)
Discussion started by: Soma Das
14 Replies
8. Filesystems, Disks and Memory
Hi all! I have a task that has been giving me problems.I have my sister-in-law's external hard drive that has been damaged and the filesystem is not mountable.
I am on a Mac running OSX 10.6.8
Using ddrescue, I recovered the contents and transferred to my external drive:
sudo ddrescue... (0 Replies)
Discussion started by: Imhotep1963
0 Replies
9. UNIX for Dummies Questions & Answers
I am on SCO UNIX, I need to write a script to monitor root file system. For some reason it's not working for me.
#!/bin/sh
df -B / | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
if ; then
echo "Running out of space \ HOSTNAME"
# mail -s "Alert: Almost out of... (8 Replies)
Discussion started by: samnyc
8 Replies
10. HP-UX
I have noticed that after I clear up the /var filesystem, there are always problems.
Does deleting the OLDsyslog.log file cause problems like missing directories and problems accessing crontabs?
There were two situations where I faced problems.
1) Deleted OLDsyslog.log (Of course I created... (5 Replies)
Discussion started by: anaigini45
5 Replies