This commit is contained in:
fbsanchez 2019-06-25 12:07:56 +02:00
parent a469ff29da
commit fc0d9463d5
2 changed files with 326 additions and 127 deletions

View File

@ -1,26 +1,60 @@
<?php <?php
/**
* Net tools utils.
*
* @category Extensions
* @package Pandora FMS
* @subpackage NetTools
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
* Please see http://pandorafms.org for full contribution list
* 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 for version 2.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* ============================================================================
*/
// Pandora FMS - http://pandorafms.com // Begin.
// ================================================== global $config;
// Copyright (c) 2005-2011 Artica Soluciones Tecnologicas
// Please see http://pandorafms.org for full contribution list // Requires.
// This program is free software; you can redistribute it and/or require_once $config['homedir'].'/include/functions.php';
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; version 2 // This extension is usefull only if the agent has associated IP.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
$id_agente = get_parameter('id_agente'); $id_agente = get_parameter('id_agente');
// This extension is usefull only if the agent has associated IP
$address = agents_get_address($id_agente); $address = agents_get_address($id_agente);
if (!empty($address) || empty($id_agente)) { if (!empty($address) || empty($id_agente)) {
extensions_add_opemode_tab_agent('network_tools', 'Network Tools', 'extensions/net_tools/nettool.png', 'main_net_tools', 'v1r1', 'AW'); extensions_add_opemode_tab_agent(
'network_tools',
'Network Tools',
'extensions/net_tools/nettool.png',
'main_net_tools',
'v1r1',
'AW'
);
} }
/**
* Searchs for command.
*
* @param string $command Command.
*
* @return string Path.
*/
function whereis_the_command($command) function whereis_the_command($command)
{ {
global $config; global $config;
@ -63,6 +97,9 @@ function whereis_the_command($command)
return $snmpget_path; return $snmpget_path;
} }
break; break;
default:
return null;
} }
} }
@ -87,83 +124,18 @@ function whereis_the_command($command)
} }
function main_net_tools() /**
* Execute net tools action.
*
* @param integer $operation Operation.
* @param string $ip Ip.
* @param string $community Community.
* @param string $snmp_version SNMP version.
*
* @return void
*/
function net_tools_execute($operation, $ip, $community, $snmp_version)
{ {
$id_agente = get_parameter('id_agente');
$principal_ip = db_get_sql("SELECT direccion FROM tagente WHERE id_agente = $id_agente");
$list_address = db_get_all_rows_sql('select id_a from taddress_agent where id_agent = '.$id_agente);
foreach ($list_address as $address) {
$ids[] = join(',', $address);
}
$ids_address = implode(',', $ids);
$ips = db_get_all_rows_sql('select ip from taddress where id_a in ('.$ids_address.')');
if ($ips == '') {
echo "<div class='error' style='margin-top:5px'>".__('The agent hasn\'t got IP').'</div>';
return;
}
echo "
<script type='text/javascript'>
function mostrarColumns(ValueSelect) {
value = ValueSelect.value;
if (value == 3) {
$('netToolTable').css('width','100%');
$('#snmpcolumn').show();
}
else {
$('netToolTable').css('width','100%');
$('#snmpcolumn').hide();
}
}
</script>";
echo '<div>';
echo "<form name='actionbox' method='post'>";
echo "<table class='databox filters' width=100% id=netToolTable>";
echo '<tr><td>';
echo __('Operation');
ui_print_help_tip(
__('You can set the command path in the menu Administration -&gt; Extensions -&gt; Config Network Tools')
);
echo '</td><td>';
echo "<select name='operation' onChange='mostrarColumns(this);'>";
echo "<option value='1'>".__('Traceroute');
echo "<option value='2'>".__('Ping host & Latency');
echo "<option value='3'>".__('SNMP Interface status');
echo "<option value='4'>".__('Basic TCP Port Scan');
echo "<option value='5'>".__('DiG/Whois Lookup');
echo '</select>';
echo '</td>';
echo '<td>';
echo __('IP address');
echo '</td><td>';
echo "<select name='select_ips'>";
foreach ($ips as $ip) {
if ($ip['ip'] == $principal_ip) {
echo "<option value='".$ip['ip']."' selected = 'selected'>".$ip['ip'];
} else {
echo "<option value='".$ip['ip']."'>".$ip['ip'];
}
}
echo '</select>';
echo '</td>';
echo "<td id='snmpcolumn' style=\"display:none;\">";
echo __('SNMP Community').'&nbsp;';
echo "<input name=community type=text value='public'>";
echo '</td><td>';
echo "<input style='margin:0px;' name=submit type=submit class='sub next' value='".__('Execute')."'>";
echo '</td>';
echo '</tr></table>';
echo '</form>';
$operation = get_parameter('operation', 0);
$community = get_parameter('community', 'public');
$ip = get_parameter('select_ips');
if (!validate_address($ip)) { if (!validate_address($ip)) {
ui_print_error_message(__('The ip or dns name entered cannot be resolved')); ui_print_error_message(__('The ip or dns name entered cannot be resolved'));
} else { } else {
@ -175,7 +147,7 @@ function main_net_tools()
} else { } else {
echo '<h3>'.__('Traceroute to ').$ip.'</h3>'; echo '<h3>'.__('Traceroute to ').$ip.'</h3>';
echo '<pre>'; echo '<pre>';
echo system("$traceroute $ip"); echo system($traceroute.' '.$ip);
echo '</pre>'; echo '</pre>';
} }
break; break;
@ -187,7 +159,7 @@ function main_net_tools()
} else { } else {
echo '<h3>'.__('Ping to %s', $ip).'</h3>'; echo '<h3>'.__('Ping to %s', $ip).'</h3>';
echo '<pre>'; echo '<pre>';
echo system("$ping -c 5 $ip"); echo system($ping.' -c 5 '.$ip);
echo '</pre>'; echo '</pre>';
} }
break; break;
@ -199,7 +171,7 @@ function main_net_tools()
} else { } else {
echo '<h3>'.__('Basic TCP Scan on ').$ip.'</h3>'; echo '<h3>'.__('Basic TCP Scan on ').$ip.'</h3>';
echo '<pre>'; echo '<pre>';
echo system("$nmap -F $ip"); echo system($nmap.' -F '.$ip);
echo '</pre>'; echo '</pre>';
} }
break; break;
@ -212,7 +184,7 @@ function main_net_tools()
ui_print_error_message(__('Dig executable does not exist.')); ui_print_error_message(__('Dig executable does not exist.'));
} else { } else {
echo '<pre>'; echo '<pre>';
echo system("dig $ip"); echo system('dig '.$ip);
echo '</pre>'; echo '</pre>';
} }
@ -221,51 +193,227 @@ function main_net_tools()
ui_print_error_message(__('Whois executable does not exist.')); ui_print_error_message(__('Whois executable does not exist.'));
} else { } else {
echo '<pre>'; echo '<pre>';
echo system("whois $ip"); echo system('whois '.$ip);
echo '</pre>'; echo '</pre>';
} }
break; break;
case 3: case 3:
echo '<h3>'.__('SNMP information for ').$ip.'</h3>'; $snmp_obj = [
'ip_target' => $ip,
'snmp_version' => $snmp_version,
'snmp_community' => $community,
'format' => '-Oqn',
];
$snmpget = whereis_the_command('snmpget'); $snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.3.0';
if (empty($snmpget)) { $result = get_h_snmpwalk($snmp_obj);
ui_print_error_message(__('SNMPget executable does not exist.')); echo '<h3>'.__('SNMP information for ').$ip.'</h3>';
} else {
echo '<h4>'.__('Uptime').'</h4>'; echo '<h4>'.__('Uptime').'</h4>';
echo '<pre>'; echo '<pre>';
echo exec("$snmpget -Ounv -v1 -c $community $ip .1.3.6.1.2.1.1.3.0 "); if (empty($result)) {
ui_print_error_message(__('Target unreachable.'));
break;
} else {
echo array_pop($result);
}
echo '</pre>'; echo '</pre>';
echo '<h4>'.__('Device info').'</h4>'; echo '<h4>'.__('Device info').'</h4>';
echo '<pre>'; echo '<pre>';
$snmp_obj['base_oid'] = '.1.3.6.1.2.1.1.1.0';
$result = get_h_snmpwalk($snmp_obj);
if (empty($result)) {
ui_print_error_message(__('Target unreachable.'));
break;
} else {
echo array_pop($result);
}
echo system("$snmpget -Ounv -v1 -c $community $ip .1.3.6.1.2.1.1.1.0 ");
echo '</pre>'; echo '</pre>';
echo '<h4>Interface Information</h4>'; echo '<h4>Interface Information</h4>';
echo '<table class=databox>';
echo '<tr><th>'.__('Interface');
echo '<th>'.__('Status');
$int_max = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.1.0 "); $table = new StdClass();
$table->class = 'databox';
$table->head = [];
$table->head[] = __('Interface');
$table->head[] = __('Status');
for ($ax = 0; $ax < $int_max; $ax++) { $i = 0;
$interface = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.2.1.2.$ax ");
$estado = exec("$snmpget -Oqunv -v1 -c $community $ip .1.3.6.1.2.1.2.2.1.8.$ax "); $base_oid = '.1.3.6.1.2.1.2.2.1';
echo "<tr><td>$interface<td>$estado"; $idx_oids = '.1';
$names_oids = '.2';
$status_oids = '.8';
$snmp_obj['base_oid'] = $base_oid.$idx_oids;
$idx = get_h_snmpwalk($snmp_obj);
$snmp_obj['base_oid'] = $base_oid.$names_oids;
$names = get_h_snmpwalk($snmp_obj);
$snmp_obj['base_oid'] = $base_oid.$status_oids;
$statuses = get_h_snmpwalk($snmp_obj);
foreach ($idx as $k => $v) {
$index = str_replace($base_oid.$idx_oids, '', $k);
$name = $names[$base_oid.$names_oids.$index];
$status = $statuses[$base_oid.$status_oids.$index];
$table->data[$i][0] = $name;
$table->data[$i++][1] = $status;
} }
echo '</table>'; html_print_table($table);
} break;
default:
// Ignore.
break; break;
} }
} }
}
/**
* Main function.
*
* @return void
*/
function main_net_tools()
{
$operation = get_parameter('operation', 0);
$community = get_parameter('community', 'public');
$ip = get_parameter('select_ips');
$snmp_version = get_parameter('select_version');
// Show form.
$id_agente = get_parameter('id_agente', 0);
$principal_ip = db_get_sql(
sprintf(
'SELECT direccion FROM tagente WHERE id_agente = %d',
$id_agente
)
);
$list_address = db_get_all_rows_sql(
sprintf(
'SELECT id_a FROM taddress_agent WHERE id_agent = %d',
$id_agente
)
);
foreach ($list_address as $address) {
$ids[] = join(',', $address);
}
$ips = db_get_all_rows_sql(
sprintf(
'SELECT ip FROM taddress WHERE id_a IN (%s)',
join($ids)
)
);
if ($ips == '') {
echo "<div class='error' style='margin-top:5px'>".__('The agent hasn\'t got IP').'</div>';
return;
}
// Javascript.
?>
<script type='text/javascript'>
$(document).ready(function(){
mostrarColumns($('#operation :selected').val());
});
function mostrarColumns(value) {
if (value == 3) {
$('.snmpcolumn').show();
}
else {
$('.snmpcolumn').hide();
}
}
</script>
<?php
echo '<div>';
echo "<form name='actionbox' method='post'>";
echo "<table class='databox filters' width=100% id=netToolTable>";
echo '<tr><td>';
echo __('Operation');
ui_print_help_tip(
__('You can set the command path in the menu Administration -&gt; Extensions -&gt; Config Network Tools')
);
echo '</td><td>';
html_print_select(
[
1 => __('Traceroute'),
2 => __('Ping host & Latency'),
3 => __('SNMP Interface status'),
4 => __('Basic TCP Port Scan'),
5 => __('DiG/Whois Lookup'),
],
'operation',
$operation,
'mostrarColumns(this.value)',
__('Please select')
);
echo '</td>';
echo '<td>';
echo __('IP address');
echo '</td><td>';
$ips_for_select = array_reduce(
$ips,
function ($carry, $item) {
$carry[$item['ip']] = $item['ip'];
return $carry;
}
);
html_print_select(
$ips_for_select,
'select_ips',
$principal_ip
);
echo '</td>';
echo "<td class='snmpcolumn'>";
echo __('SNMP Version');
html_print_select(
[
'1' => 'v1',
'2c' => 'v2c',
],
'select_version',
$snmp_version
);
echo '</td><td class="snmpcolumn">';
echo __('SNMP Community').'&nbsp;';
html_print_input_text('community', $community);
echo '</td><td>';
echo "<input style='margin:0px;' name=submit type=submit class='sub next' value='".__('Execute')."'>";
echo '</td>';
echo '</tr></table>';
echo '</form>';
if ($operation) {
// Execute form.
net_tools_execute($operation, $ip, $community, $snmp_version);
}
echo '</div>'; echo '</div>';
} }
/**
* Add option.
*
* @return void
*/
function godmode_net_tools() function godmode_net_tools()
{ {
global $config; global $config;

View File

@ -1836,6 +1836,52 @@ function array_key_to_offset($array, $key)
} }
/**
* Undocumented function
*
* @param array $arguments Following format:
* [
* 'ip_target'
* 'snmp_version'
* 'snmp_community'
* 'snmp3_auth_user'
* 'snmp3_security_level'
* 'snmp3_auth_method'
* 'snmp3_auth_pass'
* 'snmp3_privacy_method'
* 'snmp3_privacy_pass'
* 'quick_print'
* 'base_oid'
* 'snmp_port'
* 'server_to_exec'
* 'extra_arguments'
* 'format'
* ]
*
* @return array SNMP result.
*/
function get_h_snmpwalk(array $arguments)
{
return get_snmpwalk(
$arguments['ip_target'],
$arguments['snmp_version'],
isset($arguments['snmp_community']) ? $arguments['snmp_community'] : '',
isset($arguments['snmp3_auth_user']) ? $arguments['snmp3_auth_user'] : '',
isset($arguments['snmp3_security_level']) ? $arguments['snmp3_security_level'] : '',
isset($arguments['snmp3_auth_method']) ? $arguments['snmp3_auth_method'] : '',
isset($arguments['snmp3_auth_pass']) ? $arguments['snmp3_auth_pass'] : '',
isset($arguments['snmp3_privacy_method']) ? $arguments['snmp3_privacy_method'] : '',
isset($arguments['snmp3_privacy_pass']) ? $arguments['snmp3_privacy_pass'] : '',
isset($arguments['quick_print']) ? $arguments['quick_print'] : 0,
isset($arguments['base_oid']) ? $arguments['base_oid'] : '',
isset($arguments['snmp_port']) ? $arguments['snmp_port'] : '',
isset($arguments['server_to_exec']) ? $arguments['server_to_exec'] : 0,
isset($arguments['extra_arguments']) ? $arguments['extra_arguments'] : '',
isset($arguments['format']) ? $arguments['format'] : '-Oa'
);
}
/** /**
* Make a snmpwalk and return it. * Make a snmpwalk and return it.
* *
@ -1956,11 +2002,16 @@ function get_snmpwalk(
exec($command_str, $output, $rc); exec($command_str, $output, $rc);
} }
// Parse the output of snmpwalk // Parse the output of snmpwalk.
$snmpwalk = []; $snmpwalk = [];
foreach ($output as $line) { foreach ($output as $line) {
// Separate the OID from the value // Separate the OID from the value.
$full_oid = explode(' = ', $line); if (strpos($format, 'q') === false) {
$full_oid = explode(' = ', $line, 2);
} else {
$full_oid = explode(' ', $line, 2);
}
if (isset($full_oid[1])) { if (isset($full_oid[1])) {
$snmpwalk[$full_oid[0]] = $full_oid[1]; $snmpwalk[$full_oid[0]] = $full_oid[1];
} }