免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3971 | 回复: 4
打印 上一主题 下一主题

perl 读取某日志文件的内容,然后过滤出需要的字段,并进行统计! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-03-02 15:30 |只看该作者 |倒序浏览
我是一个perl初学者.

目前想实现对某日志文件针对某些关键字进行过滤,然后对过滤出来的内容进行统计排序, 最后邮件把统计的结果发送到一指定邮箱.


你们提出思路就可以了,不用给出具体程序.我自己根据你们的思路研究一下.如果有疑问再请教各位.

日志文件内的大致内容如下:

文件名2009-02-02.log

2009-02-02 16:00:12 radious-server Level.warning [root]system-information-000435: badly user passcode from 1.1.1.1:1234 to 2.2.2.2:5678 proto TCP Occurred 1 times.
2009-02-02 16:00:13 radious-server Level.warning [root]system-information-000435: badly user passcode from 1.1.1.1:1234 to 2.2.2.2:5678 proto TCP Occurred 1 times.
2009-02-02 16:00:22 radious-server Level.info [root]system-information-000322: accept passcode from 3.3.3.3:1234 to 4.4.4.4:5678 proto TCP, Occurred 1 times.

过滤出所有 非 Level.info 等级的日志. 然后再对过滤出来的日志将[root]system-information 到 proto 的部分选出来 , 最后对选出的内容进行统计降幂排序.

论坛徽章:
0
2 [报告]
发表于 2009-03-02 15:44 |只看该作者
我也是perl初学者,
你可以利用散列
把关键值做为“键”,而“值”是一个数组的引用,这个数组是由相应的内容组成;
统计的话,可以通过散列的“键”找到数组的个数
排序就是对“键”的“值”所指向的数组排序,也就是数组排序。
以上供参考

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
3 [报告]
发表于 2009-03-02 15:51 |只看该作者
呵呵,不明白LZ说的排序指针对哪个进行排序,瞎写了一个,仅供参考吧:

  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;

  4. open(LOG, "2009-02-02.log") or die ("Open failed:$!");
  5. my @data = <LOG>;
  6. my @tmp = grep { ! /Level.info/ } @data;
  7. my @result = ();
  8. foreach my $value (@tmp) {
  9.         push @result, $1 if $value =~ /(\[.*proto)/;
  10. }

  11. foreach(sort @result) {
  12.         print "$_\n";
  13. }
  14. close(LOG);

复制代码

论坛徽章:
0
4 [报告]
发表于 2009-03-02 16:43 |只看该作者
我试了一下 finalBSD 的脚本 可以正确选出想要的内容.
但是存在有些问题,是因为我没把问题考虑全面.
[root]system-information-000435: badly user passcode from 1.1.1.1  后面的":源端口号" 不需要选出来 to 2.2.2.2:5678 proto 后面的目的地址和端口又需要选出来.

还有sort 出来的结果 不能显示出相同条目的数量.

论坛徽章:
0
5 [报告]
发表于 2009-03-03 10:03 |只看该作者
$ cat tmp/2009-02-02.log
2009-02-02 16:00:12 radious-server Level.warning [root]system-information-000435: badly user passcode from 1.1.1.1:1234 to 2.2.2.2:5678 proto TCP Occurred 1 times.
2009-02-02 16:00:13 radious-server Level.warning [root]system-information-000435: badly user passcode from 1.1.1.1:1234 to 2.2.2.2:5678 proto TCP Occurred 1 times.
2009-02-02 16:00:22 radious-server Level.info [root]system-information-000322: accept passcode from 3.3.3.3:1234 to 4.4.4.4:5678 proto TCP, Occurred 1 times.
2009-02-02 16:00:22 radious-server Level.err [root]system-information-000322: accept passcode from 3.3.3.3:1234 to 4.4.4.4:5678 proto TCP, Occurred 1 times.

$ perl -Wne '$count{"$1$2"}++ if ( /(\[.*):\d+( to [\d\.:]+ proto)/ and ! /Level.info/) ;END{ foreach(sort {$count{$b} <=> $count{$a}} keys %count) {print "$_ $count{$_}\n";}}' tmp/2009-02-02.log
[root]system-information-000435: badly user passcode from 1.1.1.1 to 2.2.2.2:5678 proto 2
[root]system-information-000322: accept passcode from 3.3.3.3 to 4.4.4.4:5678 proto 1
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP