问题
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