Skip to content

Commit

Permalink
Merge pull request #63 from Calvario/patch-1
Browse files Browse the repository at this point in the history
Patch 1
  • Loading branch information
ottorei authored Oct 29, 2024
2 parents 2c78729 + ec14999 commit a383f78
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
50 changes: 50 additions & 0 deletions LibreNMS/queuemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,56 @@ def __init__(self, config, lock_manager):

def do_work(self, device_id, group):
if self.lock(device_id, timeout=self.config.poller.frequency):
# We need to check if the device was polled recently to prevent a race condition
# We have 3 cases:
# 1. The last_polled_timetaken is greater than the poller frequency, we can poll the device
# The last_polled_timetaken is less than the poller frequency cap to maximum 15 seconds
# 2. If the last_polled is greater than the cap, we can poll the device
# 3. If the last_polled is less than the cap, we skip polling the device
dbPollerQueueManager = LibreNMS.DB(self.config)
try:
select_last_polled = dbPollerQueueManager.query(
"SELECT last_polled >= NOW() - INTERVAL LEAST(%s - last_polled_timetaken, 15) SECOND AS is_poll_recent, "
"last_polled, last_polled_timetaken FROM devices WHERE device_id=%s",
(self.config.poller.frequency, device_id),
)
value_last_polled = select_last_polled.fetchone()
logger.info(
"Last polled timestamp for device {}: {} (is_poll_recent: {}, last_polled_timetaken: {})".format(
device_id,
value_last_polled[1],
value_last_polled[0],
value_last_polled[2],
)
)
# If is_poll_recent is 1, then the device was polled recently
if value_last_polled[0] == 1:
logger.warning(
"Skipping polling for device {} as it was polled recently".format(
device_id
)
)
self.unlock(device_id)
return
except pymysql.err.Error as e:
logger.critical(
"A DB Exception occurred while checking the last_polled of device {}: {}".format(
device_id, str(e)
)
)
self.unlock(device_id)
return
except Exception as e:
logger.error(
"An exception occurred while checking the last_polled of device {}: {}".format(
device_id, str(e)
)
)
self.unlock(device_id)
return
finally:
dbPollerQueueManager.close()

logger.info("Polling device {}".format(device_id))

args = (
Expand Down
8 changes: 4 additions & 4 deletions LibreNMS/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,14 +611,14 @@ def fetch_immediate_device_list(self):
result = self._db.query(
"""SELECT `device_id`,
`poller_group`,
COALESCE(`last_polled` <= DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_polled_timetaken`, 0) SECOND), 1) AS `poll`,
IF(status=0, 0, IF (%s < `last_discovered_timetaken` * 1.25, 0, COALESCE(`last_discovered` <= DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_discovered_timetaken`, 0) SECOND), 1))) AS `discover`
COALESCE(`last_polled` < DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_polled_timetaken`, 0) SECOND), 1) AS `poll`,
IF(status=0, 0, IF (%s < `last_discovered_timetaken` * 1.25, 0, COALESCE(`last_discovered` < DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_discovered_timetaken`, 0) SECOND), 1))) AS `discover`
FROM `devices`
WHERE `disabled` = 0 AND (
`last_polled` IS NULL OR
`last_discovered` IS NULL OR
`last_polled` <= DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_polled_timetaken`, 0) SECOND) OR
`last_discovered` <= DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_discovered_timetaken`, 0) SECOND)
`last_polled` < DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_polled_timetaken`, 0) SECOND) OR
`last_discovered` < DATE_ADD(DATE_ADD(NOW(), INTERVAL -%s SECOND), INTERVAL COALESCE(`last_discovered_timetaken`, 0) SECOND)
)
ORDER BY `last_polled_timetaken` DESC""",
(
Expand Down

0 comments on commit a383f78

Please sign in to comment.