def add_new_device(mac_addr, device_data, db): """ Adds a new device to the Data Base if not exists :param device_data: :param db: database connection :return: The Device object """ ip4 = device_data['ipv4'] if device_data.get('ipv4') else None ip6 = device_data['ipv6'] if device_data.get('ipv6') else None ip4_status = network.check_device_status(ip4) ip6_status = network.check_device_status(ip6) if ip4_status == 'Online' or ip6_status == 'Online': cur_status = 'Online' else: cur_status = 'Offline' new_device = db.Device(mac_addr=mac_addr, name=device_data['hostname'], eth_vendor=network.get_mac_vendor(mac_addr), cur_status=cur_status) db.ConnectTime(id=uuid4(), lease_time=device_data['lease_time'], time=datetime.datetime.now(), transition=Transition.Unknown.value, device=new_device) db.IPLease(id=uuid4(), ipv4=str(ip4) if ip4 is not None else None, ipv6=str(ip6) if ip6 is not None else None, current=True, device=new_device) return new_device
def updated_device_data(device, data_dict, db): """ If a device already exists in database this routine will check if something has changed. If so, perform the proper updates. :param device: A device ORM to check possible updated :param data_dict: A dict with some data from dhcp_lease file to check :param db: database connection :return: The object if this was updated or None """ updated = False last_il = last_ip_lease(device, db) # Updates IPLease data if necessary if last_il is not None: if str(data_dict['ipv4']) != last_il.ipv4 or \ str(data_dict['ipv6']) != last_il.ipv6: last_il.current = False db.IPLease(id=uuid4(), ipv4=str(data_dict['ipv4']), ipv6=str(data_dict['ipv6']), current=True, device=device) updated = True # Updates Connection Infor if necessary device_status_v4 = network.check_device_status(data_dict['ipv4']) device_status_v6 = network.check_device_status(data_dict['ipv6']) device_status = 'Online' if device_status_v4 == 'Online' or \ device_status_v6 == 'Online' else 'Offline' if device_status != device.cur_status: db.ConnectTime(id=uuid4(), lease_time=data_dict['lease_time'], time=datetime.datetime.now(), transition=Transition.Off_to_On.value if device_status == 'Online' else Transition.On_to_Off.value, device=device) device.cur_status = device_status updated = True if updated: return device else: return None
def test_host_ipv6_is_down(self, mm): res = check_device_status(self.ipv6_addr) self.assertEqual(res, "Offline")
def test_host_ipv4_is_up(self, mm): res = check_device_status(self.ipv4_addr) self.assertEqual(res, "Online")