mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
updates to addhost and cleanups. add ability to do colour on console :D :D :D
git-svn-id: http://www.observium.org/svn/observer/trunk@2430 61d68cd4-352d-0410-923a-c4978735b2b8
This commit is contained in:
47
addhost.php
47
addhost.php
@ -1,6 +1,19 @@
|
||||
#!/usr/bin/env php
|
||||
<?php
|
||||
|
||||
/* Observium Network Management and Monitoring System
|
||||
* Copyright (C) 2006-2011, Observium Developers - http://www.observium.org
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* See COPYING for more details.
|
||||
*/
|
||||
|
||||
$debug=1;
|
||||
|
||||
include("includes/defaults.inc.php");
|
||||
include("config.php");
|
||||
include("includes/functions.php");
|
||||
@ -37,36 +50,18 @@ if (isset($argv[1]) && $argv[1])
|
||||
$config['snmp']['community'][] = $community;
|
||||
}
|
||||
|
||||
list($hostshort) = explode(".", $host);
|
||||
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0')
|
||||
{
|
||||
if (isDomainResolves($argv[1]))
|
||||
{
|
||||
if (isPingable($argv[1]))
|
||||
{
|
||||
$added = 0;
|
||||
addHost($host, $community, $snmpver, $port = '161', $transport = 'udp');
|
||||
|
||||
foreach ($config['snmp']['community'] as $community)
|
||||
{
|
||||
$device = deviceArray($host, $community, $snmpver, $port, $transport);
|
||||
} else {
|
||||
|
||||
if (isSNMPable($device))
|
||||
{
|
||||
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
||||
print Console_Color::convert("
|
||||
Observium v".$config['version']." Add Host Tool
|
||||
|
||||
if ($snmphost == "" || ($snmphost && ($snmphost == $host || $hostshort = $host)))
|
||||
{
|
||||
$added = createHost ($host, $community, $snmpver, $port, $transport);
|
||||
if($added) { echo($added . "\n"); break; }
|
||||
Usage: ./addhost.php <%Whostname%n> [community] [v1|v2c] [port] [" . join("|",$config['snmp']['transports']) . "]
|
||||
|
||||
} else { echo("Given hostname does not match SNMP-read hostname ($snmphost)!\n"); }
|
||||
}
|
||||
}
|
||||
%rRemeber to discover the host afterwards.%n
|
||||
|
||||
if (!$added) { echo("Could not reach $host with given SNMP community\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] [" . join("|",$config['snmp']['transports']) . "]\n"); }
|
||||
");
|
||||
}
|
||||
|
||||
?>
|
||||
|
@ -370,16 +370,6 @@ function humanspeed($speed)
|
||||
return $speed;
|
||||
}
|
||||
|
||||
function print_error($text)
|
||||
{
|
||||
echo('<div class="errorbox"><img src="/images/16/exclamation.png" align="absmiddle"> '.$text.'</div>');
|
||||
}
|
||||
|
||||
function print_message($text)
|
||||
{
|
||||
echo('<div class="messagebox"><img src="/images/16/tick.png" align="absmiddle"> '.$text.'</div>');
|
||||
}
|
||||
|
||||
function devclass($device)
|
||||
{
|
||||
if (isset($device['status']) && $device['status'] == '0') { $class = "list-device-down"; } else { $class = "list-device"; }
|
||||
|
@ -2,6 +2,34 @@
|
||||
|
||||
## Common Functions
|
||||
|
||||
function isCli() {
|
||||
|
||||
if(php_sapi_name() == 'cli' && empty($_SERVER['REMOTE_ADDR'])) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function print_error($text)
|
||||
{
|
||||
if(isCli()) {
|
||||
print Console_Color::convert("%r".$text."%n\n", false);
|
||||
} else {
|
||||
echo('<div class="errorbox"><img src="/images/16/exclamation.png" align="absmiddle"> '.$text.'</div>');
|
||||
}
|
||||
}
|
||||
|
||||
function print_message($text)
|
||||
{
|
||||
if(isCli()) {
|
||||
print Console_Color::convert("%g".$text."%n\n", false);
|
||||
} else {
|
||||
echo('<div class="messagebox"><img src="/images/16/tick.png" align="absmiddle"> '.$text.'</div>');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function delete_port($int_id)
|
||||
{
|
||||
global $config;
|
||||
|
301
includes/console_colour.php
Normal file
301
includes/console_colour.php
Normal file
@ -0,0 +1,301 @@
|
||||
<?php
|
||||
/**
|
||||
* Color.php
|
||||
*
|
||||
* PHP version 4
|
||||
*
|
||||
* Copyright (c) 2007 Stefan Walk
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* @category Console
|
||||
* @package Console_Color
|
||||
* @author Stefan Walk <et@php.net>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
* @link http://pear.php.net/package/Console_Color
|
||||
*/
|
||||
|
||||
|
||||
$GLOBALS['_CONSOLE_COLOR_CODES'] = array (
|
||||
'color' => array(
|
||||
'black' => 30,
|
||||
'red' => 31,
|
||||
'green' => 32,
|
||||
'brown' => 33,
|
||||
'blue' => 34,
|
||||
'purple' => 35,
|
||||
'cyan' => 36,
|
||||
'grey' => 37,
|
||||
'yellow' => 33
|
||||
),
|
||||
'style' => array(
|
||||
'normal' => 0,
|
||||
'bold' => 1,
|
||||
'light' => 1,
|
||||
'underscore' => 4,
|
||||
'underline' => 4,
|
||||
'blink' => 5,
|
||||
'inverse' => 6,
|
||||
'hidden' => 8,
|
||||
'concealed' => 8
|
||||
),
|
||||
'background' => array(
|
||||
'black' => 40,
|
||||
'red' => 41,
|
||||
'green' => 42,
|
||||
'brown' => 43,
|
||||
'yellow' => 43,
|
||||
'blue' => 44,
|
||||
'purple' => 45,
|
||||
'cyan' => 46,
|
||||
'grey' => 47
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
/**
|
||||
* A simple class to use ANSI Colorcodes.
|
||||
*
|
||||
* Of all the functions, you probably only want to use convert() and escape().
|
||||
* They are easier to use. However, if you want to access colorcodes more
|
||||
* directly, look into the other functions.
|
||||
*
|
||||
* @category Console
|
||||
* @package Console_Color
|
||||
* @author Stefan Walk <et@php.net>
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
* @link http://pear.php.net/package/Console_Color
|
||||
*/
|
||||
class Console_Color
|
||||
{
|
||||
|
||||
/**
|
||||
* Returns an ANSI-Controlcode
|
||||
*
|
||||
* Takes 1 to 3 Arguments: either 1 to 3 strings containing the name of the
|
||||
* FG Color, style and BG color, or one array with the indices color, style
|
||||
* or background.
|
||||
*
|
||||
* @param mixed $color Optional.
|
||||
* Either a string with the name of the foreground
|
||||
* color, or an array with the indices 'color',
|
||||
* 'style', 'background' and corresponding names as
|
||||
* values.
|
||||
* @param string $style Optional name of the style
|
||||
* @param string $background Optional name of the background color
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function color($color = null, $style = null, $background = null) // {{{
|
||||
{
|
||||
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
||||
if (is_array($color)) {
|
||||
$style = @$color['style'];
|
||||
$background = @$color['background'];
|
||||
$color = @$color['color'];
|
||||
}
|
||||
|
||||
if ($color == 'reset') {
|
||||
return "\033[0m";
|
||||
}
|
||||
|
||||
$code = array();
|
||||
if (isset($color)) {
|
||||
$code[] = $colors['color'][$color];
|
||||
}
|
||||
|
||||
if (isset($style)) {
|
||||
$code[] = $colors['style'][$style];
|
||||
}
|
||||
|
||||
if (isset($background)) {
|
||||
$code[] = $colors['background'][$background];
|
||||
}
|
||||
|
||||
if (empty($code)) {
|
||||
$code[] = 0;
|
||||
}
|
||||
|
||||
$code = implode(';', $code);
|
||||
return "\033[{$code}m";
|
||||
} // }}}
|
||||
|
||||
/**
|
||||
* Returns a FG color controlcode
|
||||
*
|
||||
* @param string $name Name of controlcode
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function fgcolor($name)
|
||||
{
|
||||
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
||||
return "\033[".$colors['color'][$name].'m';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a style controlcode
|
||||
*
|
||||
* @param string $name Name of controlcode
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function style($name)
|
||||
{
|
||||
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
||||
return "\033[".$colors['style'][$name].'m';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a BG color controlcode
|
||||
*
|
||||
* @param string $name Name of controlcode
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function bgcolor($name)
|
||||
{
|
||||
$colors = &$GLOBALS['_CONSOLE_COLOR_CODES'];
|
||||
return "\033[".$colors['background'][$name].'m';
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts colorcodes in the format %y (for yellow) into ansi-control
|
||||
* codes. The conversion table is: ('bold' meaning 'light' on some
|
||||
* terminals). It's almost the same conversion table irssi uses.
|
||||
* <pre>
|
||||
* text text background
|
||||
* ------------------------------------------------
|
||||
* %k %K %0 black dark grey black
|
||||
* %r %R %1 red bold red red
|
||||
* %g %G %2 green bold green green
|
||||
* %y %Y %3 yellow bold yellow yellow
|
||||
* %b %B %4 blue bold blue blue
|
||||
* %m %M %5 magenta bold magenta magenta
|
||||
* %p %P magenta (think: purple)
|
||||
* %c %C %6 cyan bold cyan cyan
|
||||
* %w %W %7 white bold white white
|
||||
*
|
||||
* %F Blinking, Flashing
|
||||
* %U Underline
|
||||
* %8 Reverse
|
||||
* %_,%9 Bold
|
||||
*
|
||||
* %n Resets the color
|
||||
* %% A single %
|
||||
* </pre>
|
||||
* First param is the string to convert, second is an optional flag if
|
||||
* colors should be used. It defaults to true, if set to false, the
|
||||
* colorcodes will just be removed (And %% will be transformed into %)
|
||||
*
|
||||
* @param string $string String to convert
|
||||
* @param bool $colored Should the string be colored?
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function convert($string, $colored = true)
|
||||
{
|
||||
static $conversions = array ( // static so the array doesn't get built
|
||||
// everytime
|
||||
// %y - yellow, and so on... {{{
|
||||
'%y' => array('color' => 'yellow'),
|
||||
'%g' => array('color' => 'green' ),
|
||||
'%b' => array('color' => 'blue' ),
|
||||
'%r' => array('color' => 'red' ),
|
||||
'%p' => array('color' => 'purple'),
|
||||
'%m' => array('color' => 'purple'),
|
||||
'%c' => array('color' => 'cyan' ),
|
||||
'%w' => array('color' => 'grey' ),
|
||||
'%k' => array('color' => 'black' ),
|
||||
'%n' => array('color' => 'reset' ),
|
||||
'%Y' => array('color' => 'yellow', 'style' => 'light'),
|
||||
'%G' => array('color' => 'green', 'style' => 'light'),
|
||||
'%B' => array('color' => 'blue', 'style' => 'light'),
|
||||
'%R' => array('color' => 'red', 'style' => 'light'),
|
||||
'%P' => array('color' => 'purple', 'style' => 'light'),
|
||||
'%M' => array('color' => 'purple', 'style' => 'light'),
|
||||
'%C' => array('color' => 'cyan', 'style' => 'light'),
|
||||
'%W' => array('color' => 'grey', 'style' => 'light'),
|
||||
'%K' => array('color' => 'black', 'style' => 'light'),
|
||||
'%N' => array('color' => 'reset', 'style' => 'light'),
|
||||
'%3' => array('background' => 'yellow'),
|
||||
'%2' => array('background' => 'green' ),
|
||||
'%4' => array('background' => 'blue' ),
|
||||
'%1' => array('background' => 'red' ),
|
||||
'%5' => array('background' => 'purple'),
|
||||
'%6' => array('background' => 'cyan' ),
|
||||
'%7' => array('background' => 'grey' ),
|
||||
'%0' => array('background' => 'black' ),
|
||||
// Don't use this, I can't stand flashing text
|
||||
'%F' => array('style' => 'blink'),
|
||||
'%U' => array('style' => 'underline'),
|
||||
'%8' => array('style' => 'inverse'),
|
||||
'%9' => array('style' => 'bold'),
|
||||
'%_' => array('style' => 'bold')
|
||||
// }}}
|
||||
);
|
||||
|
||||
if ($colored) {
|
||||
$string = str_replace('%%', '% ', $string);
|
||||
foreach ($conversions as $key => $value) {
|
||||
$string = str_replace($key, Console_Color::color($value),
|
||||
$string);
|
||||
}
|
||||
$string = str_replace('% ', '%', $string);
|
||||
|
||||
} else {
|
||||
$string = preg_replace('/%((%)|.)/', '$2', $string);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes % so they don't get interpreted as color codes
|
||||
*
|
||||
* @param string $string String to escape
|
||||
*
|
||||
* @access public
|
||||
* @return string
|
||||
*/
|
||||
function escape($string)
|
||||
{
|
||||
return str_replace('%', '%%', $string);
|
||||
}
|
||||
|
||||
/**
|
||||
* Strips ANSI color codes from a string
|
||||
*
|
||||
* @param string $string String to strip
|
||||
*
|
||||
* @acess public
|
||||
* @return string
|
||||
*/
|
||||
function strip($string)
|
||||
{
|
||||
return preg_replace('/\033\[[\d;]+m/', '', $string);
|
||||
}
|
||||
|
||||
}
|
||||
?>
|
@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
error_reporting(E_ERROR);
|
||||
|
||||
### Default directories
|
||||
|
||||
$config['temp_dir'] = "/tmp";
|
||||
|
@ -16,6 +16,8 @@ include_once($config['install_dir'] . "/includes/rewrites.php");
|
||||
include_once($config['install_dir'] . "/includes/snmp.inc.php");
|
||||
include_once($config['install_dir'] . "/includes/services.inc.php");
|
||||
include_once($config['install_dir'] . "/includes/dbFacile.php");
|
||||
include_once($config['install_dir'] . "/includes/console_colour.php");
|
||||
|
||||
|
||||
function mac_clean_to_readable($mac)
|
||||
{
|
||||
@ -173,27 +175,51 @@ function delete_device($id)
|
||||
return $ret;
|
||||
}
|
||||
|
||||
function addHost($host, $community, $snmpver, $port = 161, $transport = 'udp')
|
||||
function addHost($host, $community, $snmpver, $port = '161', $transport = 'udp')
|
||||
{
|
||||
global $config;
|
||||
|
||||
list($hostshort) = explode(".", $host);
|
||||
if (isDomainResolves($host))
|
||||
/// Test Database Exists
|
||||
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($host)) == '0')
|
||||
{
|
||||
if (isPingable($host))
|
||||
/// Test DNS lookup
|
||||
if (isDomainResolves($argv[1]))
|
||||
{
|
||||
if (dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = '$host'") == '0')
|
||||
/// Test reachability
|
||||
if (isPingable($argv[1]))
|
||||
{
|
||||
# FIXME internalize -- but we don't have $device yet!
|
||||
# FIXME this needs to be addhost.php's content instead, kindof, also use this function there then.
|
||||
$snmphost = shell_exec($config['snmpget'] ." -m SNMPv2-MIB -Oqv -$snmpver -c $community $host:$port sysName.0");
|
||||
if ($snmphost == $host || $hostshort = $host)
|
||||
$added = 0;
|
||||
/// try each community from config
|
||||
foreach ($config['snmp']['community'] as $community)
|
||||
{
|
||||
createHost($host, $community, $snmpver, $port, $transport);
|
||||
} else { print_error("Given hostname does not match SNMP-read hostname!\n"); }
|
||||
} else { print_error("Already got host $host\n"); }
|
||||
} else { print_error("Could not ping $host\n"); }
|
||||
} else { print_error("Could not resolve $host\n"); }
|
||||
$device = deviceArray($host, $community, $snmpver, $port, $transport);
|
||||
|
||||
if (isSNMPable($device))
|
||||
{
|
||||
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
||||
if ($snmphost == "" || ($snmphost && ($snmphost == $host || $hostshort = $host)))
|
||||
{
|
||||
$added = createHost ($host, $community, $snmpver, $port, $transport);
|
||||
if($added) { echo($added . "\n"); }
|
||||
} else {
|
||||
print_error("Given hostname does not match SNMP-read hostname ($snmphost)!");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!$added)
|
||||
{
|
||||
/// Faild SNMP
|
||||
print_error("Could not reach $host with given SNMP community"); }
|
||||
} else {
|
||||
/// failed Reachability
|
||||
print_error("Could not ping $host"); }
|
||||
} else {
|
||||
/// Failed DNS lookup
|
||||
print_error("Could not resolve $host"); }
|
||||
} else {
|
||||
/// found in database
|
||||
print_error("Already got host $host"); }
|
||||
}
|
||||
|
||||
function scanUDP($host, $port, $timeout)
|
||||
|
Reference in New Issue
Block a user