Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

+ create centreon_health script #5990

Closed
wants to merge 19 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions bin/centreon_health
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env perl

use warnings;
use centreon::script::centreon_health;

centreon::script::centreon_health->new()->run();

__END__

=head1 NAME

centreon_health - a tool/script to gather informations about Centreon ("noroot" script)

=head1 SYNOPSIS

centreon_health [options]

=head1 OPTIONS

=over 8

=item B<--centstorage-db> <value>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should require /etc/centreon/conf.pm to retrieve this information.
The centreon database can also be non standard.


Specify the name of your centstorage db if not standard
(default: centreon_storage).

=item B<--centreon-branch> <value>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it could be better if you get this information in the centreon database directly.


Specify centreon version (X.Y format) if older than 2.8
(default: 2.8).

=item B<--check-protocol> <value>

Specify favorite check protocol (will be implemented later, 'snmp' is the only one available for now)

=item B<--snmp-community> <value>

Specify snmp community to execute plugins

=item B<--output-type> <value>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Option is --ouput-format


Specify output type. Can be: 'MARKDOWN', 'JSON', 'TEXT' or 'DUMPER' (last if for debugging only, raw perl struct)

=item B<--skip-*>

Skip check that might be long (rrd and db e.g) on large setup or overkill in some situation
Can be : --skip-rrd, --skip-db, --skip-logs

=head1 DESCRIPTION

B<centreon_health> will gather some informations about centreon global metrics
sample usage: su - centreon -c "/usr/share/centreon/bin/centreon_health --skip-logs"

=cut
103 changes: 103 additions & 0 deletions lib/perl/centreon/health/checkbroker.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
#
# Copyright 2017 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 centreon::health::checkbroker;

use strict;
use warnings;
use JSON;
use POSIX qw(strftime);
use centreon::common::misc;
use centreon::health::ssh;

sub new {
my $class = shift;
my $self = {};
$self->{output} = {};

bless $self, $class;
return $self;
}

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

my $json_content = JSON->new->decode($options{json_content});
foreach my $key (keys %$json_content) {
if ($key =~ m/^endpoint/) {
foreach my $broker_metric (keys %{$json_content->{$key}}) {
next if ($broker_metric !~ m/version|event_processing|last_connection|queued|state/);
$self->{output}->{$options{poller_name}}->{$options{file_name}}->{$broker_metric} = ($broker_metric =~ m/^last_connection/ && $json_content->{$key}->{$broker_metric} != -1)
? strftime("%m/%d/%Y %H:%M:%S",localtime($json_content->{$key}->{$broker_metric}))
: $json_content->{$key}->{$broker_metric} ;
}
} elsif ($key =~ m/version/) {
$self->{output}->{$options{poller_name}}->{$options{file_name}}->{$key} = $json_content->{$key};
}

}

return $self->{output}

}

