首页 技术 正文
技术 2022年11月13日
0 收藏 497 点赞 4,338 浏览 1931 个字

需求描述:

日志记录了一次操作的时间,即server端接收包到发送结果到client端的时间,取出操作时间最长的100个记录。

日志信息片段:

[2013-09-13 15:23:50,445.500] [47028700024080] FATAL – socket = 9
[2013-09-13 15:23:50,446.156] [47028700024080] FATAL – a client connected with ip: 10.10.10.127, name: <unknown>, port: 2314
[2013-09-13 15:23:50,447.375] [1103333696] INFO  – recv: with 64 bytes from 10.10.10.127.
[2013-09-13 15:23:50,449.461] [1103333696] INFO  – send: 1 with 1 bytes.

………

[root@sjs_131_126 analyse_time]# cat sort.sh

#!/bin/sh
#cat $1 | perl split.pl
grep -n 'FATAL - socket\|INFO - send:' $1 |awk -F']' '{print $1 $3}'|awk -F'[' '{print $1 "\t"t$2}'> result1.txt
echo "result1 success!" ./analyze.pl result1.txt
echo "result2 success!"
#
cat result2.txt | sort -t: -r | head -100 >result.txt
echo "success!"

说明:

1.第一步,将开始和结束的日志提取到result1.txt

2.第二步,通过analyze.pl计算每次操作的时间写入result2.txt

3.第三步,排序取出前100条

[root@sjs_131_126 analyse_time]# cat analyze.pl

#!/usr/bin/perl -w
use strict;
use warnings ;
use Time::Local;open FD1, ">> result_error.txt" ;
open FD2, ">> result2.txt";my $need_end = 0;
my ($second, $minute, $hour, $date, $month, $year);my $begin = 0;
my $end = 0;
my $begin_time = 0;
my $end_time = 0;#sample
#6653: 2013-09-11 15:04:35,815.499 FATAL - socket = 8
#6656: 2013-09-11 15:04:35,821.075 INFO - send: 1 with 1 bytes.
while(<>)
{
chomp ;
if($need_end == 0)
{
if(/(\d+):\t(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+),.*\s(INFO|FATAL).*/s)
{
if($8 eq "FATAL")
{
$begin = $1;
$need_end = 1;
($second, $minute, $hour, $date, $month, $year) = ($7, $6, $5, $4, $3 - 1, $2 - 1900);
$begin_time = timelocal($second, $minute, $hour, $date, $month, $year);
#print "$year-$month-$date $hour:$minute:$second\n"
}
else
{
print FD1 "$_\n" ;
$need_end = 0 ;
}
}
else
{
die "match error\n" ;
}
}
elsif($need_end == 1)
{
if(/(\d+):\t(\d+)-(\d+)-(\d+)\s(\d+):(\d+):(\d+),.*\s(INFO|FATAL).*/s)
{
$end = $1;
if( $end == $begin + 3 && $8 eq "INFO")
{
$end = $1;
$need_end = 0;
($second, $minute, $hour, $date, $month, $year) = ($7, $6, $5, $4, $3 - 1, $2 - 1900);
$end_time = timelocal($second, $minute, $hour, $date, $month, $year);
my $duration = $end_time - $begin_time;
print FD2 "$duration:[$begin to $end]\n";
}
else
{
$need_end = 0 ;
print FD1 "$_\n" ; }
}
}
else
{
die "control error\n" ;
}
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,951
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,477
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,290
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,107
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,739
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,773