(plugin) cloud::cadvisor::restapi - manage cgroupv2 api (#3599)

This commit is contained in:
qgarnier 2022-04-15 14:42:26 +02:00 committed by GitHub
parent 3db06de4d9
commit 5b5ae8334f
2 changed files with 45 additions and 37 deletions

View File

@ -161,7 +161,8 @@ sub internal_api_list_containers {
my $response = $self->{http}->request( my $response = $self->{http}->request(
hostname => $options{node_name}, hostname => $options{node_name},
url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path}, 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 $containers = []; my $containers = [];
my $containers_ids; my $containers_ids;
eval { eval {
@ -174,21 +175,35 @@ sub internal_api_list_containers {
short_msg => "Node '$options{node_name}': cannot decode json get containers response: $@" short_msg => "Node '$options{node_name}': cannot decode json get containers response: $@"
); );
} }
foreach my $container (@{$containers_ids->{subcontainers}}) {
my $json_response = JSON::XS->new->utf8->decode( # cgroup v1
$self->{http}->request( if (defined($containers_ids->{subcontainers})) {
hostname => $options{node_name}, foreach my $container (@{$containers_ids->{subcontainers}}) {
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/' . $container->{name} my $json_response = JSON::XS->new->utf8->decode(
) $self->{http}->request(
); hostname => $options{node_name},
url_path => '/api/' . $self->{option_results}->{api_version} . '/containers/' . $container->{name}
push @$containers, { )
id => defined($json_response->{id}) ? $json_response->{id} : $json_response->{name}, );
names => defined($json_response->{aliases}) ? $json_response->{aliases} : [$json_response->{name}],
node => $options{node_name} push @$containers, {
}; id => defined($json_response->{id}) ? $json_response->{id} : $json_response->{name},
names => defined($json_response->{aliases}) ? $json_response->{aliases} : [$json_response->{name}],
node => $options{node_name}
};
}
} else {
# cgroup v2
foreach (values %$containers_ids) {
next if (!defined($_->{id}));
push @$containers, {
id => defined($_->{id}) ? $_->{id} : $_->{name},
names => defined($_->{aliases}) ? $_->{aliases} : [$_->{name}],
node => $options{node_name}
};
}
} }
return $containers; return $containers;
} }
@ -218,10 +233,12 @@ sub internal_api_get_machine_stats {
sub internal_api_get_container_stats { sub internal_api_get_container_stats {
my ($self, %options) = @_; my ($self, %options) = @_;
my $response = $self->{http}->request( my $response = $self->{http}->request(
hostname => $options{node_name}, hostname => $options{node_name},
url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path} . '/' . $options{container_id}, url_path => '/api/' . $self->{option_results}->{api_version} . $self->{option_results}->{api_path} . '/' . $options{container_id},
unknown_status => '', critical_status => '', warning_status => ''); unknown_status => '', critical_status => '', warning_status => ''
);
my $container_stats; my $container_stats;
my $full_container_stats; my $full_container_stats;
eval { eval {

View File

@ -48,7 +48,7 @@ sub set_counters {
my ($self, %options) = @_; my ($self, %options) = @_;
$self->{maps_counters_type} = [ $self->{maps_counters_type} = [
{ name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', message_multiple => 'All containers are ok', skipped_code => { -11 => 1 } }, { name => 'containers', type => 1, cb_prefix_output => 'prefix_containers_output', message_multiple => 'All containers are ok', skipped_code => { -11 => 1 } }
]; ];
$self->{maps_counters}->{containers} = [ $self->{maps_counters}->{containers} = [
@ -56,8 +56,7 @@ sub set_counters {
key_values => [ { name => 'cpu_number'}, { name => 'display' } ], key_values => [ { name => 'cpu_number'}, { name => 'display' } ],
output_template => 'CPU: %d core(s)', output_template => 'CPU: %d core(s)',
perfdatas => [ perfdatas => [
{ label => 'cpu_number', template => '%d', { template => '%d', min => 0, label_extra_instance => 1, instance_use => 'display' }
min => 0, label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -65,8 +64,7 @@ sub set_counters {
key_values => [ { name => 'cpu_total'}, { name => 'display' } ], key_values => [ { name => 'cpu_total'}, { name => 'display' } ],
output_template => 'CPU Usage: %.2f %%', output_template => 'CPU Usage: %.2f %%',
perfdatas => [ perfdatas => [
{ label => 'cpu_total', template => '%.2f', { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -74,8 +72,7 @@ sub set_counters {
key_values => [ { name => 'cpu_user'}, { name => 'display' } ], key_values => [ { name => 'cpu_user'}, { name => 'display' } ],
output_template => 'CPU User: %.2f %%', output_template => 'CPU User: %.2f %%',
perfdatas => [ perfdatas => [
{ label => 'cpu_user', template => '%.2f', { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -83,8 +80,7 @@ sub set_counters {
key_values => [ { name => 'cpu_system' }, { name => 'display' } ], key_values => [ { name => 'cpu_system' }, { name => 'display' } ],
output_template => 'CPU System: %.2f %%', output_template => 'CPU System: %.2f %%',
perfdatas => [ perfdatas => [
{ label => 'cpu_system', template => '%.2f', { template => '%.2f', unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -93,8 +89,7 @@ sub set_counters {
closure_custom_output => $self->can('custom_memory_output'), closure_custom_output => $self->can('custom_memory_output'),
output_change_bytes => 1, output_change_bytes => 1,
perfdatas => [ perfdatas => [
{ label => 'memory_used', template => '%s', { template => '%s', min => 0, max => 'memory_total',unit => 'B', label_extra_instance => 1, instance_use => 'display' }
min => 0, max => 'memory_total',unit => 'B', label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -103,8 +98,7 @@ sub set_counters {
output_template => 'Memory Cache: %s %s', output_template => 'Memory Cache: %s %s',
output_change_bytes => 1, output_change_bytes => 1,
perfdatas => [ perfdatas => [
{ label => 'memory_cache', template => '%s', { template => '%s', min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -113,8 +107,7 @@ sub set_counters {
output_template => 'Memory RSS: %s %s', output_template => 'Memory RSS: %s %s',
output_change_bytes => 1, output_change_bytes => 1,
perfdatas => [ perfdatas => [
{ label => 'memory_rss', template => '%s', { template => '%s', min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
] ]
} }
}, },
@ -123,8 +116,7 @@ sub set_counters {
output_template => 'Swap: %s %s', output_template => 'Swap: %s %s',
output_change_bytes => 1, output_change_bytes => 1,
perfdatas => [ perfdatas => [
{ label => 'swap', template => '%s', { template => '%s', min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display' }
] ]
} }
} }
@ -181,8 +173,7 @@ sub manage_selection {
my $last_cpu_system = $last_stat->{cpu}{usage}{system}; my $last_cpu_system = $last_stat->{cpu}{usage}{system};
my $diff_ts = $last_dt - $first_dt; my $diff_ts = $last_dt - $first_dt;
my $cpu_number = $machine_stats->{$result->{$container_id}->{NodeName}}->{num_cores}; my $cpu_number = $machine_stats->{ $result->{$container_id}->{NodeName} }->{num_cores};
$self->{containers}->{$container_id} = { $self->{containers}->{$container_id} = {
node_name => $result->{$container_id}->{NodeName}, node_name => $result->{$container_id}->{NodeName},
@ -196,7 +187,7 @@ sub manage_selection {
memory_cache => $last_stat->{memory}{cache}, memory_cache => $last_stat->{memory}{cache},
memory_rss => $last_stat->{memory}{rss}, memory_rss => $last_stat->{memory}{rss},
swap => $last_stat->{memory}{swap}, swap => $last_stat->{memory}{swap},
memory_total => $machine_stats->{$result->{$container_id}->{NodeName}}->{memory_capacity}, memory_total => $machine_stats->{$result->{$container_id}->{NodeName}}->{memory_capacity}
}; };
} }
@ -204,7 +195,7 @@ sub manage_selection {
$self->{output}->add_option_msg(short_msg => "No containers found."); $self->{output}->add_option_msg(short_msg => "No containers found.");
$self->{output}->option_exit(); $self->{output}->option_exit();
} }
my $hostnames = $options{custom}->get_hostnames(); my $hostnames = $options{custom}->get_hostnames();
} }