@@ -24,14 +24,38 @@ def metric_records(router_entry, *, metric_labels = None):
2424 if metric_labels is None :
2525 metric_labels = []
2626 try :
27- res = router_entry .api_connection .router_api ().get_resource ('/ip/firewall/connection/' ).call ('print' , {'count-only' : '' })
28- # result processing as described at: https://github.com/socialwifi/RouterOS-api/issues/79#issuecomment-2089744809
27+ api = router_entry .api_connection .router_api ()
28+ ipv4_cnt_str = '0'
29+ ipv6_cnt_str = '0'
30+
31+ res = api .get_resource ('/ip/firewall/connection/' ).call ('print' , {'count-only' : '' })
2932 cnt_str = res .done_message .get ('ret' )
33+ if cnt_str is not None :
34+ ipv4_cnt_str = cnt_str
35+
36+ try :
37+ res_v6 = api .get_resource ('/ipv6/firewall/connection/' ).call ('print' , {'count-only' : '' })
38+ cnt_str_v6 = res_v6 .done_message .get ('ret' )
39+ if cnt_str_v6 is not None :
40+ ipv6_cnt_str = cnt_str_v6
41+ except Exception :
42+ pass
43+
44+ try :
45+ ipv4_count = int (ipv4_cnt_str )
46+ except (ValueError , TypeError ):
47+ ipv4_count = 0
48+
3049 try :
31- count = int (cnt_str )
50+ ipv6_count = int (ipv6_cnt_str )
3251 except (ValueError , TypeError ):
33- cnt_str = '0'
34- records = [{'count' : cnt_str }]
52+ ipv6_count = 0
53+
54+ records = [{
55+ 'count' : str (ipv4_count + ipv6_count ),
56+ 'ipv4_count' : str (ipv4_count ),
57+ 'ipv6_count' : str (ipv6_count )
58+ }]
3559 return BaseDSProcessor .trimmed_records (router_entry , router_records = records , metric_labels = metric_labels )
3660 except Exception as exc :
3761 print (f'Error getting IP connection info from router { router_entry .router_name } @{ router_entry .config_entry .hostname } : { exc } ' )
@@ -41,6 +65,15 @@ def metric_records(router_entry, *, metric_labels = None):
4165class IPConnectionStatsDatasource :
4266 ''' IP connections stats data provider
4367 '''
68+ @staticmethod
69+ def strip_port (address ):
70+ if address .startswith ('[' ):
71+ return address [1 :address .find (']' )]
72+ colons = address .count (':' )
73+ if colons == 1 :
74+ return address .split (':' )[0 ]
75+ return address
76+
4477 @staticmethod
4578 def metric_records (router_entry , * , metric_labels = None , add_router_id = True ):
4679 if metric_labels is None :
@@ -56,12 +89,18 @@ def metric_records(router_entry, *, metric_labels = None, add_router_id = True):
5689 else :
5790 proplist = 'src-address'
5891
59- connection_records = router_entry .api_connection .router_api ().get_resource ('/ip/firewall/connection/' ).call ('print' , \
60- {'proplist' : proplist })
92+ api = router_entry .api_connection .router_api ()
93+ connection_records = api .get_resource ('/ip/firewall/connection/' ).call ('print' , {'proplist' : proplist })
94+ try :
95+ connection_records_v6 = api .get_resource ('/ipv6/firewall/connection/' ).call ('print' , {'proplist' : proplist })
96+ connection_records .extend (connection_records_v6 )
97+ except Exception :
98+ pass
99+
61100 # calculate number of connections per src-address
62101 connections_per_src_address = {}
63102 for connection_record in connection_records :
64- address = connection_record [ 'src-address' ]. split ( ':' )[ 0 ]
103+ address = IPConnectionStatsDatasource . strip_port ( connection_record . get ( 'src-address' , '' ))
65104
66105 count , destinations = 0 , set ()
67106 if connections_per_src_address .get (address ):
0 commit comments