472,121 Members | 1,523 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

Scripting help...monitoring disk space

1
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.


Expand|Select|Wrap|Line Numbers
  1. Filesystem             Size   Used  Avail Use% Mounted on
  2. /dev/mapper/VolGroup00-LogVol00
  3.                         77G   8.2G    65G  12% /
  4. /dev/sda1              104M    14M    86M  14% /boot
  5. none                   525M      0   525M   0% /dev/shm
  6.  

This is the script I'm using.


Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. df -H | grep -vE '^Filesystem|none|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  3. do
  4.    #echo $output
  5.    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  6.    partition=$(echo $output | awk '{ print $2 }' )
  7.    if [ $usep -ge 10 ]; then
  8.    echo "Running out of space \"$partition ($usep%)\" on $(hostname)" | mail -s "Alert: Almost out of disk space $usep" some@someone.net
  9. fi
  10. done
Mar 13 '08 #1
2 3419
Hi,
I use the below to monitor the disk space....
Expand|Select|Wrap|Line Numbers
  1. df -kt | grep "%" | grep "^/dev" >/tmp/file_sys.txt
  2. chmod 755 /tmp/file_sys.txt
  3. Flag=0
  4. while read LINE
  5. do
  6.         pcnt=`echo $LINE | cut -d"%" -f1 | awk '{print $NF}'`
  7.         f_sys=`echo $LINE | cut -d"%" -f2 | awk '{print $NF}'`
  8.         if [ $Flag -eq 0 ]
  9.         then
  10.         if [ $pcnt -ge 80 ] && [ $pcnt -le 100 ]
  11.         then
  12.                 echo $dashes >/tmp/`hostname`_alert.txt
  13.                 echo "The below file systems need house keeping.....!" >>/tmp/`hostname`_alert.txt
  14.                 echo $dashes >>/tmp/`hostname`_alert.txt
  15.                 #echo "File System $f_sys is $pcnt%"
  16.                 Flag=1
  17.         else
  18.                 Flag=0
  19.         fi
  20.         fi
  21.         if [[ $Flag -eq 1 ]]
  22.         then
  23.                 if [ $pcnt -ge 80 ] && [ $pcnt -le 100 ]
  24.                 then
  25.                         echo "File System $f_sys is $pcnt%" >>/tmp/`hostname`_alert.txt
  26.                 fi
  27.         fi
  28. done </tmp/file_sys.txt
  29. if [ -f /tmp/`hostname`_alert.txt ]
  30. then
  31.         if [ -s /tmp/`hostname`_alert.txt ]
  32.         then
  33.                 #cat /tmp/`hostname`_alert.txt
  34.                 mail_subject="ITRM:Alert from vista70"
  35.                 mail_file=/tmp/`hostname`_alert.txt
  36.                 mail_recp="abc@xyz.com abc@xyz.com abc@xyz.com" 
  37.                 mailx -s "$mail_subject" $mail_recp < $mail_file
  38.         fi
  39. fi
  40.  
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%


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.


Expand|Select|Wrap|Line Numbers
  1. Filesystem             Size   Used  Avail Use% Mounted on
  2. /dev/mapper/VolGroup00-LogVol00
  3.                         77G   8.2G    65G  12% /
  4. /dev/sda1              104M    14M    86M  14% /boot
  5. none                   525M      0   525M   0% /dev/shm
  6.  

This is the script I'm using.


Expand|Select|Wrap|Line Numbers
  1. #!/bin/sh
  2. df -H | grep -vE '^Filesystem|none|cdrom' | awk '{ print $5 " " $1 }' | while read output;
  3. do
  4.    #echo $output
  5.    usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1  )
  6.    partition=$(echo $output | awk '{ print $2 }' )
  7.    if [ $usep -ge 10 ]; then
  8.    echo "Running out of space \"$partition ($usep%)\" on $(hostname)" | mail -s "Alert: Almost out of disk space $usep" some@someone.net
  9. fi
  10. done
Mar 14 '08 #2
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:


Expand|Select|Wrap|Line Numbers
  1. Nearly full filesystems:
  2. Filesystem               Size  Used  Avail  Use%  Mounted-on
  3. /dev/mapper/System-home  100G  69G   26G    73%   /home
  4. /dev/hda6                38G   34G   2.3G   94%   /mnt/unsafe

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

The more interesting parts, for text processing:

Expand|Select|Wrap|Line Numbers
  1. df -h > $filesystems
  2. sed '1d; /%/! { N; s/[\n\t]/ /; }' $filesystems | grep -E '([7-9][0-9]|[0-9]{3})%' > $filesystems.2
Jun 7 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jay Blanchard | last post: by
1 post views Thread by Peter Ang | last post: by
1 post views Thread by Oddsavant | last post: by
1 post views Thread by andrewcw | last post: by
1 post views Thread by laurie | last post: by
4 posts views Thread by jonathan.sabo | last post: by
3 posts views Thread by rohit | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.