-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_poller.py
233 lines (208 loc) · 9.25 KB
/
data_poller.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import click
import time
import json
from sqlite3_utilities import read_username_password_from_database, write_data_to_database, get_chassis_type_from_ip, delte_half_data_from_performace_metric_table, read_poll_setting_from_database
import IxOSRestAPICaller as ixOSRestCaller
from RestApi.IxOSRestInterface import IxRestSession
def get_chassis_summary_data():
"""This is a call to RestAPI to get chassis summary data
"""
list_of_chassis = []
serv_list = read_username_password_from_database()
if serv_list:
chassis_list = json.loads(serv_list)
for chassis in chassis_list:
try:
session = IxRestSession(
chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_chassis_information(session)
out["chassisIp"] = chassis["ip"]
list_of_chassis.append(out)
except Exception:
list_of_chassis.append({ "chassisIp": chassis["ip"],
"chassisSerial#": "NA",
"controllerSerial#": "NA",
"chassisType": "NA",
"physicalCards#": "NA",
"chassisStatus": "Not Reachable",
"lastUpdatedAt_UTC": "NA",
"mem_bytes": "NA",
"mem_bytes_total": "NA",
"cpu_pert_usage": "NA",
"os": "NA",
"IxOS": "NA",
"IxNetwork Protocols": "NA",
"IxOS REST": "NA"})
write_data_to_database(table_name="chassis_summary_details",
records=list_of_chassis, ip_tags_dict={})
else:
print("No Chassis List")
def get_chassis_card_data():
"""This is a call to RestAPI to get chassis card summary data
"""
list_of_cards = []
serv_list = read_username_password_from_database()
if serv_list:
chassis_list = json.loads(serv_list)
for chassis in chassis_list:
try:
session = IxRestSession(
chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_chassis_cards_information(
session, chassis["ip"], get_chassis_type_from_ip(chassis["ip"]))
list_of_cards.append(out)
except Exception:
out = [{'chassisIp': chassis["ip"],
'chassisType': 'NA',
'cardNumber': 'NA',
'serialNumber': 'NA',
'cardType': 'NA',
'cardState': 'NA',
'numberOfPorts': 'NA',
'lastUpdatedAt_UTC': 'NA'}]
list_of_cards.append(out)
write_data_to_database(table_name="chassis_card_details",
records=list_of_cards, ip_tags_dict={})
def get_chassis_port_data():
"""This is a call to RestAPI to get chassis card port summary data
"""
port_list_details = []
serv_list = read_username_password_from_database()
if serv_list:
chassis_list = json.loads(serv_list)
if chassis_list:
for chassis in chassis_list:
try:
session = IxRestSession(
chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_chassis_ports_information(
session, chassis["ip"], get_chassis_type_from_ip(chassis["ip"]))
port_list_details.append(out)
except Exception:
a = [{
'owner': 'NA',
'transceiverModel': 'NA',
'transceiverManufacturer': 'NA',
'portNumber': 'NA',
'linkState': 'NA',
'cardNumber': 'NA',
'lastUpdatedAt_UTC': 'NA',
'totalPorts': 'NA',
'ownedPorts': 'NA',
'freePorts': 'NA',
'chassisIp': chassis["ip"],
'typeOfChassis': 'NA',
'transmitState': 'NA'
}]
port_list_details.append(a)
write_data_to_database(
table_name="chassis_port_details", records=port_list_details)
def get_chassis_licensing_data():
"""This is a call to RestAPI to get chassis licensing data
"""
list_of_licenses = []
serv_list = read_username_password_from_database()
if serv_list:
chassis_list = json.loads(serv_list)
for chassis in chassis_list:
try:
session = IxRestSession(
chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_license_activation(
session, chassis["ip"], get_chassis_type_from_ip(chassis["ip"]))
list_of_licenses.append(out)
except Exception:
a = [{
'chassisIp': chassis["ip"],
'typeOfChassis': 'NA',
'hostId': 'NA',
'partNumber': 'NA',
'activationCode': 'NA',
'quantity': 'NA',
'description': 'NA',
'maintenanceDate': 'NA',
'expiryDate': 'NA',
'isExpired': 'NA',
'lastUpdatedAt_UTC': 'NA'
}]
list_of_licenses.append(a)
write_data_to_database(
table_name="license_details_records", records=list_of_licenses)
def get_sensor_information():
"""This is a call to RestAPI to get chassis sensors summary data
"""
sensor_list_details = []
serv_list = read_username_password_from_database()
if serv_list:
chassis_list = json.loads(serv_list)
for chassis in chassis_list:
try:
session = IxRestSession(chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_sensor_information(session, chassis["ip"], get_chassis_type_from_ip(chassis["ip"]))
sensor_list_details.append(out)
except Exception:
a = [{
'type': 'NA',
'unit': 'NA',
'name': 'NA',
'value': 'NA',
'chassisIp': chassis["ip"],
'typeOfChassis': 'NA',
'lastUpdatedAt_UTC': 'NA'
}]
sensor_list_details.append(a)
write_data_to_database(
table_name="chassis_sensor_details", records=sensor_list_details)
def get_perf_metrics():
"""This is a call to RestAPI to get chassis performance metrics data
"""
serv_list = read_username_password_from_database()
perf_list_details = []
if serv_list:
chassis_list = json.loads(serv_list)
for chassis in chassis_list:
try:
session = IxRestSession(chassis["ip"], chassis["username"], chassis["password"], verbose=False)
out = ixOSRestCaller.get_perf_metrics(session, chassis["ip"])
perf_list_details.append(out)
except Exception:
a = {'chassisIp': chassis["ip"],
'mem_utilization': 0,
'cpu_utilization': 0,
'lastUpdatedAt_UTC': '03/15/2023, 03:31:47'}
perf_list_details.append(a)
write_data_to_database(
table_name="chassis_utilization_details", records=perf_list_details)
def delete_half_metric_records_weekly():
"""This method will do periodic cleanup of inventord DB performance metrics data
"""
delte_half_data_from_performace_metric_table()
def controller(category_of_poll=None):
categoryToFuntionMap[category_of_poll]()
categoryToFuntionMap = {"chassis": get_chassis_summary_data,
"cards": get_chassis_card_data,
"ports": get_chassis_port_data,
"licensing": get_chassis_licensing_data,
"sensors": get_sensor_information,
"perf": get_perf_metrics,
"data_purge": delete_half_metric_records_weekly}
@click.command()
@click.option('--category', default= "", help='What chassis aspect to poll. chassis, cards, ports, licensing')
@click.option('--interval', default= "", help='Interval between Polls')
def start_poller(category, interval):
"""Since not all the parameters are modified with same interval, this way, we can specify exactly what we want to monitor at what interval
Args:
category (_type_): _description_
interval (_type_): _description_
"""
while True:
poll_interval = read_poll_setting_from_database()
if poll_interval:
interval = poll_interval[category]
categoryToFuntionMap[category]()
# Data Purge would be in days
if category == "data_purge":
interval = int(interval) * 24 * 60 * 60
time.sleep(int(interval))
if __name__ == '__main__':
start_poller()