-
Notifications
You must be signed in to change notification settings - Fork 2
/
p4_counter.py
executable file
·43 lines (38 loc) · 1.53 KB
/
p4_counter.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from p4_runtimes import *
# 读取交换机的计数器
def read_counter(p4info_helper, sw):
print("\n----- 正在读取 {} 的计数器 -----".format(sw.name))
for response in sw.ReadCounters():
counters = []
for entity in response.entities:
counter = entity.counter_entry
counter_entries = {}
counter_name = p4info_helper.get_counters_name(counter.counter_id)
index = counter.index.index
packet_count = counter.data.packet_count
byte_count = counter.data.byte_count
print("{} {}: {} packets ({} bytes)".format(counter_name, index,
packet_count, byte_count))
counter_entries.update({"port":index})
counter_entries.update({"packets":packet_count})
counter_entries.update({"bytes":byte_count})
counters.append(counter_entries)
print("----- {} 的计数器读取完毕 -----".format(sw.name))
return counters
def read_counters(p4info_helper, sws):
sws_counters = {}
for sw in sws:
# 读取交换机计数器
counters = read_counter(p4info_helper=p4info_helper, sw=sw)
sws_counters.update({sw.name:counters})
return sws_counters
if __name__ == "__main__":
args = check_parser()
try:
p4info_helper, sws = sw_connection(args.p4info)
read_counters(p4info_helper, sws)
except grpc.RpcError as e:
printGrpcError(e)
shut_down()