Skip to content

Commit cd209f8

Browse files
committed
Wrap in main function
Signed-off-by: Monica Hanson <[email protected]>
1 parent fcdfe20 commit cd209f8

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

iptables.py

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,63 +4,67 @@
44
import re
55
import os
66

7-
tables = ['filter', 'nat', 'mangle', 'raw']
8-
re_chain = re.compile('^Chain')
9-
re_header = re.compile('^num')
10-
re_blankline = re.compile('^(?:^ *\n)$')
7+
def gather_tables():
8+
tables = ['filter', 'nat', 'mangle', 'raw']
9+
re_chain = re.compile('^Chain')
10+
re_header = re.compile('^num')
11+
re_blankline = re.compile('^(?:^ *\n)$')
1112

12-
iptables_packet_lines = []
13-
iptables_byte_lines = []
13+
iptables_packet_lines = []
14+
iptables_byte_lines = []
1415

15-
for ip_proto in ["iptables", "ip6tables"]:
16-
for table in tables:
17-
# Run iptables with the following options:
18-
# -L: Listing all rules for chain
19-
# -n: Numeric lookup
20-
# -v: Verbose output
21-
# -x: Exact values
22-
# -t table: Specified table table
23-
# --line-numbers: Show line numbers
24-
cmd = [f'/sbin/{ip_proto}', '-L', '-n', '-v', '-x', '-t', table, "--line-numbers"]
25-
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
26-
for line in proc.stdout.readlines():
27-
line = line.decode('utf8')
16+
for ip_proto in ["iptables", "ip6tables"]:
17+
for table in tables:
18+
# Run iptables with the following options:
19+
# -L: Listing all rules for chain
20+
# -n: Numeric lookup
21+
# -v: Verbose output
22+
# -x: Exact values
23+
# -t table: Specified table table
24+
# --line-numbers: Show line numbers
25+
cmd = [f'/sbin/{ip_proto}', '-L', '-n', '-v', '-x', '-t', table, "--line-numbers"]
26+
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
27+
for line in proc.stdout.readlines():
28+
line = line.decode('utf8')
2829

29-
if re_blankline.match(str(line)):
30-
continue
30+
if re_blankline.match(str(line)):
31+
continue
3132

32-
line_pieces = line.split()
33+
line_pieces = line.split()
3334

34-
# Check if line is the beginning of a chain
35-
if re_chain.match(str(line_pieces[0])):
36-
l_chain_name = line_pieces[1]
37-
continue
35+
# Check if line is the beginning of a chain
36+
if re_chain.match(str(line_pieces[0])):
37+
l_chain_name = line_pieces[1]
38+
continue
3839

39-
# Check if the line is the header for the given chain
40-
if re_header.match(str(line_pieces[0])):
41-
continue
40+
# Check if the line is the header for the given chain
41+
if re_header.match(str(line_pieces[0])):
42+
continue
4243

43-
l_line_number = line_pieces[0]
44-
l_packets = line_pieces[1]
45-
l_bytes = line_pieces[2]
46-
l_target = line_pieces[3]
47-
l_prot = line_pieces[4]
48-
l_in = line_pieces[6]
49-
l_out = line_pieces[7]
50-
l_src = line_pieces[8]
51-
l_dest = line_pieces[9]
52-
l_options = ' '.join(line_pieces[10:]).replace('"','\\"')
44+
l_line_number = line_pieces[0]
45+
l_packets = line_pieces[1]
46+
l_bytes = line_pieces[2]
47+
l_target = line_pieces[3]
48+
l_prot = line_pieces[4]
49+
l_in = line_pieces[6]
50+
l_out = line_pieces[7]
51+
l_src = line_pieces[8]
52+
l_dest = line_pieces[9]
53+
l_options = ' '.join(line_pieces[10:]).replace('"','\\"')
5354

54-
# To the best of my knowledge, this can't be an fstring
55-
iptables_packet_lines.append('%s_packets_total{table="%s",chain="%s",line_number=%s,target="%s",prot="%s",in="%s",out="%s",src="%s",dest="%s",opt="%s"} %s' % (ip_proto,table,l_chain_name,l_line_number,l_target,l_prot,l_in,l_out,l_src,l_dest,l_options,l_packets))
56-
iptables_byte_lines.append('%s_bytes_total{table="%s",chain="%s",line_number=%s,target="%s",prot="%s",in="%s",out="%s",src="%s",dest="%s",opt="%s"} %s' % (ip_proto,table,l_chain_name,l_line_number,l_target,l_prot,l_in,l_out,l_src,l_dest,l_options,l_bytes))
55+
# To the best of my knowledge, this can't be an fstring
56+
iptables_packet_lines.append('%s_packets_total{table="%s",chain="%s",line_number=%s,target="%s",prot="%s",in="%s",out="%s",src="%s",dest="%s",opt="%s"} %s' % (ip_proto,table,l_chain_name,l_line_number,l_target,l_prot,l_in,l_out,l_src,l_dest,l_options,l_packets))
57+
iptables_byte_lines.append('%s_bytes_total{table="%s",chain="%s",line_number=%s,target="%s",prot="%s",in="%s",out="%s",src="%s",dest="%s",opt="%s"} %s' % (ip_proto,table,l_chain_name,l_line_number,l_target,l_prot,l_in,l_out,l_src,l_dest,l_options,l_bytes))
5758

58-
print(f'# HELP {ip_proto}_packets_total packet counters for {ip_proto} rules.')
59-
print(f'# TYPE {ip_proto}_packets_total counter')
60-
for line in iptables_packet_lines:
61-
print(line)
59+
print(f'# HELP {ip_proto}_packets_total packet counters for {ip_proto} rules.')
60+
print(f'# TYPE {ip_proto}_packets_total counter')
61+
for line in iptables_packet_lines:
62+
print(line)
6263

63-
print(f'# HELP {ip_proto}_bytes_total byte counters for {ip_proto} rules.')
64-
print(f'# TYPE {ip_proto}_bytes_total counter')
65-
for line in iptables_byte_lines:
66-
print(line)
64+
print(f'# HELP {ip_proto}_bytes_total byte counters for {ip_proto} rules.')
65+
print(f'# TYPE {ip_proto}_bytes_total counter')
66+
for line in iptables_byte_lines:
67+
print(line)
68+
69+
if __name__ == "__main__":
70+
gather_tables()

0 commit comments

Comments
 (0)