ipv4/ipv6 udp/tcp transport support patch

git-svn-id: http://www.observium.org/svn/observer/trunk@1860 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
Adam Amstrong
2011-03-15 16:12:44 +00:00
parent 0a86e1c675
commit ba2e142c09
8 changed files with 85 additions and 50 deletions

View File

@@ -20,8 +20,15 @@ if (isset($argv[1]) && $argv[1])
$port = 161;
}
$transport = $argv[5];
if (!$snmpver) $snmpver = "v2c";
if ($community) { unset($config['snmp']['community']); $config['snmp']['community'][] = $community; }
if ($community) {
unset($config['snmp']['community']);
$config['snmp']['community'][] = $community;
}
$device = deviceArray($host, $community, $snmpver, $port, $transport);
list($hostshort) = explode(".", $host);
if (mysql_result(mysql_query("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '".mres($host)."'"), 0) == '0' )
@@ -32,18 +39,18 @@ if (isset($argv[1]) && $argv[1])
{
# FIXME should be a foreach $config['snmp']['community'][0] as $community
$community = $config['snmp']['community'][0];
if (isSNMPable($argv[1], $community, $snmpver, $port))
{
$snmphost = trim(str_replace("\"", "", shell_exec($config['snmpget'] ." -m SNMPv2-MIB -Oqv -$snmpver -c ".$config['snmp']['community'][0]." $host:$port sysName.0")));
if ($snmphost == "" || ($snmphost && ($snmphost == $host || $hostshort = $host)))
{
$return = createHost ($host, $community, $snmpver, $port);
if ($return) { echo($return . "\n"); } else { echo("Adding $host failed\n"); }
} else { echo("Given hostname does not match SNMP-read hostname ($snmphost)!\n"); }
} else { echo("Could not reach $host with SNMP\n"); }
if ( isSNMPable($device))
{
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if ($snmphost == "" || ($snmphost && ($snmphost == $host || $hostshort = $host)))
{
$return = createHost ($host, $community, $snmpver, $port, $transport);
if($return) { echo($return . "\n"); } else { echo("Adding $host failed\n"); }
} else { echo("Given hostname does not match SNMP-read hostname ($snmphost)!\n"); }
} else { echo("Could not reach $host with SNMP\n"); }
} else { echo("Could not ping $host\n"); }
} else { echo("Could not resolve $host\n"); }
} else { echo("Already got host $host\n"); }
} else { echo("Add Host Tool\nUsage: ./addhost.php <hostname> [community] [v1|v2c] [port]\n"); }
} else { echo("Add Host Tool\nUsage: ./addhost.php <hostname> [community] [v1|v2c] [port] [udp|udp6|tcp|tcp6]\n"); }
?>
?>

View File

@@ -15,3 +15,5 @@ ALTER TABLE `devices` ADD `timeout` INT NULL DEFAULT NULL AFTER `port`;
ALTER TABLE `devices` ADD `retries` INT NULL DEFAULT NULL AFTER `timeout`;
ALTER TABLE `perf_times` CHANGE `duration` `duration` DOUBLE( 8, 2 ) NOT NULL
ALTER TABLE `sensors` ADD `poller_type` VARCHAR(16) NOT NULL DEFAULT 'snmp' AFTER `device_id`;
## Add transport
ALTER TABLE `devices` ADD `transport` VARCHAR(16) NOT NULL DEFAULT 'udp' AFTER `port`;

View File

