-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiptables-stats.awk
executable file
·89 lines (74 loc) · 2.04 KB
/
iptables-stats.awk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/awk -f
function dump_table(name, filename)
{
cmd = "echo :" name " >> " filename
cmd | getline
cmd = "iptables -S -v -t " name " >> " filename
cmd | getline
}
function add_arg(filename)
{
ARGV[ARGC++] = filename
}
BEGIN {
STATS_F = "/tmp/iptables-stats.txt"
INDICIES_F = "/tmp/iptables-stats-indicies.txt"
SORTED_F = "/tmp/iptables-stats-sorted.txt"
# Start from clean slate
print "" > INDICIES_F
print "" > STATS_F
print "" > SORTED_F
dump_table("raw", STATS_F)
dump_table("nat", STATS_F)
dump_table("mangle", STATS_F)
dump_table("filter", STATS_F)
line = 0
last_table = ""
add_arg(STATS_F)
add_arg(INDICIES_F)
add_arg(SORTED_F)
printf "------------|------------|----------|----------------------------\n"
printf " Packets | Bytes | Table | Rule\n"
printf "------------|------------|----------|----------------------------\n"
}
{
if (FILENAME == STATS_F) {
line++
pkts[line] = 0
bytes[line] = 0
rule[line] = ""
table[line] = last_table
if ($1 ~ /^:.*/) {
last_table = substr($1, 2)
next
}
for (i = 1; i <= NF; i++) {
if ($(i) == "-c") {
pkts[line] = $(i + 1)
bytes[line] = $(i + 2)
i += 2
} else {
if (rule[line] != "") {
rule[line] = rule[line] " " $i
} else {
rule[line] = $i
}
}
}
if (pkts[line] < 1 || bytes[line] < 1) {
next
}
cmd = "echo " line " " pkts[line] " " bytes[line] " >> " INDICIES_F
cmd | getline
}
if (FILENAME == INDICIES_F) {
cmd = "sort -r -n -k2,3 " INDICIES_F " -o " SORTED_F
cmd | getline
nextfile
}
if (FILENAME == SORTED_F) {
if ($1 != "") {
printf " %10d | %10d | %-8s | %s\n", pkts[$1], bytes[$1], table[$1], rule[$1]
}
}
}