@@ -14,31 +14,31 @@ def gather_tables():
1414
1515 for ip_proto in ["iptables" , "ip6tables" ]:
1616 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' )
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' )
2828
2929 if re_blankline .match (str (line )):
30- continue
30+ continue
3131
3232 line_pieces = line .split ()
3333
3434 # Check if line is the beginning of a chain
3535 if re_chain .match (str (line_pieces [0 ])):
36- l_chain_name = line_pieces [1 ]
37- continue
36+ l_chain_name = line_pieces [1 ]
37+ continue
3838
3939 # Check if the line is the header for the given chain
4040 if re_header .match (str (line_pieces [0 ])):
41- continue
41+ continue
4242
4343 l_line_number = line_pieces [0 ]
4444 l_packets = line_pieces [1 ]
@@ -52,16 +52,27 @@ def gather_tables():
5252 l_options = ' ' .join (line_pieces [10 :]).replace ('"' ,'\\ "' )
5353
5454 # 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+ iptables_packet_lines .append (
56+ '%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' %
57+ (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 )
58+ )
59+ iptables_byte_lines .append (
60+ '%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' %
61+ (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 )
62+ )
5763
5864 packet_lines_helper = f'# HELP { ip_proto } _packets_total packet counters for { ip_proto } rules.'
5965 packet_lines_type = f'# TYPE { ip_proto } _packets_total counter'
6066
6167 byte_lines_helper = f'# HELP { ip_proto } _bytes_total byte counters for { ip_proto } rules.'
6268 byte_lines_type = f'# TYPE { ip_proto } _bytes_total counter'
6369
64- return f"{ packet_lines_helper } \n { packet_lines_type } \n { '\n ' .join (iptables_packet_lines )} \n { byte_lines_helper } \n { byte_lines_type } \n { '\n ' .join (iptables_byte_lines )} "
70+ return (f"{ packet_lines_helper } \n "
71+ f"{ packet_lines_type } \n "
72+ f"{ '\n ' .join (iptables_packet_lines )} \n "
73+ f"{ byte_lines_helper } \n "
74+ f"{ byte_lines_type } \n "
75+ f"{ '\n ' .join (iptables_byte_lines )} " )
6576
6677if __name__ == "__main__" :
6778 print (gather_tables ())
0 commit comments