';
// first load enabled, after that check snmp variable
$snmp_enabled = !isset($_POST['hostname']) || isset($_POST['snmp']);
if (!empty($_POST['hostname'])) {
$hostname = clean($_POST['hostname']);
if (!is_valid_hostname($hostname) && !IP::isValid($hostname)) {
print_error("Invalid hostname or IP: $hostname");
}
if (LegacyAuth::user()->hasGlobalRead()) {
// Settings common to SNMPv2 & v3
if ($_POST['port']) {
$port = clean($_POST['port']);
} else {
$port = $config['snmp']['port'];
}
if ($_POST['transport']) {
$transport = clean($_POST['transport']);
} else {
$transport = 'udp';
}
$additional = array();
if (!$snmp_enabled) {
$snmpver = 'v2c';
$additional = array(
'snmp_disable' => 1,
'os' => $_POST['os'] ? mres($_POST['os_id']) : "ping",
'hardware' => mres($_POST['hardware']),
'sysName' => mres($_POST['sysName'])
);
} elseif ($_POST['snmpver'] === 'v2c' || $_POST['snmpver'] === 'v1') {
if ($_POST['community']) {
$config['snmp']['community'] = array(clean($_POST['community'], false));
}
$snmpver = clean($_POST['snmpver']);
print_message("Adding host $hostname communit".(count($config['snmp']['community']) == 1 ? 'y' : 'ies').' '.implode(', ', $config['snmp']['community'])." port $port using $transport");
} elseif ($_POST['snmpver'] === 'v3') {
$v3 = array(
'authlevel' => clean($_POST['authlevel']),
'authname' => clean($_POST['authname'], false),
'authpass' => clean($_POST['authpass'], false),
'authalgo' => clean($_POST['authalgo']),
'cryptopass' => clean($_POST['cryptopass'], false),
'cryptoalgo' => clean($_POST['cryptoalgo'], false),
);
array_push($config['snmp']['v3'], $v3);
$snmpver = 'v3';
print_message("Adding SNMPv3 host $hostname port $port");
} else {
print_error('Unsupported SNMP Version. There was a dropdown menu, how did you reach this error ?');
}//end if
$poller_group = clean($_POST['poller_group']);
$force_add = ($_POST['force_add'] == 'on');
$port_assoc_mode = clean($_POST['port_assoc_mode']);
try {
$device_id = addHost($hostname, $snmpver, $port, $transport, $poller_group, $force_add, $port_assoc_mode, $additional);
$link = generate_device_url(array('device_id' => $device_id));
print_message("Device added
$hostname ($device_id)");
} catch (HostUnreachableException $e) {
print_error($e->getMessage());
foreach ($e->getReasons() as $reason) {
print_error($reason);
}
} catch (Exception $e) {
print_error($e->getMessage());
}
} else {
print_error("You don't have the necessary privileges to add hosts.");
}
}
echo '