Asterisk: How to convert time to seconds

259 views Asked by At

I want print only any result when $9 is greather than one hour
how can i convert this to seconds? so i can use 3600sec for the check.

$9 = duration of a call 00:00:00

asterisk -rx "core show channels verbose" | awk '$9 > "01:00:00"' | awk '$6 == "Dial"' | wc -l > test.txt

2

There are 2 answers

0
Mike S On BEST ANSWER

As someone mentioned, the easiest thing is to check if the first two digits of the time are not "00". Like this:

asterisk -rx "core show channels verbose" | awk '$9 !~ /00:..:../ && $6 == "Dial"' | wc -l

That means that it will match and record "01:00:00" which may not be what you want :-( . It matches "$9 greater than or equal to one hour".

This would match your "greater than" condition:

asterisk -rx "core show channels verbose" | awk '$9 !~ /00:..:../ && $9 != "01:00:00" && $6 == "Dial"' | wc -l
0
Jahid On

You can simply extract hour minute and sec from the format hh:mm:ss, like this:

h=$(echo "$9" | cut -d":" -f1)
m=$(echo "$9" | cut -d":" -f2)
s=$(echo "$9" | cut -d":" -f3)

As you already have the h m and s values at hand now, you can convert it to seconds like this:

totalsec=$(echo "$h*3600+$m*60+$s"|bc)

or use $h if you need to check only for hours.