From 8c96be4053a80d04b3a80a1ae8c2a6cdc40844fd Mon Sep 17 00:00:00 2001 From: Johan Marcusson Date: Thu, 16 Sep 2021 10:31:42 +0200 Subject: [PATCH] Update device last_seen on syncto, update facts, firmware post flight, device discovered, init step2. Not on sqlalchemy update since you can PUT data to unreachable device. --- src/cnaas_nms/confpush/firmware.py | 2 ++ src/cnaas_nms/confpush/init_device.py | 5 ++++- src/cnaas_nms/confpush/sync_devices.py | 4 ++++ src/cnaas_nms/confpush/update.py | 2 ++ src/cnaas_nms/db/device.py | 2 +- 5 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/cnaas_nms/confpush/firmware.py b/src/cnaas_nms/confpush/firmware.py index c96ee856..2bd8e723 100644 --- a/src/cnaas_nms/confpush/firmware.py +++ b/src/cnaas_nms/confpush/firmware.py @@ -1,4 +1,5 @@ import time +import datetime from typing import Optional from nornir.core.task import MultiResult @@ -90,6 +91,7 @@ def arista_post_flight_check(task, post_waittime: int, job_id: Optional[str] = N else: dev.confhash = None dev.synchronized = False + dev.last_seen = datetime.datetime.utcnow() except Exception as e: logger.exception("Could not update OS version on device {}: {}".format(task.host.name, str(e))) return 'Post-flight failed, could not update OS version: {}'.format(str(e)) diff --git a/src/cnaas_nms/confpush/init_device.py b/src/cnaas_nms/confpush/init_device.py index 022bd24d..cc3a37b7 100644 --- a/src/cnaas_nms/confpush/init_device.py +++ b/src/cnaas_nms/confpush/init_device.py @@ -1,3 +1,5 @@ +import datetime +import os from typing import Optional, List from ipaddress import IPv4Interface, IPv4Address @@ -6,7 +8,6 @@ from nornir_utils.plugins.functions import print_result from apscheduler.job import Job import yaml -import os import cnaas_nms.confpush.nornir_helper import cnaas_nms.confpush.get @@ -667,6 +668,7 @@ def init_device_step2(device_id: int, iteration: int = -1, set_facts(dev, facts) management_ip = dev.management_ip dev.dhcp_ip = None + dev.last_seen = datetime.datetime.utcnow() # Plugin hook: new managed device # Send: hostname , device type , serial , platform , vendor , model , os version @@ -770,6 +772,7 @@ def discover_device(ztp_mac: str, dhcp_ip: str, iteration: int, dev.model = facts['model'][:64] dev.os_version = facts['os_version'][:64] dev.state = DeviceState.DISCOVERED + dev.last_seen = datetime.datetime.utcnow() new_hostname = dev.hostname logger.info(f"Device with ztp_mac {ztp_mac} successfully scanned" + f"(attempt {iteration}), moving to DISCOVERED state") diff --git a/src/cnaas_nms/confpush/sync_devices.py b/src/cnaas_nms/confpush/sync_devices.py index dd3b9e3c..9edd7784 100644 --- a/src/cnaas_nms/confpush/sync_devices.py +++ b/src/cnaas_nms/confpush/sync_devices.py @@ -1,5 +1,6 @@ import os import yaml +import datetime from typing import Optional, List from ipaddress import IPv4Interface, IPv4Address from hashlib import sha256 @@ -659,12 +660,15 @@ def exclude_filter(host, exclude_list=failed_hosts+unchanged_hosts): if dry_run: dev: Device = session.query(Device).filter(Device.hostname == hostname).one() dev.synchronized = False + dev.last_seen = datetime.datetime.utcnow() else: dev: Device = session.query(Device).filter(Device.hostname == hostname).one() dev.synchronized = True + dev.last_seen = datetime.datetime.utcnow() for hostname in unchanged_hosts: dev: Device = session.query(Device).filter(Device.hostname == hostname).one() dev.synchronized = True + dev.last_seen = datetime.datetime.utcnow() if not dry_run: logger.info("Releasing lock for devices from syncto job: {}".format(job_id)) Joblock.release_lock(session, job_id=job_id) diff --git a/src/cnaas_nms/confpush/update.py b/src/cnaas_nms/confpush/update.py index 57831edb..2407a947 100644 --- a/src/cnaas_nms/confpush/update.py +++ b/src/cnaas_nms/confpush/update.py @@ -1,3 +1,4 @@ +import datetime from typing import Optional, List from nornir_napalm.plugins.tasks import napalm_get @@ -186,6 +187,7 @@ def update_facts(hostname: str, with sqla_session() as session: dev: Device = session.query(Device).filter(Device.hostname == hostname).one() diff = set_facts(dev, facts) + dev.last_seen = datetime.datetime.utcnow() logger.debug("Updating facts for device {}, new values: {}, {}, {}, {}".format( hostname, facts['serial_number'], facts['vendor'], facts['model'], facts['os_version'] diff --git a/src/cnaas_nms/db/device.py b/src/cnaas_nms/db/device.py index 81ad26a3..e9ad79f3 100644 --- a/src/cnaas_nms/db/device.py +++ b/src/cnaas_nms/db/device.py @@ -89,7 +89,7 @@ class Device(cnaas_nms.db.base.Base): state = Column(Enum(DeviceState), nullable=False) # type: ignore device_type = Column(Enum(DeviceType), nullable=False) confhash = Column(String(64)) # SHA256 = 64 characters - last_seen = Column(DateTime, default=datetime.datetime.utcnow) # onupdate=now + last_seen = Column(DateTime, default=datetime.datetime.utcnow) port = Column(Integer) def as_dict(self) -> dict: