-
Notifications
You must be signed in to change notification settings - Fork 3
/
get_network_health.py
68 lines (57 loc) · 1.91 KB
/
get_network_health.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
import time
from distutils.util import strtobool
from typing import Any, AnyStr, Dict, List
import requests
from requests.exceptions import ConnectionError, HTTPError
from requests.packages import urllib3
from termcolor import colored, cprint
from urllib3.exceptions import InsecureRequestWarning
# Disable SSL warnings. Not needed in production environments with valid certificates
# (REMOVE if you are not sure of its purpose)
urllib3.disable_warnings(category=InsecureRequestWarning)
def get_network_health(
token: AnyStr, ENV: Dict[AnyStr, Any]
) -> List[Dict[AnyStr, Any]]:
"""Gets network health information
Parameters
----------
token : AnyStr
Auth token
ENV : Dict[AnyStr, Any]
Environment variables
Returns
-------
List[Dict[AnyStr, Any]]
Health of network devices
Raises
------
SystemExit
ConnectionError, HTTPError
SystemExit
KeyboardInterrupt
"""
headers = {
"X-Auth-Token": token,
"Content-Type": "application/json",
"Accept": "application/json",
}
epoch_time = int(time.time()) * 1000 # in milliseconds
NETWORK_HEALTH_URL = f"dna/intent/api/v1/network-health?timestamp={epoch_time}"
try:
cprint(text="\nGetting network health...", color="magenta")
response = requests.get(
url=f"{ENV['BASE_URL']}/{NETWORK_HEALTH_URL}",
headers=headers,
data=None,
verify=bool(strtobool(val=ENV["SSL_CERTIFICATE"])),
)
response.raise_for_status()
except (ConnectionError, HTTPError) as e:
raise SystemExit(colored(text=e, color="red"))
except KeyboardInterrupt:
raise SystemExit(
colored(text="Process interrupted by the user", color="yellow")
)
else:
cprint(text="The request was successful.\n", color="green")
return response.json()["healthDistirubution"]