LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-17-2022, 07:14 AM   #1
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Rep: Reputation: Disabled
Bash Script to check Disk Usage


Hi All,

Trying to run the below script to notify us if the disk goes over 80% usage but only wish to see the file systems over 80%.

Code:
#!/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 80 ]; then
  echo "Running out of space \"$partition ($usep%)\" on $(hostname) as on $(date)"
  fi
done
When i run the script i get the below output:

Code:
linux@server03:~$ ./diskcheck.sh
0% udev
84% /dev/sda1
Running out of space "/dev/sda1 (84%)" on linux as on Thu Mar 17 12:16:49 UTC 2022
4% /dev/sda15
1% /dev/sdb1
84% localhost:/gv0
Running out of space "localhost:/gv0 (84%)" on linux as on Thu Mar 17 12:16:49 UTC 2022
linux@server03:~$
How do i only extract the 84% ?

Last edited by LinuxRSA; 03-17-2022 at 07:20 AM.
 
Old 03-17-2022, 07:23 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
I would do it with AWK all the way through:

Code:
df -HT \
| awk '!/^F/ && $2!~/tmpfs/ && $6+0>=80 {
        print $7 " is almost full (" $6 ")"; }'
Or is there are reason to complicate it?
 
1 members found this post helpful.
Old 03-17-2022, 07:31 AM   #3
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Thumbs up

Hi Turbocapitalist

This works perfect, i have over complicated it. Thanks its looks good now.

Code:
linux@server03:~$ ./disk2.sh
/ is almost full (84%)
/mnt/gv0 is almost full (84%)
linux@server03:~$

Last edited by LinuxRSA; 03-17-2022 at 07:33 AM.
 
Old 03-17-2022, 07:55 AM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,153

Rep: Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125Reputation: 4125
The problem with using specific checks is that that are specific.

tmpfs replaced ramdisk - but not entirely. What's next ?. Might never be a concern ... or not. What if the syntax of the report changes.
Stay aware.
 
Old 03-17-2022, 08:00 AM   #5
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Code:
while read -r a b c d e f g; 
do 
    [[ "${f%%%}" -lt 80 ]] && continue
    [[ $b = tmpfs ]] && continue
    echo "$g is almost full ($f)"
done < <(df -HT)

Last edited by pan64; 03-17-2022 at 08:32 AM.
 
1 members found this post helpful.
Old 03-17-2022, 08:37 AM   #6
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
How would i add the below into the script ?

echo 'Disk Healthy' if disk utilization is below 80%

echo 'Disk Issue' if disk utilization is above 80%

Instead of getting the output
Quote:
/ is almost full (84%)
it says Disk Healthy or Disk Issue ?

I tried this

