-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtelemetry_client_udp_json.py
148 lines (119 loc) · 4.58 KB
/
telemetry_client_udp_json.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
'''
b'\x00\x01\x00\x02\x00\x01\x00\x00\x00\x00\x01Q
{"node_id_str":"A9KV",
"subscription_id_str":"health",
"encoding_path":"Cisco-IOS-XR-shellutil-oper:system-time/uptime",
"collection_id":46,
"collection_start_time":1547142057043,
"msg_timestamp":1547142057060,
"data_json":[{"timestamp":1547142057056,
"keys":{},
"content":{"hostname":"A9KV",
"uptime":1648
}
}
],
"collection_end_time":1547142057060}
'
b'\x00\x01\x00\x02\x00\x01\x00\x00\x00\x00\x03y
{"node_id_str":"A9KV",
"subscription_id_str":"health",
"encoding_path":"Cisco-IOS-XR-nto-misc-oper:memory-summary/nodes/node/summary",
"collection_id":33,
"collection_start_time":1547141907315,
"msg_timestamp":1547141907332,
"data_json":[{"timestamp":1547141907331,
"keys":{"node-name":"0/RP0/CPU0"},
"content":{"page-size":4096,
"ram-memory":15032385536,
"free-physical-memory":10592419840,
"system-ram-memory":15032385536,
"free-application-memory":11047075840,
"image-memory":4194304,
"boot-ram-size":0,
"reserved-memory":0,
"io-memory":0,
"flash-system":0
}
},
{"timestamp":1547141907338,
"keys":{"node-name":"0/0/CPU0"},
"content":{"page-size":4096,
"ram-memory":8589934592,
"free-physical-memory":5439266816,
"system-ram-memory":8589934592,
"free-application-memory":5439307776,
"image-memory":4194304,
"boot-ram-size":0,
"reserved-memory":0,
"io-memory":0,
"flash-system":0}
}
],
"collection_end_time":1547141907339
}
'
'''
import socket, struct
import json
import time
import pprint
from socket import inet_ntoa
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('0.0.0.0', 57500))
End = '''}\''''
Start = '''b\''''
Left = '''{'''
bug_left = '''{{'''
miss_start = 0
miss_end = 0
miss_left = 0
count = 0
start_time = time.time()
while True:
whole_buf = []
data=''
while True:
count += 1
buf, addr = sock.recvfrom(65535)
print(buf.hex())
print("Message Length {}".format(len(buf)))
#print(buf)
text_buf = str(buf)
#workaround, cos find a9kv telemetry random add more { in message , if find {{ just move right 1 position
if bug_left in text_buf:
json_buff = json.loads(text_buf[text_buf.find('{')+1:-1])
#print('bug')
else:
json_buff = json.loads(text_buf[text_buf.find('{'):-1])
#print('no bug')
print(json_buff)
pprint.pprint(json_buff)
tele_node_id = json_buff['node_id_str']
tele_path = json_buff['encoding_path']
tele_collection_id = str(json_buff['collection_id'])
#tele_data = json_buff['data_json'][0]['content']
tele_data = json_buff['data'] #
if tele_path == 'Cisco-IOS-XR-wdsysmon-fd-oper:system-monitoring/cpu-utilization':
print(addr)
print('This is a CPU utilization telemetry messgae')
print('Node =' + tele_node_id)
print('Encoding Path ='+tele_path)
print('Collection ID =' + tele_collection_id)
print('CPU 1 Min ='+ str(tele_data['total-cpu-one-minute']))
print('CPU 5 Min ='+ str(tele_data['total-cpu-five-minute']))
print('CPU 15 Min ='+ str(tele_data['total-cpu-fifteen-minute']))
print('Recevice Count =' + str(count))
print('Timestamp from Start ='+str(time.time() - start_time))
print('=' * 200)
if tele_path == 'Cisco-IOS-XR-shellutil-oper:system-time/uptime':
print(addr)
print('This is a System Uptime telemetry messgae')
print('Node =' + tele_node_id)
print('Encoding Path =' + tele_path)
print('Collection ID =' + tele_collection_id)
print('Host Name =' + str(tele_data['hostname']))
print('Uptime =' + str(tele_data['uptime']))
print('Recevice Count =' + str(count))
print('Timestamp from Start =' + str(time.time() - start_time))
print('=' * 200)