tnt5273
tnt5273

Reputation: 79

shell script to monitor Disk IO not working properly

I'm to monitor Disk IO using sar command, requirement is in output 1) if %busy is above 80% for any disk, disk name & value should displayed in 1st line if all disk are below threshold then output should just display 'ok' 2) if %busy is above 60% but below 80% for any disk, disk name & value should displayed in 2nd line if all disk are below threshold then output should just display 'ok'

I've prepared below script but its not working properly is display some disk not found error for 1 disk and just display's 'ok'

can you please help me to fix my script and help me to achieve my goal

here is my script

`bash-3.00$ cat diskio.sh

 #!/bin/bash
a=`sar -d 2 2 |sed -n '/Average/,$ {s/Average//g;p;}'`
e=`$a|awk 'NR> 1{ if ($2 >80)  { print $1, $2; f=1; }  }  
    END { if (!f) print "ok" }'`
w=`$a|awk 'NR> 1{ if ($2 <80 && $2 >60)  { print $1, $2; f=1; }  }
    END { if (!f) print "ok" }'`
echo $e
echo $w`

below is output

`bash-3.00$ diskio.sh
./diskio.sh: line 3: hdisk18:  command not found.
./diskio.sh: line 4: hdisk18:  command not found.
ok
ok`

TIA regards, tnt5273

Upvotes: 0

Views: 919

Answers (2)

Jotne
Jotne

Reputation: 41454

You should use parentheses $(code) instead of back tics and echo or <<< to get the variable:

e=$(awk 'NR>1 {if ($2>40) {print $1,$2; f=1}} END {if (!f) print "ok"}' <<<"a$")

e=$(echo "$a" | awk 'NR>1 {if ($2>40) {print $1,$2; f=1}} END {if (!f) print "ok"}')

And always use double quote around variable: echo $a wrong, echo "$a" ok

Upvotes: 3

imp25
imp25

Reputation: 2357

You should put an echo before $a in the following two lines:

e=`$a|awk 'NR> 1{ if ($2 >40)  { print $1, $2; f=1; }  }  
END { if (!f) print "ok" }'`
w=`$a|awk 'NR> 1{ if ($2 <40 && $2 >20)  { print $1, $2; f=1; }  }
END { if (!f) print "ok" }'`

Such that they look like this:

e=`echo $a|awk 'NR> 1{ if ($2 >40)  { print $1, $2; f=1; }  }  
END { if (!f) print "ok" }'`
w=`echo $a|awk 'NR> 1{ if ($2 <40 && $2 >20)  { print $1, $2; f=1; }  }
END { if (!f) print "ok" }'`

Currently you just have $a which starts with hdisk18, and bash interperets it as a command, whilst what you want to do is echo the value held in $a and pipe this to awk.

Upvotes: 2

Related Questions