sub run {
my $self = shift;
my ($centreon_db, $server_list, $centreon_version) = @_;

my $sth;

if ($centreon_version ne "2.8") {
$self->{output}->{not_compliant} = "Incompatible file format, work only with JSON format";
return $self->{output}
}

return if ($centreon_version ne "2.8");
foreach my $server (keys %$server_list) {
$sth = $centreon_db->query("SELECT config_name, cache_directory
FROM cfg_centreonbroker
WHERE stats_activate='1'
AND ns_nagios_server=".$centreon_db->quote($server)."");

if ($server_list->{$server}->{localhost} eq "YES") {
while (my $row = $sth->fetchrow_hashref()) {
my ($lerror, $stdout) = centreon::common::misc::backtick(command => "cat " . $row->{cache_directory} . "/" . $row->{config_name} . "-stats.json");
$self->{output} = $self->json_parsing(json_content => $stdout,
poller_name => $server_list->{$server}->{name},
file_name => $row->{config_name}. "-stats.json");
}
} else {
while (my $row = $sth->fetchrow_hashref()) {
my $stdout = centreon::health::ssh->new->main(host => $server_list->{$server}->{address},
port => $server_list->{$server}->{ssh_port},
userdata => $row->{cache_directory} . "/" . $row->{config_name} . "-stats.json",
command => "cat " . $row->{cache_directory} . "/" . $row->{config_name} . "-stats.json");
$self->{output} = $self->json_parsing(json_content => $stdout,
poller_name => $server_list->{$server}->{name},
file_name => $row->{config_name}. "-stats.json");

}
}

}
return $self->{output}
}

1;
95 changes: 95 additions & 0 deletions lib/perl/centreon/health/checkdb.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#
# Copyright 2017 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 centreon::health::checkdb;

use strict;
use warnings;
use POSIX qw(strftime);
use centreon::health::misc;

sub new {
my $class = shift;
my $self = {};
$self->{output} = {};

bless $self, $class;
return $self;
}

sub run {
my $self = shift;
my ($centreon_db, $centstorage_db, $centstorage_db_name, $flag, $logger) = @_;
my $size = 0;
my ($sth, $status);

foreach my $db_name ('centreon', $centstorage_db_name) {
$sth = $centreon_db->query("SELECT table_schema AS db_name, SUM(data_length+index_length) AS db_size
FROM information_schema.tables
WHERE table_schema=".$centreon_db->quote($db_name)."");
while (my $row = $sth->fetchrow_hashref) {
$self->{output}->{db_size}->{$row->{db_name}} = centreon::health::misc::format_bytes(bytes_value => $row->{db_size});
}
next if $db_name !~ /$centstorage_db_name/;
foreach my $table ('data_bin', 'logs', 'log_archive_host', 'log_archive_service', 'downtimes') {

$sth = $centreon_db->query("SELECT table_name, SUM(data_length+index_length) AS table_size
FROM information_schema.tables
WHERE table_schema=".$centreon_db->quote($db_name)."
AND table_name=".$centreon_db->quote($table)."");

while (my $row = $sth->fetchrow_hashref()) {
$self->{output}->{table_size}->{$row->{table_name}} = centreon::health::misc::format_bytes(bytes_value =>$row->{table_size});
}

next if ($table =~ m/downtimes/);
$sth = $centreon_db->query("SELECT MAX(CONVERT(PARTITION_DESCRIPTION, SIGNED INTEGER)) as lastPart
FROM INFORMATION_SCHEMA.PARTITIONS
WHERE TABLE_NAME='" . $table . "'
AND TABLE_SCHEMA='" . $db_name . "' GROUP BY TABLE_NAME;");

while (my $row = $sth->fetchrow_hashref()) {
$self->{output}->{partitioning_last_part}->{$table} = defined($row->{lastPart}) ? strftime("%m/%d/%Y %H:%M:%S",localtime($row->{lastPart})) : $table . " has no partitioning !";
}
}

}

my $var_list = { 'innodb_file_per_table' => 0,
'open_files_limit' => 0,
'read_only' => 0,
'key_buffer_size' => 1,
'sort_buffer_size' => 1,
'join_buffer_size' => 1,
'read_buffer_size' => 1,
'read_rnd_buffer_size' => 1,
'max_allowed_packet' => 1 };

foreach my $var (keys %{$var_list}) {
my $sth = $centreon_db->query("SHOW GLOBAL VARIABLES LIKE " . $centreon_db->quote($var));
my $value = $sth->fetchrow();
$self->{output}->{interesting_variables}->{$var} = ($var_list->{$var} == 1) ? centreon::health::misc::format_bytes(bytes_value => $value) : $value;
}

return $self->{output};

}

1;
102 changes: 102 additions & 0 deletions lib/perl/centreon/health/checklogs.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#
# Copyright 2017 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 centreon::health::checklogs;

use strict;
use warnings;
use POSIX qw(strftime);
use centreon::common::misc;
use centreon::health::ssh;

sub new {
my $class = shift;
my $self = {};
$self->{logs_path_broker} = {};
$self->{logs_path_engine} = {};
$self->{output} = {};

bless $self, $class;
return $self;
}

sub run {
my $self = shift;
my ($centreon_db, $server_list, $centreon_version, $logger) = @_;

my $sth;
my ($lerror, $stdout);

foreach my $server (keys %$server_list) {
$sth = $centreon_db->query("SELECT log_file
FROM cfg_nagios
WHERE nagios_id=" . $centreon_db->quote($server));

while (my $row = $sth->fetchrow_hashref) {
push @{$self->{logs_path_engine}->{engine}->{$server_list->{$server}->{name}}}, $row->{log_file};
}

foreach my $log_file (@{$self->{logs_path_engine}->{engine}->{$server_list->{$server}->{name}}}) {
if ($server_list->{$server}->{localhost} eq "YES") {
($lerror, $self->{output}->{$server_list->{$server}->{name}}->{engine}->{$log_file}) = centreon::common::misc::backtick(command => "tail -n20 " . $log_file);
} else {
$self->{output}->{$server_list->{$server}->{name}}->{engine}->{$log_file} = centreon::health::ssh->new->main(host => $server_list->{$server}->{address},
port => $server_list->{$server}->{ssh_port},
userdata => $log_file,
command => "tail -n20 " . $log_file);
}
}

$sth = $centreon_db->query("SELECT DISTINCT(config_value) FROM cfg_centreonbroker, cfg_centreonbroker_info
WHERE config_group='logger' AND config_key='name'
AND cfg_centreonbroker.config_id=cfg_centreonbroker_info.config_id
AND cfg_centreonbroker.ns_nagios_server=" . $centreon_db->quote($server));

while (my $row = $sth->fetchrow_hashref) {
push @{$self->{logs_path_broker}->{broker}->{$server_list->{$server}->{name}}}, $row->{config_value};
}

foreach my $log_file (@{$self->{logs_path_broker}->{broker}->{$server_list->{$server}->{name}}}) {
if ($server_list->{$server}->{localhost} eq "YES") {
($lerror, $self->{output}->{$server_list->{$server}->{name}}->{broker}->{$log_file}) = centreon::common::misc::backtick(command => "tail -n20 " . $log_file);
} else {
$self->{output}->{$server_list->{$server}->{name}}->{broker}->{$log_file} = centreon::health::ssh->new->main(host => $server_list->{$server}->{address},
port => $server_list->{$server}->{ssh_port},
userdata => $log_file,
command => "tail -n20 " . $log_file);
}
}

if ($server_list->{$server}->{localhost} eq "YES") {
$sth = $centreon_db->query("SELECT `value` FROM options WHERE `key`='debug_path'");

my $centreon_log_path = $sth->fetchrow();
($lerror, $stdout) = centreon::common::misc::backtick(command => "find " . $centreon_log_path . " -type f -name *.log");

foreach my $log_file (split '\n', $stdout) {
($lerror, $self->{output}->{$server_list->{$server}->{name}}->{centreon}->{$log_file}) = centreon::common::misc::backtick(command => "tail -n10 " . $log_file);
}
}
}

return $self->{output}
}

1;
Loading