Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

84 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2020-05-19 22:08:41 -05:00
<?php
/*
2020-05-19 22:08:41 -05:00
* 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/>.
2020-05-19 22:08:41 -05:00
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2021 Tony Murray
2020-05-19 22:08:41 -05:00
* @author Tony Murray <murraytony@gmail.com>
*/
namespace LibreNMS\Data\Source;
2020-05-19 22:08:41 -05:00
use LibreNMS\Config;
2020-05-19 22:08:41 -05:00
use Log;
use Symfony\Component\Process\Process;
class Fping
{
/**
* Run fping against a hostname/ip in count mode and collect stats.
*
2021-09-08 23:35:56 +02:00
* @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
2020-05-19 22:08:41 -05:00
*/
public function ping($host, $count = 3, $interval = 1000, $timeout = 500, $address_family = 'ipv4'): FpingResponse
2020-05-19 22:08:41 -05:00
{
$interval = max($interval, 20);
2020-06-28 00:46:39 -05:00
$fping = Config::get('fping');
2023-08-11 02:16:59 +01:00
$fping6 = Config::get('fping6');
2021-11-11 22:33:01 +01:00
$fping_tos = Config::get('fping_options.tos', 0);
2023-08-11 02:16:59 +01:00
2020-06-28 00:46:39 -05:00
if ($address_family == 'ipv6') {
2020-06-28 21:58:19 -05:00
$cmd = is_executable($fping6) ? [$fping6] : [$fping, '-6'];
2023-08-11 02:16:59 +01:00
} else {
$cmd = is_executable($fping6) ? [$fping] : [$fping, '-4'];
2020-06-28 00:46:39 -05:00
}
2020-05-19 22:08:41 -05:00
// build the command
2020-06-28 00:46:39 -05:00
$cmd = array_merge($cmd, [
2020-05-19 22:08:41 -05:00
'-e',
'-q',
'-c',
max($count, 1),
'-p',
$interval,
'-t',
max($timeout, $interval),
2021-11-11 22:33:01 +01:00
'-O',
$fping_tos,
2020-05-19 22:08:41 -05:00
$host,
2020-06-28 00:46:39 -05:00
]);
2020-05-19 22:08:41 -05:00
$process = app()->make(Process::class, ['command' => $cmd]);
Log::debug('[FPING] ' . $process->getCommandLine() . PHP_EOL);
$process->run();
$response = FpingResponse::parseOutput($process->getErrorOutput(), $process->getExitCode());
2020-05-19 22:08:41 -05:00
Log::debug("response: $response");
2020-05-19 22:08:41 -05:00
return $response;
}
}