Skip to content

Commit

Permalink
(plugin) cloud::cadvisor::restapi - mode container-usage fix uninitia… (
Browse files Browse the repository at this point in the history
  • Loading branch information
garnier-quentin authored Sep 27, 2023
1 parent 5863ac7 commit aa45bfa
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/cloud/cadvisor/restapi/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/cloud/cadvisor/restapi/mode/containerusage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit aa45bfa

Please sign in to comment.