Files
librenms-librenms/LibreNMS/Snmptrap/Dispatcher.php
Tony Murray 4c6f917d9e Updates to snmptrap handling (#9010)
* Updates to snmptrap handling
fix a bug in findDeviceByIP.  Add more tests for that.
Move handle outside of the Trap class, it doesn't fit.
Add developer docs.

* fix tests copy paste issue.

* Fix findByIp when port may not exist.

* Logging: Output context (and extra) if they exist

* Generic trap event logging and new config setting.
2018-08-14 07:56:16 +01:00

60 lines
2.0 KiB
PHP

<?php
/**
* Dispatcher.php
*
* Creates the correct handler for the trap and then sends it the trap.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Snmptrap;
use LibreNMS\Config;
use LibreNMS\Snmptrap\Handlers\Fallback;
use Log;
class Dispatcher
{
/**
* Instantiate the correct handler for this trap and call it's handle method
*
*/
public static function handle(Trap $trap)
{
if (empty($trap->getDevice())) {
Log::warning("Could not find device for trap", ['trap_text' => $trap->getRaw()]);
return false;
}
// note, this doesn't clear the resolved SnpmtrapHandler so only one per run
/** @var \LibreNMS\Interfaces\SnmptrapHandler $handler */
$handler = app(\LibreNMS\Interfaces\SnmptrapHandler::class, [$trap->getTrapOid()]);
$handler->handle($trap->getDevice(), $trap);
// log an event if appropriate
$fallback = $handler instanceof Fallback;
$logging = Config::get('snmptraps.eventlog', 'unhandled');
if ($logging == 'all' || ($fallback && $logging == 'unhandled')) {
log_event("SNMP trap received: " . $trap->getTrapOid(), $trap->getDevice()->toArray(), 'trap');
}
return !$fallback;
}
}