Skip to content

Commit

Permalink
Add container-usage mode
Browse files Browse the repository at this point in the history
  • Loading branch information
sdepassio committed Feb 6, 2025
1 parent aa3a5b3 commit 8cb78dd
Show file tree
Hide file tree
Showing 7 changed files with 395 additions and 20 deletions.
37 changes: 33 additions & 4 deletions src/apps/podman/restapi/custom/api.pm
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,35 @@ sub get_pod_infos {
return $pod;
}

sub get_container_infos {
my ($self, %options) = @_;

my $stats = $self->request(
endpoint => 'containers/stats?stream=false&containers=' . $options{container_name},
method => 'GET'
);

my $containers = $self->list_containers();
my $state;
foreach my $container_id (sort keys %{$containers}) {
if ($containers->{$container_id}->{Name} eq $options{container_name}) {
$state = $containers->{$container_id}->{State};
}
}

my $container = {
cpu_usage => $stats->{Stats}->[0]->{CPU},
memory_usage => $stats->{Stats}->[0]->{MemUsage},
io_read => $stats->{Stats}->[0]->{BlockInput},
io_write => $stats->{Stats}->[0]->{BlockOutput},
network_in => $stats->{Stats}->[0]->{NetInput},
network_out => $stats->{Stats}->[0]->{NetOutput},
state => $state
};

return $container;
}

1;

__END__
Expand All @@ -254,11 +283,11 @@ Podman REST API.
Podman Rest API custom mode.
To connect to the API with a socket, you must add the following command:
--curl-opt="CURLOPT_UNIX_SOCKET_PATH => 'PATH_TO_THE_SOCKET'"
C<--curl-opt="CURLOPT_UNIX_SOCKET_PATH => 'PATH_TO_THE_SOCKET'">
If you use a certificate, you must add the following commands:
--curl-opt="CURLOPT_CAINFO => 'PATH_TO_THE_CA_CERTIFICATE'"
--curl-opt="CURLOPT_SSLCERT => 'PATH_TO_THE_CERTIFICATE'"
--curl-opt="CURLOPT_SSLKEY => 'PATH_TO_THE_KEY'"
C<--curl-opt="CURLOPT_CAINFO = 'PATH_TO_THE_CA_CERTIFICATE'">
C<--curl-opt="CURLOPT_SSLCERT => 'PATH_TO_THE_CERTIFICATE'">
C<--curl-opt="CURLOPT_SSLKEY => 'PATH_TO_THE_KEY'">
=head1 REST API OPTIONS
Expand Down
248 changes: 248 additions & 0 deletions src/apps/podman/restapi/mode/containerusage.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
#
# Copyright 2025 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and application monitoring for
# service performance.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

package apps::podman::restapi::mode::containerusage;

use base qw(centreon::plugins::templates::counter);

use strict;
use warnings;
use Digest::MD5 qw(md5_hex);
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);

sub set_counters {
my ($self, %options) = @_;

$self->{maps_counters_type} = [
{ name => 'container', type => 0 }
];

$self->{maps_counters}->{container} = [
{ label => 'cpu-usage',
nlabel => 'podman.container.cpu.usage.percent',
set => {
key_values => [ { name => 'cpu_usage' } ],
output_template => 'CPU: %.2f%%',
perfdatas => [
{ label => 'cpu',
value => 'cpu_usage',
template => '%.2f',
unit => '%',
min => 0,
max => 100 }
]
}
},
{ label => 'memory-usage',
nlabel => 'podman.container.memory.usage.bytes',
set => {
key_values => [ { name => 'memory_usage' } ],
output_template => 'Memory: %s%s',
output_change_bytes => 1,
perfdatas => [
{ label => 'memory',
value => 'memory_usage',
template => '%s',
unit => 'B',
min => 0 }
]
}
},
{ label => 'read-io',
nlabel => 'podman.container.io.read',
set => {
key_values => [ { name => 'io_read' } ],
output_template => 'Read : %s%s',
output_change_bytes => 1,
perfdatas => [
{ label => 'read.io',
value => 'io_read',
template => '%s',
unit => 'B',
min => 0 }
]
}
},
{ label => 'write-io',
nlabel => 'podman.container.io.write',
set => {
key_values => [ { name => 'io_write' } ],
output_template => 'Write : %s%s',
output_change_bytes => 1,
perfdatas => [
{ label => 'write.io',
value => 'io_write',
template => '%s',
unit => 'B',
min => 0 }
]
}
},
{ label => 'network-in',
nlabel => 'podman.container.network.in',
set => {
key_values => [ { name => 'network_in' } ],
output_template => 'Network in: %s%s',
output_change_bytes => 1,
perfdatas => [
{ label => 'network_in',
value => 'network_in',
template => '%s',
unit => 'B',
min => 0 }
]
}
},
{ label => 'network-out',
nlabel => 'podman.container.network.out',
set => {
key_values => [ { name => 'network_out' } ],
output_template => 'Network out: %s%s',
output_change_bytes => 1,
perfdatas => [
{ label => 'network_out',
value => 'network_out',
template => '%s',
unit => 'B',
min => 0 }
]
}
},
{ label => 'state',
type => 2,
warning_default => '%{state} =~ /Paused/',
critical_default => '%{state} =~ /Exited/',
set => {
key_values => [ { name => 'state' } ],
output_template => 'State: %s',
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
}
];
}

sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;

$options{options}->add_options(arguments => {
'container-name:s' => { name => 'container_name' }
});

return $self;
}

sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);

if (centreon::plugins::misc::is_empty($self->{option_results}->{container_name})) {
$self->{output}->add_option_msg(short_msg => "Need to specify --container-name option.");
$self->{output}->option_exit();
}
}

sub manage_selection {
my ($self, %options) = @_;

my $container = $options{custom}->get_container_infos(
container_name => $self->{option_results}->{container_name}
);

$self->{container} = $container;
}

1;

__END__
=head1 MODE
Check container usage.
=over 8
=item B<--container-name>
Container name.
=item B--warning-cpu-usage>
Threshold warning for CPU usage.
=item B--critical-cpu-usage>
Threshold critical for CPU usage.
=item B--warning-memory-usage>
Threshold warning for memory usage.
=item B--critical-memory-usage>
Threshold critical for memory usage.
=item B<--warning-read-io>
Threshold warning for read IO.
=item B<--critical-read-io>
Threshold critical for read IO.
=item B<--warning-write-io>
Threshold warning for write IO.
=item B<--critical-write-io>
Threshold critical for write IO.
=item B<--warning-network-in>
Threshold warning for network in.
=item B<--critical-network-in>
Threshold critical for network in.
=item B<--warning-network-out>
Threshold warning for network out.
=item B<--critical-network-out>
Threshold critical for network out.
=item B<--warning-container-state>
Define the conditions to match for the state to be WARNING (default: '%{state} =~ /Paused/').
You can use the following variables: %{state}
=item B<--critical-container-state>
Define the conditions to match for the state to be CRITICAL (default: '%{state} =~ /Exited/').
You can use the following variables: %{state}
=back
=cut
6 changes: 4 additions & 2 deletions src/apps/podman/restapi/mode/podstatus.pm
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ Threshold critical for paused containers.
=item B<--warning-state>
Threshold warning for pod state.
Define the conditions to match for the state to be WARNING (default: '%{state} =~ /Exited/').
You can use the following variables: %{state}
=item B<--critical-state>
Threshold critical for pod state.
Define the conditions to match for the state to be CRITICAL (default: '%{state} =~ /Degraded/').
You can use the following variables: %{state}
=back
Expand Down
24 changes: 12 additions & 12 deletions src/apps/podman/restapi/mode/systemstatus.pm
Original file line number Diff line number Diff line change
Expand Up @@ -187,51 +187,51 @@ Check Podman system status.
=over 8
=item B < --warning-cpu-usage>
=item B<--warning-cpu-usage>
Threshold warning in percent for CPU usage.
=item B < --critical-cpu-usage>
=item B<--critical-cpu-usage>
Threshold critical in percent for CPU usage.
=item B < --warning-memory-usage>
=item B<--warning-memory-usage>
Threshold warning in bytes for memory usage.
=item B < --critical-memory-usage>
=item B<--critical-memory-usage>
Threshold critical in bytes for memory usage.
=item B < --warning-swap-usage>
=item B<--warning-swap-usage>
Threshold warning in bytes for swap usage.
=item B < --critical-swap-usage>
=item B<--critical-swap-usage>
Threshold critical in bytes for swap usage.
=item B < --warning-containers-running>
=item B<--warning-containers-running>
Threshold warning for the number of running containers.
=item B < --critical-containers-running>
=item B<--critical-containers-running>
Threshold critical for the number of running containers.
=item B < --warning-containers-stopped>
=item B<--warning-containers-stopped>
Threshold warning for the number of stopped containers.
=item B < --critical-containers-stopped>
=item B<--critical-containers-stopped>
Threshold critical for the number of stopped containers.
=item B < --warning-uptime>
=item B<--warning-uptime>
Threshold warning for uptime in seconds.
=item B < --critical-uptime>
=item B<--critical-uptime>
Threshold critical for uptime in seconds.
Expand Down
Loading

0 comments on commit 8cb78dd

Please sign in to comment.