Skip to content

Commit 8776717

Browse files
committed
add example for log filter
1 parent a0991ad commit 8776717

File tree

3 files changed

+196
-0
lines changed

3 files changed

+196
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
* [bash](bash)
55
* [python](python)
66
* [erlang](erlang)
7+
* [perl](perl)

perl/forbid.pl

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#!/usr/bin/env perl
2+
3+
use strict;
4+
use warnings;
5+
use utf8;
6+
7+
# cpan JSON
8+
use JSON;
9+
10+
my %forbid_record;
11+
12+
my $file = $ARGV[0];
13+
14+
sub say {print @_, "\n"}
15+
16+
sub to_str {
17+
18+
my $ans = "";
19+
my $split = "";
20+
foreach (@_) {
21+
$ans .= $split .= $_;
22+
$split = ":";
23+
}
24+
25+
return $ans;
26+
}
27+
28+
sub filter_forbid {
29+
my ($key, $endtime) = @_;
30+
31+
if ( exists($forbid_record{$key}) ) {
32+
if ($endtime == 0) {
33+
delete $forbid_record{$key};
34+
} elsif ($forbid_record{$key} < $endtime) {
35+
$forbid_record{$key} = $endtime;
36+
}
37+
} else {
38+
my $cur_time = time();
39+
if ($endtime != 0 and $endtime > $cur_time ) {
40+
$forbid_record{$key} = $endtime;
41+
}
42+
}
43+
}
44+
45+
sub dump_json_file {
46+
my ($file) = @_;
47+
48+
open my $fh, ">", $file or die "Counld open $file: $!";
49+
print $fh encode_json \%forbid_record;
50+
close $fh;
51+
}
52+
53+
open my $info, $file or die "Could not open $file: $!";
54+
55+
while( my $line = <$info> ) {
56+
57+
my $domain = "";
58+
my $app = "all";
59+
my $stream = "all";
60+
my $end_time = "";
61+
62+
if ($line =~ "/stream_forbid/domain") {
63+
# filter domain
64+
if ($line =~ "domain=(?<domain>[^&]*)") {
65+
$domain = $+{domain};
66+
# filter end_time
67+
if ($line =~ "end_time=(?<end_time>\\d+)") {
68+
$end_time = $+{end_time};
69+
# handle
70+
filter_forbid(to_str($domain, $app, $stream), $end_time);
71+
}
72+
}
73+
} elsif($line =~ "/stream_forbid/app") {
74+
# filter domain
75+
if ($line =~ "domain=(?<domain>[^&]*)") {
76+
$domain = $+{domain};
77+
# filter app
78+
if ($line =~ "app=(?<app>[^&]*)") {
79+
$app = $+{app};
80+
# filter end_time
81+
if ($line =~ "end_time=(?<end_time>\\d+)") {
82+
$end_time = $+{end_time};
83+
# handle
84+
filter_forbid(to_str($domain, $app, $stream), $end_time);
85+
}
86+
}
87+
}
88+
} elsif($line =~ "/stream_forbid/stream") {
89+
# filter domain
90+
if ($line =~ "domain=(?<domain>[^&]*)") {
91+
$domain = $+{domain};
92+
# filter app
93+
if ($line =~ "app=(?<app>[^&]*)") {
94+
$app = $+{app};
95+
# filter stream
96+
if ($line =~ "stream=(?<stream>[^&]*)") {
97+
$stream = $+{stream};
98+
# filter end_time
99+
if ($line =~ "end_time=(?<end_time>\\d+)") {
100+
$end_time = $+{end_time};
101+
# handle
102+
filter_forbid(to_str($domain, $app, $stream), $end_time);
103+
}
104+
}
105+
}
106+
}
107+
} else {
108+
say $line
109+
}
110+
}
111+
112+
dump_json_file("/tmp/forbid.json");
113+
114+
close $info;

python/forbid.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python
2+
# _*_ encoding: utf-8 _*_
3+
4+
import sys
5+
import re
6+
import json
7+
import time
8+
9+
domain_forbid = {}
10+
app_forbid = {}
11+
stream_forbid = {}
12+
13+
def to_str(*val):
14+
ans = ""
15+
split = ""
16+
for v in val:
17+
ans = ans + split + str(v)
18+
split = ":"
19+
20+
return str(ans)
21+
22+
def to_number(val):
23+
return int(val)
24+
25+
def forbid_filter(dicts, key, endtime):
26+
27+
if key in dicts:
28+
if endtime == 0:
29+
dicts.pop(key)
30+
elif dicts[key] < endtime:
31+
dicts[key] = endtime
32+
else:
33+
cur_time = int(time.time())
34+
if endtime != 0 and endtime > cur_time:
35+
dicts[key] = endtime
36+
37+
38+
def write_forbid_file(name, data):
39+
40+
with open(name, 'w') as fp:
41+
json.dump(data, fp, indent=4)
42+
43+
44+
if __name__ == '__main__':
45+
46+
for line in open(sys.argv[1], 'r'):
47+
if re.findall(r'/stream_forbid/domain', line):
48+
domain = re.findall(r'domain=([^&]*)', line)
49+
end_time = re.findall(r'end_time=(\d*)', line)
50+
if end_time and domain:
51+
dm = domain[0]
52+
et = end_time[0]
53+
forbid_filter(domain_forbid, to_str(dm), to_number(et))
54+
elif re.findall(r'/stream_forbid/app', line):
55+
domain = re.findall(r'domain=([^&]*)', line)
56+
app = re.findall(r'app=([^&]*)', line)
57+
end_time = re.findall(r'end_time=(\d*)', line)
58+
if end_time and domain and app:
59+
dm = domain[0]
60+
ap = app[0]
61+
et = end_time[0]
62+
forbid_filter(app_forbid, to_str(dm, ap), to_number(et))
63+
elif re.findall(r'/stream_forbid/stream', line):
64+
domain = re.findall(r'domain=([^&]*)', line)
65+
app = re.findall(r'app=([^&]*)', line)
66+
stream = re.findall(r'stream=([^&]*)', line)
67+
end_time = re.findall(r'end_time=(\d*)', line)
68+
if end_time and domain and app and stream:
69+
dm = domain[0]
70+
ap = app[0]
71+
sm = stream[0]
72+
et = end_time[0]
73+
forbid_filter(stream_forbid, to_str(dm, ap, sm), to_number(et))
74+
else:
75+
print(line)
76+
77+
78+
write_forbid_file("domain_forbid.json", domain_forbid)
79+
write_forbid_file("app_forbid.json", app_forbid)
80+
write_forbid_file("stream_forbid.json", stream_forbid)
81+

0 commit comments

Comments
 (0)