2018-08-14 01:56:16 -05:00
< ? 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
2021-02-09 00:29:04 +01:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2018-08-14 01:56:16 -05:00
*
2021-02-09 00:29:04 +01:00
* @ link https :// www . librenms . org
2021-09-10 20:09:53 +02:00
*
2018-08-14 01:56:16 -05:00
* @ copyright 2018 Tony Murray
* @ author Tony Murray < murraytony @ gmail . com >
*/
namespace LibreNMS\Snmptrap ;
2022-11-09 09:47:19 +01:00
use App\Models\Eventlog ;
2019-07-18 08:36:02 -05:00
use LibreNMS\Alert\AlertRules ;
2018-08-14 01:56:16 -05:00
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
*/
2022-11-05 14:43:54 -05:00
public static function handle ( Trap $trap ) : bool
2018-08-14 01:56:16 -05:00
{
if ( empty ( $trap -> getDevice ())) {
2022-11-05 14:43:54 -05:00
Log :: warning ( 'Could not find device for trap' , [ 'trap_text' => $trap -> raw ]);
2020-09-21 14:54:51 +02:00
2018-08-14 01:56:16 -05:00
return false ;
}
2020-09-23 17:00:02 +02:00
if ( $trap -> findOid ( 'iso.3.6.1.6.3.1.1.4.1.0' )) {
// Even the TrapOid is not properly converted to text, so snmptrapd is probably not
// configured with any MIBs (-M and/or -m).
// LibreNMS snmptraps code cannot process received data. Let's inform the user.
2022-11-09 09:47:19 +01:00
Eventlog :: log ( 'Misconfigured MIBS or MIBDIRS for snmptrapd, check https://docs.librenms.org/Extensions/SNMP-Trap-Handler/ : ' . $trap -> raw , $trap -> getDevice (), 'system' );
2020-09-23 17:00:02 +02:00
return false ;
}
2018-08-14 01:56:16 -05:00
// 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' );
2020-09-23 17:00:02 +02:00
$detailed = Config :: get ( 'snmptraps.eventlog_detailed' , false );
2018-08-14 01:56:16 -05:00
if ( $logging == 'all' || ( $fallback && $logging == 'unhandled' )) {
2022-11-05 14:43:54 -05:00
$trap -> log ( $trap -> toString ( $detailed ));
2019-07-18 08:36:02 -05:00
} else {
$rules = new AlertRules ;
$rules -> runRules ( $trap -> getDevice () -> device_id );
2018-08-14 01:56:16 -05:00
}
return ! $fallback ;
}
}