From aa45bfa7ffe2a2c461beeeb0146453cb1d653625 Mon Sep 17 00:00:00 2001 From: qgarnier Date: Wed, 27 Sep 2023 16:22:29 +0200 Subject: [PATCH] =?UTF-8?q?(plugin)=20cloud::cadvisor::restapi=20-=20mode?= =?UTF-8?q?=20container-usage=20fix=20uninitia=E2=80=A6=20(#4667)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cloud/cadvisor/restapi/custom/api.pm | 30 ++++++++++++++----- .../cadvisor/restapi/mode/containerusage.pm | 1 + 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/cloud/cadvisor/restapi/custom/api.pm b/src/cloud/cadvisor/restapi/custom/api.pm index 5c61fc0eb7..9982d8ee20 100644 --- a/src/cloud/cadvisor/restapi/custom/api.pm +++ b/src/cloud/cadvisor/restapi/custom/api.pm @@ -111,11 +111,13 @@ sub get_port { sub internal_api_list_nodes { my ($self, %options) = @_; - + my $response = $self->{http}->request( hostname => $options{node_name}, url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path}, - unknown_status => '', critical_status => '', warning_status => ''); + unknown_status => '', critical_status => '', warning_status => '' + ); + my $nodes; eval { $nodes = JSON::XS->new->utf8->decode($response); @@ -139,7 +141,9 @@ sub internal_api_info { my $response = $self->{http}->request( hostname => $options{node_name}, url_path => '/api/' . $self->{option_results}->{api_version} . '/machine/', - unknown_status => '', critical_status => '', warning_status => ''); + unknown_status => '', critical_status => '', warning_status => '' + ); + my $nodes; eval { $nodes = JSON::XS->new->utf8->decode($response); @@ -157,12 +161,13 @@ sub internal_api_info { sub internal_api_list_containers { my ($self, %options) = @_; - + my $response = $self->{http}->request( hostname => $options{node_name}, url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path}, unknown_status => '', critical_status => '', warning_status => '' ); + my $containers = []; my $containers_ids; eval { @@ -209,10 +214,13 @@ sub internal_api_list_containers { sub internal_api_get_machine_stats { my ($self, %options) = @_; + my $response = $self->{http}->request( hostname => $options{node_name}, url_path => '/api/' . $self->{option_results}->{api_version} . '/machine', - unknown_status => '', critical_status => '', warning_status => ''); + unknown_status => '', critical_status => '', warning_status => '' + ); + my $machine_stats; my $full_machine_stats; eval { @@ -239,6 +247,7 @@ sub internal_api_get_container_stats { url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path} . '/' . $options{container_id}, unknown_status => '', critical_status => '', warning_status => '' ); + my $container_stats; my $full_container_stats; eval { @@ -251,8 +260,15 @@ sub internal_api_get_container_stats { short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@" ); } else { - $container_stats->[0] = $full_container_stats->{stats}[0]; - $container_stats->[1] = $full_container_stats->{stats}[scalar(@{$full_container_stats->{stats}}) - 1]; + my $stats; + if (defined($full_container_stats->{stats})) { + $stats = $full_container_stats->{stats}; + } else { + my @keys = keys(%$full_container_stats); + $stats = $full_container_stats->{ $keys[0] }->{stats}; + } + $container_stats->[0] = $stats->[0]; + $container_stats->[1] = $stats->[scalar(@$stats) - 1]; } return $container_stats; } diff --git a/src/cloud/cadvisor/restapi/mode/containerusage.pm b/src/cloud/cadvisor/restapi/mode/containerusage.pm index e9d0f2658c..648c9bf23f 100644 --- a/src/cloud/cadvisor/restapi/mode/containerusage.pm +++ b/src/cloud/cadvisor/restapi/mode/containerusage.pm @@ -156,6 +156,7 @@ sub manage_selection { $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1); next; } + my $first_index = 0; my $first_stat = $result->{$container_id}->{Stats}[$first_index]; my $first_ts = $first_stat->{timestamp};