-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetDeviceHealth.py
46 lines (46 loc) · 1.48 KB
/
NetDeviceHealth.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
import time
import requests
import psycopg
import logging.handlers
import vars
def get_net_device_health():
ret = False
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
API_OBJ_RESPONSE_SIZE = 150
fields = [
'cellular_health_category',
'cellular_health_score',
'id',
'net_device'
]
fields = ','.join(fields)
try:
conn = psycopg.connect(vars.conn_string)
cursor = conn.cursor()
cursor.execute('DELETE FROM cradlepoint.net_device_health WHERE "date">=CURRENT_DATE;')
with cursor.copy("COPY cradlepoint.net_device_health ({}) FROM STDIN;".format(fields)) as copy:
url = "https://www.cradlepointecm.com/api/v2/net_device_health/?fields={0}&limit={1}".format(fields, API_OBJ_RESPONSE_SIZE)
while url:
vars.hits+=1
if vars.hits%50==0:
time.sleep(10)
req = requests.get(url, headers=vars.headers)
req.raise_for_status()
resp = req.json()
url = resp['meta']['next']
for health in resp['data']:
copy.write_row(health[column] for column in health.keys())
conn.commit()
except Exception as e:
logger.exception(e)
ret = True
try:
if (ret):
conn.rollback()
cursor.close()
conn.close()
except Exception as e:
logger.exception(e)
ret = True
return ret