Files
librenms-librenms/LibreNMS/Data/Source/Fping.php
T
Tony MurrayandGitHub 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

79 lines
2.3 KiB
PHP

<?php
/*
* Fping.php
*
* -Description-
*
* 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 2021 Tony Murray
* @author Tony Murray <[email protected]>
*/
namespace LibreNMS\Data\Source;
use LibreNMS\Config;
use Log;
use Symfony\Component\Process\Process;
class Fping
{
/**
* Run fping against a hostname/ip in count mode and collect stats.
*
* @param string $host
* @param int $count (min 1)
* @param int $interval (min 20)
* @param int $timeout (not more than $interval)
* @param string $address_family ipv4 or ipv6
* @return \LibreNMS\Data\Source\FpingResponse
*/
public function ping($host, $count = 3, $interval = 1000, $timeout = 500, $address_family = 'ipv4'): FpingResponse
{
$interval = max($interval, 20);
$fping = Config::get('fping');
$cmd = [$fping];
if ($address_family == 'ipv6') {
$fping6 = Config::get('fping6');
$cmd = is_executable($fping6) ? [$fping6] : [$fping, '-6'];
}
// build the command
$cmd = array_merge($cmd, [
'-e',
'-q',
'-c',
max($count, 1),
'-p',
$interval,
'-t',
max($timeout, $interval),
$host,
]);
$process = app()->make(Process::class, ['command' => $cmd]);
Log::debug('[FPING] ' . $process->getCommandLine() . PHP_EOL);
$process->run();
$response = FpingResponse::parseOutput($process->getErrorOutput(), $process->getExitCode());
Log::debug("response: $response");
return $response;
}
}