fix(docker,cadvisor): output_add errors (#2932)

This commit is contained in:
qgarnier 2021-07-02 11:43:48 +02:00 committed by GitHub
parent 2f9a99d256
commit c19314dbb8
2 changed files with 23 additions and 13 deletions

View File

@ -122,8 +122,10 @@ sub internal_api_list_nodes {
}; };
if ($@) { if ($@) {
$nodes = {}; $nodes = {};
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Node '$options{node_name}': cannot decode json list nodes response: $@"); severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json list nodes response: $@"
);
} else { } else {
$nodes = {} if (ref($nodes) eq 'ARRAY'); # nodes is not in a swarm $nodes = {} if (ref($nodes) eq 'ARRAY'); # nodes is not in a swarm
} }
@ -144,8 +146,10 @@ sub internal_api_info {
}; };
if ($@) { if ($@) {
$nodes = []; $nodes = [];
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Node '$options{node_name}': cannot decode json info response: $@"); severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json info response: $@"
);
} }
return $nodes; return $nodes;
@ -165,8 +169,10 @@ sub internal_api_list_containers {
}; };
if ($@) { if ($@) {
$containers = []; $containers = [];
$self->{output}->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Node '$options{node_name}': cannot decode json get containers response: $@"); severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get containers response: $@"
);
} }
foreach my $container (@{$containers_ids->{subcontainers}}) { foreach my $container (@{$containers_ids->{subcontainers}}) {
my $json_response = JSON::XS->new->utf8->decode( my $json_response = JSON::XS->new->utf8->decode(
@ -199,8 +205,10 @@ sub internal_api_get_machine_stats {
}; };
if ($@) { if ($@) {
$machine_stats = {}; $machine_stats = {};
$self->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@"); severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@"
);
} else { } else {
$machine_stats->{num_cores} = $full_machine_stats->{num_cores}; $machine_stats->{num_cores} = $full_machine_stats->{num_cores};
$machine_stats->{memory_capacity} = $full_machine_stats->{memory_capacity}; $machine_stats->{memory_capacity} = $full_machine_stats->{memory_capacity};
@ -221,8 +229,10 @@ sub internal_api_get_container_stats {
}; };
if ($@) { if ($@) {
$container_stats = []; $container_stats = [];
$self->output_add(severity => 'UNKNOWN', $self->{output}->output_add(
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@"); severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@"
);
} else { } else {
$container_stats->[0] = $full_container_stats->{stats}[0]; $container_stats->[0] = $full_container_stats->{stats}[0];
$container_stats->[1] = $full_container_stats->{stats}[scalar(@{$full_container_stats->{stats}}) - 1]; $container_stats->[1] = $full_container_stats->{stats}[scalar(@{$full_container_stats->{stats}}) - 1];

View File

@ -116,7 +116,7 @@ sub api_display {
} }
if (!open (FH, '>', $self->{option_results}->{api_write_file})) { if (!open (FH, '>', $self->{option_results}->{api_write_file})) {
$self->output_add( $self->{output}->output_add(
severity => 'UNKNOWN', severity => 'UNKNOWN',
short_msg => "cannot open file '" . $self->{option_results}->{api_write_file} . "': $!" short_msg => "cannot open file '" . $self->{option_results}->{api_write_file} . "': $!"
); );
@ -125,7 +125,7 @@ sub api_display {
FH->autoflush(1); FH->autoflush(1);
print FH $options{content}; print FH $options{content};
close FH; close FH;
$self->output_add( $self->{output}->output_add(
severity => 'OK', severity => 'OK',
short_msg => "Data written in file '" . $self->{option_results}->{api_write_file} . "': $!" short_msg => "Data written in file '" . $self->{option_results}->{api_write_file} . "': $!"
); );
@ -300,7 +300,7 @@ sub internal_api_get_container_stats {
}; };
if ($@) { if ($@) {
$container_stats = {}; $container_stats = {};
$self->output_add( $self->{output}->output_add(
severity => 'UNKNOWN', severity => 'UNKNOWN',
short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@" short_msg => "Node '$options{node_name}': cannot decode json get container stats response: $@"
); );