Operating System - HP-UX
1753852 Members
8325 Online
108808 Solutions
New Discussion юеВ

Script for monitoring file sysem

 
jitjose
Advisor

Script for monitoring file sysem

Hi,

Need help for monitroing a file-system across a set of servers. The script will ssh from one server to the remaing servers and send e-mail alert when the threshold limit is crossed. The below script for some reasons retains the same values across all the servers.

Appreciate any help in this regard.

 

#!/bin/sh
for i in `cat hosts`
do
ssh $i 'df -k' | grep -vE '^Filesystem|dev|cdrom' | awk '{ print $5 " " $1 }' | while read output;
do
echo $output
usep=$(echo $output | awk '{ print $2}' | cut -d'%' -f1 )
partition=$(echo $output | awk '{ print $1 }' )
if [ $usep -ge 40 ]; then
echo "Running out of space \"$partition ($usep%)\" on $i as on $(date)" |mailx -s "Alert: Almost out of disk space $usep%" xyx@abd.com
fi
done
done

 

 

 

P.S. This thread has been moved from HP-UX>System Administration to  HP-UX > languages. -HP Forum Moderator

3 REPLIES 3
Dennis Handly
Acclaimed Contributor

Re: Script for monitoring file sysem

>The below script for some reasons retains the same values across all the servers.

 

Have you tried adding more echoes to see where the problem lies?

It also helps if you have a better indentation style so you can see where the blocks begin and end.

 

#!/bin/sh
for host in $(< hosts); do
   ssh $host 'df -k' | grep -vE '^Filesystem|dev|cdrom' | awk '{ print $5, $1 }' | while read output; do

 

A common problem with ssh/remsh in a while loop is that it eats up stdin.  It doesn't look like it in this case but add -n:

   ssh -n $host 'df -k'

Dennis Handly
Acclaimed Contributor

Re: Script for monitoring file sysem

>The below script for some reasons retains the same values across all the servers.

 

Have you tried adding more echoes to see where the problem lies?

It also helps if you have a better indentation style so you can see where the blocks begin and end.

 

#!/bin/sh
for host in $(< hosts); do
   ssh $host 'df -k' | grep -vE '^Filesystem|dev|cdrom' | awk '{ print $5, $1 }' | while read output; do

 

A common problem with ssh/remsh in a while loop is that it eats up stdin.  It doesn't look like it in this case but add -n:

   ssh -n $host 'df -k'

pooderbill
Valued Contributor

Re: Script for monitoring file sysem

df is particularly awkward to look at filesystem usage. bdf is much easier to determine usage although the annoying line splits can be tricky to handle. Also, be sure to use -l (for local) filesystems (df or bdf) to avoid analyzing network filesystems and also to avoid hangs when NFS has problems.

 

Now the construct:

 

 df -k| grep -vE '^Filesystem|dev|cdrom' | awk '{ print $5 " " $1 }'

 doesn't seem to produce what you need to determine percentage usage:

 

 190590
 1568
 0
 123955
 891795
 87
 78644
 121554
 60
 235625
 772735
 76
 2109390
 3901877
 64
 498864
 34152
 6
 434103
 49006
 10

Here is df -kl output:

 

 # df -kl
/home                  (/dev/vg00/lvol4       ) :   192158 total allocated Kb
                                                    190590 free allocated Kb
                                                      1568 used allocated Kb
                                                         0 % allocation used
/opt                   (/dev/vg00/lvol5       ) :  1015750 total allocated Kb
                                                    123955 free allocated Kb
                                                    891795 used allocated Kb
                                                        87 % allocation used
/tmp                   (/dev/vg00/lvol6       ) :   200198 total allocated Kb
                                                     78644 free allocated Kb
                                                    121554 used allocated Kb
                                                        60 % allocation used
/usr                   (/dev/vg00/lvol7       ) :  1008360 total allocated Kb
                                                    235625 free allocated Kb
                                                    772735 used allocated Kb
                                                        76 % allocation used
/var                   (/dev/vg00/lvol8       ) :  6011267 total allocated Kb
                                                   2109390 free allocated Kb
                                                   3901877 used allocated Kb
                                                        64 % allocation used
/stand                 (/dev/vg00/lvol1       ) :   533016 total allocated Kb
                                                    498864 free allocated Kb
                                                     34152 used allocated Kb
                                                         6 % allocation used
/                      (/dev/vg00/lvol3       ) :   483109 total allocated Kb
                                                    434103 free allocated Kb
                                                     49006 used allocated Kb
                                                        10 % allocation used

 But for bdf, the output is easier to parse:

 

# bdf -l
Filesystem          kbytes    used   avail %used Mounted on
/dev/vg00/lvol3     512000   49006  434103   10% /
/dev/vg00/lvol1     592240   34152  498864    6% /stand
/dev/vg00/lvol8    6144000 3901877 2109390   65% /var
/dev/vg00/lvol7    1024000  772735  235625   77% /usr
/dev/vg00/lvol6     204800  121554   78644   61% /tmp
/dev/vg00/really_long_name
                    102400    1133   94945    1% /mnt1
/dev/vg00/lvol5 1024000 891795 123955 88% /opt /dev/vg00/lvol4 204800 1568 190590 1% /home

To turn the bdf output into 1 liners, use something like this:

 

 bdf -l 2>/dev/null | while read FS TOT USED AVAIL PERCENT MNT
 do
    if [[ $FS != "Filesystem" ]]                 # skip header
    then
       if [[ "$TOT" = "" ]]                      # check for split line
       then
          read TOT USED AVAIL PERCENT MNT # pickup second line
       fi
       echo  $FS $TOT $USED $AVAIL $PERCENT $MNT # 1 line result
    fi
 done

/dev/vg00/lvol3 512000 49008 434101 10% / /dev/vg00/lvol1 592240 34152 498864 6% /stand /dev/vg00/lvol8 6144000 3901877 2109390 65% /var /dev/vg00/lvol7 1024000 772735 235625 77% /usr /dev/vg00/lvol6 204800 121554 78644 61% /tmp /dev/vg00/lvol5 1024000 891795 123955 88% /opt /dev/vg00/lvol4 204800 1568 190590 1% /home /dev/vg00/really_long_name 102400 1133 94945 1% /mnt1

 

Now you can easily find the percentage by filtering with: awk '{print $5}' | tr -d %