-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathipsec_traffic.py
executable file
·105 lines (90 loc) · 3.81 KB
/
ipsec_traffic.py
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python
###############################################################################
# ipsec_traffic.py
###############################################################################
# Collects Libreswan IPsec traffic information using ipsec cli
# The result is reported in Bytes per IPsec connection
# Script arguments (not mandatory) to be used:
# -h, --help show this help message and exit
# -a ADDRESS, -address ADDRESS, --address ADDRESS
# IPsec Traffic Metrics are exposed on this IP address (default = 0.0.0.0)
# -p PORT, -port PORT, --port PORT
# IPsec Traffic Metrics are exposed on this port (default = 9754)
# -i INTERVAL, -interval INTERVAL, --interval INTERVAL
# IPsec Traffic Metrics read interval in seconds (default = 15)
###############################################################################
import prometheus_client as prom
import os
import time
import argparse as ap
# exporter default port
exporter_port = 9754
# default interval in seconds for generating metrics
scrape_interval = 15
# default IP address is 0.0.0.0
listen_address = '0.0.0.0'
# get command line arguments
parser = ap.ArgumentParser(description='IPsec Traffic Exporter arguments')
parser.add_argument('-a', '-address', '--address', dest='address', required=False,
help='IPsec Traffic Metrics are exposed on this IP address')
parser.add_argument('-p', '-port', '--port', dest='port', required=False, type=int,
help='IPsec Traffic Metrics are exposed on this port')
parser.add_argument('-i', '-interval', '--interval', dest='interval', required=False, type=int,
help='IPsec Traffic Metrics read interval in seconds')
args = parser.parse_args()
if args.address is not None:
listen_address = args.address
if args.port is not None:
exporter_port = args.port
if args.interval is not None:
scrape_interval = args.interval
def get_ipsec_info(cmd):
output = os.popen(cmd).read()
lines = output.split('\n')
return lines
def main():
gauge = prom.Gauge(
'ipsec_traffic',
'Display IPsec Traffic Info',
['connection', 'left_subnet', 'right_subnet', 'direction']
)
prom.start_http_server(exporter_port, addr=listen_address)
while True:
connections = {}
traffic_list = get_ipsec_info("sudo ipsec trafficstatus")
if len(traffic_list[-1]) == 0:
del traffic_list[-1]
for line in traffic_list:
connection = line.split('"')[1]
tmp = line.split(',')
in_bytes = (tmp[3]).split('=')[1]
out_bytes = (tmp[4]).split('=')[1]
connections[connection] = {"in": in_bytes, "out": out_bytes}
connection_list = get_ipsec_info("sudo ipsec status|grep '; eroute owner:'")
if len(connection_list[-1]) == 0:
del connection_list[-1]
for line in connection_list:
connection = line.split('"')[1]
tmp = line.split('=')
left_subnet = tmp[0].split(' ')[-1]
right_subnet = tmp[-1].split(';')[0]
if connection in connections:
connections[connection]["left_subnet"] = left_subnet
connections[connection]["right_subnet"] = right_subnet
gauge.clear()
for i in connections.keys():
gauge.labels(
i,
connections[i]['left_subnet'],
connections[i]['right_subnet'],
'in'
).set(connections[i]['in'])
gauge.labels(
i,
connections[i]['left_subnet'],
connections[i]['right_subnet'],
'out'
).set(connections[i]['out'])
time.sleep(scrape_interval)
if __name__ == '__main__':
main()