Catch exceptions generated by alert transports (#10565)

* Catch exceptions generated by alert transports

The alert.php script would completely die if a single alert
transport generated an exception. For example the API transport
if CURL couldn't connect to the api endpoint. The script
should attempt to send alerts to all working transports and not
die because a single transport fails.

* Move alert transport creation into try block

Catch any potential exceptions during transport creation.
None of the transports currently implement __construct()
but the DB could throw and in the future there may be
a transport that does implement it.
This commit is contained in:
Lee Keitel
2019-09-04 21:13:32 -05:00
committed by Tony Murray
parent e4314922c1
commit 85492d245d

View File

@@ -1,7 +1,7 @@
<?php
/*
* RunAlerts.php
*
*
* Copyright (C) 2014 Daniel Preussker <f0o@devilcode.org>
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -25,7 +25,7 @@
*
* Modified by:
* @author Heath Barnhart <barnhart@kanren.net>
*
*
*/
namespace LibreNMS\Alert;
@@ -523,9 +523,13 @@ class RunAlerts
$obj['alert']['title'] = $obj['title'];
$obj['msg'] = $type->getBody($obj);
c_echo(" :: $transport_title => ");
$instance = new $class($item['transport_id']);
$tmp = $instance->deliverAlert($obj, $item['opts']);
$this->alertLog($tmp, $obj, $obj['transport']);
try {
$instance = new $class($item['transport_id']);
$tmp = $instance->deliverAlert($obj, $item['opts']);
$this->alertLog($tmp, $obj, $obj['transport']);
} catch (\Exception $e) {
$this->alertLog($e, $obj, $obj['transport']);
}
unset($instance);
echo PHP_EOL;
}