2010-09-03 18:26:59 +00:00
|
|
|
#!/usr/bin/env php
|
2009-09-07 11:07:59 +00:00
|
|
|
<?php
|
2008-03-12 20:01:46 +00:00
|
|
|
|
2012-05-09 10:01:42 +00:00
|
|
|
/**
|
2013-10-28 12:01:36 -07:00
|
|
|
* Observium
|
2011-09-08 12:24:18 +00:00
|
|
|
*
|
2013-10-28 12:01:36 -07:00
|
|
|
* This file is part of Observium.
|
2012-05-09 10:01:42 +00:00
|
|
|
*
|
2013-10-28 12:01:36 -07:00
|
|
|
* @package observium
|
2012-05-09 10:01:42 +00:00
|
|
|
* @subpackage cli
|
2013-10-28 12:01:36 -07:00
|
|
|
* @author Adam Armstrong <adama@memetic.org>
|
|
|
|
* @copyright (C) 2006 - 2012 Adam Armstrong
|
2011-09-08 12:24:18 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-04-08 10:44:41 +00:00
|
|
|
chdir(dirname($argv[0]));
|
|
|
|
|
2010-02-27 14:44:38 +00:00
|
|
|
include("includes/defaults.inc.php");
|
2011-03-16 09:29:46 +00:00
|
|
|
include("config.php");
|
2012-05-09 10:01:42 +00:00
|
|
|
include("includes/definitions.inc.php");
|
2008-03-19 19:16:16 +00:00
|
|
|
include("includes/functions.php");
|
2007-04-03 14:10:23 +00:00
|
|
|
|
2015-03-19 22:06:13 +00:00
|
|
|
$options = getopt("g: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'];
|
2015-05-09 10:34:55 +01:00
|
|
|
} elseif ($config['distributed_poller_group'] > 0 && $config['distributed_poller'] === TRUE) {
|
|
|
|
$poller_group = $config['distributed_poller_group'];
|
2015-03-19 22:06:13 +00:00
|
|
|
}
|
2015-05-09 10:34:55 +01:00
|
|
|
|
2015-03-19 22:06:13 +00:00
|
|
|
if (isset($options['f']) && $options['f'] == 0) {
|
|
|
|
$cmd = array_shift($argv);
|
|
|
|
array_shift($argv);
|
|
|
|
array_unshift($argv, $cmd);
|
|
|
|
$force_add = 1;
|
|
|
|
}
|
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
if (!empty($argv[1]))
|
2011-03-16 09:29:46 +00:00
|
|
|
{
|
2011-10-04 15:05:27 +00:00
|
|
|
$host = strtolower($argv[1]);
|
2011-03-23 09:54:56 +00:00
|
|
|
$community = $argv[2];
|
|
|
|
$snmpver = strtolower($argv[3]);
|
2011-03-14 18:56:26 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
$port = 161;
|
|
|
|
$transport = 'udp';
|
2009-02-02 16:00:11 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
if ($snmpver === "v3")
|
2011-03-23 09:54:56 +00:00
|
|
|
{
|
2012-05-09 16:18:23 +00:00
|
|
|
$seclevel = $community;
|
|
|
|
|
|
|
|
// These values are the same as in defaults.inc.php
|
|
|
|
$v3 = array(
|
|
|
|
'authlevel' => "noAuthNoPriv",
|
2013-11-05 09:33:32 +10:00
|
|
|
'authname' => "root",
|
2012-05-09 16:18:23 +00:00
|
|
|
'authpass' => "",
|
|
|
|
'authalgo' => "MD5",
|
|
|
|
'cryptopass' => "",
|
|
|
|
'cryptoalgo' => "AES"
|
|
|
|
);
|
|
|
|
|
|
|
|
if ($seclevel === "nanp" or $seclevel === "any" or $seclevel === "noAuthNoPriv")
|
|
|
|
{
|
|
|
|
$v3['authlevel'] = "noAuthNoPriv";
|
|
|
|
$v3args = array_slice($argv, 4);
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
while ($arg = array_shift($v3args))
|
|
|
|
{
|
|
|
|
// parse all remaining args
|
|
|
|
if (is_numeric($arg))
|
|
|
|
{
|
|
|
|
$port = $arg;
|
|
|
|
}
|
|
|
|
elseif (preg_match ('/^(' . implode("|",$config['snmp']['transports']) . ')$/', $arg))
|
|
|
|
{
|
|
|
|
$transport = $arg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// should add a sanity check of chars allowed in user
|
|
|
|
$user = $arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($seclevel === "nanp")
|
|
|
|
{ array_push($config['snmp']['v3'], $v3); }
|
|
|
|
|
2015-03-19 22:06:13 +00:00
|
|
|
$device_id = addHost($host, $snmpver, $port, $transport,0,$poller_group,$force_add);
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
}
|
|
|
|
elseif ($seclevel === "anp" or $seclevel === "authNoPriv")
|
|
|
|
{
|
|
|
|
|
|
|
|
$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 ('/^(' . implode("|",$config['snmp']['transports']) . ')$/i', $arg))
|
|
|
|
{
|
|
|
|
$transport = $arg;
|
|
|
|
}
|
2012-05-15 17:03:35 +00:00
|
|
|
elseif (preg_match ('/^(sha|md5)$/i', $arg))
|
2012-05-09 16:18:23 +00:00
|
|
|
{
|
|
|
|
$v3['authalgo'] = $arg;
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
array_push($config['snmp']['v3'], $v3);
|
2015-03-19 22:06:13 +00:00
|
|
|
$device_id = addHost($host, $snmpver, $port, $transport,0,$poller_group,$force_add);
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
}
|
|
|
|
elseif ($seclevel === "ap" or $seclevel === "authPriv")
|
|
|
|
{
|
|
|
|
$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 ('/^(' . implode("|",$config['snmp']['transports']) . ')$/i', $arg))
|
|
|
|
{
|
|
|
|
$transport = $arg;
|
|
|
|
}
|
2012-05-15 17:03:35 +00:00
|
|
|
elseif (preg_match ('/^(sha|md5)$/i', $arg))
|
2012-05-09 16:18:23 +00:00
|
|
|
{
|
|
|
|
$v3['authalgo'] = $arg;
|
|
|
|
}
|
|
|
|
elseif (preg_match ('/^(aes|des)$/i', $arg))
|
|
|
|
{
|
|
|
|
$v3['cryptoalgo'] = $arg;
|
|
|
|
}
|
|
|
|
}
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
array_push($config['snmp']['v3'], $v3);
|
2015-03-19 22:06:13 +00:00
|
|
|
$device_id = addHost($host, $snmpver, $port, $transport,0,$poller_group,$force_add);
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Error or do nothing ?
|
|
|
|
}
|
2011-03-23 09:54:56 +00:00
|
|
|
}
|
2012-05-09 16:18:23 +00:00
|
|
|
else // v1 or v2c
|
2011-03-23 09:54:56 +00:00
|
|
|
{
|
2012-05-09 16:18:23 +00:00
|
|
|
$v2args = array_slice($argv, 2);
|
2011-03-15 16:12:44 +00:00
|
|
|
|
2012-05-09 16:18:23 +00:00
|
|
|
while ($arg = array_shift($v2args))
|
|
|
|
{
|
|
|
|
// parse all remaining args
|
|
|
|
if (is_numeric($arg))
|
|
|
|
{
|
|
|
|
$port = $arg;
|
|
|
|
}
|
|
|
|
elseif (preg_match ('/(' . implode("|",$config['snmp']['transports']) . ')/i', $arg))
|
|
|
|
{
|
|
|
|
$transport = $arg;
|
|
|
|
}
|
|
|
|
elseif (preg_match ('/^(v1|v2c)$/i', $arg))
|
|
|
|
{
|
|
|
|
$snmpver = $arg;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($community)
|
|
|
|
{
|
|
|
|
$config['snmp']['community'] = array($community);
|
|
|
|
}
|
2012-05-15 15:18:57 +00:00
|
|
|
|
2015-03-19 22:06:13 +00:00
|
|
|
$device_id = addHost($host, $snmpver, $port, $transport,0,$poller_group,$force_add);
|
2011-03-23 09:54:56 +00:00
|
|
|
}
|
2012-04-09 15:30:45 +00:00
|
|
|
|
2012-04-08 11:04:43 +00:00
|
|
|
if ($snmpver)
|
|
|
|
{
|
|
|
|
$snmpversions[] = $snmpver;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-05-09 16:18:23 +00:00
|
|
|
$snmpversions = array('v2c', 'v3', 'v1');
|
2012-04-08 11:04:43 +00:00
|
|
|
}
|
2011-10-04 15:05:27 +00:00
|
|
|
|
2012-04-08 11:04:43 +00:00
|
|
|
while (!$device_id && count($snmpversions))
|
|
|
|
{
|
|
|
|
$snmpver = array_shift($snmpversions);
|
2015-03-19 22:06:13 +00:00
|
|
|
$device_id = addHost($host, $snmpver, $port, $transport,0,$poller_group,$force_add);
|
2012-04-08 11:04:43 +00:00
|
|
|
}
|
2011-03-15 16:12:44 +00:00
|
|
|
|
2011-09-20 15:48:12 +00:00
|
|
|
if ($device_id)
|
|
|
|
{
|
2011-09-14 17:26:59 +00:00
|
|
|
$device = device_by_id_cache($device_id);
|
2011-10-04 15:05:27 +00:00
|
|
|
echo("Added device ".$device['hostname']." (".$device_id.")\n");
|
2012-05-09 16:18:23 +00:00
|
|
|
exit;
|
2011-09-14 17:26:59 +00:00
|
|
|
}
|
2012-05-09 16:18:23 +00:00
|
|
|
}
|
2011-03-22 20:27:39 +00:00
|
|
|
|
2015-03-01 17:06:38 +00:00
|
|
|
print $console_color->convert("\n" . $config['project_name_version']." Add Host Tool
|
2011-03-22 20:27:39 +00:00
|
|
|
|
2015-03-19 22:06:13 +00:00
|
|
|
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']) . "]
|
|
|
|
-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.
|
2011-09-20 15:48:12 +00:00
|
|
|
%rRemember to run discovery for the host afterwards.%n
|
2011-03-16 14:23:38 +00:00
|
|
|
|
2011-09-08 12:24:18 +00:00
|
|
|
");
|
2007-04-03 14:10:23 +00:00
|
|
|
|
2011-05-13 11:42:26 +00:00
|
|
|
?>
|