Files
librenms-librenms/routes/console.php
Jellyfrog 9ef94323f5 Update dependencies (#13310)
* Bump composer dependencies

  - Upgrading brick/math (0.9.2 => 0.9.3)
  - Upgrading composer/ca-bundle (1.2.10 => 1.2.11)
  - Upgrading composer/composer (2.1.3 => 2.1.8)
  - Upgrading composer/xdebug-handler (2.0.1 => 2.0.2)
  - Upgrading dapphp/radius (2.5.5 => 2.5.6)
  - Upgrading doctrine/annotations (1.13.1 => 1.13.2)
  - Upgrading doctrine/cache (2.0.3 => 2.1.1)
  - Upgrading doctrine/dbal (2.13.2 => 2.13.3)
  - Upgrading facade/flare-client-php (1.8.1 => 1.9.1)
  - Upgrading facade/ignition (2.11.0 => 2.14.0)
  - Upgrading fakerphp/faker (v1.15.0 => v1.16.0)
  - Upgrading filp/whoops (2.13.0 => 2.14.3)
  - Upgrading friendsofphp/php-cs-fixer (v2.19.0 => v2.19.2)
  - Upgrading graham-campbell/result-type (v1.0.1 => v1.0.2)
  - Upgrading laravel/dusk (v6.15.1 => v6.18.1)
  - Upgrading laravel/framework (v8.49.2 => v8.62.0)
  - Locking laravel/serializable-closure (v1.0.2)
  - Upgrading laravel/tinker (v2.6.1 => v2.6.2)
  - Upgrading league/commonmark (1.6.5 => 1.6.6)
  - Upgrading league/flysystem (1.1.4 => 1.1.5)
  - Upgrading league/mime-type-detection (1.7.0 => 1.8.0)
  - Upgrading maximebf/debugbar (v1.16.5 => v1.17.1)
  - Upgrading mockery/mockery (1.4.3 => 1.4.4)
  - Upgrading monolog/monolog (2.3.0 => 2.3.4)
  - Upgrading nesbot/carbon (2.50.0 => 2.53.1)
  - Upgrading nikic/php-parser (v4.11.0 => v4.13.0)
  - Upgrading nunomaduro/collision (v5.5.0 => v5.10.0)
  - Upgrading phar-io/manifest (2.0.1 => 2.0.3)
  - Upgrading php-parallel-lint/php-parallel-lint (v1.3.0 => v1.3.1)
  - Upgrading phpdocumentor/type-resolver (1.4.0 => 1.5.0)
  - Upgrading phpmailer/phpmailer (v6.5.0 => v6.5.1)
  - Upgrading phpoption/phpoption (1.7.5 => 1.8.0)
  - Upgrading phpseclib/phpseclib (3.0.9 => 3.0.10)
  - Upgrading phpspec/prophecy (1.13.0 => 1.14.0)
  - Upgrading phpstan/phpstan (0.12.92 => 0.12.99)
  - Upgrading phpunit/php-code-coverage (9.2.6 => 9.2.7)
  - Upgrading phpunit/phpunit (9.5.6 => 9.5.10)
  - Upgrading predis/predis (v1.1.7 => v1.1.8)
  - Upgrading ramsey/collection (1.1.3 => 1.2.1)
  - Upgrading ramsey/uuid (4.1.1 => 4.2.3)
  - Upgrading seld/phar-utils (1.1.1 => 1.1.2)
  - Upgrading symfony/console (v5.3.2 => v5.3.7)
  - Upgrading symfony/css-selector (v5.3.0 => v5.3.4)
  - Upgrading symfony/debug (v4.4.25 => v4.4.31)
  - Upgrading symfony/error-handler (v5.3.3 => v5.3.7)
  - Upgrading symfony/event-dispatcher (v5.3.0 => v5.3.7)
  - Upgrading symfony/filesystem (v5.3.3 => v5.3.4)
  - Upgrading symfony/finder (v5.3.0 => v5.3.7)
  - Upgrading symfony/http-foundation (v5.3.3 => v5.3.7)
  - Upgrading symfony/http-kernel (v5.3.3 => v5.3.9)
  - Upgrading symfony/mime (v5.3.2 => v5.3.8)
  - Upgrading symfony/options-resolver (v5.3.0 => v5.3.7)
  - Upgrading symfony/polyfill-intl-grapheme (v1.23.0 => v1.23.1)
  - Upgrading symfony/polyfill-mbstring (v1.23.0 => v1.23.1)
  - Upgrading symfony/polyfill-php80 (v1.23.0 => v1.23.1)
  - Locking symfony/polyfill-php81 (v1.23.0)
  - Upgrading symfony/process (v5.3.2 => v5.3.7)
  - Upgrading symfony/routing (v5.3.0 => v5.3.7)
  - Upgrading symfony/stopwatch (v5.3.0 => v5.3.4)
  - Upgrading symfony/string (v5.3.3 => v5.3.7)
  - Upgrading symfony/translation (v5.3.3 => v5.3.9)
  - Upgrading symfony/var-dumper (v5.3.3 => v5.3.8)
  - Upgrading symfony/yaml (v4.4.26 => v4.4.29)
  - Upgrading tecnickcom/tcpdf (6.4.1 => 6.4.2)
  - Upgrading theseer/tokenizer (1.2.0 => 1.2.1)

* Update npm dependecies

* Update phpstan ignores after laravel upgrades

* Merge laravel/laravel repo changes
2021-10-02 18:04:59 -05:00

333 lines
13 KiB
PHP

<?php
use App\Models\Device;
use Illuminate\Support\Facades\Artisan;
use LibreNMS\Exceptions\HostUnreachableException;
use LibreNMS\Util\Debug;
use Symfony\Component\Process\Process;
/*
|--------------------------------------------------------------------------
| Console Routes
|--------------------------------------------------------------------------
|
| This file is where you may define all of your Closure based console
| commands. Each Closure is bound to a command instance allowing a
| simple approach to interacting with each command's IO methods.
|
*/
Artisan::command('device:rename
{old hostname : ' . __('The existing hostname, IP, or device id') . '}
{new hostname : ' . __('The new hostname or IP') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
(new Process([
base_path('renamehost.php'),
$this->argument('old hostname'),
$this->argument('new hostname'),
]))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Rename a device, this can be used to change the hostname or IP of a device'));
Artisan::command('device:add
{device spec : Hostname or IP to add}
{--v1 : ' . __('Use SNMP v1') . '}
{--v2c : ' . __('Use SNMP v2c') . '}
{--v3 : ' . __('Use SNMP v3') . '}
{--f|force : ' . __('Just add the device, do not make any safety checks') . '}
{--g|group= : ' . __('Poller group (for distributed polling)') . '}
{--b|ping-fallback : ' . __('Add the device as ping only if it does not respond to SNMP') . '}
{--p|port-association-mode=ifIndex : ' . __('Sets how ports are mapped :modes, ifName is suggested for Linux/Unix', ['modes' => '[ifIndex, ifName, ifDescr, ifAlias]']) . '}
{--c|community= : ' . __('SNMP v1 or v2 community') . '}
{--t|transport=udp : ' . __('Transport to connect to the device') . ' [udp, udp6, tcp, tcp6]}
{--r|port=161 : ' . __('SNMP transport port') . '}
{--u|security-name=root : ' . __('SNMPv3 security username') . '}
{--A|auth-password= : ' . __('SNMPv3 authentication password') . '}
{--a|auth-protocol=md5 : ' . __('SNMPv3 authentication protocol') . ' [' . implode(', ', \LibreNMS\SNMPCapabilities::supportedAuthAlgorithms()) . ']}
{--x|privacy-protocol=aes : ' . __('SNMPv3 privacy protocol') . ' [' . implode(', ', \LibreNMS\SNMPCapabilities::supportedCryptoAlgorithms()) . ']}
{--X|privacy-password= : ' . __('SNMPv3 privacy password') . '}
{--P|ping-only : ' . __('Add a ping only device') . '}
{--o|os=ping : ' . __('Ping only: specify OS') . '}
{--w|hardware= : ' . __('Ping only: specify hardware') . '}
{--s|sysName= : ' . __('Ping only: specify sysName') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
// Value Checks
if (! in_array($this->option('port-association-mode'), ['ifIndex', 'ifName', 'ifDescr', 'ifAlias'])) {
$this->error(__('Invalid port association mode'));
}
if (! in_array($this->option('transport'), ['udp', 'udp6', 'tcp', 'tcp6'])) {
$this->error(__('Invalid SNMP transport'));
}
if (! in_array($this->option('auth-protocol'), \LibreNMS\SNMPCapabilities::supportedAuthAlgorithms())) {
$this->error(__('Invalid authentication protocol'));
}
if (! in_array($this->option('privacy-protocol'), \LibreNMS\SNMPCapabilities::supportedCryptoAlgorithms())) {
$this->error(__('Invalid privacy protocol'));
}
$port = (int) $this->option('port');
if ($port < 1 || $port > 65535) {
$this->error(__('Port should be 1-65535'));
}
// build additional
$additional = [
'os' => $this->option('os'),
'hardware' => $this->option('hardware'),
'sysName' => $this->option('sysName'),
];
if ($this->option('ping-only')) {
$additional['snmp_disable'] = 1;
} elseif ($this->option('ping-fallback')) {
$additional['ping_fallback'] = 1;
}
if ($this->option('community')) {
$community_config = \LibreNMS\Config::get('snmp.community');
array_unshift($community_config, $this->option('community'));
\LibreNMS\Config::set('snmp.community', $community_config);
}
$auth = $this->option('auth-password');
$priv = $this->option('privacy-password');
$v3_config = \LibreNMS\Config::get('snmp.v3');
array_unshift($v3_config, [
'authlevel' => ($auth ? 'auth' : 'noAuth') . (($priv && $auth) ? 'Priv' : 'NoPriv'),
'authname' => $this->option('security-name'),
'authpass' => $this->option('auth-password'),
'authalgo' => $this->option('auth-protocol'),
'cryptopass' => $this->option('privacy-password'),
'cryptoalgo' => $this->option('privacy-protocol'),
]);
\LibreNMS\Config::set('snmp.v3', $v3_config);
try {
$init_modules = [];
include base_path('includes/init.php');
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
Debug::set();
if ($verbosity >= 256) {
global $verbose;
$verbose = true;
}
}
$device_id = addHost(
$this->argument('device spec'),
$this->option('v3') ? 'v3' : ($this->option('v2c') ? 'v2c' : ($this->option('v1') ? 'v1' : '')),
$port,
$this->option('transport'),
$this->option('group'),
$this->option('force'),
$this->option('port-association-mode'),
$additional
);
$hostname = Device::where('device_id', $device_id)->value('hostname');
$this->info("Added device $hostname ($device_id)");
return 0;
} catch (HostUnreachableException $e) {
$this->error($e->getMessage() . PHP_EOL . implode(PHP_EOL, $e->getReasons()));
return 1;
} catch (Exception $e) {
$this->error($e->getMessage());
return 3;
}
})->purpose('Add a new device');
Artisan::command('device:remove
{device spec : ' . __('Hostname, IP, or device id to remove') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
(new Process([
base_path('delhost.php'),
$this->argument('device spec'),
]))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose('Remove a device');
Artisan::command('update', function () {
(new Process([base_path('daily.sh')]))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Update LibreNMS and run maintenance routines'));
Artisan::command('poller:ping
{groups?* : ' . __('Optional List of distributed poller groups to poll') . '}
', function () {
// PingCheck::dispatch(new PingCheck($this->argument('groups')));
$command = [base_path('ping.php')];
if ($this->argument('groups')) {
$command[] = '-g';
$command[] = implode(',', $this->argument('groups'));
}
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Check if devices are up or down via icmp'));
Artisan::command('poller:discovery
{device spec : ' . __('Device spec to discover: device_id, hostname, wildcard, odd, even, all, new') . '}
{--o|os= : ' . __('Only devices with the specified operating system') . '}
{--t|type= : ' . __('Only devices with the specified type') . '}
{--m|modules= : ' . __('Specify single module to be run. Comma separate modules, submodules may be added with /') . '}
', function () {
$command = [base_path('discovery.php'), '-h', $this->argument('device spec')];
if ($this->option('os')) {
$command[] = '-o';
$command[] = $this->option('os');
}
if ($this->option('type')) {
$command[] = '-t';
$command[] = $this->option('type');
}
if ($this->option('modules')) {
$command[] = '-m';
$command[] = $this->option('modules');
}
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Discover information about existing devices, defines what will be polled'));
Artisan::command('poller:poll
{device spec : ' . __('Device spec to poll: device_id, hostname, wildcard, odd, even, all') . '}
{--m|modules= : ' . __('Specify single module to be run. Comma separate modules, submodules may be added with /') . '}
{--x|no-data : ' . __('Do not update datastores (RRD, InfluxDB, etc)') . '}
', function () {
$command = [base_path('poller.php'), '-h', $this->argument('device spec')];
if ($this->option('no-data')) {
array_push($command, '-r', '-f', '-p');
}
if ($this->option('modules')) {
$command[] = '-m';
$command[] = $this->option('modules');
}
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Poll data from devices as defined by discovery'));
Artisan::command('poller:alerts', function () {
$command = [base_path('alerts.php')];
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Check for any pending alerts and deliver them via defined transports'));
Artisan::command('poller:billing
{bill id? : ' . __('The bill id to poll') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
$command = [base_path('poll-billing.php')];
if ($this->argument('bill id')) {
$command[] = '-b';
$command[] = $this->argument('bill id');
}
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Collect billing data'));
Artisan::command('poller:services
{device spec : ' . __('Device spec to poll: device_id, hostname, wildcard, all') . '}
{--x|no-data : ' . __('Do not update datastores (RRD, InfluxDB, etc)') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
$command = [base_path('check-services.php')];
if ($this->option('no-data')) {
array_push($command, '-r', '-f', '-p');
}
if ($this->argument('device spec') !== 'all') {
$command[] = '-h';
$command[] = $this->argument('device spec');
}
if (($verbosity = $this->getOutput()->getVerbosity()) >= 128) {
$command[] = '-d';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Update LibreNMS and run maintenance routines'));
Artisan::command('poller:billing-calculate
{--c|clear-history : ' . __('Delete all billing history') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
$command = [base_path('billing-calculate.php')];
if ($this->option('clear-history')) {
$command[] = '-r';
}
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Run billing calculations'));
Artisan::command('scan
{network?* : ' . __('CIDR notation network(s) to scan, can be ommited if \'nets\' config is set') . '}
{--P|ping-only : ' . __('Add the device as a ping only device if it replies to ping but not SNMP') . '}
{--t|threads=32 : ' . __('How many IPs to scan at a time, more will increase the scan speed, but could overload your system') . '}
{--l|legend : ' . __('Print the legend') . '}
', function () {
/** @var \Illuminate\Console\Command $this */
$command = [base_path('snmp-scan.py')];
if (empty($this->argument('network')) && ! \LibreNMS\Config::has('nets')) {
$this->error(__('Network is required if \'nets\' is not set in the config'));
return 1;
}
if ($this->option('ping-only')) {
$command[] = '-P';
}
$command[] = '-t';
$command[] = $this->option('threads');
if ($this->option('legend')) {
$command[] = '-l';
}
$verbosity = $this->getOutput()->getVerbosity();
if ($verbosity >= 64) {
$command[] = '-v';
if ($verbosity >= 128) {
$command[] = '-v';
if ($verbosity >= 256) {
$command[] = '-v';
}
}
}
$command = array_merge($command, $this->argument('network'));
(new Process($command))->setTimeout(null)->setIdleTimeout(null)->setTty(true)->run();
})->purpose(__('Scan the network for hosts and try to add them to LibreNMS'));