ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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:~$
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:~$
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.
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).
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.