比如这样的日志,需要统计后一条记录和前一条记录之间的时间差值
[2024-10-11 10:42:24 023047] DEBUG 478832,readermgn.cpp:17:run,tid:0x7f80697fa700,479378,reader mgn running
[2024-10-11 10:42:30 329273] DEBUG 478832,readermgn.cpp:17:run,tid:0x7f80697fa700,479378,reader mgn running
[2024-10-11 10:42:36 565110] DEBUG 478832,readermgn.cpp:17:run,tid:0x7f80697fa700,479378,reader mgn running
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
grep 'current_ownjob_num' sys_binlogconsumer_mgn.log.2024-10-11.0 > tmp.log log_num=$(cat tmp.log | wc -l) s="" e="" for i in $(seq 1 $log_num) do item=$(sed -n "$i"p tmp.log) if [ "$item" == "" ];then continue fi if [ "$s" == "" ];then s=$(echo $(echo $item | cut -c 2-20).$(echo $item | cut -c 22-27)) s=$(date -d "$s" +"%s.%N") last_item=$item else e=$(echo $(echo $item | cut -c 2-20).$(echo $item | cut -c 22-27)) e=$(date -d "$e" +"%s.%N") escaped_time=$(echo "${e} - ${s}" | bc) echo $last_item " + " $escaped_time "(s)" last_item=$item s=$e fi done |
执行后是这个样子的
[2024-10-11 10:07:47 941734] DEBUG 45670,readermgn.cpp:17:run,tid:0x7f33b2ffd700,45820,reader mgn running + 9.934856000 (s)
[2024-10-11 10:07:57 876590] DEBUG 45670,readermgn.cpp:17:run,tid:0x7f33b2ffd700,45820,reader mgn running + 7.822612000 (s)
[2024-10-11 10:08:05 699202] DEBUG 45670,readermgn.cpp:17:run,tid:0x7f33b2ffd700,45820,reader mgn running + 7.875902000 (s)
[2024-10-11 10:08:13 575104] DEBUG 45670,readermgn.cpp:17:run,tid:0x7f33b2ffd700,45820,reader mgn running + 7.806684000 (s)