Code:
linux@server03:~$ df -HT \
> | awk '!/^F/ && $2!~/tmpfs/ && $6+0>=80 {
>         print $7 " is almost full (" $6 ")"; }'
/ is almost full (84%)
/mnt/gv0 is almost full (84%)
cc-scilo@eun-p-ca88x-app-03:~$ if [ $6 -ge 80 ]; then echo 'Disk Healthy'; else echo 'Disk Issue'; fi
-bash: [: -ge: unary operator expected
Disk Issue
linux@server03:~$

Last edited by LinuxRSA; 03-17-2022 at 08:49 AM.
 
Old 03-17-2022, 09:01 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,359
Blog Entries: 3

Rep: Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767Reputation: 3767
AWK is a scripting language with a lot of abbreviations. In pseudocode, the above would be

Code:
if $0 ~ !/^F/ 
 AND if $2!~/tmpfs/ 
 AND if $6+0>=80 THEN {
       print $7 " is almost full (" $6 ")";
}
The first line above checks if the whole line starts with a capital F
The second line checks if the second field contains the string "tmpfs"
The third line converts the sixth field to a number and then compares it to 80.
The fourth line needs no further explanation I would hope.

So you could add another check with similar conditions but checks for less than a value instead.

Please see one of excellent AWK guides in conjunction with the reference manual for the version of AWK on your particular system, 'man awk'.


PS. For actual disk health, see smartmontools instead.
 
Old 03-17-2022, 09:41 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
Quote:
Originally Posted by LinuxRSA View Post
I tried this

Code:
linux@server03:~$ df -HT \
> | awk '!/^F/ && $2!~/tmpfs/ && $6+0>=80 {
>         print $7 " is almost full (" $6 ")"; }'
/ is almost full (84%)
/mnt/gv0 is almost full (84%)
cc-scilo@eun-p-ca88x-app-03:~$ if [ $6 -ge 80 ]; then echo 'Disk Healthy'; else echo 'Disk Issue'; fi
-bash: [: -ge: unary operator expected
Disk Issue
linux@server03:~$
$6 is a variable inside awk, cannot be used in the shell (because that is unavailable, and in shell $6 has another meaning).
 
Old 03-17-2022, 10:17 AM   #9
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Thanks, would something like this be possible to script ?

Detect the values above 80% using the below command, then take that output and send it to the if statement that will say Disk Healthy or Disk Issue ?

Code:
linux@server03:~$ df -HT | awk '{print $6}'
Use%
0%
11%
84%
1%
0%
0%
4%
1%
84%
0%
0%
linux@server03:~$
Based on the above output how do i use it to create a IF statement

Code:
df -HT | awk '{print $6}'  
(( TAKE THIS OUTPUT ))

if [ USE OUTPUT IN HERE ]; then
echo "Disk Issue"
else
echo "Disk Healthy"
 
Old 03-17-2022, 10:29 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,041

Rep: Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348Reputation: 7348
you can do it inside awk:
Code:
 awk '{ if ($6+0>10) { print "apple" } else { print "worm" } } '
 
1 members found this post helpful.
Old 03-17-2022, 10:31 AM   #11
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,628

Rep: Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557Reputation: 2557

Awk can be very powerful - you often don't need to switch between it and Bash...
Code:
df -h | awk '$5 > 80 {print}; $5 <= 80 {print $1,"ok"}'
My df outputs "Use%" in $5, not $6, but also accepts a "--output=FIELD_LIST" which a script should use to guarantee what is output.

edit: Just realised previous posts were using "df -HT" instead of "df -h" which explains the difference. I would still use --output to be on safe side.


Last edited by boughtonp; 03-17-2022 at 10:37 AM.
 
2 members found this post helpful.
Old 03-17-2022, 10:40 AM   #12
LinuxRSA
Member
 
Registered: Apr 2015
Location: South Africa
Posts: 71

Original Poster
Rep: Reputation: Disabled
Awesome Guys, this one is working as expected.
Code:
linux@server03:~$
linux@server03~$ df -HT | awk '{print $6}'
Use%
0%
10%
83%
1%
0%
0%
4%
1%
83%
0%
linux@server03:~$
linux@server03:~$ df -HT |  awk '{ if ($6+0>80) { print "Disk Issue" } else { print "Disk Healthy" } } '
Disk Healthy
Disk Healthy
Disk Healthy
Disk Issue
Disk Healthy
Disk Healthy
Disk Healthy
Disk Healthy
Disk Healthy
Disk Issue
Disk Healthy
linux@server03:~$
Thank you, much appreciated!

Last edited by LinuxRSA; 03-17-2022 at 10:41 AM.
 
Old 03-17-2022, 10:44 AM   #13
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Quote:
Detect the values above 80% using the below command, then take that output and send it to the if statement that will say Disk Healthy or Disk Issue ?
My only comment and depends on your perspective and how you define the term disk. The term disk for means a physical device and filesystem percent usage does not determine the health of a disk.
 
Old 03-17-2022, 10:51 AM   #14
dc.901
Senior Member
 
Registered: Aug 2018
Location: Atlanta, GA - USA
Distribution: CentOS/RHEL, openSuSE/SLES, Ubuntu
Posts: 1,005

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
There is also "--output" option with df command...

For example:

Code:
$ df --output=pcent /
Use%
 10%

$ df -h --output=used /
 Used
  12G
You can also have specific columns with:
Code:
$ df -h --output=used,pcent /
 Used Use%
  12G  10%
 
Old 03-17-2022, 11:29 AM   #15
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
To expand on dc.901 post.
Code:
 df --type ext4 --output=source,pcent | awk '!/^F/ { if ($2+0>80) { print $1,"Full" } else { print $1,"Ok" } } '
This outputs only physical filesystems not temporary and you can expand filesystems with additional --type FS option if you have others besides ext4.

Last edited by michaelk; 03-17-2022 at 11:32 AM.
 
2 members found this post helpful.
  


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
Shell script for CPU usage, memory usage, disk partition space and service status reetesh.amity Linux - Server 6 10-12-2015 07:51 PM
[SOLVED] Bash script to check if file is present or not, check periodically every 30 mins Iyyappan Linux - Server 10 07-03-2013 05:19 AM
how to determine cpu usage, memory usage, I/O usage by a particular user logged on li rags2k Programming 4 08-21-2004 04:45 AM
Boot disk; check. CD in drive; check. Doesn't work; check. Hal DamnSmallLinux 7 02-04-2004 02:10 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:14 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