requirement:
Please help to write the following program:
1. the script will be used in email aliase
2. usage in aliase file:
bigeye: "|
efc -f 10"
notes: -f is a parameter to specify the frequence of the email threshold, if email flow recorded, count/(update_time - create_time)sec >= 10email/sec for same email, it will drop the same email without any output.
3. input via stdin and stdout as output
4. it will use mysql as backend to store email statistic
5. each email in, the efc program will generate a md5sum and store in database.
6. if email's check sum already exist in database, it will update that entries, email count and update the email update time.
7. if the email is first time appear, it will create the entries.
8. table schema:
md5sum, create time, update time, count
programme:
#!/usr/bin/perl
use DBI; use Date::Manip; $database='efc'; $localhost='localhost'; $localdbuser='root'; $localdbpass=''; $table='filter';
my $dbh=DBI->connect("DBI:mysql:database=$database;host=$localhost","$localdbuser","$localdbpass",{'RaiseError'=>1, 'AutoCommit' => 0}) or die "cannot connect!\n";
$count=1;
#$threshold=10;
$threshold=$ARGV[0]; $create_time=&time;
sub time{ @day=localtime(time()); $mon=$day[4]+1; $year="2".$day[5]-100; $date=$year."-".$mon."-".$day[3]." ".$day[2].":".$day[1].":".$day[0]; }
$mail='root@localhost';
$file=<STDIN>; open(FILE,">file") or die "can't open file :$!"; while (<STDIN>){ print FILE $_; }
`md5sum file>mail.md5`;
$sth=$dbh->prepare("select * from $table"); $sth->execute();
open(YMD,"mail.md5") or die "can't open file.md5:$!\n"; foreach $ymd(<YMD>){ while(@row=$sth->fetchrow_array) { $md5sum=$row[0]; if ($ymd=$md5sum){ $count++; $create_time=$row[1]; } else {$create_time=&time;} }
#count>1
if($count>1) {
$update_time=&time(); } #count=1
else{
$update_time=$create_time; } $dbh->do(qq/insert into $table values("$ymd","$create_time","$update_time","$count")/); } #print "count:$count\n";
if($$create_time){
$delta=DateCalc($create_time,$update_time,\$err,1); @fi=split /:/ ,$delta; $dtime=(((($fi[0]*365+$fi[1])*30+$fi[3])*24+$fi[4])*60+$fi[5])*60+$fi[6]; print "dtime:$dtime\n";
$fre=$count/$dtime; #print "fre:$fre\n";
}
if ($fre>10){ print 'exit'."\n"; exit (0); } else{ #print "check ok\n";
open(CHECK,file) or die; foreach(<CHECK>){ print; } } close FILE; close YMD;
$sth->finish(); $dbh->disconnect();
|
阅读(338) | 评论(0) | 转发(0) |