Files
librenms-librenms/app/Console/Commands/DevicePing.php
Tony Murray aa35d2f0c0 Connectivity Helper to check and record device reachability (#13315)
* Fping WIP

* Update availability, move ping rrd update in the same place as db update.

* move classes around

* make device:ping command work

* use new code, remove legacy code

* save metrics boolean prevents all saves
style fixes

* update device array

* style fixes

* Update unit test

* fix whitespace

* Fix Fping stub

* fix backwards if

* fix phpstan complaining

* Fix return type

* add fillable to DeviceOutage model.

* device_outage migration to add id...

* missed line in db_schema.yaml

* 1 billion more comments on the brain damage up/down code

* tests for status and status_reason fields

* fix style again :D

* Duplicate legacy isSNMPable() functionality
but with only one snmp call ever 😎

* Remove unused variable

* fix migrations for sqlite
2021-10-03 22:45:10 -05:00

59 lines
1.5 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Console\LnmsCommand;
use App\Models\Device;
use Illuminate\Database\Eloquent\Builder;
use LibreNMS\Config;
use LibreNMS\Polling\ConnectivityHelper;
use Symfony\Component\Console\Input\InputArgument;
class DevicePing extends LnmsCommand
{
protected $name = 'device:ping';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->addArgument('device spec', InputArgument::REQUIRED);
}
/**
* Execute the console command.
*
* @return int
*/
public function handle(): int
{
$spec = $this->argument('device spec');
$devices = Device::query()->when($spec !== 'all', function (Builder $query) use ($spec) {
/** @phpstan-var Builder<Device> $query */
return $query->where('device_id', $spec)
->orWhere('hostname', $spec)
->limit(1);
})->get();
if ($devices->isEmpty()) {
$devices = [new Device(['hostname' => $spec])];
}
Config::set('icmp_check', true); // ignore icmp disabled, this is an explicit user action
/** @var Device $device */
foreach ($devices as $device) {
$helper = new ConnectivityHelper($device);
$response = $helper->isPingable();
$this->line($device->displayName() . ' : ' . ($response->wasSkipped() ? 'skipped' : $response));
}
return 0;
}
}