您的位置:首页 > 职场人生

perl脚本学习笔记

2008-08-01 10:27 459 查看
#!/usr/bin/perl
my $totaltimes=0; //定义变量
my %tong_visit_times; //定义散列
my %tong_unique_users;
my %userlist;
my %tongidall;
my $totalusers=0;
my $totaltongs=0;
my ($logfile)=@ARGV; //传递脚本的参数给变量

if(-e $logfile){
&readlogfile($logfile); //子程序调用
}
#here read_tong
&read_tongs();
$activeusers=(keys(%userlist)); //取散列的关键字的数量,keys在标量上下文中返回key的数量
$activetongs=(keys(%tong_visit_times));
print "总活动人次:".$totaltimes."\n";
print "活动用户数:".$activeusers."占总用户$totalusers的".(100*$activeusers/$totalusers)."%\n";
print "涉及到桶数:".$activetongs."占总桶数$totaltongs的".(100*$activetongs/$totaltongs)."%\n";
print "------------------------\n";

open(USER,">last".$days."day.txt"); //定义文件句柄USER,输出到last$daysday.txt
foreach $key(keys %userlist){
print USER $key.":".$userlist{$key}."\n";
@i=split(/\|/,$userlist{$key});
if (defined($tong_unique_users{$i[0]})){
$tong_unique_users{$i[0]}=$tong_unique_users{$i[0]}+1;
}else{
$tong_unique_users{$i[0]}=1;
}
}
close(USER);

print "\n桶号 活动人次 活动用户数 总数 活动比率\n";
foreach $key(sort(keys %tongidall)){
if (defined($tong_unique_users{$key})){
print $key." ".$tong_visit_times{$key}." ".$tong_unique_users{$key}." ".$tongidall{$key}." ".(defined($
tongidall{$key})?100*$tong_unique_users{$key}/$tongidall{$key}."%":"此行数据有问题")."\n";
}
}

#--------------end of main program----------

sub readlogfile{
my($curlogfile) =@_; //@_表示缺省的参数
open(FILE,$curlogfile); //打开文件句柄
while(<FILE>){
if ($_=~/UNI-LOG\|/){ //模式匹配UNI-LOG的行,计数器totaltimes加1
$totaltimes++;
chop(); //去除换行符号
@items=split(/\|/); //以|进行分割,存入数组items中
my $web=0;
my $pop3=0;
my $smtp=0;
my $visitmethod=$items[1]; //通过数组下标取数组中的元素内容
my $username=lc($items[2]);
my $remote_ip=$items[3];
my $tongid=$items[4];
my $visithistory=0;
if (defined($userlist{$username})){ //defined()如果为undef返回false,即变量为空的话返回假
$oldvalue=$userlist{$username};
@olditems=split(/\|/,$oldvalue);
$visithistory=$olditems[0];
$web=$olditems[1];
$pop3=$olditems[2];
$smtp=$olditems[3];
}
if ($visitmethod eq "web"){
$web=1;
}
if ($visitmethod eq "pop3"){
$pop3=1;
}
if ($visitmethod eq "smtp"){
$smtp=1;
}
$visithistory="$web|$pop3|$smtp";
$userlist{$username}=$tongid."|".$visithistory;
if (defined($tong_visit_times{$tongid})){
$tong_visit_times{$tongid}=$tong_visit_times{$tongid}+1;
}else{
$tong_visit_times{$tongid}=1;
}
}
}
}

sub read_tongs{
my $server="192.168.41.168";
my $port=9901;

use IO::Socket; //使用模块
my $sk = IO::Socket::INET->new(PeerAddr => $server, //使用方法,具体参考perldoc IO::Socket::INET或者www.cpan.org
PeerPort => $port,
Proto => 'tcp',
timeout =>30);

if (!($sk)) {
print STDERR "fail to connect dbgate:".$server."\n";
exit;
}
print $sk "system.get servers\n"; //打印到文件句柄$sk
$echo=<$sk>; //从$sk中读取输出
chop($echo);
if (substr($echo,0,1) eq "0"){
$echo=substr($echo,2); //跳过开头的0
@items=split/\\s/,$echo;
foreach $tongid(@items){
print $sk "server.get ".$tongid." user_count\n";
$echo=<$sk>;
my $tongcount=substr($echo,2);
$totalusers=$totalusers+$tongcount;
$tongidall{$tongid}=$tongcount;
}
$totaltongs=(keys(%tongidall));
}

} perl脚本学习
将看书学习的基础知识都用注释写在程序里面了

脚本用来处理一段log,log格式如下

Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: UNI-LOG|pop3|[email]wuyongguo123@sohu.com[/email]|210.51.1.80|34
Jul 30 00:00:02 192.168.95.126 LOG_UNILOG: UNI-LOG|pop3|[email]wj1952@sohu.com[/email]|125.37.172.184|24
Jul 30 00:00:02 192.168.95.125 LOG_UNILOG: UNI-LOG|pop3|[email]xgwang@sohu.com[/email]|116.15.32.105|37
Jul 30 00:00:02 192.168.95.126 LOG_UNILOG: UNI-LOG|pop3|[email]sdg521837@sohu.com[/email]|159.226.8.165|37
Jul 30 00:00:02 10.10.71.21 LOG_UNILOG: UNI-LOG|pop3|[email]zhouquanjun0196@sohu.com[/email]|59.40.168.63|43
Jul 30 00:00:02 10.10.71.19 LOG_UNILOG: UNI-LOG|pop3|[email]michael_d@sohu.com[/email]|117.93.12.185|30
Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: UNI-LOG|pop3|[email]wbg-79315@sohu.com[/email]|210.51.1.80|39
Jul 30 00:00:02 10.10.71.19 LOG_UNILOG: UNI-LOG|pop3|[email]ww12872@sohu.com[/email]|218.206.212.236|50
Jul 30 00:00:02 192.168.95.127 LOG_UNILOG: hans7281@sohu.com[/email]|210.51.1.80|46]UNI-LOG|pop3|[email]hans7281@sohu.com[/email]|210.51.1.80|46

脚本作用。统计活动用户数。活动的桶数。

统计结果如下

[@20.85 dailysummary]# /root/unilog_d.pl u.1
总活动人次:9
活动用户数:9占总用户96728766的9.30436763764773e-06%
涉及到桶数:8占总桶数89的8.98876404494382%
------------------------
桶号 活动人次 活动用户数 总数 活动比率
24 1 1 1379631
7.24831494798247e-05%
30 1 1 50160
0.00199362041467305%
34 1 1 2761982
3.62058840354499e-05%
37 2 2 91151
0.00219416133668309%
39 1 1 680190
0.000147017745041827%
43 1 1 3455293
2.89411057180968e-05%
46 1 1 3838615
2.60510626879747e-05%
50 1 1 1750166
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  职场 脚本 休闲 perl