From db0fcd604ddd561bec7aec5bd8e43832f6c2c7b6 Mon Sep 17 00:00:00 2001 From: garnier-quentin Date: Mon, 29 Jun 2020 13:55:01 +0200 Subject: [PATCH] some change for netdata --- apps/monitoring/netdata/restapi/custom/api.pm | 66 +++++++++-------- .../monitoring/netdata/restapi/mode/alarms.pm | 51 +++++++------- apps/monitoring/netdata/restapi/mode/cpu.pm | 55 ++++++++------- .../restapi/mode/{diskusage.pm => disks.pm} | 38 ++++------ .../netdata/restapi/mode/getchart.pm | 26 ++----- .../monitoring/netdata/restapi/mode/inodes.pm | 15 ++-- .../restapi/mode/{loadaverage.pm => load.pm} | 24 +++---- .../monitoring/netdata/restapi/mode/memory.pm | 31 ++++---- apps/monitoring/netdata/restapi/mode/swap.pm | 60 +++++++--------- .../netdata/restapi/mode/traffic.pm | 70 +++++++++---------- apps/monitoring/netdata/restapi/plugin.pm | 26 +++---- 11 files changed, 213 insertions(+), 249 deletions(-) rename apps/monitoring/netdata/restapi/mode/{diskusage.pm => disks.pm} (83%) rename apps/monitoring/netdata/restapi/mode/{loadaverage.pm => load.pm} (80%) diff --git a/apps/monitoring/netdata/restapi/custom/api.pm b/apps/monitoring/netdata/restapi/custom/api.pm index 2ec137969..ac78f0443 100644 --- a/apps/monitoring/netdata/restapi/custom/api.pm +++ b/apps/monitoring/netdata/restapi/custom/api.pm @@ -22,12 +22,8 @@ package apps::monitoring::netdata::restapi::custom::api; use strict; use warnings; -use DateTime; use centreon::plugins::http; -use centreon::plugins::statefile; use JSON::XS; -use URI::Encode; -use Digest::MD5 qw(md5_hex); sub new { my ($class, %options) = @_; @@ -45,12 +41,11 @@ sub new { if (!defined($options{noptions})) { $options{options}->add_options(arguments => { - 'hostname:s' => { name => 'hostname' }, - 'port:s' => { name => 'port' }, - 'proto:s' => { name => 'proto' }, - 'timeout:s' => { name => 'timeout' }, - 'reload-cache-time:s' => { name => 'reload_cache_time' }, - 'endpoint:s' => { name => 'endpoint'}, + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'timeout:s' => { name => 'timeout' }, + 'endpoint:s' => { name => 'endpoint'} }); } $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); @@ -93,6 +88,11 @@ sub check_options { $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10; $self->{endpoint} = (defined($self->{option_results}->{endpoint})) ? $self->{option_results}->{endpoint} : '/api/v1'; + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + return 0; } @@ -106,25 +106,23 @@ sub build_options_for_httplib { $self->{option_results}->{timeout} = $self->{timeout}; $self->{option_results}->{warning_status} = ''; $self->{option_results}->{critical_status} = ''; - $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} > 400'; + $self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} >= 400'; } sub settings { my ($self, %options) = @_; + return if (defined($self->{settings_done})); $self->build_options_for_httplib(); $self->{http}->add_header(key => 'Accept', value => 'application/json'); $self->{http}->set_options(%{$self->{option_results}}); + $self->{settings_done} = 1; } sub request_api { my ($self, %options) = @_; - $self->settings(content_type => 'application/x-www-form-urlencoded'); - - $self->{output}->output_add(long_msg => "URL: '" . $self->{proto} . '://' . $self->{hostname} . ':' . $self->{port} . - $options{url_path} . "'", debug => 1); - + $self->settings(); my $content = $self->{http}->request(%options); if (!defined($content) || $content eq '') { @@ -137,7 +135,6 @@ sub request_api { $decoded = JSON::XS->new->utf8->decode($content); }; if ($@) { - $self->{output}->output_add(long_msg => $content, debug => 1); $self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)"); $self->{output}->option_exit(); } @@ -163,7 +160,10 @@ sub list_charts { my ($self, %options) = @_; my $url_path = $self->{endpoint} . '/charts'; - my $response = $self->request_api(method => 'GET', url_path => $url_path); + my $response = $self->request_api( + method => 'GET', + url_path => $url_path + ); return $response; } @@ -172,8 +172,11 @@ sub get_chart_properties { my ($self, %options) = @_; my $url_path = $self->{endpoint} . '/chart'; - $url_path .= '?chart=' . $options{chart}; - my $response = $self->request_api(method => 'GET', url_path => $url_path); + my $response = $self->request_api( + method => 'GET', + url_path => $url_path, + get_param => ['chart=' . $options{chart}] + ); my $filter_info = defined ($options{filter_info}) ? $options{filter_info} : ''; return defined ($filter_info) ? $response->{$filter_info} : $response; @@ -183,14 +186,21 @@ sub get_data { my ($self, %options) = @_; my $url_path = $self->{endpoint} . '/data'; - $url_path .= '?chart=' . $options{chart}; - $url_path .= '&options=null2zero'; - $url_path .= '&options=abs' if defined ($options{absolute}); - $url_path .= '&after=-' . $options{after_period}; - $url_path .= '&group=' . $options{group}; - $url_path .= defined ($options{points}) ? '&points=' . $options{points} : '&points=1'; - $url_path .= '&dimensions=' . $options{dimensions} if defined ($options{dimensions}); - my $response = $self->request_api(method => 'GET', url_path => $url_path),; + my $get_param = [ + 'chart=' . $options{chart}, + 'options=null2zero', + 'after=-' . $options{after_period}, + 'group=' . $options{group}, + defined($options{points}) ? 'points=' . $options{points} : 'points=1' + ]; + push @$get_param, 'options=abs' if (defined($options{absolute})); + push @$get_param, 'dimensions=' . $options{dimensions} if (defined($options{dimensions})); + + my $response = $self->request_api( + method => 'GET', + url_path => $url_path, + get_param => $get_param + ); return $response; } diff --git a/apps/monitoring/netdata/restapi/mode/alarms.pm b/apps/monitoring/netdata/restapi/mode/alarms.pm index 0ee964fd0..54e0cd9fc 100644 --- a/apps/monitoring/netdata/restapi/mode/alarms.pm +++ b/apps/monitoring/netdata/restapi/mode/alarms.pm @@ -24,12 +24,12 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); sub custom_status_output { my ($self, %options) = @_; - return sprintf('Status: %s, Current state: %s', $self->{result_values}->{status}, $self->{result_values}->{value_string}); + return sprintf('status: %s, current state: %s', $self->{result_values}->{status}, $self->{result_values}->{value_string}); } sub set_counters { @@ -43,28 +43,27 @@ sub set_counters { $self->{maps_counters}->{global} = [ { label => 'alarms-total', nlabel => 'netdata.alarms.current.total.count', set => { key_values => [ { name => 'total' } ], - output_template => "%s", - perfdatas => [ { value => 'total', template => '%d', min => 0 } ] + output_template => 'total: %s', + perfdatas => [ { template => '%d', min => 0 } ] } }, { label => 'alarms-warning', nlabel => 'netdata.alarms.current.warning.count', set => { key_values => [ { name => 'warning' } ], - output_template => "Warning Alarms : %s", - perfdatas => [ { value => 'warning', template => '%d', min => 0 } ], + output_template => 'warning: %s', + perfdatas => [ { template => '%d', min => 0 } ] } }, { label => 'alarms-critical', nlabel => 'netdata.alarms.current.critical.count', set => { key_values => [ { name => 'critical' } ], - output_template => "Critical Alarms : %s", - perfdatas => [ { value => 'critical', template => '%d', min => 0 } ], + output_template => 'critical: %s', + perfdatas => [ { template => '%d', min => 0 } ] + } } - }, ]; $self->{maps_counters}->{alarms} = [ { label => 'alarm', threshold => 0, set => { key_values => [ { name => 'display' }, { name => 'name' }, { name => 'status' }, { name => 'value_string'} ], - closure_custom_calc => \&catalog_status_calc, closure_custom_output => $self->can('custom_status_output'), closure_custom_perfdata => sub { return 0; }, closure_custom_threshold_check => \&catalog_status_threshold @@ -79,21 +78,16 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'filter-status:s' => { name => 'filter_status' }, + 'filter-status:s' => { name => 'filter_status' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub prefix_global_output { my ($self, %options) = @_; - return "Total alarms "; + return 'Alarms '; } sub prefix_alarm_output { @@ -111,20 +105,25 @@ sub manage_selection { next if ( defined($self->{option_results}->{filter_status}) && $self->{option_results}->{filter_status} ne '' && $alarm->{status} !~ /$self->{option_results}->{filter_status}/ ); + $self->{alarms}->{$alarm} = { - display => $alarm, - id => $alarm->{id}, - name => $alarm->{name}, - chart => $alarm->{chart}, - status => $alarm->{status}, - value_string => $alarm->{value_string} + display => $alarm, + id => $alarm->{id}, + name => $alarm->{name}, + chart => $alarm->{chart}, + status => $alarm->{status}, + value_string => $alarm->{value_string} }; - $self->{global}->{warning}++ if $alarm->{status} =~ m/WARNING/; - $self->{global}->{critical}++ if $alarm->{status} =~ m/CRITICAL/; + $self->{global}->{warning}++ if ($alarm->{status} =~ m/WARNING/); + $self->{global}->{critical}++ if ($alarm->{status} =~ m/CRITICAL/); } - $self->{global}->{total} = scalar (keys %{$self->{alarms}}); + $self->{global} = { + total => scalar(keys %{$self->{alarms}}), + warning => 0, + critical => 0 + }; } 1; diff --git a/apps/monitoring/netdata/restapi/mode/cpu.pm b/apps/monitoring/netdata/restapi/mode/cpu.pm index 5967323bd..38a65afe6 100644 --- a/apps/monitoring/netdata/restapi/mode/cpu.pm +++ b/apps/monitoring/netdata/restapi/mode/cpu.pm @@ -38,23 +38,21 @@ sub set_counters { key_values => [ { name => 'average' }, { name => 'count' } ], output_template => '%.2f %%', perfdatas => [ - { label => 'total_cpu_avg', value => 'average', template => '%.2f', - min => 0, max => 100, unit => '%' }, - ], + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] } - }, + } ]; $self->{maps_counters}->{cpu_results} = [ { label => 'core', nlabel => 'core.cpu.utilization.percentage', set => { key_values => [ { name => 'usage' }, { name => 'display' } ], - output_template => 'usage : %.2f %%', + output_template => 'usage: %.2f %%', perfdatas => [ - { label => 'cpu', value => 'usage', template => '%.2f', - min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }, - ], + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } + ] } - }, + } ]; } @@ -76,19 +74,13 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - -} - sub manage_selection { my ($self, %options) = @_; @@ -98,21 +90,30 @@ sub manage_selection { my $cpu_core = 'cpu.cpu' . $i; my $result = $options{custom}->get_data( chart => $cpu_core, - points => $self->{option_results}->{chart_point}, after_period => $self->{option_results}->{chart_period}, group => $self->{option_results}->{chart_statistics} ); - foreach my $cpu_value (@{$result->{data}}) { - foreach my $cpu_label (@{$result->{labels}}) { - $self->{cpu_core}->{$i}->{$cpu_label} = shift @{$cpu_value}; + + my ($usage, $count) = (0, 0); + foreach my $data (@{$result->{data}}) { + while (my ($index, $label) = each(@{$result->{labels}})) { + next if ($label eq 'time'); + + $usage += $data->[$index]; } + $count++; + } + + if ($count > 0) { + $self->{cpu_results}->{$i} = { + display => $i, + usage => $usage / $count + }; + + $cpu_total_usage += ($usage / $count); } - $self->{cpu_results}->{$i} = { - display => $cpu_core, - usage => $self->{cpu_core}->{$i}->{user} - }; - $cpu_total_usage += $self->{cpu_results}->{$i}->{usage}; } + my $avg_cpu = $cpu_total_usage / $cpu_number; $self->{cpu_avg} = { average => $avg_cpu, @@ -165,4 +166,4 @@ Critical threshold for each CPU core =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/diskusage.pm b/apps/monitoring/netdata/restapi/mode/disks.pm similarity index 83% rename from apps/monitoring/netdata/restapi/mode/diskusage.pm rename to apps/monitoring/netdata/restapi/mode/disks.pm index 59b7a25d3..3f80b0168 100644 --- a/apps/monitoring/netdata/restapi/mode/diskusage.pm +++ b/apps/monitoring/netdata/restapi/mode/disks.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::monitoring::netdata::restapi::mode::diskusage; +package apps::monitoring::netdata::restapi::mode::disks; use base qw(centreon::plugins::templates::counter); @@ -32,7 +32,7 @@ sub custom_usage_output { my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); return sprintf( - 'Usage - Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)', + 'total: %s used: %s (%.2f%%) free: %s (%.2f%%)', $total_size_value . " " . $total_size_unit, $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} @@ -50,7 +50,7 @@ sub set_counters { $self->{maps_counters_type} = [ { name => 'global', type => 0 }, - { name => 'diskpath', type => 1, cb_prefix_output => 'prefix_diskpath_output', message_multiple => 'All partitions are ok' }, + { name => 'diskpath', type => 1, cb_prefix_output => 'prefix_diskpath_output', message_multiple => 'All partitions are ok' } ]; $self->{maps_counters}->{global} = [ @@ -67,7 +67,7 @@ sub set_counters { key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], closure_custom_output => $self->can('custom_usage_output'), perfdatas => [ - { label => 'used', value => 'used', template => '%d', min => 0, max => 'total', + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ] } @@ -76,16 +76,16 @@ sub set_counters { key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ], closure_custom_output => $self->can('custom_usage_output'), perfdatas => [ - { label => 'free', value => 'free', template => '%d', min => 0, max => 'total', + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ] } }, { label => 'usage-prct', display_ok => 0, nlabel => 'storage.space.usage.percentage', set => { key_values => [ { name => 'prct_used' }, { name => 'display' } ], - output_template => 'Used : %.2f %%', + output_template => 'used: %.2f %%', perfdatas => [ - { label => 'used_prct', value => 'prct_used', template => '%.2f', min => 0, max => 100, + { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' } ] } @@ -99,20 +99,15 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, - 'fs-name:s' => { name => 'fs_name' }, - 'space-reservation' => { name => 'space_reservation'} + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'fs-name:s' => { name => 'fs_name' }, + 'space-reservation' => { name => 'space_reservation'} }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub manage_selection { my ($self, %options) = @_; @@ -127,7 +122,6 @@ sub manage_selection { my $result = $options{custom}->get_data( chart => $fs, dimensions => 'used,avail,reserved_for_root', - points => $self->{option_results}->{chart_point}, after_period => $self->{option_results}->{chart_period}, group => $self->{option_results}->{chart_statistics} ); @@ -146,7 +140,7 @@ sub manage_selection { } } - my $reserved_space = defined($self->{option_results}->{space_reservation}) ? $self->{fs}->{$fs}->{"reserved for root"} * (1024 ** 3) : '0'; + my $reserved_space = defined($self->{option_results}->{space_reservation}) ? $self->{fs}->{$fs}->{'reserved for root'} * (1024 ** 3) : '0'; my $used = $self->{fs}->{$fs}->{used} * (1024 ** 3); my $free = $self->{fs}->{$fs}->{avail} * (1024 ** 3); my $total = $used + $free + $reserved_space; @@ -169,11 +163,6 @@ sub manage_selection { }; $self->{global}->{count}++; } - - if (scalar(keys %{$self->{diskpath}}) <= 0) { - $self->{output}->add_option_msg(short_msg => 'Issue with disk path information (see details)'); - $self->{output}->option_exit(); - } }; 1; @@ -237,7 +226,6 @@ Critical threshold on FS free space. On specific systems, partitions can have reserved space (like ext4 for root). This option will consider this space in the calculation (like for the 'df' command). - =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/getchart.pm b/apps/monitoring/netdata/restapi/mode/getchart.pm index 17ec4e04d..4029fc0aa 100644 --- a/apps/monitoring/netdata/restapi/mode/getchart.pm +++ b/apps/monitoring/netdata/restapi/mode/getchart.pm @@ -24,7 +24,7 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); sub custom_metric_perfdata { my ($self, %options) = @_; @@ -34,7 +34,7 @@ sub custom_metric_perfdata { value => $self->{result_values}->{value}, unit => $self->{result_values}->{unit}, warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-metric'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-metric') ); } @@ -60,34 +60,22 @@ sub custom_metric_output { ); } -sub custom_metric_calc { - my ($self, %options) = @_; - - $self->{result_values}->{value} = $options{new_datas}->{$self->{instance} . '_value'}; - $self->{result_values}->{perf_label} = $options{new_datas}->{$self->{instance} . '_perf_label'}; - $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'}; - $self->{result_values}->{unit} = $options{new_datas}->{$self->{instance} . '_unit'}; - return 0; -} - sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'metric', type => 1, message_multiple => 'All metrics are ok' }, + { name => 'metric', type => 1, message_multiple => 'All metrics are ok' } ]; $self->{maps_counters}->{metric} = [ { label => 'metric', set => { key_values => [ { name => 'value' }, { name => 'perf_label' }, { name => 'display' }, { name => 'unit' } ], - closure_custom_calc => $self->can('custom_metric_calc'), closure_custom_output => $self->can('custom_metric_output'), closure_custom_perfdata => $self->can('custom_metric_perfdata'), - closure_custom_threshold_check => $self->can('custom_metric_threshold'), + closure_custom_threshold_check => $self->can('custom_metric_threshold') } } ]; - } sub new { @@ -113,7 +101,6 @@ sub check_options { $self->{output}->add_option_msg(short_msg => "Missing --chart-name option or value."); $self->{output}->option_exit(); } - } sub manage_selection { @@ -141,14 +128,15 @@ sub manage_selection { $self->{metrics}->{$chart_name}->{$chart_label} = shift @{$chart_value}; } } + foreach my $metric (keys %{$self->{metrics}->{$chart_name}}) { - next if $metric eq 'time'; + next if ($metric eq 'time'); foreach my $value (values %{$self->{metrics}->{$chart_name}}) { $self->{metric}->{$metric . '_' . $stat} = { display => $metric . '_' . $stat, value => $value, unit => $unit, - perf_label => $metric . '_' . $stat, + perf_label => $metric . '_' . $stat }; } } diff --git a/apps/monitoring/netdata/restapi/mode/inodes.pm b/apps/monitoring/netdata/restapi/mode/inodes.pm index a9375212a..fb82a1728 100644 --- a/apps/monitoring/netdata/restapi/mode/inodes.pm +++ b/apps/monitoring/netdata/restapi/mode/inodes.pm @@ -37,11 +37,10 @@ sub set_counters { key_values => [ { name => 'prct_used' }, { name => 'display' } ], output_template => 'Used: %.2f %%', perfdatas => [ - { label => 'prct_used', value => 'prct_used', template => '%d', - unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }, - ], + { template => '%d', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }, + ] } - }, + } ]; } @@ -66,11 +65,6 @@ sub new { return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub manage_selection { my ($self, %options) = @_; @@ -173,7 +167,6 @@ Critical threshold on FS used Inodes (in %). On specific systems, partitions can have reserved space/inodes (like ext4 for root). This option will consider this space in the calculation (like for the 'df' command). - =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/loadaverage.pm b/apps/monitoring/netdata/restapi/mode/load.pm similarity index 80% rename from apps/monitoring/netdata/restapi/mode/loadaverage.pm rename to apps/monitoring/netdata/restapi/mode/load.pm index 881a52e0c..175d8c470 100644 --- a/apps/monitoring/netdata/restapi/mode/loadaverage.pm +++ b/apps/monitoring/netdata/restapi/mode/load.pm @@ -18,7 +18,7 @@ # limitations under the License. # -package apps::monitoring::netdata::restapi::mode::loadaverage; +package apps::monitoring::netdata::restapi::mode::load; use base qw(centreon::plugins::templates::counter); @@ -28,14 +28,14 @@ use warnings; sub prefix_load_output { my ($self, %options) = @_; - return "Load average "; + return 'Load average '; } sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'loadaverage', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 }, cb_prefix_output => 'prefix_load_output' } + { name => 'loadaverage', type => 0, skipped_code => { -10 => 1 }, cb_prefix_output => 'prefix_load_output' } ]; $self->{maps_counters}->{loadaverage} = [ @@ -43,7 +43,7 @@ sub set_counters { key_values => [ { name => 'load1' } ], output_template => '%.2f (1m)', perfdatas => [ - { label => 'load1', template => '%.2f', min => 0 } + { template => '%.2f', min => 0 } ] } }, @@ -51,7 +51,7 @@ sub set_counters { key_values => [ { name => 'load5' } ], output_template => '%.2f (5m)', perfdatas => [ - { label => 'load5', template => '%.2f', min => 0 } + { template => '%.2f', min => 0 } ] } }, @@ -59,7 +59,7 @@ sub set_counters { key_values => [ { name => 'load15' }, { name => 'load1' }, { name => 'load5' } ], output_template => '%.2f (15m)', perfdatas => [ - { label => 'load15', template => '%.2f', min => 0 } + { template => '%.2f', min => 0 } ] } } @@ -72,20 +72,16 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub manage_selection { my ($self, %options) = @_; + my $result = $options{custom}->get_data( chart => 'system.load', points => $self->{option_results}->{chart_point}, @@ -142,4 +138,4 @@ Critical threshold where '*' can be: load1, load5, load15 =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/memory.pm b/apps/monitoring/netdata/restapi/mode/memory.pm index 134502c3b..e0a07ac7e 100644 --- a/apps/monitoring/netdata/restapi/mode/memory.pm +++ b/apps/monitoring/netdata/restapi/mode/memory.pm @@ -29,7 +29,7 @@ sub custom_usage_output { my ($self, %options) = @_; return sprintf( - 'Ram Total: %s %s Used (-buffers/cache): %s %s (%.2f%%) Free: %s %s (%.2f%%)', + 'Ram total: %s %s used (-buffers/cache): %s %s (%.2f%%) free: %s %s (%.2f%%)', $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), $self->{result_values}->{prct_used}, @@ -42,7 +42,7 @@ sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'ram', type => 0, skipped_code => { -10 => 1 } }, + { name => 'ram', type => 0, skipped_code => { -10 => 1 } } ]; $self->{maps_counters}->{ram} = [ @@ -50,7 +50,7 @@ sub set_counters { key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], closure_custom_output => $self->can('custom_usage_output'), perfdatas => [ - { label => 'used', template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } ] } }, @@ -58,15 +58,15 @@ sub set_counters { key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], closure_custom_output => $self->can('custom_usage_output'), perfdatas => [ - { label => 'free', template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } ] } }, { label => 'usage-prct', display_ok => 0, nlabel => 'memory.usage.percentage', set => { key_values => [ { name => 'prct_used' } ], - output_template => 'Ram Used : %.2f %%', + output_template => 'Ram used : %.2f %%', perfdatas => [ - { label => 'used_prct', template => '%.2f', min => 0, max => 100, unit => '%' } + { template => '%.2f', min => 0, max => 100, unit => '%' } ] } }, @@ -75,7 +75,7 @@ sub set_counters { output_template => 'Buffer: %s %s', output_change_bytes => 1, perfdatas => [ - { label => 'buffer', template => '%d', min => 0, unit => 'B' } + { template => '%d', min => 0, unit => 'B' } ] } }, @@ -84,7 +84,7 @@ sub set_counters { output_template => 'Cached: %s %s', output_change_bytes => 1, perfdatas => [ - { label => 'cached', template => '%d', min => 0, unit => 'B' } + { template => '%d', min => 0, unit => 'B' } ] } }, @@ -93,7 +93,7 @@ sub set_counters { output_template => 'Shared: %s %s', output_change_bytes => 1, perfdatas => [ - { label => 'shared', template => '%d', min => 0, unit => 'B' } + { template => '%d', min => 0, unit => 'B' } ] } } @@ -106,21 +106,16 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); - -} - sub manage_selection { my ($self, %options) = @_; + my $result = $options{custom}->get_data( chart => 'system.ram', points => $self->{option_results}->{chart_point}, @@ -216,4 +211,4 @@ buffer,cached,shared =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/swap.pm b/apps/monitoring/netdata/restapi/mode/swap.pm index 5086d1a3a..36ab44fe6 100644 --- a/apps/monitoring/netdata/restapi/mode/swap.pm +++ b/apps/monitoring/netdata/restapi/mode/swap.pm @@ -28,20 +28,21 @@ use warnings; sub custom_swap_output { my ($self, %options) = @_; - my $msg = sprintf("Swap Total: %s %s Used: %s %s (%.2f%%) Free: %s %s (%.2f%%)", + return sprintf( + 'Swap total: %s %s used: %s %s (%.2f%%) free: %s %s (%.2f%%)', $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}), $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}), $self->{result_values}->{prct_used}, $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}), - $self->{result_values}->{prct_free}); - return $msg; + $self->{result_values}->{prct_free} + ); } sub set_counters { my ($self, %options) = @_; $self->{maps_counters_type} = [ - { name => 'swap', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } }, + { name => 'swap', type => 0, message_separator => ' - ', skipped_code => { -10 => 1 } } ]; $self->{maps_counters}->{swap} = [ @@ -49,29 +50,26 @@ sub set_counters { key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], closure_custom_output => $self->can('custom_swap_output'), perfdatas => [ - { label => 'used', value => 'used', template => '%d', min => 0, max => 'total', - unit => 'B', cast_int => 1 }, - ], + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } + ] } }, { label => 'usage-free', display_ok => 0, nlabel => 'swap.free.bytes', set => { key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], closure_custom_output => $self->can('custom_swap_output'), perfdatas => [ - { label => 'free', value => 'free', template => '%d', min => 0, max => 'total', - unit => 'B', cast_int => 1 }, - ], + { template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1 } + ] } }, { label => 'usage-prct', display_ok => 0, nlabel => 'swap.usage.percentage', set => { key_values => [ { name => 'prct_used' } ], - output_template => 'Used : %.2f %%', + output_template => 'Swap used: %.2f %%', perfdatas => [ - { label => 'used_prct', value => 'prct_used', template => '%.2f', min => 0, max => 100, - unit => '%' }, - ], + { template => '%.2f', min => 0, max => 100, unit => '%' } + ] } - }, + } ]; } @@ -81,31 +79,27 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub manage_selection { my ($self, %options) = @_; - my $result = $options{custom}->get_data( - chart => 'system.swap', - points => $self->{option_results}->{chart_point}, - after_period => $self->{option_results}->{chart_period}, - group => $self->{option_results}->{chart_statistics} - ); - foreach my $swap_value (@{$result->{data}}) { - foreach my $swap_label (@{$result->{labels}}) { - $self->{swap_data}->{$swap_label} = shift @{$swap_value}; - } + + my $result = $options{custom}->get_data( + chart => 'system.swap', + points => $self->{option_results}->{chart_point}, + after_period => $self->{option_results}->{chart_period}, + group => $self->{option_results}->{chart_statistics} + ); + foreach my $swap_value (@{$result->{data}}) { + foreach my $swap_label (@{$result->{labels}}) { + $self->{swap_data}->{$swap_label} = shift @{$swap_value}; } + } my $used = $self->{swap_data}->{used} * 1024 * 1024; my $free = $self->{swap_data}->{free} * 1024 * 1024; @@ -175,4 +169,4 @@ Critical threshold on free swap (in B) =back -=cut \ No newline at end of file +=cut diff --git a/apps/monitoring/netdata/restapi/mode/traffic.pm b/apps/monitoring/netdata/restapi/mode/traffic.pm index f4cd88707..bb1567c46 100644 --- a/apps/monitoring/netdata/restapi/mode/traffic.pm +++ b/apps/monitoring/netdata/restapi/mode/traffic.pm @@ -39,26 +39,24 @@ sub set_counters { ]; $self->{maps_counters}->{interfaces} = [ - { label => 'traffic-in', nlabel => 'network.trafficin.bitspersecond', set => { - key_values => [ { name => 'traffic_in' }, { name => 'traffic_out' }, { name => 'display' } ], - output_template => 'Traffic In: %.2f%s/s', + { label => 'traffic-in', nlabel => 'network.traffic.in.bitspersecond', set => { + key_values => [ { name => 'traffic_in' }, { name => 'speed' }, { name => 'display' } ], + output_template => 'traffic in: %.2f%s/s', output_change_bytes => 2, perfdatas => [ - { label => 'traffic_in', value => 'traffic_in', template => '%d', min => 0, max => 'speed', - unit => 'b/s', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } + { template => '%d', min => 0, max => 'speed', unit => 'b/s', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ] } }, - { label => 'traffic-out', nlabel => 'network.trafficin.bitspersecond', set => { - key_values => [ { name => 'traffic_out' }, { name => 'traffic_in' }, { name => 'display' } ], - output_template => 'Traffic Out: %.2f%s/s', + { label => 'traffic-out', nlabel => 'network.traffic.out.bitspersecond', set => { + key_values => [ { name => 'traffic_out' }, { name => 'speed' }, { name => 'display' } ], + output_template => 'traffic out: %.2f%s/s', output_change_bytes => 2, perfdatas => [ - { label => 'traffic_out', value => 'traffic_out', template => '%d', min => 0, max => 'speed', - unit => 'b/s', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } + { template => '%d', min => 0, max => 'speed', unit => 'b/s', cast_int => 1, label_extra_instance => 1, instance_use => 'display' } ] } - }, + } ]; } @@ -68,33 +66,30 @@ sub new { bless $self, $class; $options{options}->add_options(arguments => { - 'chart-period:s' => { name => 'chart_period', default => '300' }, - 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, - 'interface-name:s' => { name => 'interface_name' }, - 'speed:s' => { name => 'speed', default => '1000000000'} + 'chart-period:s' => { name => 'chart_period', default => '300' }, + 'chart-statistics:s' => { name => 'chart_statistics', default => 'average' }, + 'interface-name:s' => { name => 'interface_name' }, + 'speed:s' => { name => 'speed' } }); return $self; } -sub check_options { - my ($self, %options) = @_; - $self->SUPER::check_options(%options); -} - sub manage_selection { my ($self, %options) = @_; my $full_list = $options{custom}->list_charts(); + my $interface_list = []; foreach my $chart (values %{$full_list->{charts}}) { next if ($chart->{name} !~ '^net\.'); - push @{$self->{interface_list}}, $chart->{name}; + push @$interface_list, { name => $chart->{name}, speed => $chart->{chart_variables}->{nic_speed_max} }; } - foreach my $interface (@{$self->{interface_list}}) { + $self->{interfaces} = {}; + foreach my $int (@$interface_list) { my $result = $options{custom}->get_data( - chart => $interface, + chart => $int->{name}, dimensions => 'received,sent', points => $self->{option_results}->{chart_point}, after_period => $self->{option_results}->{chart_period}, @@ -102,24 +97,29 @@ sub manage_selection { absolute => 1 ); - $interface =~ s/net.//; + $int->{name} =~ s/net.//; next if (defined($self->{option_results}->{interface_name}) && $self->{option_results}->{interface_name} ne '' && - $interface !~ /$self->{option_results}->{interface_name}/ + $int->{name} !~ /$self->{option_results}->{interface_name}/ ); - foreach my $interface_value (@{$result->{data}}) { - foreach my $interface_label (@{$result->{labels}}) { - $self->{interface}->{$interface}->{$interface_label} = shift @{$interface_value}; + my $count = 0; + # In Kb/s + my $metrics = { received => 0, sent => 0 }; + foreach my $data (@{$result->{data}}) { + while (my ($index, $label) = each(@{$result->{labels}})) { + $metrics->{$label} += $data->[$index]; } + $count++; } - $self->{interfaces}->{$interface} = { - display => $interface, - traffic_in => $self->{interface}->{$interface}->{received} * 1000, - traffic_out => $self->{interface}->{$interface}->{sent} * 1000, - speed => $self->{option_results}->{speed} + $self->{interfaces}->{ $int->{name} } = { + display => $int->{name}, + traffic_in => $metrics->{received} * 1000, + traffic_out => $metrics->{sent} * 1000, + speed => defined($self->{option_results}->{speed}) && $self->{option_results}->{speed} ne '' ? + $self->{option_results}->{speed} : $int->{speed} * 1000 * 1000 }; } @@ -163,8 +163,8 @@ Example: --interface-name='^eth0$' =item B<--speed> -Set interfaces speed in B/s. -Default: 1000000000 (1GB/s). +Set interfaces speed in b/s. +Default: 1000000000 (1Gb/s). =item B<--warning-traffic-*> diff --git a/apps/monitoring/netdata/restapi/plugin.pm b/apps/monitoring/netdata/restapi/plugin.pm index 8b31b3d3c..1b4f3c299 100644 --- a/apps/monitoring/netdata/restapi/plugin.pm +++ b/apps/monitoring/netdata/restapi/plugin.pm @@ -30,20 +30,20 @@ sub new { bless $self, $class; $self->{version} = '1.0'; - %{$self->{modes}} = ( - 'alarms' => 'apps::monitoring::netdata::restapi::mode::alarms', - 'cpu' => 'apps::monitoring::netdata::restapi::mode::cpu', - 'diskusage' => 'apps::monitoring::netdata::restapi::mode::diskusage', - 'get-chart' => 'apps::monitoring::netdata::restapi::mode::getchart', - 'inodes' => 'apps::monitoring::netdata::restapi::mode::inodes', - 'list-charts' => 'apps::monitoring::netdata::restapi::mode::listcharts', - 'loadaverage' => 'apps::monitoring::netdata::restapi::mode::loadaverage', - 'memory' => 'apps::monitoring::netdata::restapi::mode::memory', - 'swap' => 'apps::monitoring::netdata::restapi::mode::swap', - 'traffic' => 'apps::monitoring::netdata::restapi::mode::traffic' - ); + $self->{modes} = { + 'alarms' => 'apps::monitoring::netdata::restapi::mode::alarms', + 'cpu' => 'apps::monitoring::netdata::restapi::mode::cpu', + 'disks' => 'apps::monitoring::netdata::restapi::mode::disks', + 'get-chart' => 'apps::monitoring::netdata::restapi::mode::getchart', + 'inodes' => 'apps::monitoring::netdata::restapi::mode::inodes', + 'list-charts' => 'apps::monitoring::netdata::restapi::mode::listcharts', + 'load' => 'apps::monitoring::netdata::restapi::mode::load', + 'memory' => 'apps::monitoring::netdata::restapi::mode::memory', + 'swap' => 'apps::monitoring::netdata::restapi::mode::swap', + 'traffic' => 'apps::monitoring::netdata::restapi::mode::traffic' + }; - $self->{custom_modes}{restapi} = 'apps::monitoring::netdata::restapi::custom::api'; + $self->{custom_modes}->{restapi} = 'apps::monitoring::netdata::restapi::custom::api'; return $self; }