-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapproute_statistics_api.py
175 lines (145 loc) · 6.74 KB
/
approute_statistics_api.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
from influxdb import InfluxDBClient
import requests
import sys
import json
import time
import yaml
requests.packages.urllib3.disable_warnings()
from requests.packages.urllib3.exceptions import InsecureRequestWarning
class Authentication:
@staticmethod
def get_jsessionid(vmanage_host, vmanage_port, username, password):
api = "/j_security_check"
base_url = "https://%s:%s"%(vmanage_host, vmanage_port)
url = base_url + api
payload = {'j_username' : username, 'j_password' : password}
response = requests.post(url=url, data=payload, verify=False)
try:
cookies = response.headers["Set-Cookie"]
jsessionid = cookies.split(";")
return(jsessionid[0])
except:
print("\nNo valid JSESSION ID returned")
exit()
@staticmethod
def get_token(vmanage_host, vmanage_port, jsessionid):
headers = {'Cookie': jsessionid}
base_url = "https://%s:%s"%(vmanage_host, vmanage_port)
api = "/dataservice/client/token"
url = base_url + api
response = requests.get(url=url, headers=headers, verify=False)
if response.status_code == 200:
return(response.text)
else:
return None
if __name__ == '__main__':
try:
with open("vmanage_login.yaml") as f:
config = yaml.safe_load(f.read())
vmanage_host = config["vmanage_host"]
vmanage_port = config["vmanage_port"]
username = config["vmanage_username"]
password = config["vmanage_password"]
Auth = Authentication()
jsessionid = Auth.get_jsessionid(vmanage_host,vmanage_port,username,password)
token = Auth.get_token(vmanage_host,vmanage_port,jsessionid)
if token is not None:
headers = {'Content-Type': "application/json",'Cookie': jsessionid, 'X-XSRF-TOKEN': token}
else:
headers = {'Content-Type': "application/json",'Cookie': jsessionid}
base_url = "https://%s:%s/dataservice"%(vmanage_host,vmanage_port)
# Get app route statistics for tunnels between Hub routers and Spoke routers.
series = []
total_records = 0
for hub in config["hub_routers"]:
api_url = "/statistics/approute/fec/aggregation"
payload = {
"query": {
"condition": "AND",
"rules": [
{
"value": [
"24"
],
"field": "entry_time",
"type": "date",
"operator": "last_n_hours"
},
{
"value": [
hub["system_ip"]
],
"field": "local_system_ip",
"type": "string",
"operator": "in"
}
]
},
"aggregation": {
"field": [
{
"property": "name",
"sequence": 1,
"size": 6000
},
{
"property": "proto",
"sequence": 2
}
],
"histogram": {
"property": "entry_time",
"type": "minute",
"interval": 30,
"order": "asc"
},
"metrics": [
{
"property": "latency",
"type": "avg"
},
{
"property": "jitter",
"type": "avg"
},
{
"property": "loss_percentage",
"type": "avg"
}
]
}
}
url = base_url + api_url
response = requests.post(url=url, headers=headers, data=json.dumps(payload), verify=False)
# loop over the API response variable items and create records to be stored in InfluxDB
if response.status_code == 200:
app_route_stats = response.json()["data"]
for item in app_route_stats:
temp = {
"measurement": "approute_stats",
"tags": {
"Tunnel": item['name'] + "(" + item['proto'] + ")",
},
"time": time.strftime('%m/%d/%Y %H:%M:%S', time.gmtime(item['entry_time']/1000.)),
"fields": {
"latency": float(item['latency']),
"loss": float(item['loss_percentage']),
"jitter": float(item['jitter'])
}
}
series.append(temp)
total_records = total_records+1
else:
print("Failed to retrieve app route statistics\n")
# login credentials for InfluxDB
USER = 'root'
PASSWORD = 'root'
DBNAME = 'approute_stats'
host='localhost'
port=8086
client = InfluxDBClient(host, port, USER, PASSWORD, DBNAME)
client.write_points(series)
time.sleep(2)
print("Stored %s records in influxdb"%total_records)
except Exception as e:
print('Exception line number: {}'.format(sys.exc_info()[-1].tb_lineno), type(e).__name__, e)