add prometheus plugin with node_exporter (#1168)
* add prometheus plugin with node_exporter * add prometheus plugin with node_exporter
This commit is contained in:
parent
a10c543b8a
commit
fa97ed20a4
|
@ -0,0 +1,144 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::exporters::nodeexporter::mode::cpu;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes CPU usage are ok',
|
||||
counters => [ { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output' } ] },
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{nodes} = [
|
||||
{ label => 'node-usage', set => {
|
||||
key_values => [ { name => 'average' }, { name => 'display' } ],
|
||||
output_template => 'usage %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'node', value => 'average_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
$self->{maps_counters}->{cpu} = [
|
||||
{ label => 'cpu-usage', set => {
|
||||
key_values => [ { name => 'average' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'usage %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'cpu', value => 'average_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
sub prefix_nodes_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub prefix_cpu_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{multi} . "' " . "Cpu '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.0';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"filter:s" => { name => 'filter', default => '' },
|
||||
});
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{nodes} = {};
|
||||
$self->{cpu} = {};
|
||||
|
||||
my $filter = (defined($self->{option_results}->{filter}) && $self->{option_results}->{filter} ne '') ? $self->{option_results}->{filter} . ',' : '';
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ "(1 - irate(node_cpu_seconds_total{" . $filter . "mode='idle'}[1m])) * 100" ]);
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{average} += $average;
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{multi} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{display} = $metric->{metric}->{cpu};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{average} = $average;
|
||||
}
|
||||
|
||||
foreach my $node (keys %{$self->{nodes}}) {
|
||||
$self->{nodes}->{$node}->{average} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
$self->{output}->add_option_msg(short_msg => "No nodes found.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
Check CPU usage for nodes and each of their cores.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--filter>
|
||||
|
||||
Set a PromQL filter (Example : 'instance=~".*master.*"')
|
||||
|
||||
=item B<--warning-*>
|
||||
|
||||
Threshold warning.
|
||||
Can be: 'node-usage', 'cpu-usage'.
|
||||
|
||||
=item B<--critical-*>
|
||||
|
||||
Threshold critical.
|
||||
Can be: 'node-usage', 'cpu-usage'.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -0,0 +1,292 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::exporters::nodeexporter::mode::cpudetailed;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'nodes', type => 3, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes CPU usage are ok',
|
||||
counters => [ { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output' } ] },
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{nodes} = [
|
||||
{ label => 'node-wait', set => {
|
||||
key_values => [ { name => 'iowait' }, { name => 'display' } ],
|
||||
output_template => 'Wait: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'wait', value => 'iowait_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-user', set => {
|
||||
key_values => [ { name => 'user' }, { name => 'display' } ],
|
||||
output_template => 'User: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'user', value => 'user_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-softirq', set => {
|
||||
key_values => [ { name => 'softirq' }, { name => 'display' } ],
|
||||
output_template => 'Soft Irq: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'softirq', value => 'softirq_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-interrupt', set => {
|
||||
key_values => [ { name => 'irq' }, { name => 'display' } ],
|
||||
output_template => 'Interrupt: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'interrupt', value => 'irq_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-idle', set => {
|
||||
key_values => [ { name => 'idle' }, { name => 'display' } ],
|
||||
output_template => 'Idle: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'idle', value => 'idle_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-steal', set => {
|
||||
key_values => [ { name => 'steal' }, { name => 'display' } ],
|
||||
output_template => 'Steal: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'steal', value => 'steal_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-system', set => {
|
||||
key_values => [ { name => 'system' }, { name => 'display' } ],
|
||||
output_template => 'System: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'system', value => 'system_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'node-nice', set => {
|
||||
key_values => [ { name => 'nice' }, { name => 'display' } ],
|
||||
output_template => 'Nice: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'nice', value => 'nice_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
$self->{maps_counters}->{cpu} = [
|
||||
{ label => 'cpu-wait', set => {
|
||||
key_values => [ { name => 'iowait' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Wait: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'wait', value => 'iowait_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-user', set => {
|
||||
key_values => [ { name => 'user' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'User: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'user', value => 'user_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-softirq', set => {
|
||||
key_values => [ { name => 'softirq' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Soft Irq: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'softirq', value => 'softirq_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-interrupt', set => {
|
||||
key_values => [ { name => 'irq' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Interrupt: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'interrupt', value => 'irq_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-idle', set => {
|
||||
key_values => [ { name => 'idle' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Idle: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'idle', value => 'idle_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-steal', set => {
|
||||
key_values => [ { name => 'steal' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Steal: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'steal', value => 'steal_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-system', set => {
|
||||
key_values => [ { name => 'system' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'System: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'system', value => 'system_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cpu-nice', set => {
|
||||
key_values => [ { name => 'nice' }, { name => 'multi' }, { name => 'display' } ],
|
||||
output_template => 'Nice: %.2f %%',
|
||||
perfdatas => [
|
||||
{ label => 'nice', value => 'nice_absolute', template => '%.2f',
|
||||
min => 0, max => 100, unit => '%',
|
||||
label_multi_instances => 1, multi_use => 'multi_absolute',
|
||||
label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
sub prefix_nodes_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub prefix_cpu_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{multi} . "' " . "Cpu '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.0';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"filter:s" => { name => 'filter', default => '' },
|
||||
});
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{nodes} = {};
|
||||
$self->{cpu} = {};
|
||||
|
||||
my $filter = (defined($self->{option_results}->{filter}) && $self->{option_results}->{filter} ne '') ? $self->{option_results}->{filter} : '';
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ "(irate(node_cpu_seconds_total{" . $filter . "}[1m])) * 100" ]);
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{mode}} += $average;
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{multi} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{display} = $metric->{metric}->{cpu};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{cpu}->{$metric->{metric}->{cpu}}->{$metric->{metric}->{mode}} = $average;
|
||||
}
|
||||
|
||||
foreach my $node (keys %{$self->{nodes}}) {
|
||||
foreach my $metric (keys %{$self->{nodes}->{$node}}) {
|
||||
next if ($metric =~ /cpu|display/);
|
||||
$self->{nodes}->{$node}->{$metric} /= scalar(keys %{$self->{nodes}->{$node}->{cpu}});
|
||||
}
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
$self->{output}->add_option_msg(short_msg => "No nodes found.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
Check CPU detailed usage for nodes and each of their cores.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--filter>
|
||||
|
||||
Set a PromQL filter (Example : 'instance=~".*master.*"')
|
||||
|
||||
=item B<--warning-*>
|
||||
|
||||
Threshold warning.
|
||||
Can be: 'node-idle', 'node-wait', 'node-irq', 'node-nice',
|
||||
'node-softirq', 'node-steal', 'node-system', 'node-user',
|
||||
'cpu-idle', 'cpu-wait', 'cpu-irq', 'cpu-nice', 'cpu-softirq',
|
||||
'cpu-steal', 'cpu-system', 'cpu-user'.
|
||||
|
||||
=item B<--critical-*>
|
||||
|
||||
Threshold critical.
|
||||
Can be: 'node-idle', 'node-wait', 'node-irq', 'node-nice',
|
||||
'node-softirq', 'node-steal', 'node-system', 'node-user',
|
||||
'cpu-idle', 'cpu-wait', 'cpu-irq', 'cpu-nice', 'cpu-softirq',
|
||||
'cpu-steal', 'cpu-system', 'cpu-user'.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -0,0 +1,138 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::exporters::nodeexporter::mode::load;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes load are ok' },
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{nodes} = [
|
||||
{ label => 'load1', set => {
|
||||
key_values => [ { name => 'node_load1' }, { name => 'display' } ],
|
||||
output_template => 'Load 1 minute: %.2f',
|
||||
output_change_bytes => 1,
|
||||
perfdatas => [
|
||||
{ label => 'load1', value => 'node_load1_absolute', template => '%.2f',
|
||||
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'load5', set => {
|
||||
key_values => [ { name => 'node_load5' }, { name => 'display' } ],
|
||||
output_template => 'Load 5 minutes: %.2f',
|
||||
output_change_bytes => 1,
|
||||
perfdatas => [
|
||||
{ label => 'load5', value => 'node_load5_absolute', template => '%.2f',
|
||||
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'load15', set => {
|
||||
key_values => [ { name => 'node_load15' }, { name => 'display' } ],
|
||||
output_template => 'Load 15 minutes: %.2f',
|
||||
output_change_bytes => 1,
|
||||
perfdatas => [
|
||||
{ label => 'load15', value => 'node_load15_absolute', template => '%.2f',
|
||||
min => 0, label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
sub prefix_nodes_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.0';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"filter:s" => { name => 'filter', default => '' },
|
||||
});
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{nodes} = {};
|
||||
$self->{cpu} = {};
|
||||
|
||||
my $filter = (defined($self->{option_results}->{filter}) && $self->{option_results}->{filter} ne '') ? $self->{option_results}->{filter} . ',' : '';
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ "node_load1{" . $filter . "}", "node_load5{" . $filter . "}",
|
||||
"node_load15{" . $filter . "}" ]);
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = $average;
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
$self->{output}->add_option_msg(short_msg => "No nodes found.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
Check CPU usage for nodes and each of their cores.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--filter>
|
||||
|
||||
Set a PromQL filter (Example : 'instance=~".*master.*"')
|
||||
|
||||
=item B<--warning-*>
|
||||
|
||||
Threshold warning.
|
||||
Can be: 'node-usage', 'cpu-usage'.
|
||||
|
||||
=item B<--critical-*>
|
||||
|
||||
Threshold critical.
|
||||
Can be: 'node-usage', 'cpu-usage'.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -0,0 +1,207 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::exporters::nodeexporter::mode::memory;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $instance_mode;
|
||||
|
||||
sub custom_usage_perfdata {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my $label = 'used';
|
||||
my $value_perf = $self->{result_values}->{used};
|
||||
my $extra_label = '';
|
||||
$extra_label = '_' . $self->{result_values}->{display} if (!defined($options{extra_instance}) || $options{extra_instance} != 0);
|
||||
my %total_options = ();
|
||||
if ($instance_mode->{option_results}->{units} eq '%') {
|
||||
$total_options{total} = $self->{result_values}->{total};
|
||||
$total_options{cast_int} = 1;
|
||||
}
|
||||
|
||||
$self->{output}->perfdata_add(label => $label . $extra_label, unit => 'B',
|
||||
value => $value_perf,
|
||||
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, %total_options),
|
||||
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, %total_options),
|
||||
min => 0, max => $self->{result_values}->{total});
|
||||
}
|
||||
|
||||
sub custom_usage_threshold {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my ($exit, $threshold_value);
|
||||
$threshold_value = $self->{result_values}->{used};
|
||||
if ($instance_mode->{option_results}->{units} eq '%') {
|
||||
$threshold_value = $self->{result_values}->{prct_used};
|
||||
}
|
||||
$exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' },
|
||||
{ label => 'warning-'. $self->{label}, exit_litteral => 'warning' } ]);
|
||||
return $exit;
|
||||
}
|
||||
|
||||
sub custom_usage_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
|
||||
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
|
||||
my $msg = sprintf("Ram Total: %s, Used (-buffers/cache): %s (%.2f%%)",
|
||||
$total_size_value . " " . $total_size_unit,
|
||||
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used});
|
||||
return $msg;
|
||||
}
|
||||
|
||||
sub custom_usage_calc {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
|
||||
$self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_node_memory_MemTotal_bytes'};
|
||||
$self->{result_values}->{available} = $options{new_datas}->{$self->{instance} . '_node_memory_MemAvailable_bytes'};
|
||||
$self->{result_values}->{buffer} = $options{new_datas}->{$self->{instance} . '_node_memory_Buffers_bytes'};
|
||||
$self->{result_values}->{cached} = $options{new_datas}->{$self->{instance} . '_node_memory_Cached_bytes'};
|
||||
$self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{available} - $self->{result_values}->{buffer} - $self->{result_values}->{cached};
|
||||
$self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / ($self->{result_values}->{total});
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'nodes', type => 1, cb_prefix_output => 'prefix_nodes_output', message_multiple => 'All nodes memory usage are ok' },
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{nodes} = [
|
||||
{ label => 'usage', set => {
|
||||
key_values => [ { name => 'node_memory_MemTotal_bytes' }, { name => 'node_memory_MemAvailable_bytes' },
|
||||
{ name => 'node_memory_Buffers_bytes' }, { name => 'node_memory_Cached_bytes' }, { name => 'display' } ],
|
||||
closure_custom_calc => $self->can('custom_usage_calc'),
|
||||
closure_custom_output => $self->can('custom_usage_output'),
|
||||
closure_custom_perfdata => $self->can('custom_usage_perfdata'),
|
||||
closure_custom_threshold_check => $self->can('custom_usage_threshold'),
|
||||
}
|
||||
},
|
||||
{ label => 'buffer', set => {
|
||||
key_values => [ { name => 'node_memory_Buffers_bytes' }, { name => 'display' } ],
|
||||
output_template => 'Buffer: %.2f %s',
|
||||
output_change_bytes => 1,
|
||||
perfdatas => [
|
||||
{ label => 'buffer', value => 'node_memory_Buffers_bytes_absolute', template => '%s',
|
||||
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'cached', set => {
|
||||
key_values => [ { name => 'node_memory_Cached_bytes' }, { name => 'display' } ],
|
||||
output_template => 'Cached: %.2f %s',
|
||||
output_change_bytes => 1,
|
||||
perfdatas => [
|
||||
{ label => 'cached', value => 'node_memory_Cached_bytes_absolute', template => '%s',
|
||||
min => 0, unit => 'B', label_extra_instance => 1, instance_use => 'display_absolute' },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
sub prefix_nodes_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Node '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.0';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"filter:s" => { name => 'filter', default => '' },
|
||||
"units:s" => { name => 'units', default => '%' },
|
||||
});
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
|
||||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
$self->SUPER::check_options(%options);
|
||||
|
||||
$instance_mode = $self;
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{nodes} = {};
|
||||
my %nodes;
|
||||
|
||||
my $filter = (defined($self->{option_results}->{filter}) && $self->{option_results}->{filter} ne '') ? $self->{option_results}->{filter} . ',' : '';
|
||||
|
||||
my $results = $options{custom}->query_range(queries => [ "node_memory_MemTotal_bytes{" . $filter . "}", "node_memory_MemAvailable_bytes{" . $filter . "}",
|
||||
"node_memory_Cached_bytes{" . $filter . "}", "node_memory_Buffers_bytes{" . $filter . "}" ]);
|
||||
|
||||
|
||||
foreach my $metric (@{$results}) {
|
||||
my $average = $options{custom}->compute(aggregation => 'average', values => $metric->{values});
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{display} = $metric->{metric}->{instance};
|
||||
$self->{nodes}->{$metric->{metric}->{instance}}->{$metric->{metric}->{__name__}} = $average;
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{nodes}}) <= 0) {
|
||||
$self->{output}->add_option_msg(short_msg => "No nodes found.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
Check memory usage.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--filter>
|
||||
|
||||
Set a PromQL filter (Example : 'instance=~".*master.*"')
|
||||
|
||||
=item B<--warning-*>
|
||||
|
||||
Threshold warning.
|
||||
Can be: 'usage', 'buffer', 'cached'.
|
||||
|
||||
=item B<--critical-*>
|
||||
|
||||
Threshold critical.
|
||||
Can be: 'usage', 'buffer', 'cached'.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -0,0 +1,59 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::exporters::nodeexporter::plugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use base qw(centreon::plugins::script_custom);
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '0.1';
|
||||
%{$self->{modes}} = (
|
||||
'cpu' => 'cloud::prometheus::exporters::nodeexporter::mode::cpu',
|
||||
'cpu-detailed' => 'cloud::prometheus::exporters::nodeexporter::mode::cpudetailed',
|
||||
'load' => 'cloud::prometheus::exporters::nodeexporter::mode::load',
|
||||
'memory' => 'cloud::prometheus::exporters::nodeexporter::mode::memory',
|
||||
);
|
||||
|
||||
$self->{custom_modes}{api} = 'cloud::prometheus::restapi::custom::api';
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub init {
|
||||
my ( $self, %options ) = @_;
|
||||
|
||||
$self->SUPER::init(%options);
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 PLUGIN DESCRIPTION
|
||||
|
||||
Check node_exporter metrics through Prometheus server.
|
||||
|
||||
=cut
|
|
@ -0,0 +1,327 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::restapi::custom::api;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use centreon::plugins::http;
|
||||
use DateTime;
|
||||
use JSON::XS;
|
||||
use URI::Encode;
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = {};
|
||||
bless $self, $class;
|
||||
|
||||
if (!defined($options{output})) {
|
||||
print "Class Custom: Need to specify 'output' argument.\n";
|
||||
exit 3;
|
||||
}
|
||||
if (!defined($options{options})) {
|
||||
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
|
||||
$options{output}->option_exit();
|
||||
}
|
||||
|
||||
if (!defined($options{noptions})) {
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"hostname:s" => { name => 'hostname' },
|
||||
"url-path:s" => { name => 'url_path' },
|
||||
"port:s" => { name => 'port' },
|
||||
"proto:s" => { name => 'proto' },
|
||||
"credentials" => { name => 'credentials' },
|
||||
"basic" => { name => 'basic' },
|
||||
"username:s" => { name => 'username' },
|
||||
"password:s" => { name => 'password' },
|
||||
"proxyurl:s" => { name => 'proxyurl' },
|
||||
"timeout:s" => { name => 'timeout' },
|
||||
"ssl-opt:s@" => { name => 'ssl_opt' },
|
||||
"timeframe:s" => { name => 'timeframe' },
|
||||
"step:s" => { name => 'step' },
|
||||
});
|
||||
}
|
||||
$options{options}->add_help(package => __PACKAGE__, sections => 'RESTAPI OPTIONS', once => 1);
|
||||
|
||||
$self->{output} = $options{output};
|
||||
$self->{mode} = $options{mode};
|
||||
$self->{http} = centreon::plugins::http->new(output => $self->{output});
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub set_options {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{option_results} = $options{option_results};
|
||||
}
|
||||
|
||||
sub set_defaults {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
foreach (keys %{$options{default}}) {
|
||||
if ($_ eq $self->{mode}) {
|
||||
for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) {
|
||||
foreach my $opt (keys %{$options{default}->{$_}[$i]}) {
|
||||
if (!defined($self->{option_results}->{$opt}[$i])) {
|
||||
$self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'undef';
|
||||
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 9090;
|
||||
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http';
|
||||
$self->{url_path} = (defined($self->{option_results}->{url_path})) ? $self->{option_results}->{url_path} : '/api/v1';
|
||||
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
|
||||
$self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? $self->{option_results}->{proxyurl} : undef;
|
||||
$self->{ssl_opt} = (defined($self->{option_results}->{ssl_opt})) ? $self->{option_results}->{ssl_opt} : undef;
|
||||
$self->{username} = (defined($self->{option_results}->{username})) ? $self->{option_results}->{username} : undef;
|
||||
$self->{password} = (defined($self->{option_results}->{password})) ? $self->{option_results}->{password} : undef;
|
||||
$self->{credentials} = (defined($self->{option_results}->{credentials})) ? 1 : undef;
|
||||
$self->{basic} = (defined($self->{option_results}->{basic})) ? 1 : undef;
|
||||
$self->{timeframe} = (defined($self->{option_results}->{timeframe})) ? $self->{option_results}->{timeframe} : undef;
|
||||
$self->{step} = (defined($self->{option_results}->{step})) ? $self->{option_results}->{step} : undef;
|
||||
|
||||
if (!defined($self->{hostname}) && $self->{hostname} ne '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify hostname option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
if (!defined($self->{timeframe}) && $self->{timeframe} ne '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify timeframe option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
if (!defined($self->{step}) && $self->{step} ne '') {
|
||||
$self->{output}->add_option_msg(short_msg => "Need to specify step option.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub build_options_for_httplib {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{option_results}->{hostname} = $self->{hostname};
|
||||
$self->{option_results}->{timeout} = $self->{timeout};
|
||||
$self->{option_results}->{port} = $self->{port};
|
||||
$self->{option_results}->{proto} = $self->{proto};
|
||||
$self->{option_results}->{proxyurl} = $self->{proxyurl};
|
||||
$self->{option_results}->{credentials} = $self->{credentials};
|
||||
$self->{option_results}->{basic} = $self->{basic};
|
||||
$self->{option_results}->{username} = $self->{username};
|
||||
$self->{option_results}->{password} = $self->{password};
|
||||
$self->{option_results}->{warning_status} = '';
|
||||
$self->{option_results}->{critical_status} = '';
|
||||
}
|
||||
|
||||
sub settings {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->build_options_for_httplib();
|
||||
$self->{http}->set_options(%{$self->{option_results}});
|
||||
}
|
||||
|
||||
sub get_connection_info {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return $self->{hostname} . ":" . $self->{port};
|
||||
}
|
||||
|
||||
sub get_hostname {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return $self->{hostname};
|
||||
}
|
||||
|
||||
sub get_port {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return $self->{port};
|
||||
}
|
||||
|
||||
sub query_range {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my $data;
|
||||
my $start_time = DateTime->now->subtract(seconds => $self->{timeframe})->iso8601.'Z';
|
||||
my $end_time = DateTime->now->iso8601.'Z';
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
|
||||
foreach my $query (@{$options{queries}}) {
|
||||
my $result = $self->get_endpoint(url_path => '/query_range?query=' . $uri->encode($query) .
|
||||
'&start=' . $start_time . '&end=' . $end_time . '&step=' . $self->{step});
|
||||
push @{$data}, @{$result->{result}};
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
sub query {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my $uri = URI::Encode->new({encode_reserved => 1});
|
||||
|
||||
return $self->get_endpoint(url_path => '/query?query=' . $uri->encode($options{query}));
|
||||
}
|
||||
|
||||
sub get_endpoint {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->settings;
|
||||
my $response = $self->{http}->request(url_path => $self->{url_path} . $options{url_path});
|
||||
|
||||
my $content;
|
||||
eval {
|
||||
$content = JSON::XS->new->utf8->decode($response);
|
||||
};
|
||||
if ($@) {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot decode json response: $@");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
if ($content->{status} ne 'success') {
|
||||
$self->{output}->add_option_msg(short_msg => "Cannot get data: " . $content->{status});
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
|
||||
return $content->{data};
|
||||
}
|
||||
|
||||
sub compute {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
my $result;
|
||||
|
||||
if ($options{aggregation} eq 'average') {
|
||||
my $points = 0;
|
||||
foreach my $value (@{$options{values}}) {
|
||||
$result = 0 if (!defined($result));
|
||||
$result += $$value[1];
|
||||
$points++;
|
||||
}
|
||||
$result /= $points;
|
||||
} elsif ($options{aggregation} eq 'minimum') {
|
||||
foreach my $value (@{$options{values}}) {
|
||||
$result = $$value[1] if (!defined($result) || $$value[1] < $result);
|
||||
}
|
||||
} elsif ($options{aggregation} eq 'maximum') {
|
||||
foreach my $value (@{$options{values}}) {
|
||||
$result = $$value[1] if (!defined($result) || $$value[1] > $result);
|
||||
}
|
||||
} elsif ($options{aggregation} eq 'sum') {
|
||||
foreach my $value (@{$options{values}}) {
|
||||
$result = 0 if (!defined($result));
|
||||
$result += $$value[1];
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 NAME
|
||||
|
||||
Prometheus REST API
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
Prometheus Rest API custom mode
|
||||
|
||||
=head1 REST API OPTIONS
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--timeframe>
|
||||
|
||||
Set timeframe in seconds (i.e. 3600 to check last hour).
|
||||
|
||||
=item B<--step>
|
||||
|
||||
Set the step of the metric query (Examples: '30s', '1m', '15m', '1h').
|
||||
|
||||
=item B<--hostname>
|
||||
|
||||
Prometheus hostname.
|
||||
|
||||
=item B<--url-path>
|
||||
|
||||
API url path (Default: '/api/v1')
|
||||
|
||||
=item B<--port>
|
||||
|
||||
API port (Default: 9090)
|
||||
|
||||
=item B<--proto>
|
||||
|
||||
Specify https if needed (Default: 'http')
|
||||
|
||||
=item B<--credentials>
|
||||
|
||||
Specify this option if you access the API with authentication
|
||||
|
||||
=item B<--username>
|
||||
|
||||
Specify username for authentication (Mandatory if --credentials is specified)
|
||||
|
||||
=item B<--password>
|
||||
|
||||
Specify password for authentication (Mandatory if --credentials is specified)
|
||||
|
||||
=item B<--basic>
|
||||
|
||||
Specify this option if you access the API over basic authentication and don't want a '401 UNAUTHORIZED' error to be logged on your webserver.
|
||||
|
||||
Specify this option if you access the API over hidden basic authentication or you'll get a '404 NOT FOUND' error.
|
||||
|
||||
(Use with --credentials)
|
||||
|
||||
=item B<--proxyurl>
|
||||
|
||||
Proxy URL if any
|
||||
|
||||
=item B<--timeout>
|
||||
|
||||
Set HTTP timeout
|
||||
|
||||
=item B<--ssl-opt>
|
||||
|
||||
Set SSL Options (--ssl-opt="SSL_version => TLSv1" --ssl-opt="SSL_verify_mode => SSL_VERIFY_NONE").
|
||||
|
||||
=back
|
||||
|
||||
=head1 DESCRIPTION
|
||||
|
||||
B<custom>.
|
||||
|
||||
=cut
|
|
@ -0,0 +1,247 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::restapi::mode::targetstatus;
|
||||
|
||||
use base qw(centreon::plugins::templates::counter);
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $instance_mode;
|
||||
|
||||
sub custom_status_threshold {
|
||||
my ($self, %options) = @_;
|
||||
my $status = 'ok';
|
||||
my $message;
|
||||
|
||||
eval {
|
||||
local $SIG{__WARN__} = sub { $message = $_[0]; };
|
||||
local $SIG{__DIE__} = sub { $message = $_[0]; };
|
||||
|
||||
if (defined($instance_mode->{option_results}->{critical_status}) && $instance_mode->{option_results}->{critical_status} ne '' &&
|
||||
eval "$instance_mode->{option_results}->{critical_status}") {
|
||||
$status = 'critical';
|
||||
} elsif (defined($instance_mode->{option_results}->{warning_status}) && $instance_mode->{option_results}->{warning_status} ne '' &&
|
||||
eval "$instance_mode->{option_results}->{warning_status}") {
|
||||
$status = 'warning';
|
||||
}
|
||||
};
|
||||
if (defined($message)) {
|
||||
$self->{output}->output_add(long_msg => 'filter status issue: ' . $message);
|
||||
}
|
||||
|
||||
return $status;
|
||||
}
|
||||
|
||||
sub custom_status_output {
|
||||
my ($self, %options) = @_;
|
||||
my $msg = "health is '" . $self->{result_values}->{health} . "'";
|
||||
$msg .= " [last error: " . $self->{result_values}->{last_error} . "]" if ($self->{result_values}->{last_error} ne '');
|
||||
|
||||
return $msg;
|
||||
}
|
||||
|
||||
sub custom_status_calc {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{result_values}->{health} = $options{new_datas}->{$self->{instance} . '_health'};
|
||||
$self->{result_values}->{last_error} = $options{new_datas}->{$self->{instance} . '_last_error'};
|
||||
$self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
sub prefix_targets_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Target '" . $options{instance_value}->{display} . "' ";
|
||||
}
|
||||
|
||||
sub prefix_global_output {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
return "Targets ";
|
||||
}
|
||||
|
||||
sub set_counters {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{maps_counters_type} = [
|
||||
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output' },
|
||||
{ name => 'targets', type => 1, cb_prefix_output => 'prefix_targets_output', message_multiple => 'All targets status are ok', skipped_code => { -11 => 1 } },
|
||||
];
|
||||
|
||||
$self->{maps_counters}->{global} = [
|
||||
{ label => 'active', set => {
|
||||
key_values => [ { name => 'active' } ],
|
||||
output_template => 'Active : %s',
|
||||
perfdatas => [
|
||||
{ label => 'active_targets', value => 'active_absolute', template => '%s',
|
||||
min => 0 },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'dropped', set => {
|
||||
key_values => [ { name => 'dropped' } ],
|
||||
output_template => 'Dropped : %s',
|
||||
perfdatas => [
|
||||
{ label => 'dropped_targets', value => 'dropped_absolute', template => '%s',
|
||||
min => 0 },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'up', set => {
|
||||
key_values => [ { name => 'up' } ],
|
||||
output_template => 'Up : %s',
|
||||
perfdatas => [
|
||||
{ label => 'up_targets', value => 'up_absolute', template => '%s',
|
||||
min => 0 },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'down', set => {
|
||||
key_values => [ { name => 'down' } ],
|
||||
output_template => 'Down : %s',
|
||||
perfdatas => [
|
||||
{ label => 'down_targets', value => 'down_absolute', template => '%s',
|
||||
min => 0 },
|
||||
],
|
||||
}
|
||||
},
|
||||
{ label => 'unknown', set => {
|
||||
key_values => [ { name => 'unknown' } ],
|
||||
output_template => 'Unknown : %s',
|
||||
perfdatas => [
|
||||
{ label => 'unknown_targets', value => 'unknown_absolute', template => '%s',
|
||||
min => 0 },
|
||||
],
|
||||
}
|
||||
},
|
||||
];
|
||||
$self->{maps_counters}->{targets} = [
|
||||
{ label => 'status', threshold => 0, set => {
|
||||
key_values => [ { name => 'health' }, { name => 'last_error' }, { name => 'display' } ],
|
||||
closure_custom_calc => $self->can('custom_status_calc'),
|
||||
closure_custom_output => $self->can('custom_status_output'),
|
||||
closure_custom_perfdata => sub { return 0; },
|
||||
closure_custom_threshold_check => $self->can('custom_status_threshold'),
|
||||
}
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '1.0';
|
||||
$options{options}->add_options(arguments =>
|
||||
{
|
||||
"warning-status:s" => { name => 'warning_status', default => '' },
|
||||
"critical-status:s" => { name => 'critical_status', default => '%{health} !~ /up/' },
|
||||
});
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub check_options {
|
||||
my ($self, %options) = @_;
|
||||
$self->SUPER::check_options(%options);
|
||||
|
||||
$instance_mode = $self;
|
||||
$self->change_macros();
|
||||
}
|
||||
|
||||
sub change_macros {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
foreach (('warning_status', 'critical_status')) {
|
||||
if (defined($self->{option_results}->{$_})) {
|
||||
$self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub manage_selection {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
$self->{global} = { active => 0, dropped => 0, up => 0, down => 0, unknown => 0 };
|
||||
$self->{targets} = {};
|
||||
|
||||
my $result = $options{custom}->get_endpoint(url_path => '/targets');
|
||||
|
||||
foreach my $active (@{$result->{activeTargets}}) {
|
||||
$self->{global}->{active}++;
|
||||
$self->{targets}->{$active->{scrapeUrl}} = {
|
||||
display => $active->{scrapeUrl},
|
||||
health => $active->{health},
|
||||
last_error => $active->{lastError},
|
||||
|
||||
};
|
||||
$self->{global}->{$active->{health}}++;
|
||||
}
|
||||
|
||||
foreach my $dropped (@{$result->{droppedTargets}}) {
|
||||
$self->{global}->{dropped}++;
|
||||
}
|
||||
|
||||
if (scalar(keys %{$self->{targets}}) <= 0) {
|
||||
$self->{output}->add_option_msg(short_msg => "No targets found.");
|
||||
$self->{output}->option_exit();
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 MODE
|
||||
|
||||
Check targets status.
|
||||
|
||||
=over 8
|
||||
|
||||
=item B<--warning-status>
|
||||
|
||||
Set warning threshold for status (Default: '')
|
||||
Can used special variables like: %{display}, %{health}.
|
||||
|
||||
=item B<--critical-status>
|
||||
|
||||
Set critical threshold for status (Default: '%{health} !~ /up/').
|
||||
Can used special variables like: %{display}, %{health}
|
||||
|
||||
=item B<--warning-*>
|
||||
|
||||
Threshold warning.
|
||||
Can be: 'active', 'dropped', 'up',
|
||||
'down', 'unknown'.
|
||||
|
||||
=item B<--critical-*>
|
||||
|
||||
Threshold critical.
|
||||
Can be: 'active', 'dropped', 'up',
|
||||
'down', 'unknown'.
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
|
@ -0,0 +1,56 @@
|
|||
#
|
||||
# Copyright 2018 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 cloud::prometheus::restapi::plugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use base qw(centreon::plugins::script_custom);
|
||||
|
||||
sub new {
|
||||
my ($class, %options) = @_;
|
||||
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
|
||||
bless $self, $class;
|
||||
|
||||
$self->{version} = '0.1';
|
||||
%{$self->{modes}} = (
|
||||
'target-status' => 'cloud::prometheus::restapi::mode::targetstatus',
|
||||
);
|
||||
|
||||
$self->{custom_modes}{api} = 'cloud::prometheus::restapi::custom::api';
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub init {
|
||||
my ( $self, %options ) = @_;
|
||||
|
||||
$self->SUPER::init(%options);
|
||||
}
|
||||
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
||||
|
||||
=head1 PLUGIN DESCRIPTION
|
||||
|
||||
Check Prometheus server.
|
||||
|
||||
=cut
|
Loading…
Reference in New Issue