Script to monitor diskspace of /home/ not to exceed 80%


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to monitor diskspace of /home/ not to exceed 80%
# 1  
Old 03-14-2016
Hammer & Screwdriver Script to monitor diskspace of /home/ not to exceed 80%

I want to monitor the diskspace of the /home/ directory NOT to exceed 80%.
I want a script_file.sh for this.

I am trying "df -h" command and I am stuck unable to extract the percentage.

1. HOW TO extract the percentage (I believe 5th column)?

2. IF we were able to extract the column, will it return just the value or the value with "%" sign?

3. I want this value to be tested in a IF condition to check its >80%

Please help me quick, its urgent!!
Thanks Smilie
# 2  
Old 03-14-2016
You better use df -kP or df -k in a script.
The % in bash/ksh/ash/psh can be chopped off.
Code:
percent=`df -kP /home/. | awk 'NR>1 {print $5}'`
percent=${percent%*%}

or
Code:
percent=`df -kP /home/. | (read _; read _ _ _ _ percent _; echo  ${percent%*%})`

This User Gave Thanks to MadeInGermany For This Post:
# 3  
Old 03-14-2016
thanks, but what does NR>1 do? and what is ${percenta%*%}

does it return only one value of the entire /home/ directory ? coz I need that.

will
Code:
IF [ $( df -kP /home | awk '{print $5}' ) -gt 80]; then
                  echo "exceeds"
               else
                   echo "don't"
               FI

this work?
# 4  
Old 03-14-2016
NR > 1 skips the first (= header) line in awk. shell's parameter expansion ${percent%*%} (no "a"!) removes the trailing % sign.

It will not, due to syntactical and semantical errors:
- IF should be lower case
- the closing ] needs a leading space
- the command substitution needs double quotes
- the test ( or [ ) command can't handle multiline arguments, it needs a single item output from the command substitution.
- the -gt comparison can't handle the % sign
This User Gave Thanks to RudiC For This Post:
# 5  
Old 03-14-2016
${percent%*%} is a variable modifier. So you must store it in a variable first.
Code:
percent=`df -kP /home/. | awk 'NR>1 {print $5}'`
if [ ${percent%*%} -gt 80 ]; then
  echo "exceeds"
else
  echo "ok"
done

Or
Code:
if [ `df -kP /home/. | (read _; read _ _ _ _ percent _; echo  ${percent%*%})` -gt 80 ]
then
  echo "exceeds"
else
  echo "ok"
fi

The NR>1 or the read _ skip the first line (header).
# 6  
Old 03-14-2016
Code:
if [ $( df -kP /home | awk 'NR>1 {print $5}' ) -gt 80 ]; then
                  echo "exceeds"
               else
                   echo "don't"
               fi

this might work if we can remove the % sign..
can't we remove without storing into a variable?
IF not then as per ur first suggestion,
if [ percentage -gt 80 ]; then
echo "exceeds";
else
echo "dont "
fi

this will work anyway right?
# 7  
Old 03-14-2016
How about
Code:
df -kP /home | awk 'NR>1 {print ($5+0 > 80)?"exceeds":"doesn''t"}'


Last edited by RudiC; 03-14-2016 at 07:22 AM.. Reason: typo corrected: $5+1 --> $5+0
This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

DiskSpace Error

Hi, While am preparing the script for diskspace for different partitions different threshhold limit. am getting below error: ./test.sh: syntax error at line 5: `space=$' unexpected Script ==== #!/bin/sh df -h |grep -v '/oradata2'|grep -v '/orabkp' |grep -v '/oradata5' | awk '{ print $5... (7 Replies)
Discussion started by: Pavan83
7 Replies

2. Shell Programming and Scripting

Looking for shell script to monitor CPU utilization and send mail once exceed 75%

Dear Group, I'm look for shell script to Monitor CPU usage and send mail once it exceed 75% I'm running Suse10.4. (3 Replies)
Discussion started by: clfever
3 Replies

3. Shell Programming and Scripting

[ask]elimination with diskspace

dear all, i want elimination file with disk space for example: $ df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup00-LogVol00 62G 19G 41G 31% / /dev/sda1 99M 13M 82M 13% /boot tmpfs 1014M 0... (2 Replies)
Discussion started by: zvtral
2 Replies

4. Shell Programming and Scripting

File System exceed Alert Script!

Hello Allz, Please review my following script. The purpose of the script is to check the file system every 5 minutes & if any file systems is exceed from 90% then raise an email alert only 1 time. But the following script not functioning properly, mostly send an email that contain only... (7 Replies)
Discussion started by: telnor
7 Replies

5. Shell Programming and Scripting

ASM Diskspace

Hi I want to check Oracle ASM disk status through a PERL script. Script flow would be like this ... 1. Its taking diskgroup name in the command line... 2. Connect to Oracle database 3. If connection error send a critical message. Plz replyyyyyyy...... (1 Reply)
Discussion started by: Harikrishna
1 Replies

6. Shell Programming and Scripting

to compute diskspace

Guys, have any idea for the script like this? also to compute w/ decimal. thanks a=10 b=20 c=30 d=40 if a < b then ( a -b)*1024 = free space b + (c -d) = total space if a > b then (b / d)*1024 = cpu (3 Replies)
Discussion started by: kenshinhimura
3 Replies

7. Shell Programming and Scripting

Shel Script doesn't work from Exceed

Hi, I am using this script to load up my Oracle Databases, but when I log in through Exceed, it hangs. Can anyone tell me what else I need to add to make this work?? Details ****************************************************************************************************... (11 Replies)
Discussion started by: dnkansah
11 Replies

8. Shell Programming and Scripting

Limitation of DiskSpace Script

Hi, I'm a teacher in Germany and I want to set up a classroom with Linux 8.1 PCs. Every User should have a Max of 100 MB available space. Now I need a script which tells everyone how much diskspace is used in a certain subdirectory. It should work like that: User enters a command like... (1 Reply)
Discussion started by: Dansen
1 Replies

9. UNIX for Dummies Questions & Answers

Diskspace

Hi there, pls help me, i have a problem, how i can find out the disk amount of each harddisk per command (not the on label on the Harddisk) i need it on HP UX and Sun. can anybody helb me???? pls thanks Scotty (1 Reply)
Discussion started by: scotty
1 Replies

10. Programming

Monitor which users enter my home directory

Hi, I would like to monitor which users enter my home directory. Is it possible to write a script or code to do this. I donot have admin privileges. I have given read permissions to access my home directory. Any pointers in this direction is helpful! Thanks, Pradeep Ps: I use the... (1 Reply)
Discussion started by: mnpradeep
1 Replies
Login or Register to Ask a Question