@@ -49,6 +49,18 @@ echo("<table cellpadding=0 cellspacing=0><tr><td>
<td colspan='3'><input name='port' size='20' value='" . $device['port'] . "'></input>
</td>
</tr>
<tr>
<td width='300'><div align='right'>SNMP Transport</div></td>
<td colspan='3'>
<select name='transport'>");
foreach ($config['snmp']['transports'] as $transport) {
echo ("<option value='".$transport."'");
if ($transport == $device['transport']) { echo (" selected='selected'"); }
echo (">".$transport."</option>");
}
echo(" </select>
</td>
</tr>
<tr>
<td width='300'><div align='right'>SNMP Timeout</div></td>
<td colspan='3'><input name='timeout' size='20' value='" . $device['timeout'] . "'></input>&nbsp;

View File

@@ -62,6 +62,7 @@ $config['ports_page_default'] = "details/"; ## eg "details/" "graphs/bits/"
#$config['snmp']['retries'] = 5; # how many times to retry the query
$config['snmp']['community'][] = "public"; # Communities to try during discovery (dangerous)
$config['snmp']['internal'] = false; # Enable php_snmp functions to make gets faster
$config['snmp']['transports'] = array('udp', 'udp6', 'tcp', 'tcp6');
### Autodiscovery Settings

View File

@@ -6,7 +6,7 @@ if ($device['os_group'] == "unix")
{
# FIXME snmp_walk
# ObserverNMS-style temperature
$oids = shell_exec($config['snmpwalk'] . " -M " . $config['mibdir'] . " -M " . $config['mibdir'] . " -".$device['snmpver']." -m SNMPv2-SMI -Osqn -CI -c ".$device['community']." ".$device['hostname'].":".$device['port']." .1.3.6.1.4.1.2021.7891 | sed s/.1.3.6.1.4.1.2021.7891.// | grep '.1.1 ' | grep -v '.101.' | cut -d'.' -f 1");
$oids = shell_exec($config['snmpwalk'] . " -M " . $config['mibdir'] . " -M " . $config['mibdir'] . " -".$device['snmpver']." -m SNMPv2-SMI -Osqn -CI -c ".$device['community']." ".$device['transport'].":".$device['hostname'].":".$device['port']." .1.3.6.1.4.1.2021.7891 | sed s/.1.3.6.1.4.1.2021.7891.// | grep '.1.1 ' | grep -v '.101.' | cut -d'.' -f 1");
$oids = trim($oids);
if ($oids) echo("Observer-Style ");
foreach (explode("\n",$oids) as $oid)

View File

@@ -291,7 +291,7 @@ function delete_device($id)
return $ret;
}
function addHost($host, $community, $snmpver, $port = 161)
function addHost($host, $community, $snmpver, $port = 161, $transport = 'udp')
{
global $config;
list($hostshort) = explode(".", $host);
@@ -305,7 +305,7 @@ function addHost($host, $community, $snmpver, $port = 161)
$snmphost = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -Oqv -$snmpver -c $community $host:$port sysName.0");
if ($snmphost == $host || $hostshort = $host)
{
createHost ($host, $community, $snmpver, $port);
createHost ($host, $community, $snmpver, $port, $transport);
} else { echo("Given hostname does not match SNMP-read hostname!\n"); }
} else { echo("Already got host $host\n"); }
} else { echo("Could not ping $host\n"); }
@@ -328,6 +328,18 @@ function scanUDP ($host, $port, $timeout)
} else { fclose($handle); return 0; }
}
function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp')
{
$device = array();
$device['hostname'] = $host;
$device['port'] = $port;
$device['community'] = $community;
$device['snmpver'] = $snmpver;
$device['transport'] = $transport;
return $device;
}
function netmask2cidr($netmask)
{
$addr = Net_IPv4::parseAddress("1.2.3.4/$netmask");
@@ -372,17 +384,17 @@ function formatUptime($diff, $format="long")
return trim($uptime);
}
function isSNMPable($hostname, $community, $snmpver, $port)
function isSNMPable($device)
{
global $config;
# FIXME internalize
$pos = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -$snmpver -c $community -t 1 $hostname:$port sysObjectID.0");
if ($pos == '')
{
return false;
} else {
return true;
}
global $config;
$pos = snmp_get($device, "sysObjectID.0", "-Oqv", "SNMPv2-MIB");
if ($pos === '' || $pos === false)
{
return false;
} else {
return true;
}
}
function isPingable($hostname)
@@ -486,14 +498,15 @@ function fixIOSHardware($hardware)
}
function createHost ($host, $community, $snmpver, $port = 161)
function createHost ($host, $community, $snmpver, $port = 161, $proto = 'udp')
{
$host = trim(strtolower($host));
$device = array('hostname' => $host, 'community' => $community, 'snmpver' => $snmpver, 'port' => $port);
$host_os = getHostOS($device);
$device = deviceArray($host, $community, $snmpver, $port, $proto);
$host_os = getHostOS($device);
if ($host_os)
{
$sql = mysql_query("INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `os`, `status`,`snmpver`) VALUES ('$host', '$host', '$community', '$port', '$host_os', '1','$snmpver')");
$sql = mysql_query("INSERT INTO `devices` (`hostname`, `sysName`, `community`, `port`, `transport`, `os`, `status`,`snmpver`) VALUES ('$host', '$host', '$community', '$port', '$transport', '$host_os', '1','$snmpver')");
if (mysql_affected_rows())
{
$device_id = mysql_result(mysql_query("SELECT device_id FROM devices WHERE hostname = '$host'"),0);
@@ -514,7 +527,7 @@ function createHost ($host, $community, $snmpver, $port = 161)
function isDomainResolves($domain)
{
return gethostbyname($domain) != $domain;
return count(dns_get_record($domain)) != 0;
}
function hoststatus($id)
@@ -772,4 +785,4 @@ function include_dir($dir, $regex = "")
}
}
?>
?>

View File

@@ -34,11 +34,11 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
{
if ($device['snmpver'] == "v2c")
{
$data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
$data = @snmp2_get($device['transport'].":".$device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
}
elseif ($device['snmpver'] == "v1")
{
$data = @snmpget($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
$data = @snmpget($device['transport'].":".$device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
}
list($oid, $index) = explode(".", $oid);
@@ -56,7 +56,7 @@ function snmp_get_multi($device, $oids, $options = "-OQUs", $mib = NULL, $mibdir
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oids;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oids;
if (!$debug) { $cmd .= " 2>/dev/null"; }
if ($debug) { echo("$cmd\n"); }
$data = trim(shell_exec($cmd));
@@ -112,9 +112,9 @@ function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL)
if (isset($timeout)) { $timeout = $timeout*1000*1000; }
if ($device['snmpver'] == "v2c")
{
$data = @snmp2_get($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
} elseif ($device['snmpver'] == "v1") {
$data = @snmpget($device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
$data = @snmp2_get($device['transport'].":".$device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
} elseif ( $device['snmpver'] == "v1") {
$data = @snmpget($device['transport'].":".$device['hostname'].":".$device['port'], $device['community'], $oid, $timeout, $retries);
}
if ($debug) { print "DEBUG: $oid: $data\n"; }
}
@@ -128,7 +128,7 @@ function snmp_get($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL)
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
if ($debug) { echo("$cmd\n"); }
$data = trim(shell_exec($cmd));
@@ -178,7 +178,7 @@ function snmp_walk($device, $oid, $options = NULL, $mib = NULL, $mibdir = NULL)
if ($mibdir) { $cmd .= " -M " . $mibdir; } else { $cmd .= " -M ".$config['mibdir']; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
if ($debug) { echo("$cmd\n"); }
@@ -235,8 +235,8 @@ function snmpwalk_cache_cip($device, $oid, $array, $mib = 0)
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
@@ -440,7 +440,7 @@ function snmpwalk_cache_twopart_oid($device, $oid, $array, $mib = 0)
if ($mib) { $cmd .= " -m $mib"; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
$device_id = $device['device_id'];
@@ -490,7 +490,7 @@ function snmpwalk_cache_threepart_oid($device, $oid, $array, $mib = 0)
if ($mib) { $cmd .= " -m $mib"; }
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
@@ -542,7 +542,7 @@ function snmp_cache_slotport_oid($oid, $device, $array, $mib = 0)
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." ".$oid;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$oid;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
$device_id = $device['device_id'];
@@ -597,7 +597,7 @@ function snmp_cache_port_oids($oids, $port, $device, $array, $mib=0)
$cmd .= " -M ".$config['install_dir']."/mibs/";
if ($mib) { $cmd .= " -m $mib"; }
$cmd .= " -t " . $timeout . " -r " . $retries;
$cmd .= " ".$device['hostname'].":".$device['port']." ".$string;
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." ".$string;
if (!$debug) { $cmd .= " 2>/dev/null"; }
$data = trim(shell_exec($cmd));
$x=0;
@@ -636,7 +636,7 @@ function snmp_cache_portIfIndex($device, $array)
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." portIfIndex";
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portIfIndex";
$output = trim(shell_exec($cmd));
$device_id = $device['device_id'];
@@ -675,7 +675,7 @@ function snmp_cache_portName($device, $array)
$cmd .= " -M ".$config['install_dir']."/mibs/";
if (isset($timeout)) { $cmd .= " -t " . $timeout; }
if (isset($retries)) { $cmd .= " -r " . $retries; }
$cmd .= " ".$device['hostname'].":".$device['port']." portName";
$cmd .= " ".$device['transport'].":".$device['hostname'].":".$device['port']." portName";
$output = trim(shell_exec($cmd));
$device_id = $device['device_id'];
#echo("Caching: portName\n");
@@ -696,4 +696,4 @@ function snmp_cache_portName($device, $array)
return $array;
}
?>
?>

View File

@@ -106,7 +106,7 @@ while ($device = mysql_fetch_assoc($device_query))
$device['pingable'] = isPingable($device['hostname']);
if ($device['pingable'])
{
$device['snmpable'] = isSNMPable($device['hostname'], $device['community'], $device['snmpver'], $device['port']);
$device['snmpable'] = isSNMPable($device);
if ($device['snmpable'])
{
$status = "1";
@@ -368,4 +368,4 @@ unset($config); ### Remove this for testing
#print_r(get_defined_vars());
?>
?>