I am experiencing problems with a little awk-script:
...
### DISK ###
DISK=`/usr/bin/df -k |\
sed -n '/^\/dev/p' |\
awk '{ USED = substr($4,1,length($4)-1); \
{ STATUS = "good" }
if ( USED > "'"$SCHWELL1"'" ) { STATUS = "warning" }
if ( USED > "'"$SCHWELL2"'" ) { STATUS = "error" }
printf "%s - %s,%s ; (%s) %s_used; %u MB_free; %u MB_total;\n", USED,
STATUS, STATUS, $7, $4, $3/1024, $2/1024}'`
...
The variable USED contains a numeric value (e.g. 9) and is compared to
the values of $SCHWELL1 (e.g. 85) and $SCHWELL2 (e.g. 90), which are
os-variables.
The outout of the script shows an error in the marked line
(<<<------):
18 - good,good ; (/) 18%_used; 840 MB_free; 1024 MB_total;
21 - good,good ; (/usr) 21%_used; 13033 MB_free; 16384 MB_total;
17 - good,good ; (/var) 17%_used; 856 MB_free; 1024 MB_total;
13 - good,good ; (/tmp) 13%_used; 896 MB_free; 1024 MB_total;
66 - good,good ; (/home) 66%_used; 352 MB_free; 1024 MB_total;
73 - good,good ; (/opt) 73%_used; 70 MB_free; 256 MB_total;
63 - good,good ; (/install) 63%_used; 9708 MB_free; 25600 MB_total;
86 - warning,warning ; (/mysqlbackup) 86%_used; 46396 MB_free; 327936
MB_total;
9 - warning,warning ; (/mysqldata1) 9%_used; 138602 MB_free; 151296
MB_total; <<<------
89 - warning,warning ; (/mysqldata2) 89%_used; 27036 MB_free; 227072
MB_total;
56 - good,good ; (/mysqllog) 56%_used; 33898 MB_free; 75520 MB_total;
32 - good,good ; (/mysqltemp) 32%_used; 34294 MB_free; 50176 MB_total;
4 - good,good ; (/mksysb) 4%_used; 67404 MB_free; 69632 MB_total;
I expect the STATUS to be "good", not "warning".
Any ideas?
TIA,
Henning
I think that If you use single quotes in the body of the program awk
'{ }' you are preventing of shell variable subtitution.
Use variable arguments: awk -v
### DISK ###
DISK=`/usr/bin/df -k |\
sed -n '/^\/dev/p' |\
awk -v shwell1 = $SCHELL1 -v shwell2 = $SCHWELL2 '{ USED = substr
($4,1,length($4)-1); \
{ STATUS = "good" }
if ( USED > shwell1 ) { STATUS = "warning" }
if ( USED > shwell2 ) { STATUS = "error" }
One could also get rid of sed and include this part into the AWK
script
USED = substr($4,1,length($4)-1) could be replaced by USED = $4 + 0
The assignment for STATUS could be simplified to:
STATUS = USED > shwell2 ? "error" :
USED > shwell1 ? "warning" : "good"
Hermann
On 2009-04-22, kielhd <kie...@freenet.de> wrote:
> [...]
> I am experiencing problems with a little awk-script:
>
> ...
> ### DISK ###
> DISK=`/usr/bin/df -k |\
> sed -n '/^\/dev/p' |\
> awk '{ USED = substr($4,1,length($4)-1); \
> { STATUS = "good" }
> if ( USED > "'"$SCHWELL1"'" ) { STATUS = "warning" }
> if ( USED > "'"$SCHWELL2"'" ) { STATUS = "error" }
> printf "%s - %s,%s ; (%s) %s_used; %u MB_free; %u MB_total;\n", USED,
> STATUS, STATUS, $7, $4, $3/1024, $2/1024}'`
> ...
>
> The variable USED contains a numeric value (e.g. 9) and is compared to
> the values of $SCHWELL1 (e.g. 85) and $SCHWELL2 (e.g. 90), which are
> os-variables.
>
> The outout of the script shows an error in the marked line
> (<<<------):
>
> [...]
> 9 - warning,warning ; (/mysqldata1) 9%_used; 138602 MB_free; 151296
> MB_total; <<<------
> [...]
>
> I expect the STATUS to be "good", not "warning".
Hi,
you do a string compare instead of a numeric compare, when you compare
USED to $SCHWELLn for n = 1, 2. Add zero to both string variables inside
of awk and you get the correct result, e.g.
USED + 0 > "'"$SCHWELL1"'" + 0
to force a numeric interpretation.
(As the other posters said, use better the awk option -v var=val on the
command line to pass OS variables to awk.)
Kind regards,
Steffen
- --
Steffen Schuler (goedel) <schuler...@gmail.com>
Key ID: 0x42C5D853 / Key-server: pgp.mit.edu
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
iEYEARECAAYFAknvSPEACgkQ+/TWb0LF2FP84QCghGJhv4LErH0ROfe6gCo+TRbq
MoAAmQHtbnw+6wjQvA3ik039AYVeYe3G
=bYKd
-----END PGP SIGNATURE-----
Is this any cleaner? I added the output of our df -k since it differs
from yours. You can call this with "awk -f u -v SCHWELL1=85"
loui@euclid:/tmp$ less u
#Filesystem 1K-blocks Used Available Use% Mounted on
#/dev/sdb2 19380708 13560240 4843716 74% /
#varrun 6187596 84 6187512 1% /var/run
#varlock 6187596 0 6187596 0% /var/lock
#udev 6187596 52 6187544 1% /dev
#devshm 6187596 0 6187596 0% /dev/shm
#mozart.bio.ri.cia.gov:/export/home/studies
# 181540440 168094816 11630216 94% /studies
#mozart.bio.ri.cia.gov:/export/home/programs
# 181540440 168094816 11630216 94% /programs
#mozart.bio.ri.cia.gov:/export/home/vol
# 181540440 168094816 11630216 94% /vol
#/dev/sdb1 93307 24205 64285 28% /boot
#/dev/sdb3 58134488 9333924 45870712 17% /opt
#/dev/sda1 968841096 122203764 797810580 14% /var
#mozart:/export/home/home/chana3
# 181540864 168094720 11630592 94% /home/chanb7
BEGIN {
if ("" == SCHWELL1) SCHWELL1 = 50
if ("" == SCHWELL2) SCHWELL2 = 90
com = "df -l -k"
com | getline
while (com | getline) {
if ($1 !~ /^\/dev/) continue
USED = $5; sub(/.$/,"",$5)
printf "%s - ", $5
STATUS = (USED > SCHWELL1) ? "warning" : "good"
STATUS = (USED > SCHWELL2) ? "error" : STATUS
printf "%s,%s ; ", STATUS, STATUS
printf "(%s) ", $6
printf "%s_used; ", $5
printf "%s MB_free; ", $4/1024
printf "%s MB_total; ", $2/1024
print ""
}
}
Your hint solved my problem, thank you and all the other posters!
Best regards,
Henning
I've no idea if the above works or not but if you'd prefer to have it
written in awk:
BEGIN {
if ("" == SCHWELL1) SCHWELL1 = 50
if ("" == SCHWELL2) SCHWELL2 = 90
}
(NR > 1) && ($1 !~ /^\/dev/) {
USED = $5; sub(/.$/,"",$5)
printf "%s - ", $5
STATUS = (USED > SCHWELL1) ? "warning" : "good"
STATUS = (USED > SCHWELL2) ? "error" : STATUS
printf "%s,%s ; ", STATUS, STATUS
printf "(%s) ", $6
printf "%s_used; ", $5
printf "%s MB_free; ", $4/1024
printf "%s MB_total; ", $2/1024
print ""
}
Ed.