parsing time command in perl [closed]

旧城冷巷雨未停 提交于 2019-12-13 08:49:23

问题


When i issue the command /usr/bin/time script.sh it can vary in time because of the load of course (sometimes could be 10 secs and sometime could be 2 minutes), problem is that i need notification if time execution is higher than 30 sec (could also include minutes..) and warning if is 20-30 seconds. What is the best way to catch all time values for requested tresholds:

/usr/bin/time ./script.sh | awk'/real/ {print $2}' 
30.0
/usr/bin/time ./script.sh | awk'/real/ {print $2}'
1:24.2
/usr/bin/time ./script.sh | awk'/real/ {print $2}' 
3.1

回答1:


Given the following warning thresholds:

  • Critical: 30 seconds or longer
  • Warning: 20 seconds to 30 seconds
  • OK: less than 20 seconds

And based on the output of /usr/bin/time on your machine, which seems to use a format of hh:mm:ss.d with optional hours and minutes places:

my $time = `/usr/bin/time ./vladtest.sh | awk '/real/ {print $2}'`
my $status;
if ($time =~ /^[01]?\d\./) {       # time is 0-9 seconds or 10-19 seconds
    $status = "OK";
}
elsif ($time =~ /^(2\d|30)\./) {   # time is 20-30 seconds
    $status = "WARNING";
}
else {                             # any other time is critical
    $status = "CRITICAL";
}


来源:https://stackoverflow.com/questions/20620461/parsing-time-command-in-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!