Send event log when alert rule fails (#15440)

Instead of breaking all following alerts when one has an error, just skip that one alert rule and send and eventlog detailing the error.
This commit is contained in:
Tony Murray 2023-10-12 17:31:53 -07:00 committed by GitHub
parent afc78d7890
commit 1caa2dec42
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -31,8 +31,11 @@
namespace LibreNMS\Alert;
use App\Models\Eventlog;
use Carbon\Carbon;
use Illuminate\Database\QueryException;
use LibreNMS\Enum\AlertState;
use LibreNMS\Enum\Severity;
use Log;
class AlertRules
@ -69,7 +72,19 @@ class AlertRules
$rule['query'] = AlertDB::genSQL($rule['rule'], $rule['builder']);
}
$sql = $rule['query'];
$qry = dbFetchRows($sql, [$device_id]);
// set fetch assoc
global $PDO_FETCH_ASSOC;
$PDO_FETCH_ASSOC = true;
try {
$qry = \DB::select($sql, [$device_id]);
} catch (QueryException $e) {
c_echo('%RError: %n' . $e->getMessage() . PHP_EOL);
Eventlog::log("Error in alert rule {$rule['name']} ({$rule['id']}): " . $e->getMessage(), $device_id, 'alert', Severity::Error);
continue; // skip this rule
}
$PDO_FETCH_ASSOC = false;
$cnt = count($qry);
for ($i = 0; $i < $cnt; $i++) {
if (isset($qry[$i]['ip'])) {