From 74ee4971d0ff6387cf8152ac0849c2e3207fce87 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 16 Feb 2021 12:14:56 +0100 Subject: [PATCH] Added inUsage and outUsage, added units --- pandora_console/extras/mr/45.sql | 2 +- .../pandoradb_migrate_6.0_to_7.0.mysql.sql | 2 +- .../include/class/AgentWizard.class.php | 87 +++++++++++++- pandora_console/pandoradb_data.sql | 2 +- .../util/plugin/pandora_snmp_bandwidth.pl | 111 +++++++++++++----- 5 files changed, 167 insertions(+), 37 deletions(-) diff --git a/pandora_console/extras/mr/45.sql b/pandora_console/extras/mr/45.sql index 13ce742a8a..50b853ac64 100644 --- a/pandora_console/extras/mr/45.sql +++ b/pandora_console/extras/mr/45.sql @@ -4,6 +4,6 @@ SET @plugin_name = 'Network bandwidth SNMP'; SET @plugin_description = 'Retrieves amount of digital information sent and received from device or filtered interface index over a particular time (agent/module interval).'; SET @plugin_id = ''; SELECT @plugin_id := `id` FROM `tplugin` WHERE `name` = @plugin_name; -INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_''); +INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"},\"14\":{\"macro\":\"_field14_\",\"desc\":\"inUsage\",\"help\":\"Retrieve input usage (%)\",\"value\":\"\",\"hide\":\"\"},\"15\":{\"macro\":\"_field15_\",\"desc\":\"outUsage\",\"help\":\"Retrieve output usage (%)\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_'; -inUsage '_field14_' -outUsage '_field15_''); COMMIT; diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index 912d7e9ed5..c59867ee31 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -2878,7 +2878,7 @@ SET @plugin_name = 'Network bandwidth SNMP'; SET @plugin_description = 'Retrieves amount of digital information sent and received from device or filtered interface index over a particular time (agent/module interval).'; SET @plugin_id = ''; SELECT @plugin_id := `id` FROM `tplugin` WHERE `name` = @plugin_name; -INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_''); +INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"},\"14\":{\"macro\":\"_field14_\",\"desc\":\"inUsage\",\"help\":\"Retrieve input usage (%)\",\"value\":\"\",\"hide\":\"\"},\"15\":{\"macro\":\"_field15_\",\"desc\":\"outUsage\",\"help\":\"Retrieve output usage (%)\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_'; -inUsage '_field14_' -outUsage '_field15_''); SET @main_component_group_name = 'Wizard'; SET @component_id = ''; diff --git a/pandora_console/include/class/AgentWizard.class.php b/pandora_console/include/class/AgentWizard.class.php index 860f8f89f6..6810244320 100644 --- a/pandora_console/include/class/AgentWizard.class.php +++ b/pandora_console/include/class/AgentWizard.class.php @@ -1439,6 +1439,9 @@ class AgentWizard extends HTML } else if (empty(preg_match('/module-id_modulo/', $k)) === false) { $result[$value]['id_modulo'] = $data['module-id_modulo-'.$key]; continue; + } else if (empty(preg_match('/module-unit/', $k)) === false) { + $result[$value]['unit'] = $data['module-unit-'.$key]; + continue; } preg_match('/^(.*)-.*?_(\d+-\d+)$/', $k, $matches); @@ -1899,6 +1902,7 @@ class AgentWizard extends HTML { $modules = []; $errorflag = false; + foreach ($modulesCandidates as $candidate) { $tmp = Module::search( [ @@ -2632,6 +2636,7 @@ class AgentWizard extends HTML 'id_plugin' => $moduleData['id_plugin'], 'macros' => $moduleData['macros'], 'id_modulo' => $moduleData['id_modulo'], + 'unit' => ($moduleData['unit'] ?? $moduleData['module_unit']), ]; } @@ -4221,7 +4226,7 @@ class AgentWizard extends HTML // Unit module. $data[6] .= html_print_input_hidden( 'module-unit-'.$uniqueId, - $module['unit'], + ($module['unit'] ?? $module['module_unit']), true, $md5IdBlock, 'form="form-create-modules"' @@ -4686,6 +4691,12 @@ class AgentWizard extends HTML // Hash identifier. $macros[13]['value'] = uniqid(); + // Get input usage. + $macros[14]['value'] = 0; + + // Get output usage. + $macros[15]['value'] = 0; + $moduleName = $name.'Bandwidth'; $definition['Bandwidth'] = [ 'module_name' => $moduleName, @@ -4695,7 +4706,7 @@ class AgentWizard extends HTML $moduleDescription, $moduleName ), - 'module_info' => 'Amount of digital information sent and received from this inteerface over a particular time (see interval).', + 'module_info' => 'Amount of digital information sent and received from this interface over a particular time (see interval).', 'execution_type' => EXECUTION_TYPE_PLUGIN, 'id_plugin' => $plugin_id, 'id_modulo' => MODULE_PLUGIN, @@ -4712,6 +4723,78 @@ class AgentWizard extends HTML 'inv_critical' => false, ], ]; + + // Hash identifier. + $macros[13]['value'] = uniqid(); + + // Get input usage. + $macros[14]['value'] = 1; + + // Get output usage. + $macros[15]['value'] = 0; + + $moduleName = $name.'inUsage'; + $definition['inUsage'] = [ + 'module_name' => $moduleName, + 'module_type' => MODULE_TYPE_NUMERIC, + 'module_description' => sprintf( + '(%s%s)', + $moduleDescription, + $moduleName + ), + 'module_info' => 'Bandwidth usage received into this interface over a particular time (see interval).', + 'execution_type' => EXECUTION_TYPE_PLUGIN, + 'id_plugin' => $plugin_id, + 'id_modulo' => MODULE_PLUGIN, + 'macros' => json_encode($macros), + 'default_enabled' => true, + 'module_enabled' => false, + 'module_unit' => '%', + 'module_thresholds' => [ + 'min_warning' => '0', + 'max_warning' => '0', + 'inv_warning' => false, + 'min_critical' => '0', + 'max_critical' => '0', + 'inv_critical' => false, + ], + ]; + + // Hash identifier. + $macros[13]['value'] = uniqid(); + + // Get input usage. + $macros[14]['value'] = 0; + + // Get output usage. + $macros[15]['value'] = 1; + + $moduleName = $name.'outUsage'; + $definition['outUsage'] = [ + 'module_name' => $moduleName, + 'module_type' => MODULE_TYPE_NUMERIC, + 'module_description' => sprintf( + '(%s%s)', + $moduleDescription, + $moduleName + ), + 'module_info' => 'Bandwidth usage sent from this interface over a particular time (see interval).', + 'execution_type' => EXECUTION_TYPE_PLUGIN, + 'id_plugin' => $plugin_id, + 'id_modulo' => MODULE_PLUGIN, + 'macros' => json_encode($macros), + 'default_enabled' => true, + 'module_enabled' => false, + 'module_unit' => '%', + 'module_thresholds' => [ + 'min_warning' => '0', + 'max_warning' => '0', + 'inv_warning' => false, + 'min_critical' => '0', + 'max_critical' => '0', + 'inv_critical' => false, + ], + ]; } } diff --git a/pandora_console/pandoradb_data.sql b/pandora_console/pandoradb_data.sql index a377d4a72d..b178702bc0 100644 --- a/pandora_console/pandoradb_data.sql +++ b/pandora_console/pandoradb_data.sql @@ -1743,7 +1743,7 @@ SET @plugin_name = 'Network bandwidth SNMP'; SET @plugin_description = 'Retrieves amount of digital information sent and received from device or filtered interface index over a particular time (agent/module interval).'; SET @plugin_id = ''; SELECT @plugin_id := `id` FROM `tplugin` WHERE `name` = @plugin_name; -INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_''); +INSERT IGNORE INTO `tplugin` (`id`, `name`, `description`, `max_timeout`, `max_retries`, `execute`, `net_dst_opt`, `net_port_opt`, `user_opt`, `pass_opt`, `plugin_type`, `macros`, `parameters`) VALUES (@plugin_id,@plugin_name,@plugin_description,300,0,'perl /usr/share/pandora_server/util/plugin/pandora_snmp_bandwidth.pl','','','','',0,'{\"1\":{\"macro\":\"_field1_\",\"desc\":\"SNMP Version(1,2c,3)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"2\":{\"macro\":\"_field2_\",\"desc\":\"Community\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"3\":{\"macro\":\"_field3_\",\"desc\":\"Host\",\"help\":\"\",\"value\":\"_address_\",\"hide\":\"\"},\"4\":{\"macro\":\"_field4_\",\"desc\":\"Port\",\"help\":\"\",\"value\":\"161\",\"hide\":\"\"},\"5\":{\"macro\":\"_field5_\",\"desc\":\"Interface Index (filter)\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"6\":{\"macro\":\"_field6_\",\"desc\":\"securityName\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"7\":{\"macro\":\"_field7_\",\"desc\":\"context\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"8\":{\"macro\":\"_field8_\",\"desc\":\"securityLevel\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"9\":{\"macro\":\"_field9_\",\"desc\":\"authProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"10\":{\"macro\":\"_field10_\",\"desc\":\"authKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"11\":{\"macro\":\"_field11_\",\"desc\":\"privProtocol\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"12\":{\"macro\":\"_field12_\",\"desc\":\"privKey\",\"help\":\"\",\"value\":\"\",\"hide\":\"\"},\"13\":{\"macro\":\"_field13_\",\"desc\":\"UniqId\",\"help\":\"This plugin needs to store information in temporary directory to calculate bandwidth. Set here an unique identifier with no spaces or symbols.\",\"value\":\"\",\"hide\":\"\"},\"14\":{\"macro\":\"_field14_\",\"desc\":\"inUsage\",\"help\":\"Retrieve input usage (%)\",\"value\":\"\",\"hide\":\"\"},\"15\":{\"macro\":\"_field15_\",\"desc\":\"outUsage\",\"help\":\"Retrieve output usage (%)\",\"value\":\"\",\"hide\":\"\"}}','-version '_field1_' -community '_field2_' -host '_field3_' -port '_field4_' -ifIndex '_field5_' -securityName '_field6_' -context '_field7_' -securityLevel '_field8_' -authProtocol '_field9_' -authKey '_field10_' -privProtocol '_field11_' -privKey '_field12_' -uniqid '_field13_'; -inUsage '_field14_' -outUsage '_field15_''); SET @main_component_group_name = 'Wizard'; SET @component_id = ''; diff --git a/pandora_server/util/plugin/pandora_snmp_bandwidth.pl b/pandora_server/util/plugin/pandora_snmp_bandwidth.pl index d6edf5d897..ad0f6c8899 100644 --- a/pandora_server/util/plugin/pandora_snmp_bandwidth.pl +++ b/pandora_server/util/plugin/pandora_snmp_bandwidth.pl @@ -2,12 +2,12 @@ # ################################################################################ # -# Bandwith plugin -# +# Bandwith usage plugin +# # Requirements: # snmpget # snmpwalk -# +# # (c) Fco de Borja Sanchez # # 2018/06/27 @@ -45,18 +45,20 @@ Where OPTIONS could be: -port target port (161) [SNMPv3] - -securityName - -context - -securityLevel - -authProtocol - -authKey - -privProtocol + -securityName + -context + -securityLevel + -authProtocol + -authKey + -privProtocol -privKey [EXTRA] -ifIndex Target interface to retrieve, if not specified, total bandwith will be reported. -uniqid Use custom temporary file name. + -inUsage Show only input usage (in percentage) - 1, or not 0. + -outUsage Show only output usage (in percentage) - 1, or not 0. Note: You can also use snmpget/snmpwalk argument notation, e.g. -v is equal to -version, -c to -community, etc. @@ -125,9 +127,15 @@ sub update_config_key ($) { if ($arg eq 'ifIndex') { return "ifIndex"; } -if ($arg eq 'uniqid') { - return "uniqid"; -} + if ($arg eq 'uniqid') { + return "uniqid"; + } + if ($arg eq 'inUsage') { + return "inUsage"; + } + if ($arg eq 'outUsage') { + return "outUsage"; + } } ################################################################################ @@ -152,7 +160,7 @@ sub prepare_tree { my $raw = snmp_walk(\%snmp_call); return $raw if (ref($raw) eq "HASH"); - + my @data = split /\n/, $raw; foreach my $it (@data) { my ($key, $value) = split /=/, $it; @@ -227,7 +235,7 @@ sub prepare_tree { } else { $duplex = int $duplex->{'data'}; } - + } else { # Ignore, cannot retrieve inOctets. next; @@ -329,7 +337,7 @@ sub save_data { open($_f, ">$config->{'tmp_file'}") or die('Cannot open ' . $config->{'tmp_file'}); }; if( $@ ) { - logger($config, 'info', "Cannot save stats, please check writting permissions on [" . $config->{'tmp_file'} . "]"); + logger($config, 'info', "Cannot save stats, please check writting permissions on [" . $config->{'tmp_file'} . "]") if (is_enabled($config->{'debug'})); return; } @@ -343,7 +351,7 @@ sub save_data { # Iface. print $_f $iface . $config->{'tmp_separator'}; - + # InOctets. print $_f $tree->{$iface}{'now'}{'inOctets'} . $config->{'tmp_separator'}; @@ -359,9 +367,9 @@ sub save_data { } ################################################################################ -# Calculate bandwidth +# Calculate bandwidth usage ################################################################################ -sub get_bandwidth { +sub get_bandwidth_usage { my ($config, $tree) = @_; foreach my $iface (keys %{$tree}) { @@ -375,6 +383,8 @@ sub get_bandwidth { my $output = $tree->{$iface}{'now'}{'outOctets'} - $tree->{$iface}{'old'}{'outOctets'}; my $delta = $tree->{$iface}{'now'}{'timestamp'} - $tree->{$iface}{'old'}{'timestamp'}; my $bandwidth = 0; + my $inUsage = 0; + my $outUsage = 0; $tree->{$iface}->{'delta'} = { 'inOctets' => $input, @@ -385,7 +395,7 @@ sub get_bandwidth { $tree->{$iface}->{'speed'} = $speed; if (($speed > 0) && ($delta > 0)) { - # Information about bandwidth calculation: https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/8141-calculate-bandwidth-snmp.html + # Information about bandwidth usage calculation: https://www.cisco.com/c/en/us/support/docs/ip/simple-network-management-protocol-snmp/8141-calculate-bandwidth-snmp.html if ($tree->{$iface}{'duplex'} == HALF_DUPLEX || $tree->{$iface}{'duplex'} == UNKNOWN_DUPLEX ) { @@ -398,26 +408,40 @@ sub get_bandwidth { } else { no warnings "uninitialized"; - logger($config, 'info', "Failed to calculate bandwidth, unknown duplex mode: [" . $tree->{$iface}{'duplex_mode'} . "]"); + logger($config, 'info', "Failed to calculate bandwidth usage, unknown duplex mode: [" . $tree->{$iface}{'duplex_mode'} . "]") if (is_enabled($config->{'debug'})); } + + $inUsage = ($input * 8) / ($delta * $speed); + $outUsage = ($output * 8) / ($delta * $speed); + + if ($inUsage > 1) { + $inUsage = 1; + logger($config, 'info', "Max input usage exceeded: $inUsage") if (is_enabled($config->{'debug'})); + } + if ($outUsage > 1) { + $outUsage = 1; + logger($config, 'info', "Max output usage exceeded: $outUsage") if (is_enabled($config->{'debug'})); + } + } else { - logger($config, 'info', "Failed to calculate bandwidth, interface [" . $iface . "] speed is 0"); + logger($config, 'info', "Failed to calculate bandwidth usage, interface [" . $iface . "] speed is 0") if (is_enabled($config->{'debug'})); } - $tree->{$iface}->{'bandwidth'} = 100 * $bandwidth; + $tree->{$iface}->{'bandwidth'} = 100 * $bandwidth; + $tree->{$iface}->{'inUsage'} = 100 * $inUsage; + $tree->{$iface}->{'outUsage'} = 100 * $outUsage; + } } - ################################################################################ # # MAIN # ################################################################################ - if ($#ARGV < 0) { print $HELP; exit 0; @@ -500,11 +524,11 @@ if ($#only_int >= 0) { $config->{'only_interfaces'} = \@only_int; } -logger($config, 'info', "Plugin starts"); +logger($config, 'info', "Plugin starts") if (is_enabled($config->{'debug'})); if (is_enabled($config->{'debug'})) { eval { eval "use Data::Dumper;1;";if($@) {} - logger($config, Dumper($config)); + logger($config, Dumper($config)) if (is_enabled($config->{'debug'})); }; if($@) {} } @@ -512,30 +536,53 @@ if (is_enabled($config->{'debug'})) { my $analysis_tree = prepare_tree($config); if (!empty($analysis_tree->{'error'})) { - logger($config, 'info', "Failed: " . $analysis_tree->{'error'}); + logger($config, 'info', "Failed: " . $analysis_tree->{'error'}) if (is_enabled($config->{'debug'})); exit 0; } else { - get_bandwidth($config, $analysis_tree); + get_bandwidth_usage($config, $analysis_tree); } # Report data my @modules; my $bandwidth = 0; +my $inUsage = 0; +my $outUsage = 0; my $i = 0; +my $j = 0; +my $k = 0; foreach my $iface (keys %{$analysis_tree}) { # Calculate summary; if (is_enabled($analysis_tree->{$iface}{'bandwidth'})) { $bandwidth = $analysis_tree->{$iface}{'bandwidth'}; $i++; } + if (is_enabled($analysis_tree->{$iface}{'inUsage'})) { + $inUsage = $analysis_tree->{$iface}{'inUsage'}; + $j++; + } + if (is_enabled($analysis_tree->{$iface}{'outUsage'})) { + $outUsage = $analysis_tree->{$iface}{'outUsage'}; + $k++; + } } -if ($i > 0) { - $bandwidth /= $i; - print sprintf("%.9f\n", $bandwidth); +if ($j > 0 && is_enabled($config->{'inUsage'})) { + $inUsage /= $j; + print sprintf("%.9f\n", $inUsage); +} elsif ($k > 0 && is_enabled($config->{'outUsage'})) { + $outUsage /= $k; + print sprintf("%.9f\n", $outUsage); } -logger($config, 'info', "Plugin ends"); +if ($i > 0 + && !is_enabled($config->{'inUsage'}) + && !is_enabled($config->{'outUsage'}) +) { + $bandwidth /= $i; + print sprintf("%.9f\n", $bandwidth); +} + +logger($config, 'info', "Plugin ends") if (is_enabled($config->{'debug'}));