Refactoring funzione convert_int
This commit is contained in:
@@ -29,6 +29,11 @@ def convert_int(d: dict) -> dict:
|
|||||||
d[k] = int(v)
|
d[k] = int(v)
|
||||||
return d
|
return d
|
||||||
|
|
||||||
|
def convert_int_list(l: list[dict]) -> list[dict]:
|
||||||
|
for n,d in enumerate(l):
|
||||||
|
l[n] = convert_int(d)
|
||||||
|
return l
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
INTERVAL = int(env['INTERVAL'])
|
INTERVAL = int(env['INTERVAL'])
|
||||||
@@ -43,20 +48,20 @@ def main():
|
|||||||
plaintext_login=True)
|
plaintext_login=True)
|
||||||
api = connection.get_api()
|
api = connection.get_api()
|
||||||
|
|
||||||
run: SignalHandler = SignalHandler()
|
##############################
|
||||||
last = 0
|
########## MAIN LOOP #########
|
||||||
if_stats_old = convert_int(api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'}))
|
##############################
|
||||||
|
last: float = 0
|
||||||
### MAIN LOOP ###
|
handler: SignalHandler = SignalHandler()
|
||||||
while run:
|
if_stats_old = convert_int_list(api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'}))
|
||||||
|
while handler.running:
|
||||||
try:
|
try:
|
||||||
now = time.time()
|
now:float = time.time()
|
||||||
if_points: list[Point] = []
|
if_points: list[Point] = []
|
||||||
if_stats: list[dict] = api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'})
|
if_stats: list[dict] = convert_int_list(api.get_resource('/interface/ethernet').call('print', {'proplist': 'name,rx-bytes,tx-bytes'}))
|
||||||
hw_stats: dict[str,str] = convert_int(api.get_resource('/system/resource').call('print', {'proplist':'uptime,cpu-load,total-memory,free-memory'})[0])
|
hw_stats: dict[str,str] = convert_int(api.get_resource('/system/resource').call('print', {'proplist':'uptime,cpu-load,total-memory,free-memory'})[0])
|
||||||
# calcolo della velocita' interfaccia a ogni ciclo
|
# Calcolo della velocita' interfaccia a ogni ciclo
|
||||||
for n,d in enumerate(if_stats):
|
for n, d in enumerate(if_stats):
|
||||||
if_stats[n] = convert_int(d)
|
|
||||||
if_stats[n]['rx-rate'] = int((if_stats[n]['rx-bytes']-if_stats_old[n]['rx-bytes'])/(now-last))
|
if_stats[n]['rx-rate'] = int((if_stats[n]['rx-bytes']-if_stats_old[n]['rx-bytes'])/(now-last))
|
||||||
if_stats[n]['tx-rate'] = int((if_stats[n]['tx-bytes']-if_stats_old[n]['tx-bytes'])/(now-last))
|
if_stats[n]['tx-rate'] = int((if_stats[n]['tx-bytes']-if_stats_old[n]['tx-bytes'])/(now-last))
|
||||||
if_points.append(
|
if_points.append(
|
||||||
@@ -67,11 +72,13 @@ def main():
|
|||||||
)
|
)
|
||||||
write_client.write(record=if_points)
|
write_client.write(record=if_points)
|
||||||
|
|
||||||
|
# Risorse del router
|
||||||
hw_point = Point('resources')
|
hw_point = Point('resources')
|
||||||
for k,v in hw_stats.items():
|
for k,v in hw_stats.items():
|
||||||
hw_point.field(k,v)
|
hw_point.field(k,v)
|
||||||
write_client.write(record=hw_point)
|
write_client.write(record=hw_point)
|
||||||
|
|
||||||
|
# Salvo ultimo punto per il giro successivo
|
||||||
if_stats_old = if_stats
|
if_stats_old = if_stats
|
||||||
last = time.time()
|
last = time.time()
|
||||||
LOGGER.debug(f"\nInterfaces: {json.dumps(if_stats, indent = 2)}")
|
LOGGER.debug(f"\nInterfaces: {json.dumps(if_stats, indent = 2)}")
|
||||||
@@ -80,8 +87,9 @@ def main():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Unexpected exception: [{e}]")
|
print(f"Unexpected exception: [{e}]")
|
||||||
return 1
|
return 1
|
||||||
### END MAIN LOOP ###
|
##############################
|
||||||
|
###### END MAIN LOOP #########
|
||||||
|
##############################
|
||||||
connection.disconnect()
|
connection.disconnect()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user