免费注册 查看新帖 |

Chinaunix

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

能否用perl实现统计vsftpd.log的功能 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-06-11 17:35 |只看该作者 |倒序浏览
各位高手,现有个脚本想实现:对vsftpd.log里的记录每周统计一次,下载或者上传的文件总共大小从高到低排前十,列出用户、大小来,将结果用邮件发送出去。

文件记录示例如下:
Thu Jun 10 17:51:27 2010 1 10.94.6.72 62775 /home/user1/AB/UK19AB_LIN_top.pdf b _ o r user1 ftp 0 * c
Thu Jun 10 17:51:27 2010 1 10.94.6.72 201140 /home/user2/AB/UK19AB_LIN_transmitter.pdf b _ o r user2 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 150522 /home/user1/AB/UK19AB_LIN_wkup.pdf b _ o r user1 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 60722 /home/user2/AA/UU15AA_LIN_ESD_neg.pdf b _ o r user2 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 79139 /home/user2/AA/UU15AA_LIN_ESD_pos.pdf b _ o r user2 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 26430 /home/user1/AA/UU15AA_LIN_ESD_top.pdf b _ o r user1 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 144165 /home/user1/AA/UU15AA_LIN_rec.pdf b _ o r user1 ftp 0 * c
Thu Jun 10 17:51:28 2010 1 10.94.6.72 50140 /home/user2/AA/UU15AA_LIN_top.pdf b _ o r user2 ftp 0 * c

本人想过了方法:
1. 从文件里筛选定位出7天时间。(前5个字段)
2. 取出上传和下载的用户名。 (第14列)
3. 累加该用户上传和下载文件的大小。(不分上传或下载,累加在一起就行,第8列)
最好是要求7天时间的, 随便从星期几开始都行,只要是一周报告一次就行。

各位能帮忙写下吗? 我只想的出这些,没办法用脚本实现。谢谢了!
在shell帮帖了个帖子,不过自己感觉perl应该更强劲,对这种文本处理的脚本来讲。
所以再帖这里,请版主原谅。

论坛徽章:
0
2 [报告]
发表于 2010-06-12 14:21 |只看该作者
回复 1# shot


    你的需求脚本就能实现。我做了一个看是不是可以啦。另外还有perl的实现方法。
   本人觉得脚本挺方便实现的!
   #!/bin/sh
#截取一个星期内的日志
date=`date --date "6 day ago"|awk '{print $1,$2,$3}'`
today=`date |awk '{print $1,$2,$3}'`
#echo $date $today
cat ./vsftpd.log|sed -n "/$date/,/$today$/p" >res.log

#获取结果文件result.log
cat res.log | sort -nr -k8|head -10|awk '{print $8,$9,$14}'>result.log
#发送邮件
mail -s "test" xxx@xxx.com < /root/result.log

论坛徽章:
0
3 [报告]
发表于 2010-06-12 14:25 |只看该作者
回复 1# shot


    另外 还有通过perl写的结果:
   #!/usr/bin/perl -w
use strict;
#my $date=qx/date --date="1 week ago"|awk '{print \$1,\$2,\$3}'/;
#my $today=qx/date|awk '{print \$1,\$2,\$3}'/;
#my $today="Thu Jun 10";
#print "$today";
#print "$date";


open LOG,"vsftpd.log";
  
open RES,"+>result.log";
my @m=<LOG>;
close LOG;
#print @m,"\n";
#my @m={};
#while (<LOG>){
#  shift;      
#   @m=split(/\s/,$_);     
#   print $m[7],"\n";   
   
#   print  if $_ =~ /$date/,/$today$/;
      


#  }
my @y=
#join "\t",
   map{$_->[1],"\t",$_->[2],"\t",$_->[3],"\n"}
reverse sort {$a->[1]<=>$b->[1]}
   map { [$_,(split)[7],(split)[8],(split)[13]]}   @m;


   
   print RES @y;
   seek(RES,0,0);
   while(<RES>){
    print if $.<11;
    }

写了一部分实现,邮件的话可以通过sendmail 的发,利用SMTP模块,本人觉得还是脚本的简单啊

论坛徽章:
0
4 [报告]
发表于 2010-06-14 10:46 |只看该作者
本帖最后由 shot 于 2010-07-01 20:04 编辑

回复 2# liyangole


cat ./vsftpd.log|sed -n "/$date/,/$today$/p" >res.log
上面这句输出为0字节。
请问$/p /p 是打印, 那$ 呢?

这个方法好似有些问题, 因为$date 出来的 Tue Jun 8 里面都是一个空格,但是在vsftpd.log里的 Tue Jun  8 天数前如果是一位数那么是两个空格。
所有我在想换个方法截取日期的前三个字段,保留格式。

在坛子里问了一个兄弟,他帮我搞定了输出字段的问题, 现在脚本修改如下:

#!/bin/bash

#截取一个星期内的日志
#date=`date --date "6 day ago"|awk '{printf $1,$2,$3 %3d%5d%9d}'`
#today=`date |awk '{print $1,$2,$3}'`
date=`date --date="6 day ago" |awk '{printf("%s %s %2s\n",$1,$2,$3)}'`
today=`date |awk '{printf("%s %s %2s\n",$1,$2,$3)}'`

#echo $date $today
cat ./vsftpd.log|sed -n "/$date/,/$today$/p" >res.log

#获取结果文件result.log
cat res.log | sort -nr -k8|head -10|awk '{print $8,$9,$14}'>result.log

#发送邮件
mail -s "test" w.z@tt.com < /root/result.log


但是如何实现统计每个用户传输的数据量,而不是用命令统计每次传输的包的大小再排序呢?

论坛徽章:
33
ChinaUnix元老
日期:2015-02-02 08:55:39CU十四周年纪念徽章
日期:2019-08-20 08:30:3720周年集字徽章-周	
日期:2020-10-28 14:13:3020周年集字徽章-20	
日期:2020-10-28 14:04:3019周年集字徽章-CU
日期:2019-09-08 23:26:2519周年集字徽章-19
日期:2019-08-27 13:31:262016科比退役纪念章
日期:2022-04-24 14:33:24
5 [报告]
发表于 2010-06-14 22:21 |只看该作者
回复 4# shot


cat ./vsftpd.log|sed -n "/$date/,/$today$/p" >res.log

蓝色的$符号属于正则的范畴,它表示以某给字符串结尾的行。

论坛徽章:
0
6 [报告]
发表于 2010-06-17 09:30 |只看该作者
回复 4# shot


   兄弟,需求不明确啦?

论坛徽章:
0
7 [报告]
发表于 2010-06-17 09:32 |只看该作者
回复 4# shot


  那个时间比较方法,值得参考。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP