Fix last_reachable timestamp bug
Change-Id: I0d479c9aef59866db164790247eca2c3c6e1f0bf
diff --git a/app.py b/app.py
index 44c91ec..e7ede6a 100644
--- a/app.py
+++ b/app.py
@@ -78,9 +78,10 @@
default="3333")
return parser
-def update(roc, prom, devices):
- roc.update_devices(devices)
- prom.update_devices(devices)
+def update(roc, prom, old):
+ new = roc.update_devices(old)
+ prom.update_devices(new)
+ return new
def probe(devices):
for imsi_id, device in devices.items():
@@ -97,11 +98,11 @@
def work_thread(roc, prom):
global devices, lock
while True:
- temp_devices = {}
- update(roc, prom, temp_devices)
- probe(temp_devices)
+ new = update(roc, prom, devices)
+ probe(new)
with lock:
- devices = temp_devices
+ devices = new
+
time.sleep(5)
if __name__ == '__main__':
diff --git a/device.py b/device.py
index 2b1ffcd..963d5ce 100644
--- a/device.py
+++ b/device.py
@@ -8,10 +8,10 @@
class Device(object):
- def __init__(self, imsi_id, imsi, ip=None):
+ def __init__(self, imsi_id, imsi, last_reachable=datetime.min, ip=None):
# log.debug("creating device, imsi_id={}, imsi={}, ip={}".format(imsi_id, imsi, ip))
self.imsi_id = imsi_id
self.imsi = imsi
self.ip = ip
self.reachable = False
- self.last_reachable = datetime.min
+ self.last_reachable = last_reachable
diff --git a/prom.py b/prom.py
index 9a517e1..5c4668c 100644
--- a/prom.py
+++ b/prom.py
@@ -58,11 +58,9 @@
metric = elem['metric']
if 'imsi' in metric and 'mobile_ip' in metric:
imsi = metric['imsi']
- if imsi in devices:
- # log.info("{} added to list of monitored devices".format(imsi))
- devices[imsi].ip = metric['mobile_ip']
- else:
- log.error("Ignoring device as it was not reported by ROC - {}".format(metric))
+ for k in devices:
+ if devices[k].imsi == imsi:
+ devices[k].ip = metric['mobile_ip']
else:
log.error("Ignoring device as imsi or mobile-ip not found - {}".format(metric))
diff --git a/roc.py b/roc.py
index fe328aa..be609bf 100644
--- a/roc.py
+++ b/roc.py
@@ -9,6 +9,7 @@
from requests.structures import CaseInsensitiveDict
import logging as log
import pyaml
+from datetime import datetime
from device import Device
@@ -120,10 +121,17 @@
return devices
def update_devices(self, devices):
+ new = {}
dgs = self.get_devicegroups()
for dg in dgs:
for d in dg['imsis']:
- devices[str(d['imsi-range-from'])] = Device(str(d['imsi-id']), d['imsi-range-from'])
+ imsi_id = str(d['imsi-id'])
+ imsi = str(d['imsi-range-from'])
+ last_reachable = datetime.min
+ if imsi_id in devices:
+ last_reachable = devices[imsi_id].last_reachable
+ new[imsi_id] = Device(imsi_id, imsi, last_reachable)
+ return new
def get_subnet(self):
subnets = {}