Need way to grep the following output in bold

Hi guys,

i am not able to work this out, i need to write a script to monitor the usage of IO DISK READ whenever the IO is above 600MB it will send an email.

So far not able to. Hope you guys can assist me. Thanks!

[root@xxxx ~]# iotop -bot --iter=3 |grep DISK|egrep -v TIME
05:22:04 Total DISK READ :      20.70 M/s | Total DISK WRITE :    1157.23 K/s
05:22:04 Actual DISK READ:      14.53 M/s | Actual DISK WRITE:    1348.83 K/s
05:22:06 Total DISK READ :      24.51 M/s | Total DISK WRITE :    1067.07 K/s
05:22:06 Actual DISK READ:      22.92 M/s | Actual DISK WRITE:      10.54 M/s
05:22:07 Total DISK READ :      20.78 M/s | Total DISK WRITE :    1034.70 K/s
05:22:07 Actual DISK READ:      23.04 M/s | Actual DISK WRITE:       6.33 M/s
[root@xxxx ~]#

And where did you fail?

BTW, I don't see any DISK READ above 600MB in your sample?

1 Like

Yes, i would like to set up an email monitoring whenever the DISK READ went above 600MB per second it will trigger an email to me personally.

I was able to get the output of 3 lines but not managed to create a logic when the read go above 600MB.

[root@xxxx ~]# iotop -bot --iter=3 |grep "Actual DISK READ"| awk {'print $5'}
127.99
151.71
157.06
[root@xxxx ~]#

ok using one second data might help.

[root@xxxx ~]# iotop -bot --iter=1 |grep "Actual DISK READ"| awk {'print $5'}
145.13
[root@xxxx ~]#

So, basically, you want to test, whether a number containing a decimal point is larger than 600?

If you are using Zsh as a shell, you have decimal arithmetic, and can simply compare the number you get numerically to 600. This is probably the most straightforward way.

If you are using a shell, which has only integer arithmetic, you can just remove everything starting from the dot, which in your example would leave you with 145, and if this is numerically greater or equal to 600, you are sending your email.

The third possibility is to move the logic into awk. Currently, you always output $5. You could print this only if $5 is numerically greater than 600. This means that whenever you get a non-empty output, you have to send the email.

Which approach you take, is just a matter of taste.....

1 Like

Not sure I understand. To monitor, you want that script to run all day, i.e. 24/7? In what intervals? Will iotop allow for that, do you want to loop inside your script, or do you plan to schedule a cron job?
As a starting point, how far would this get you:

iotop -bot --iter=3 | awk  '/Actual DISK READ/ && $5 > 22 {system ("echo mail -s\\\"High disk IO: " $5 "\\\" user@host")} '
mail -s"High disk IO: 22.92" user@host
mail -s"High disk IO: 23.04" user@host

, echo ing the mail command for testing, and adapted to use you sample from post#1.

1 Like

Thanks for helping out so far, your script did the job, but I was looking to find a write an if statement and schedule it in cron for every 15 minutes, something like below.

[root@xxxx ~]# cat write.sh
cmd1=`iotop -bot --iter=1 |grep "Actual DISK READ"| awk {'print $5'}`
if $cmd1 -ge 600; then
echo "Read IO is high" | /bin/mailx -s "Please check io usage" "user@host"
else
echo "io is fine"
fi
[root@xxxx ~]#

???? But I explained in my answer three choices you have for this, didn't I?

Still, I find the solution proposed by RudiC better.

If you schedule a command or pipe in cron , the resulting output will be mailed by default, no additional exercises necessary.

1 Like

Thanks RudiC, that works fine :slight_smile: