LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2013, 03:42 PM   #1
cobra4444
LQ Newbie
 
Registered: May 2013
Posts: 3

Rep: Reputation: Disabled
Issue writing a shell script using df


Hey i'm new to writing shell scripts and I'm trying to write a script that checks how full my disks are and notifies me when they are above 90% full. The problem i'm having is that one of the Filesystem is long enough that it is moves into the next colum of output from df there by forcing the rest of the information to the next line and throughing off what information I'm pulling out of it. I was wondering if there was any way around this problem.

Below is the script I'm using:

#!/bin/sh
df -H | grep -vE '^Filesystem|tmpfs|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 90 ]; then
echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)" |
mail -s "Alert: Almost out of disk space $usep%" myemailaddress@somewhere.com
fi
done


The output for df looks something like this
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 20G 3.9G 15G 21% /
/dev/mapper/vg_data-lv_home
1.4T 1.3T 74G 95% /home

The last two lines on the output of df are the lines that are giving me trouble.
 
Old 05-13-2013, 04:42 PM   #2
dayid
Member
 
Registered: Apr 2012
Location: Austin, TX
Posts: 44

Rep: Reputation: Disabled
For clarity:
Code:
df |  # asks df for information, -h/-H is unecessary since we only are concerned with percentages anyways.
awk '{ sub (/%/, "", $5 ); # removes the % from the output so awk can compare the integer
if ( $1 ~ /^\// ) { # only is concerned with output from df beginning with /, ignores udev/tmpfs/none
    if ( $5 > 90 ) { # compares the % output from df to whatever % we'd like to be notified at
        print $6 " on " $1 " is nearly full at " $5 "% usage." # prints lines that match with some other useful info
        }
    }
}'
For simplicity:
Code:
df | awk '{ sub (/%/, "", $5 ); if ( $1 ~ /^\// ) { if ( $5 > 90 ) { print $6 " on " $1 " is nearly full at " $5 "% usage." }}}'
Where 90 is the % you want a warning at. e.g.,
Code:
dayid@clugg:/proc$ df -h | grep ^/
/dev/sda1             9.9G  4.2G  5.3G  45% /
/dev/sda6              19G  182M   18G   1% /tmp
/dev/sdd2             398G  216G  182G  55% /web
/dev/sda7              29G  1.5G   26G   6% /var
/dev/sda2              92G   78G   15G  85% /home
Using the above script, only with '80' instead of '90', will return:
Code:
/home on /dev/sda2 is oversize at 85% usage.
Does this solve the wrap issue? If not, please respond with what your $TERM is, along with which distribution you're using that is wrapping like this.
 
Old 05-13-2013, 08:47 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
If you use the "-P" (--portability) option, df will not insert the extra line breaks:
Code:
# df /mnt/tmp
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/bananamangosweetpealemontree
                      72248132  13498548  55079596  20% /mnt/tmp
# df -P /mnt/tmp
Filesystem         1024-blocks      Used Available Capacity Mounted on
/dev/mapper/bananamangosweetpealemontree  72248132  13498548  55079596      20% /mnt/tmp
#
 
2 members found this post helpful.
Old 05-14-2013, 07:15 AM   #4
cobra4444
LQ Newbie
 
Registered: May 2013
Posts: 3

Original Poster
Rep: Reputation: Disabled
Solved

Thanks for the help. Just adding the -p solved the problem I was having.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with writing a c shell script? Ajit Gunge Linux - Newbie 5 06-21-2012 12:58 AM
Need help in writing a shell script khandu Programming 7 12-02-2011 03:29 AM
writing a shell script tchoua Linux - General 8 02-24-2005 06:18 PM
shell script writing ust Linux - General 6 01-24-2005 01:06 PM
help writing shell script np complete Linux - Newbie 6 08-30-2004 09:43 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration