From af2930e0d953e8040ce2ebedd9673b633621e84b Mon Sep 17 00:00:00 2001 From: Neil Lathwood Date: Sat, 25 Mar 2017 21:02:21 +0000 Subject: [PATCH] fix: Clear out stale alerts (#6268) --- alerts.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/alerts.php b/alerts.php index b59ad5fcc617..36eaf924dbb5 100755 --- a/alerts.php +++ b/alerts.php @@ -63,6 +63,8 @@ if (!defined('TEST') && $config['alert']['disable'] != 'true') { echo 'Start: '.date('r')."\r\n"; + echo "ClearStaleAlerts():" . PHP_EOL; + ClearStaleAlerts(); echo "RunFollowUp():\r\n"; RunFollowUp(); echo "RunAlerts():\r\n"; @@ -74,6 +76,16 @@ unlink($config['install_dir'].'/.alerts.lock'); +function ClearStaleAlerts() +{ + $sql = "SELECT `alerts`.`id` AS `alert_id`, `devices`.`hostname` AS `hostname` FROM `alerts` LEFT JOIN `devices` ON `alerts`.`device_id`=`devices`.`device_id` RIGHT JOIN `alert_rules` ON `alerts`.`rule_id`=`alert_rules`.`id` WHERE `alerts`.`state`!=0 AND `devices`.`hostname` IS NULL"; + foreach (dbFetchRows($sql) as $alert) { + if (empty($alert['hostname']) && isset($alert['alert_id'])) { + dbDelete('alerts', '`id` = ?', array($alert['alert_id'])); + echo "Stale-alert: #{$alert['alert_id']}" . PHP_EOL; + } + } +} /** * Re-Validate Rule-Mappings