Add support for setting port association mode when adding a host.

This adds a '-p' switch to addhost.php and a select box to the WebUI to
  set the port association mode for newly added devices.

Signed-off-by: Maximilian Wilhelm <max@rfc2324.org>
This commit is contained in:
Maximilian Wilhelm
2016-01-21 22:04:52 +01:00
parent 4d78a0261e
commit 2ae99e50f5
2 changed files with 51 additions and 12 deletions

View File

@@ -19,7 +19,7 @@ require 'config.php';
require 'includes/definitions.inc.php';
require 'includes/functions.php';
$options = getopt('g:f::');
$options = getopt('g:p:f::');
if (isset($options['g']) && $options['g'] >= 0) {
$cmd = array_shift($argv);
@@ -39,6 +39,22 @@ if (isset($options['f']) && $options['f'] == 0) {
$force_add = 1;
}
$port_assoc_mode = $config['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;
}
$cmd = array_shift($argv);
array_shift($argv);
array_shift($argv);
array_unshift($argv, $cmd);
}
if (!empty($argv[1])) {
$host = strtolower($argv[1]);
$community = $argv[2];
@@ -82,7 +98,7 @@ if (!empty($argv[1])) {
array_push($config['snmp']['v3'], $v3);
}
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
}
else if ($seclevel === 'anp' or $seclevel === 'authNoPriv') {
$v3['authlevel'] = 'authNoPriv';
@@ -108,7 +124,7 @@ if (!empty($argv[1])) {
}
array_push($config['snmp']['v3'], $v3);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
}
else if ($seclevel === 'ap' or $seclevel === 'authPriv') {
$v3['authlevel'] = 'authPriv';
@@ -138,7 +154,7 @@ if (!empty($argv[1])) {
}//end while
array_push($config['snmp']['v3'], $v3);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
}
else {
// Error or do nothing ?
@@ -164,7 +180,7 @@ if (!empty($argv[1])) {
$config['snmp']['community'] = array($community);
}
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
}//end if
if ($snmpver) {
@@ -180,7 +196,7 @@ if (!empty($argv[1])) {
while (!$device_id && count($snmpversions)) {
$snmpver = array_shift($snmpversions);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add);
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
}
if ($device_id) {
@@ -193,14 +209,18 @@ if (!empty($argv[1])) {
print $console_color->convert(
"\n".$config['project_name_version'].' Add Host Tool
Usage (SNMPv1/2c): ./addhost.php [-g <poller group>] [-f] <%Whostname%n> [community] [v1|v2c] [port] ['.implode('|', $config['snmp']['transports']).']
Usage (SNMPv3) : Config Defaults : ./addhost.php [-g <poller group>] [-f]<%Whostname%n> any v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
No Auth, No Priv : ./addhost.php [-g <poller group>] [-f]<%Whostname%n> nanp v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
Auth, No Priv : ./addhost.php [-g <poller group>] [-f]<%Whostname%n> anp v3 <user> <password> [md5|sha] [port] ['.implode('|', $config['snmp']['transports']).']
Auth, Priv : ./addhost.php [-g <poller group>] [-f]<%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|dsa] [port] ['.implode('|', $config['snmp']['transports']).']
Usage (SNMPv1/2c): ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] ['.implode('|', $config['snmp']['transports']).']
Usage (SNMPv3) : Config Defaults : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> any v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
No Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> nanp v3 [user] [port] ['.implode('|', $config['snmp']['transports']).']
Auth, No Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> anp v3 <user> <password> [md5|sha] [port] ['.implode('|', $config['snmp']['transports']).']
Auth, Priv : ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> ap v3 <user> <password> <enckey> [md5|sha] [aes|dsa] [port] ['.implode('|', $config['snmp']['transports']).']
-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['default_port_association_mode'] . '\'
Valid port assoc modes are: ' . join (', ', $valid_assoc_modes) . '
%rRemember to run discovery for the host afterwards.%n
'