librenms/addhost.php

242 lines
8.7 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/**
* LibreNMS
*
* This file is part of LibreNMS.
*
* @package LibreNMS
* @subpackage cli
* @copyright (C) 2006 - 2012 Adam Armstrong
*/
use LibreNMS\Config;
use LibreNMS\Exceptions\HostUnreachableException;
$init_modules = array();
require __DIR__ . '/includes/init.php';
$options = getopt('Pbg:p:f::');
if (isset($options['g']) && $options['g'] >= 0) {
$cmd = array_shift($argv);
array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
$poller_group = $options['g'];
} elseif (Config::get('distributed_poller') === true) {
$poller_group = Config::get('distributed_poller_group');
} else {
$poller_group = 0;
}
if (isset($options['f']) && $options['f'] == 0) {
$cmd = array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
$force_add = true;
} else {
$force_add = false;
}
$port_assoc_mode = Config::get('default_port_association_mode');
$valid_assoc_modes = get_port_assoc_modes();
if (isset($options['p'])) {
$port_assoc_mode = $options['p'];
if (! in_array($port_assoc_mode, $valid_assoc_modes)) {
echo "Invalid port association mode '" . $port_assoc_mode . "'\n";
echo 'Valid modes: ' . join(', ', $valid_assoc_modes) . "\n";
exit(1);
}
$cmd = array_shift($argv);
array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
}
if (isset($options['P'])) {
$cmd = array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
}
if (isset($options['b'])) {
$cmd = array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
}
$transports_regex = implode('|', Config::get('snmp.transports'));
2015-07-13 18:10:26 +00:00
if (!empty($argv[1])) {
$host = strtolower($argv[1]);
$community = $argv[2];
$snmpver = strtolower($argv[3]);
$port = 161;
$transport = 'udp';
$additional = array();
if (isset($options['b'])) {
$additional = array(
'ping_fallback' => 1,
);
}
if (isset($options['P'])) {
$community = '';
$snmpver = 'v2c';
$additional = array(
'snmp_disable' => 1,
'os' => $argv[2] ? mres($argv[2]) : "ping",
'hardware' => $argv[3] ? mres($argv[3]) : '',
);
} elseif ($snmpver === 'v3') {
2015-07-13 18:10:26 +00:00
$seclevel = $community;
// These values are the same as in defaults.inc.php
$v3 = array(
'authlevel' => 'noAuthNoPriv',
'authname' => 'root',
'authpass' => '',
'authalgo' => 'MD5',
'cryptopass' => '',
'cryptoalgo' => 'AES',
);
// v3
2015-07-13 18:10:26 +00:00
if ($seclevel === 'nanp' or $seclevel === 'any' or $seclevel === 'noAuthNoPriv') {
$v3['authlevel'] = 'noAuthNoPriv';
$v3args = array_slice($argv, 4);
while ($arg = array_shift($v3args)) {
// parse all remaining args
if (is_numeric($arg)) {
$port = $arg;
} elseif (preg_match('/^(' . $transports_regex . ')$/', $arg)) {
2015-07-13 18:10:26 +00:00
$transport = $arg;
} else {
2015-07-13 18:10:26 +00:00
// should add a sanity check of chars allowed in user
$user = $arg;
}
}
if ($seclevel === 'nanp') {
$v3_config = Config::get('snmp.v3');
array_unshift($v3_config, $v3);
Config::set('snmp.v3', $v3_config);
2015-07-13 18:10:26 +00:00
}
} elseif ($seclevel === 'anp' or $seclevel === 'authNoPriv') {
2015-07-13 18:10:26 +00:00
$v3['authlevel'] = 'authNoPriv';
$v3args = array_slice($argv, 4);
$v3['authname'] = array_shift($v3args);
$v3['authpass'] = array_shift($v3args);
while ($arg = array_shift($v3args)) {
// parse all remaining args
if (is_numeric($arg)) {
$port = $arg;
} elseif (preg_match('/^(' . $transports_regex . ')$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$transport = $arg;
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$v3['authalgo'] = $arg;
} else {
2015-07-13 18:10:26 +00:00
echo 'Invalid argument: '.$arg."\n";
exit(1);
2015-07-13 18:10:26 +00:00
}
}
$v3_config = Config::get('snmp.v3');
array_unshift($v3_config, $v3);
Config::set('snmp.v3', $v3_config);
} elseif ($seclevel === 'ap' or $seclevel === 'authPriv') {
2015-07-13 18:10:26 +00:00
$v3['authlevel'] = 'authPriv';
$v3args = array_slice($argv, 4);
$v3['authname'] = array_shift($v3args);
$v3['authpass'] = array_shift($v3args);
$v3['cryptopass'] = array_shift($v3args);
while ($arg = array_shift($v3args)) {
// parse all remaining args
if (is_numeric($arg)) {
$port = $arg;
} elseif (preg_match('/^(' . $transports_regex . ')$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$transport = $arg;
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$v3['authalgo'] = $arg;
} elseif (preg_match('/^(aes|des)$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$v3['cryptoalgo'] = $arg;
} else {
2015-07-13 18:10:26 +00:00
echo 'Invalid argument: '.$arg."\n";
exit(1);
2015-07-13 18:10:26 +00:00
}
}//end while
$v3_config = Config::get('snmp.v3');
array_unshift($v3_config, $v3);
Config::set('snmp.v3', $v3_config);
}
} else {
// v2c or v1
2015-07-13 18:10:26 +00:00
$v2args = array_slice($argv, 2);
while ($arg = array_shift($v2args)) {
// parse all remaining args
if (is_numeric($arg)) {
$port = $arg;
} elseif (preg_match('/(' . $transports_regex . ')/i', $arg)) {
2015-07-13 18:10:26 +00:00
$transport = $arg;
} elseif (preg_match('/^(v1|v2c)$/i', $arg)) {
2015-07-13 18:10:26 +00:00
$snmpver = $arg;
}
}
2015-07-13 18:10:26 +00:00
if ($community) {
$comm_config = Config::get('snmp.community');
array_unshift($comm_config, $community);
Config::set('snmp.community', $comm_config);
}
2015-07-13 18:10:26 +00:00
}//end if
try {
$device_id = addHost($host, $snmpver, $port, $transport, $poller_group, $force_add, $port_assoc_mode, $additional);
$device = device_by_id_cache($device_id);
echo "Added device {$device['hostname']} ($device_id)\n";
exit(0);
} catch (HostUnreachableException $e) {
print_error($e->getMessage());
foreach ($e->getReasons() as $reason) {
echo " $reason\n";
}
exit(2);
} catch (Exception $e) {
print_error($e->getMessage());
exit(3);
}
} else {
c_echo(
Consolidate configuration settings and implement dynamic webui settings (#9809) * initial db/defaults dump * numeric values * Remove $config['time'] * Use config_definitions.json * try setting definition as a complete array * a little more * fix format * WIP converting dynamic setting pages * rewriting the webui * remove legacy and add translations * finish email section * improve navigation update js a bit * Import the remaining existing settings * Update backend still some wip migration * fix config loading (db not overriding defaults) * some cleanup * more array collapsing * update settings * Settings search * add purge settings to ui, order groups and sections * collapse more arrays * Auth definitions WIP * remove needless slash escapes * add warning to json * moving settings from defaults.inc * remove slow_statistics * Move rrdtool settings to the poller section * Use translations more consistently * removing more settings from defaults.inc.php * show setting name in tooltip * allow help text to be shown on mobile * disable settings that are set in config.php * Implement undo and reset to default. * Vue.js functional * Vue.js WIP * Implement tabs component * accordion WIP * lodash ver update * two items * Accordion somewhat working * hash navigation * Refine Accordion * Fix up tab styling a bit * Cleaner tab selected property Hide html while loading * spinner? * Icon support property for accordion to active * WIP * Tabs/Sections from ajax * Setting Component skeletons * Dynamic Component resolution * Basic functionality * toggle class * Refactor components * translate tabs and accordions * simple array attempt * improve readonly tooltip * array styling * array value editing * organize snmp info * Handle initial tab/section in url * Use Laravel to parse tab/section, dump old * Draggable array entries * v-tooltip, for clickable (and touch) help tooltips disable draggable * Navigation WIP * Navigation WIP * groups computed * filter settings * fix event reference * vue.js i18n initial * missing description = just setting name * en fallback * tidy up the language support and js generation * persist value to db * fix issue with 0 * Delete settings from DB instead of setting them to default * ldap-groups fixup style * Default dashboard selection * fix array of options instead of object * allow custom validation for settings * translate options in SettingSelect * SNMP v3 WIP * fix setting arrays * Split persist out of set * Hook up events for SNMP v3 Auth correct Config::persist behaviour with arrays * dependent settings (primitive for now) actually update the settings values in the LibrenmsSettings component * more complex "when" behaviour * remove un-needed seeder * add poller_modules.cisco-qfp * remove en.json (disable warning) * don't set default for log_dir or log_file, otherwise it won't be processed correctly * Fix module order add some missing settings * more config corrections * correct graphs correct loading values set to null (although it should have no difference) remove project_name_version * Add nfsen settings. Docs are very confusing, so might have flubbed something remove option for array definition of select option as numeric indexes messes it up * Correct more upstream config differences * Config cleanup after a bunch of merges. * Fixes * add version tags to js/css files remove old js * Print out full settings list read-only * Add http_proxy setting fix indents in config_definitions.json * repeaters default is 0 (aka 20) * cleanups * rewrite the dynamic config docs * add language docs * Don't show snmp v3 auth add/remove if disabled by config.php
2019-10-16 21:22:05 +00:00
"\n". Config::get('project_name').' Add Host Tool
Usage (SNMPv1/2c) : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname or IP%n> [community] [v1|v2c] [port] [' . $transports_regex . ']
Usage (SNMPv3) :
Config Defaults : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname or IP%n> any v3 [user] [port] [' . $transports_regex . ']
No Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname or IP%n> nanp v3 [user] [port] [' . $transports_regex . ']
Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname or IP%n> anp v3 <user> <password> [md5|sha] [port] [' . $transports_regex . ']
Auth, Priv : ./addhost.php [-g <poller group>] [-f] [-b] [-p <port assoc mode>] <%Whostname or IP%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|des] [port] [' . $transports_regex . ']
Usage (ICMP only) : ./addhost.php [-g <poller group>] [-f] -P <%Whostname or IP%n> [os] [hardware]
-g <poller group> allows you to add a device to be pinned to a specific poller when using distributed polling. X can be any number associated with a poller group
-f forces the device to be added by skipping the icmp and snmp check against the host.
-p <port assoc mode> allow you to set a port association mode for this device. By default ports are associated by \'ifIndex\'.
For Linux/Unix based devices \'ifName\' or \'ifDescr\' might be useful for a stable iface mapping.
The default for this installation is \'' . Config::get('default_port_association_mode') . '\'
Valid port assoc modes are: ' . join(', ', $valid_assoc_modes) . '
-b Add the host with SNMP if it replies to it, otherwise only ICMP.
-P Add the host with only ICMP, no SNMP or OS discovery.
2015-07-13 18:10:26 +00:00
%rRemember to run discovery for the host afterwards.%n
'
);
exit(1);
}