Catch all module errors (#13542)

* Catch all module errors
As of PHP 7.1, we can now catch errors and exceptions by catching throwable.
Also, log the errors in a consistent way with the entire stack trace

* Log event and improve messages

* call from facade because event might not exist on logger
This commit is contained in:
Tony Murray
2021-11-23 02:52:52 -06:00
committed by GitHub
parent 69b49d0eff
commit 4a5836f64e
3 changed files with 12 additions and 11 deletions

View File

@@ -32,9 +32,9 @@ use App\Polling\Measure\Measurement;
use App\Polling\Measure\MeasurementManager;
use Carbon\Carbon;
use DB;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Str;
use LibreNMS\Enum\Alert;
use LibreNMS\Exceptions\PollerException;
use LibreNMS\Modules\LegacyModule;
use LibreNMS\Polling\ConnectivityHelper;
@@ -44,6 +44,7 @@ use LibreNMS\Util\Dns;
use LibreNMS\Util\Git;
use LibreNMS\Util\StringHelpers;
use Psr\Log\LoggerInterface;
use Throwable;
class Poller
{
@@ -173,9 +174,10 @@ class Poller
$module_class = StringHelpers::toClass($module, '\\LibreNMS\\Modules\\');
$instance = class_exists($module_class) ? new $module_class : new LegacyModule($module);
$instance->poll($this->os);
} catch (Exception $e) {
} catch (Throwable $e) {
// isolate module exceptions so they don't disrupt the polling process
$this->logger->error("Error in $module module. " . $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
$this->logger->error("%rError polling $module module for {$this->device->hostname}.%n " . $e->getMessage() . PHP_EOL . $e->getTraceAsString(), ['color' => true]);
\Log::event("Error polling $module module. Check log file for more details.", $this->device, 'poller', Alert::ERROR);
}
app(MeasurementManager::class)->printChangedStats();

View File

@@ -15,6 +15,7 @@
use Illuminate\Support\Str;
use LibreNMS\Config;
use LibreNMS\Device\YamlDiscovery;
use LibreNMS\Enum\Alert;
use LibreNMS\Exceptions\HostExistsException;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\OS;
@@ -151,11 +152,10 @@ function discover_device(&$device, $force_module = false)
try {
include "includes/discovery/$module.inc.php";
} catch (Exception $e) {
} catch (Throwable $e) {
// isolate module exceptions so they don't disrupt the polling process
echo $e->getTraceAsString() . PHP_EOL;
c_echo("%rError in $module module.%n " . $e->getMessage() . PHP_EOL);
logfile("Error in $module module. " . $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
Log::error("%rError discovering $module module for {$device['hostname']}.%n " . $e->getMessage() . PHP_EOL . $e->getTraceAsString(), ['color' => true]);
Log::event("Error discovering $module module. Check log file for more details.", $device['device_id'], 'discovery', Alert::ERROR);
}
$module_time = microtime(true) - $module_start;

View File

@@ -327,11 +327,10 @@ function poll_device($device, $force_module = false)
try {
include "includes/polling/$module.inc.php";
} catch (Exception $e) {
} catch (Throwable $e) {
// isolate module exceptions so they don't disrupt the polling process
echo $e->getTraceAsString() . PHP_EOL;
c_echo("%rError in $module module.%n " . $e->getMessage() . PHP_EOL);
logfile("Error in $module module. " . $e->getMessage() . PHP_EOL . $e->getTraceAsString() . PHP_EOL);
Log::error("%rError polling $module module for {$device['hostname']}.%n " . $e->getMessage() . PHP_EOL . $e->getTraceAsString(), ['color' => true]);
Log::event("Error polling $module module. Check log file for more details.", $device['device_id'], 'poller', Alert::ERROR);
}
$module_time = microtime(true) - $module_start;