| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package cloud::prometheus::exporters::cadvisor::mode::listcontainers; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub new { |
| 29 |
my ($class, %options) = @_; |
| 30 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 31 |
bless $self, $class; |
| 32 |
|
| 33 |
$options{options}->add_options(arguments => |
| 34 |
{ |
| 35 |
"container:s" => { name => 'container', default => 'container_name!~".*POD.*"' }, |
| 36 |
"pod:s" => { name => 'pod', default => 'pod_name=~".*"' }, |
| 37 |
"namespace:s" => { name => 'namespace', default => 'namespace=~".*"' }, |
| 38 |
"extra-filter:s@" => { name => 'extra_filter' }, |
| 39 |
"metric-overload:s@" => { name => 'metric_overload' }, |
| 40 |
}); |
| 41 |
|
| 42 |
return $self; |
| 43 |
} |
| 44 |
|
| 45 |
sub check_options { |
| 46 |
my ($self, %options) = @_; |
| 47 |
$self->SUPER::init(%options); |
| 48 |
|
| 49 |
$self->{metrics} = { |
| 50 |
'last_seen' => '^container_last_seen$', |
| 51 |
}; |
| 52 |
foreach my $metric (@{$self->{option_results}->{metric_overload}}) { |
| 53 |
next if ($metric !~ /(.*),(.*)/); |
| 54 |
$self->{metrics}->{$1} = $2 if (defined($self->{metrics}->{$1})); |
| 55 |
} |
| 56 |
|
| 57 |
$self->{labels} = {}; |
| 58 |
foreach my $label (('container', 'pod', 'namespace')) { |
| 59 |
if ($self->{option_results}->{$label} !~ /^(\w+)[!~=]+\".*\"$/) { |
| 60 |
$self->{output}->add_option_msg(short_msg => "Need to specify --" . $label . " option as a PromQL filter."); |
| 61 |
$self->{output}->option_exit(); |
| 62 |
} |
| 63 |
$self->{labels}->{$label} = $1; |
| 64 |
} |
| 65 |
|
| 66 |
$self->{extra_filter} = ''; |
| 67 |
foreach my $filter (@{$self->{option_results}->{extra_filter}}) { |
| 68 |
$self->{extra_filter} .= ',' . $filter; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
sub manage_selection { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
$self->{containers} = $options{custom}->query(queries => [ '{__name__=~"' . $self->{metrics}->{last_seen} . '",' . |
| 76 |
$self->{option_results}->{container} . ',' . |
| 77 |
$self->{option_results}->{pod} . ',' . |
| 78 |
$self->{option_results}->{namespace} . |
| 79 |
$self->{extra_filter} . '}' ]); |
| 80 |
} |
| 81 |
|
| 82 |
sub run { |
| 83 |
my ($self, %options) = @_; |
| 84 |
|
| 85 |
$self->manage_selection(%options); |
| 86 |
foreach my $container (@{$self->{containers}}) { |
| 87 |
next if (!defined($container->{metric}->{name})); |
| 88 |
$self->{output}->output_add(long_msg => sprintf("[container_name = %s][pod_name = %s][namespace = %s]", |
| 89 |
$container->{metric}->{$self->{labels}->{container}}, $container->{metric}->{$self->{labels}->{pod}}, |
| 90 |
$container->{metric}->{$self->{labels}->{namespace}})); |
| 91 |
} |
| 92 |
|
| 93 |
$self->{output}->output_add(severity => 'OK', |
| 94 |
short_msg => 'List containers:'); |
| 95 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 96 |
$self->{output}->exit(); |
| 97 |
} |
| 98 |
|
| 99 |
sub disco_format { |
| 100 |
my ($self, %options) = @_; |
| 101 |
|
| 102 |
$self->{output}->add_disco_format(elements => ['container_name', 'pod_name', 'namespace']); |
| 103 |
} |
| 104 |
|
| 105 |
sub disco_show { |
| 106 |
my ($self, %options) = @_; |
| 107 |
|
| 108 |
$self->manage_selection(%options); |
| 109 |
foreach my $container (@{$self->{containers}}) { |
| 110 |
$self->{output}->add_disco_entry( |
| 111 |
container_name => $container->{metric}->{$self->{labels}->{container}}, |
| 112 |
pod_name => $container->{metric}->{$self->{labels}->{pod}}, |
| 113 |
namespace => $container->{metric}->{$self->{labels}->{namespace}}, |
| 114 |
); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
1; |
| 119 |
|
| 120 |
__END__ |
| 121 |
|
| 122 |
=head1 MODE |
| 123 |
|
| 124 |
List containers. |
| 125 |
|
| 126 |
=over 8 |
| 127 |
|
| 128 |
=item B<--container> |
| 129 |
|
| 130 |
Filter on a specific container (must be a PromQL filter, Default: 'container_name!~".*POD.*"') |
| 131 |
|
| 132 |
=item B<--pod> |
| 133 |
|
| 134 |
Filter on a specific pod (must be a PromQL filter, Default: 'pod_name=~".*"') |
| 135 |
|
| 136 |
=item B<--namespace> |
| 137 |
|
| 138 |
Filter on a specific namespace (must be a PromQL filter, Default: 'namespace=~".*"') |
| 139 |
|
| 140 |
=item B<--extra-filter> |
| 141 |
|
| 142 |
Add a PromQL filter (can be defined multiple times) |
| 143 |
|
| 144 |
Example : --extra-filter='name=~".*pretty.*"' |
| 145 |
|
| 146 |
=item B<--metric-overload> |
| 147 |
|
| 148 |
Overload default metrics name (can be defined multiple times, metric can be 'last_seen') |
| 149 |
|
| 150 |
Example : --metric-overload='metric,^my_metric_name$' |
| 151 |
|
| 152 |
=back |
| 153 |
|
| 154 |
=cut |