Skip to content

Commit

Permalink
Merge pull request #198 from SUNET/feature.update_lastseen
Browse files Browse the repository at this point in the history
Update device last_seen
  • Loading branch information
indy-independence authored Sep 17, 2021
2 parents 1878622 + 8c96be4 commit 01e53e6
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/cnaas_nms/confpush/firmware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import time
import datetime
from typing import Optional

from nornir.core.task import MultiResult
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion src/cnaas_nms/confpush/init_device.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime
import os
from typing import Optional, List
from ipaddress import IPv4Interface, IPv4Address

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 4 additions & 0 deletions src/cnaas_nms/confpush/sync_devices.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import yaml
import datetime
from typing import Optional, List
from ipaddress import IPv4Interface, IPv4Address
from hashlib import sha256
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/cnaas_nms/confpush/update.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
from typing import Optional, List

from nornir_napalm.plugins.tasks import napalm_get
Expand Down Expand Up @@ -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']
Expand Down
2 changes: 1 addition & 1 deletion src/cnaas_nms/db/device.py
Original file line number Diff line numbe