centreon-plugins/apps/centreon/local/mode/retentionbroker.pm

186 lines
6.0 KiB
Perl
Raw Normal View History

#
2020-01-06 15:19:23 +01:00
# Copyright 2020 Centreon (http://www.centreon.com/)
2015-07-21 11:51:02 +02:00
#
# 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.
#
2015-02-13 17:22:24 +01:00
package apps::centreon::local::mode::retentionbroker;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
use XML::LibXML;
2015-05-29 16:27:24 +02:00
use File::Basename;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
2019-10-29 15:40:07 +01:00
$options{options}->add_options(arguments => {
'broker-config:s@' => { name => 'broker_config' },
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
2015-05-29 16:27:24 +02:00
if (!defined($self->{option_results}->{broker_config}) || scalar(@{$self->{option_results}->{broker_config}}) == 0) {
$self->{output}->add_option_msg(short_msg => "Please set broker-config option.");
$self->{output}->option_exit();
}
2015-05-29 16:27:24 +02:00
}
sub check_directory {
my ($self, %options) = @_;
my ($current_total, $current_size) = (0, 0);
my $dirname = dirname($options{path});
my $basename = basename($options{path});
my $dh;
if (!opendir($dh, $dirname)) {
$self->{output}->output_add(severity => 'UNKNOWN',
short_msg => "'$options{config}': cannot open directory '$dirname'");
return 0;
}
2015-05-29 16:27:24 +02:00
while (my $file = readdir($dh)) {
if ($file =~ /^$basename\d*$/) {
$current_total++;
$current_size += -s $dirname . '/' . $file;
}
}
closedir $dh;
return (1, $current_total, $current_size);
}
sub run {
my ($self, %options) = @_;
$self->{output}->output_add(severity => 'OK',
2017-03-28 09:52:54 +02:00
short_msg => 'centreon-broker failover/temporary files are ok');
2015-05-29 16:27:24 +02:00
my $total_size = 0;
foreach my $config (@{$self->{option_results}->{broker_config}}) {
$self->{output}->output_add(long_msg => "Checking config '$config'");
if (! -f $config or ! -r $config) {
2019-10-29 15:40:07 +01:00
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => "'$config': not a file or cannot be read"
);
2015-05-29 16:27:24 +02:00
next;
}
2015-05-29 16:27:24 +02:00
my $parser = XML::LibXML->new();
my $xml;
eval {
$xml = $parser->parse_file($config);
};
if ($@) {
2019-10-29 15:40:07 +01:00
$self->{output}->output_add(
severity => 'UNKNOWN',
short_msg => "'$config': cannot parse xml"
);
2015-05-29 16:27:24 +02:00
next;
}
my %failover_found = ();
my %file_found = ();
2015-05-29 16:27:24 +02:00
my $temporary;
foreach my $node ($xml->findnodes('/centreonBroker/output')) {
my %load = ();
foreach my $element ($node->getChildrenByTagName('*')) {
if ($element->nodeName eq 'failover') {
$failover_found{$element->textContent} = 1;
2015-05-29 16:27:24 +02:00
} elsif ($element->nodeName =~ /^(name|type|path)$/) {
$load{$element->nodeName} = $element->textContent;
}
}
if (defined($load{type}) && $load{type} eq 'file') {
$file_found{$load{name}} = {%load};
2015-05-29 16:27:24 +02:00
}
}
2015-05-29 16:27:24 +02:00
foreach my $node ($xml->findnodes('/centreonBroker/temporary')) {
foreach my $element ($node->getChildrenByTagName('path')) {
$temporary = $element->textContent;
}
}
# Check failovers
my $current_total = 0;
foreach my $failover (sort keys %failover_found) {
next if (!defined($file_found{$failover}));
my ($status, $total, $size) = $self->check_directory(config => $config, path => $file_found{$failover}->{path});
2015-05-29 16:27:24 +02:00
next if (!$status);
2015-05-29 16:27:24 +02:00
$current_total += $total;
$total_size += $size;
my ($size_value, $size_unit) = $self->{perfdata}->change_bytes(value => $size);
2017-10-12 12:42:07 +02:00
$self->{output}->output_add(long_msg => sprintf("failover '%s': %d file(s) found (%s)",
2015-05-29 16:27:24 +02:00
$failover, $total, $size_value . ' ' . $size_unit));
}
if ($current_total > 0) {
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => sprintf("Some failover(s) are active"));
}
2015-05-29 16:27:24 +02:00
# Check temporary
if (!defined($temporary)) {
$self->{output}->output_add(long_msg => "skipping temporary: no configuration set");
next;
}
my ($status, $total, $size) = $self->check_directory(config => $config, path => $temporary);
if ($status) {
my ($size_value, $size_unit) = $self->{perfdata}->change_bytes(value => $size);
2017-10-12 12:42:07 +02:00
$self->{output}->output_add(long_msg => sprintf("temporary: %d file(s) found (%s)",
2015-05-29 16:27:24 +02:00
$total, $size_value . ' ' . $size_unit));
if ($total > 0) {
$self->{output}->output_add(severity => 'CRITICAL',
short_msg => sprintf("Temporary is active"));
}
}
}
$self->{output}->display();
2015-02-13 17:22:24 +01:00
$self->{output}->exit();
}
1;
__END__
=head1 MODE
2015-05-29 16:27:24 +02:00
Check failover file retention or temporary is active.
=over 8
2015-05-29 16:27:24 +02:00
=item B<--broker-config>
2015-02-23 16:18:00 +01:00
2015-05-29 16:27:24 +02:00
Specify the centreon-broker config (Required). Can be multiple.
2015-02-23 16:18:00 +01:00
=back
=cut