Scripting help...monitoring disk space

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • asmpic
    New Member
    • Mar 2008
    • 1

    Scripting help...monitoring disk space

    Hi,
    I stumbled upon a script on the internet that monitors disk space and if it reaches a certain percentage being full it send an email. However, the problem I encountered was that one of the file system names are longer than usual so it expends over to the other columns and causes the other data to be put onto a second line. So the script no longer works when this is the case. does anyone know how to fix the script to take this into account?

    This is what I get when I run df-H. The first line goes onto a second line because of the file name so the script return incorrect information when I ask to print columns 5 and 1.


    Code:
    Filesystem             Size   Used  Avail Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
                            77G   8.2G    65G  12% /
    /dev/sda1              104M    14M    86M  14% /boot
    none                   525M      0   525M   0% /dev/shm

    This is the script I'm using.


    Code:
    #!/bin/sh
    df -H | grep -vE '^Filesystem|none|cdrom' | awk '{ print $5 " " $1 }' | while read output;
    do
       #echo $output
       usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
       partition=$(echo $output | awk '{ print $2 }' )
       if [ $usep -ge 10 ]; then
       echo "Running out of space \"$partition ($usep%)\" on $(hostname)" | mail -s "Alert: Almost out of disk space $usep" some@someone.net
    fi
    done
  • kvkrishna
    New Member
    • Mar 2008
    • 1

    #2
    Hi,
    I use the below to monitor the disk space....
    Code:
    df -kt | grep "%" | grep "^/dev" >/tmp/file_sys.txt
    chmod 755 /tmp/file_sys.txt
    Flag=0
    while read LINE
    do
            pcnt=`echo $LINE | cut -d"%" -f1 | awk '{print $NF}'`
            f_sys=`echo $LINE | cut -d"%" -f2 | awk '{print $NF}'`
            if [ $Flag -eq 0 ]
            then
            if [ $pcnt -ge 80 ] && [ $pcnt -le 100 ]
            then
                    echo $dashes >/tmp/`hostname`_alert.txt
                    echo "The below file systems need house keeping.....!" >>/tmp/`hostname`_alert.txt
                    echo $dashes >>/tmp/`hostname`_alert.txt
                    #echo "File System $f_sys is $pcnt%"
                    Flag=1
            else
                    Flag=0
            fi
            fi
            if [[ $Flag -eq 1 ]]
            then
                    if [ $pcnt -ge 80 ] && [ $pcnt -le 100 ]
                    then
                            echo "File System $f_sys is $pcnt%" >>/tmp/`hostname`_alert.txt
                    fi
            fi
    done </tmp/file_sys.txt
    if [ -f /tmp/`hostname`_alert.txt ]
    then
            if [ -s /tmp/`hostname`_alert.txt ]
            then
                    #cat /tmp/`hostname`_alert.txt
                    mail_subject="ITRM:Alert from vista70"
                    mail_file=/tmp/`hostname`_alert.txt
                    mail_recp="abc@xyz.com abc@xyz.com abc@xyz.com" 
                    mailx -s "$mail_subject" $mail_recp < $mail_file
            fi
    fi
    Give me output as:
    +-----------------------------------------------------------------------------+
    The below file systems need house keeping.....!
    +-----------------------------------------------------------------------------+
    File System /vandata02 is 81%
    File System /datafi01 is 84%
    File System /data4 is 93%
    File System /vanfi is 82%
    File System /vandev is 91%
    File System /VANPOC is 91%
    File System /datauat01 is 88%
    File System /datauat02 is 87%
    File System /datahotfix01 is 96%
    File System /datavanpc01 is 82%


    Originally posted by asmpic
    Hi,
    I stumbled upon a script on the internet that monitors disk space and if it reaches a certain percentage being full it send an email. However, the problem I encountered was that one of the file system names are longer than usual so it expends over to the other columns and causes the other data to be put onto a second line. So the script no longer works when this is the case. does anyone know how to fix the script to take this into account?

    This is what I get when I run df-H. The first line goes onto a second line because of the file name so the script return incorrect information when I ask to print columns 5 and 1.


    Code:
    Filesystem             Size   Used  Avail Use% Mounted on
    /dev/mapper/VolGroup00-LogVol00
                            77G   8.2G    65G  12% /
    /dev/sda1              104M    14M    86M  14% /boot
    none                   525M      0   525M   0% /dev/shm

    This is the script I'm using.


    Code:
    #!/bin/sh
    df -H | grep -vE '^Filesystem|none|cdrom' | awk '{ print $5 " " $1 }' | while read output;
    do
       #echo $output
       usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
       partition=$(echo $output | awk '{ print $2 }' )
       if [ $usep -ge 10 ]; then
       echo "Running out of space \"$partition ($usep%)\" on $(hostname)" | mail -s "Alert: Almost out of disk space $usep" some@someone.net
    fi
    done
    Last edited by numberwhun; Jun 8 '10, 07:05 PM. Reason: Please use CODE TAGS!

    Comment

    • optikknight
      New Member
      • Jun 2010
      • 1

      #3
      removing line wraps from 'df'

      I've occasionally had the same problem... (And yes, I know I'm waking a dead thread. I figured I'd share my solution anyway.)

      Here's my script for the same purpose:
      df.cron

      It assumes all lines not containing a '%' are wrapped, and pulls the next line. Example output:


      Code:
      Nearly full filesystems:
      Filesystem               Size  Used  Avail  Use%  Mounted-on
      /dev/mapper/System-home  100G  69G   26G    73%   /home
      /dev/hda6                38G   34G   2.3G   94%   /mnt/unsafe

      (The write_preformat ted call is a routine from the included lib, in the linked script.)

      The more interesting parts, for text processing:

      Code:
      df -h > $filesystems
      sed '1d; /%/! { N; s/[\n\t]/ /; }' $filesystems | grep -E '([7-9][0-9]|[0-9]{3})%' > $filesystems.2
      Last edited by optikknight; Jun 7 '10, 08:02 PM. Reason: preformatting, code blocks

      Comment

      Working...