-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrpm_twamp_snmp.py
executable file
·187 lines (143 loc) · 6.11 KB
/
rpm_twamp_snmp.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#! /usr/bin/env python3
from pysnmp.hlapi import *
import time
from time import sleep
import sys
from influxdb import InfluxDBClient
from influx_client import *
from testbed import *
import logging
import threading
from time import sleep,strftime,strptime,gmtime,mktime
if len(sys.argv) < 8:
print(
"Usage python3 snmp_juniper_v2.py <source_ip> <destination_ip> <agent_type> <Community> <Owner> <test_name> <sleep_time_between_measurements>")
sys.exit()
else:
logging.basicConfig(level=logging.INFO, format=' %(asctime)s - %(levelname)s - %(message)s')
#sys.argv = [sys.argv[0], '193.63.63.43', '193.63.63.44', 'TWAMP', 'public', 'c32', 't32', '60']
def dateToString(myDate):
year = str((myDate[0] * 256 + myDate[1])) + '-' + str(myDate[2]) + '-' + str(myDate[3]) + ', '
hour = str(myDate[4]) + ':' + str(myDate[5]) + ':' + str(myDate[6])
return year + hour
# Gloval varialbles
results = {}
# OID for juniper devices
jnxTwampResSumDate = '1.3.6.1.4.1.2636.3.77.1.1.2.1.5'
jnxRpmResSumDate = '1.3.6.1.4.1.2636.3.50.1.2.1.5'
jnxTwampClientResultsCalculatedEntry = '1.3.6.1.4.1.2636.3.77.1.1.3.1'
jnxRpmResultsCalculatedEntry = '1.3.6.1.4.1.2636.3.50.1.3.1'
# Table indexes:
# Owner (ASCII)
# TestName (ASCII)
# SumCollection: 1 - currentTest, 2 - lastCompletedTest, 3 - movingAverage, 4 -allTests
# CalSet: 1 - roundTripTime, 4 - egress delay, 7 - ingress delay
Samples = '.2'
Min = '.3'
Max = '.4'
Average = '.5'
PktoPk = '.6'
StdDev = '.7'
Sum = '.8'
Statistics = [Samples, Min, Max, Average, PktoPk, StdDev]
StatisticsNames = ['Samples', 'Min', 'Max', 'Average', 'PktoPk', 'StdDev']
# source ip -> sys.argv[1]
# destination ip -> sys.argv[2]
# agent_type -> sys.argv[3]
# Community -> sys.argv[4]
# Owner (RPM) or control connection (TWAMP) -> sys.argv[5]
# test_name (RPM) or test connection(TWAMP) -> sys.argv[6]
# sleep time between measurements -> sys.argv[7]
source_ip = sys.argv[1]
destination_ip = sys.argv[2]
agent_type = sys.argv[3]
Community = sys.argv[4]
Owner = sys.argv[5]
TestName = sys.argv[6]
sleep_time = sys.argv[7]
if agent_type == 'RPM':
SumDate = jnxRpmResSumDate
CalculatedEntry = jnxRpmResultsCalculatedEntry
elif agent_type == 'TWAMP':
SumDate = jnxTwampResSumDate
CalculatedEntry = jnxTwampClientResultsCalculatedEntry
else:
print('agent_type specified as ' + agent_type + ' but it should be RPM or TWAMP')
sys.exit()
# constructing ASCII codes of Owner and TestName in Name
Name = '.' + str(len(Owner)) + '.'
for i in range(len(Owner)):
Name = Name + str(ord(Owner[i])) + '.'
Name = Name + str(len(TestName)) + '.'
for i in range(len(TestName)):
Name = Name + str(ord(TestName[i])) + '.'
while True:
# Obtaining date
abc = ObjectIdentifier(SumDate + Name + '2')
# print(abc)
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(Community, mpModel=0),
UdpTransportTarget((source_ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(abc)))
)
if errorIndication:
print(errorIndication)
elif errorStatus:
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
varBind = varBinds[0]
SampleDate = dateToString(varBind[1])
print()
print('TestDate: ' + SampleDate)
# adding the last two indexes
# SumCollection = 2 lastCompletedTest
# CalSet = 4 Egress delay, 7 Ingress delay
metrics = {'roundTripTime': 1, 'posRttJitter': 2, 'negRttJitter': 3, 'egress': 4, 'posEgressJitter': 5,
'negEgresslJitter': 6, 'ingress': 7, 'posIngressJitter': 8, 'negIngressJitter': 9}
# Obtaining Statistics
print ()
print ("Obtaining " + agent_type + " statistics")
for k, v in metrics.items():
i = 0
print ()
print ("Obtaining " + " " + agent_type + " " + str(k) + " statistics")
oid = Name + '2.' + str(v)
for s in Statistics:
abc = ObjectIdentifier(CalculatedEntry + s + oid)
errorIndication, errorStatus, errorIndex, varBinds = next(
getCmd(SnmpEngine(),
CommunityData(Community, mpModel=0),
UdpTransportTarget((source_ip, 161)),
ContextData(),
ObjectType(ObjectIdentity(abc)))
)
if errorIndication:
print('Error Indication' + errorIndication)
elif errorStatus:
print ('Error Status')
print('%s at %s' % (errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex) - 1][0] or '?'))
else:
varBind = varBinds[0]
value = varBind[1]
print(k + StatisticsNames[i] + ' = ', str(value))
if StatisticsNames[i] == 'Samples':
results[k + StatisticsNames[i]] = str(value)
else:
results[k + StatisticsNames[i]] = str(value/1000.0)
i = i + 1
SampleDate = strftime("%Y-%m-%d %H:%M:%S",
gmtime(mktime(strptime(SampleDate,
"%Y-%m-%d, %H:%M:%S"))))
json_body = [{
"measurement": agent_type,
"tags": {"src_ip": source_ip, "dst_ip": destination_ip},
"time": SampleDate,
"fields": results}]
print('json body:')
print(json_body)
write_to_influx(json_body)
sleep(int(sleep_time))