From 98a3eba7d00889652a313b7fa2fcd36a15a1c47e Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 29 Dec 2017 15:06:54 +0100 Subject: [PATCH 01/17] + WIP - centreon_health script to gather various data --- lib/perl/centreon/health/checkdb.pm | 79 +++++++++++ lib/perl/centreon/health/checkglobal.pm | 79 +++++++++++ lib/perl/centreon/health/checkmodules.pm | 66 +++++++++ lib/perl/centreon/health/checkrrd.pm | 91 ++++++++++++ lib/perl/centreon/health/checkservers.pm | 136 ++++++++++++++++++ lib/perl/centreon/health/checksystems.pm | 145 ++++++++++++++++++++ lib/perl/centreon/health/misc.pm | 116 ++++++++++++++++ lib/perl/centreon/health/ssh.pm | 99 +++++++++++++ lib/perl/centreon/script/centreon_health.pm | 89 ++++++++++++ 9 files changed, 900 insertions(+) create mode 100644 lib/perl/centreon/health/checkdb.pm create mode 100644 lib/perl/centreon/health/checkglobal.pm create mode 100644 lib/perl/centreon/health/checkmodules.pm create mode 100644 lib/perl/centreon/health/checkrrd.pm create mode 100644 lib/perl/centreon/health/checkservers.pm create mode 100644 lib/perl/centreon/health/checksystems.pm create mode 100644 lib/perl/centreon/health/misc.pm create mode 100644 lib/perl/centreon/health/ssh.pm create mode 100644 lib/perl/centreon/script/centreon_health.pm diff --git a/lib/perl/centreon/health/checkdb.pm b/lib/perl/centreon/health/checkdb.pm new file mode 100644 index 00000000000..14b9ca2a566 --- /dev/null +++ b/lib/perl/centreon/health/checkdb.pm @@ -0,0 +1,79 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checkdb; + +use strict; +use warnings; +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) = @_; + 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}); + } + } + } + + return $self->{output}; + + +} + +1; diff --git a/lib/perl/centreon/health/checkglobal.pm b/lib/perl/centreon/health/checkglobal.pm new file mode 100644 index 00000000000..e8229b7f4ba --- /dev/null +++ b/lib/perl/centreon/health/checkglobal.pm @@ -0,0 +1,79 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checkglobal; + +use strict; +use warnings; +use centreon::common::misc; +use centreon::health::misc; + +sub new { + my $class = shift; + my $self = {}; + $self->{output} = {}; + + bless $self, $class; + return $self; +} + +sub query_misc { + my ($self, %options) = @_; + my ($sth, $status); + + $sth = $options{cdb}->query($options{query}); + + return $sth->fetchrow() +} + +sub run { + my $self = shift; + my ($centreon_db, $centstorage_db, $centreon_version) = @_; + + my $query_misc = { count_pp => [$centreon_db, $centreon_version eq '2.7' ? "SELECT count(*) FROM mod_pluginpack" : "SELECT count(*) FROM mod_ppm_pluginpack"], + count_downtime => [$centreon_db, "SELECT count(*) FROM downtime"], + count_modules => [$centreon_db, "SELECT count(*) FROM modules_informations"], + centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], + count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; + + foreach my $info (keys $query_misc) { + my $result = $self->query_misc(cdb => $query_misc->{$info}[0], + query => $query_misc->{$info}[1] ); + $self->{output}->{$info} = $result; + + } + + return $self->{output} +} + +1; diff --git a/lib/perl/centreon/health/checkmodules.pm b/lib/perl/centreon/health/checkmodules.pm new file mode 100644 index 00000000000..c13322578e0 --- /dev/null +++ b/lib/perl/centreon/health/checkmodules.pm @@ -0,0 +1,66 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checkmodules; + +use strict; +use warnings; +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) = @_; + my $size = 0; + my ($sth, $status); + + $sth = $centreon_db->query("SELECT name, rname, author, mod_release FROM modules_informations"); + while (my $row = $sth->fetchrow_hashref) { + $self->{output}->{$row->{name}}{full_name} = $row->{rname}; + $self->{output}->{$row->{name}}{author} = $row->{author}; + $self->{output}->{$row->{name}}{version} = $row->{mod_release}; + } + + return $self->{output}; + +} + +1; diff --git a/lib/perl/centreon/health/checkrrd.pm b/lib/perl/centreon/health/checkrrd.pm new file mode 100644 index 00000000000..381f1a503d2 --- /dev/null +++ b/lib/perl/centreon/health/checkrrd.pm @@ -0,0 +1,91 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checkrrd; + +use strict; +use warnings; +use centreon::common::misc; +use centreon::health::misc; + +sub new { + my $class = shift; + my $self = {}; + $self->{rrd_metrics} = undef; + $self->{rrd_status} = undef; + $self->{output} = {}; + + bless $self, $class; + return $self; +} + +sub get_rrd_path { + my ($self, %options) = @_; + my ($sth, $status); + + $sth = $options{csdb}->query("SELECT RRDdatabase_path, RRDdatabase_status_path FROM config"); + + while (my $row = $sth->fetchrow_hashref()) { + $self->{rrd_metrics} = $row->{RRDdatabase_path}; + $self->{rrd_status} = $row->{RRDdatabase_status_path}; + } + +} + +sub get_rrd_infos { + my $self = shift; + + my ($lerror_m, $size_metrics) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_metrics}); + my ($lerror_s, $size_status) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_status}); + my ($lerror_cm, $count_metrics) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_metrics} . " | wc -l"); + my ($lerror_cs, $count_status) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_status} . " | wc -l"); + my ($lerror_lw, $count_last_written) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin 5 | wc -l"); + + $self->{output}->{$self->{rrd_metrics}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_metrics); + $self->{output}->{$self->{rrd_status}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_status); + $self->{output}->{rrd_written_last_5m} = $count_last_written; + $self->{output}->{$self->{rrd_metrics}}{count} = $count_metrics; + $self->{output}->{$self->{rrd_status}}{count} = $count_status; +} + +sub run { + my $self = shift; + my ($centstorage_db) = @_; + + $self->get_rrd_path(csdb => $centstorage_db); + $self->get_rrd_infos(); + + return $self->{output} +} + +1; diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm new file mode 100644 index 00000000000..cbda214c47e --- /dev/null +++ b/lib/perl/centreon/health/checkservers.pm @@ -0,0 +1,136 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checkservers; + +use strict; +use warnings; +use integer; + +sub new { + my $class = shift; + my $self = {}; + $self->{output} = {}; + + bless $self, $class; + return $self; +} + +sub query_misc { + my ($self, %options) = @_; + my ($sth, $status); + + $sth = $options{cdb}->query($options{query}); + + return $sth->fetchrow() +} + +sub get_servers_informations { + my ($self, %options) = @_; + + my $sth = $options{cdb}->query("SELECT id, name, localhost, ns_ip_address, ssh_port + FROM nagios_server"); + while (my $row = $sth->fetchrow_hashref()) { + $self->{output}->{poller}->{$row->{id}}{name} = $row->{name}; + $self->{output}->{poller}->{$row->{id}}{localhost} = $row->{localhost}; + $self->{output}->{poller}->{$row->{id}}{address} = $row->{ns_ip_address}; + $self->{output}->{poller}->{$row->{id}}{ssh_port} = $row->{ssh_port}; + } + + foreach my $id (keys %{$self->{output}->{poller}}) { + $self->{output}->{global}->{count_poller}++; + $sth = $options{csdb}->query("SELECT COUNT(DISTINCT hosts.host_id) as num_hosts, count(DISTINCT services.host_id, services.service_id) as num_services + FROM hosts, services WHERE services.host_id=hosts.host_id + AND hosts.enabled=1 + AND services.enabled=1 + AND hosts.instance_id=".$options{cdb}->quote($id)." + AND hosts.name NOT LIKE '%Module%'"); + + while (my $row = $sth->fetchrow_hashref()) { + $self->{output}->{poller}{$id}{hosts} = $row->{num_hosts}; + $self->{output}->{poller}{$id}{services} = $row->{num_services}; + $self->{output}->{global}{count_hosts} += $row->{num_hosts}; + $self->{output}->{global}{count_services} += $row->{num_services}; + } + + $sth = $options{csdb}->query("SELECT COUNT(DISTINCT hosts.host_id) as num_hosts, count(DISTINCT services.host_id, services.service_id) as num_services + FROM hosts, services WHERE services.host_id=hosts.host_id + AND hosts.enabled=1 + AND services.enabled=1 + AND hosts.instance_id=".$options{cdb}->quote($id).""); + + $sth = $options{csdb}->query("SELECT * + FROM instances + WHERE instance_id = " . $options{cdb}->quote($id) . ""); + + while (my $row = $sth->fetchrow_hashref()) { + $self->{output}->{poller}{$row->{instance_id}}{uptime} = centreon::health::misc::change_seconds(value => $row->{last_alive} - $row->{start_time}); + $self->{output}->{poller}{$row->{instance_id}}{running} = $row->{running}; + $self->{output}->{poller}{$row->{instance_id}}{start_time} = $row->{start_time}; + $self->{output}->{poller}{$row->{instance_id}}{last_alive} = $row->{last_alive}; + $self->{output}->{poller}{$row->{instance_id}}{last_command_check} = $row->{last_command_check}; + $self->{output}->{poller}{$row->{instance_id}}{engine} = $row->{engine}; + $self->{output}->{poller}{$row->{instance_id}}{version} = $row->{version}; + } + + $self->{output}->{global}->{hosts_by_poller_avg} = $self->{output}->{global}->{count_hosts}/$self->{output}->{global}->{count_poller}; + $self->{output}->{global}->{services_by_poller_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_poller}; + $self->{output}->{global}->{services_by_host_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_hosts}; + $self->{output}->{global}->{metrics_by_service_avg} = $self->{output}->{global}->{count_metrics}/$self->{output}->{global}->{count_services}; + + } +} + + +sub run { + my $self = shift; + my ($centreon_db, $centstorage_db, $centreon_version) = @_; + + my $query_misc = { count_pp => [$centreon_db, $centreon_version eq '2.7' ? "SELECT count(*) FROM mod_pluginpack" : "SELECT count(*) FROM mod_ppm_pluginpack"], + count_downtime => [$centreon_db, "SELECT count(*) FROM downtime"], + count_modules => [$centreon_db, "SELECT count(*) FROM modules_informations"], + centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], + count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; + + foreach my $info (keys $query_misc) { + my $result = $self->query_misc(cdb => $query_misc->{$info}[0], + query => $query_misc->{$info}[1] ); + $self->{output}->{global}->{$info} = $result; + } + + $self->get_servers_informations(cdb => $centreon_db, csdb => $centstorage_db); + + return $self->{output} +} + +1; diff --git a/lib/perl/centreon/health/checksystems.pm b/lib/perl/centreon/health/checksystems.pm new file mode 100644 index 00000000000..4693e160b62 --- /dev/null +++ b/lib/perl/centreon/health/checksystems.pm @@ -0,0 +1,145 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checksystems; + +use strict; +use warnings; +use centreon::common::misc; +use centreon::health::ssh; + +sub new { + my $class = shift; + my $self = {}; + $self->{cmd_system_health} = []; + $self->{output} = {}; + + $self->{cmd_system_health} = []; + + bless $self, $class; + return $self; +} + +sub build_command_hash { + my ($self, %options) = @_; + + if ($options{medium} eq "snmp") { + $options{snmp} = defined($options{snmp}) ? $options{snmp} : 'public'; + $self->{cmd_system_health} = [ { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode cpu-detailed \\ + --hostname localhost \\ + --statefile-suffix='_diag-cpu' \\ + --filter-perfdata='^(?!(wait|guest|user|softirq|kernel|interrupt|guestnice|idle|steal|system|nice))' \\ + --snmp-community " . $options{snmp} , + callback => \¢reon::health::ssh::ssh_callback, + userdata => "cpu_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode load \\ + --hostname localhost \\ + --filter-perfdata='^(?!(load))' \\ + --snmp-community " . $options{snmp}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "load" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode memory \\ + --hostname localhost \\ + --filter-perfdata='^(?!(cached|buffer|used))' \\ + --snmp-community " . $options{snmp}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "mem_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode swap \\ + --hostname localhost \\ + --filter-perfdata='^(?!(used))' \\ + --snmp-community " . $options{snmp}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "swap_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode storage \\ + --hostname localhost \\ + --storage='^(?!(/dev/shm|/sys/fs/cgroup|/boot|/run.*))' --name --regexp \\ + --filter-perfdata='^(?!(used))' --statefile-suffix='_diag-storage' \\ + --verbose \\ + --snmp-community " . $options{snmp}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "storage_usage" }, + ]; + } else { + return -1 + } + + +} + +sub get_remote_infos { + my ($self, %options) = @_; + + return centreon::health::ssh::new->main(host => $options{host}, port => $options{ssh_port}, command_pool => $self->{cmd_system_health}); + +} + +sub get_local_infos { + my ($self, %options) = @_; + my ($lerror, $stdout); + + my $results; + + foreach my $command (@{$self->{cmd_system_health}}) { + ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); + $results->{$command->{userdata}} = $stdout; + while ($stdout =~ m/Buffer creation/) { + # Replay command to bypass cache creation + sleep 1; + ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); + $results->{$command->{userdata}} = $stdout; + } + } + + return $results + +} + +sub run { + my $self = shift; + my ($server_list, $medium) = @_; + + $self->build_command_hash(medium => $medium); + + foreach my $server (keys $server_list) { + my $name = $server_list->{$server}->{name}; + if ($server_list->{$server}->{localhost} != 1) { + $self->{output}->{$name} = $self->get_remote_infos(host => $server_list->{$server}->{address}, ssh_port => $server_list->{$server}->{ssh_port}); + } else { + $self->{output}->{$name} = $self->get_local_infos(poller_name => $name); + } + } + + return $self->{output} +} + +1; diff --git a/lib/perl/centreon/health/misc.pm b/lib/perl/centreon/health/misc.pm new file mode 100644 index 00000000000..6b94a2e1361 --- /dev/null +++ b/lib/perl/centreon/health/misc.pm @@ -0,0 +1,116 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::misc; + +use strict; +use warnings; +use Libssh::Session qw(:all); + +sub get_ssh_connection { + my %options = @_; + + my $session = Libssh::Session->new(); + if ($session->options(host => $options{host}, port => $options{port}, user => $options{user}) != SSH_OK) { + print $session->error() . "\n"; + return 1 + } + + if ($session->connect() != SSH_OK) { + print $session->error() . "\n"; + return 1 + } + + if ($session->auth_publickey_auto() != SSH_AUTH_SUCCESS) { + printf("auth issue pubkey: %s\n", $session->error(GetErrorSession => 1)); + if ($session->auth_password(password => $options{password}) != SSH_AUTH_SUCCESS) { + printf("auth issue: %s\n", $session->error(GetErrorSession => 1)); + return 1 + } + } + + return $session + +} + +sub change_seconds { + my %options = @_; + my ($str, $str_append) = ('', ''); + my $periods = [ + { unit => 'y', value => 31556926 }, + { unit => 'M', value => 2629743 }, + { unit => 'w', value => 604800 }, + { unit => 'd', value => 86400 }, + { unit => 'h', value => 3600 }, + { unit => 'm', value => 60 }, + { unit => 's', value => 1 }, + ]; + my %values = ('y' => 1, 'M' => 2, 'w' => 3, 'd' => 4, 'h' => 5, 'm' => 6, 's' => 7); + + foreach (@$periods) { + next if (defined($options{start}) && $values{$_->{unit}} < $values{$options{start}}); + my $count = int($options{value} / $_->{value}); + + next if ($count == 0); + $str .= $str_append . $count . $_->{unit}; + $options{value} = $options{value} % $_->{value}; + $str_append = ' '; + } + + return $str; +} + +sub format_bytes { + my (%options) = @_; + my $size = $options{bytes_value}; + $size =~ s/\D//g; + + if ($size > 1099511627776) { + return sprintf("%.0fT", $size / 1099511627776); + } + elsif ($size > 1073741824) { + return sprintf("%.0fG", $size / 1073741824); + } + elsif ($size > 1048576) { + return sprintf("%.0fM", $size / 1048576); + } + elsif ($size > 1024) { + return sprintf("%.0fK", $size / 1024); + } + else { + return sprintf("%.0fB", $size); + } +} + + +1; diff --git a/lib/perl/centreon/health/ssh.pm b/lib/perl/centreon/health/ssh.pm new file mode 100644 index 00000000000..f57fa5cd18f --- /dev/null +++ b/lib/perl/centreon/health/ssh.pm @@ -0,0 +1,99 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::ssh; + +use strict; +use warnings; +use Libssh::Session qw(:all); + +my $command_results = {}; + +sub new { + my $class = shift @_ || __PACKAGE__; + my $self; + $self->{session} = undef; + $self->{host} = undef; + $self->{port} = undef; + $self->{logger} = undef; + $self->{data} = {}; + + bless $self, $class; + return $self; +} + +sub ssh_callback { + my (%options) = @_; + + if ($options{exit} == SSH_OK || $options{exit} == SSH_AGAIN) { # AGAIN means timeout + chomp($options{stdout}); + $command_results->{$options{userdata}} = $options{stdout}; + } else { + $command_results->{$options{userdata}} = "Failed action on ssh or plugin"; + return -1 + } + return 0 +} + +sub create_ssh_channel { + my ($self, %options) = @_; + + $self->{session} = Libssh::Session->new(); + if ($self->{session}->options(host => $options{host}, port => $options{port}, user => $options{user}) != SSH_OK) { + return 1 + } + + if ($self->{session}->connect() != SSH_OK) { + return 1 + } + + if ($self->{session}->auth_publickey_auto() != SSH_AUTH_SUCCESS) { + printf("auth issue pubkey: %s\n", $self->{session}->error(GetErrorSession => 1)); + if ($self->{session}->auth_password(password => $options{password}) != SSH_AUTH_SUCCESS) { + printf("auth issue: %s\n", $self->{session}->error(GetErrorSession => 1)); + return 1 + } + } + return 0 +} + +sub main { + my ($self, %options) = @_; + + $self->create_ssh_channel(host => $options{host}, port => $options{port}, user => 'centreon'); + $self->{session}->execute(commands => $options{command_pool}, timeout => 5000, timeout_nodata => 10, parallel => 5); + $self->{data} = $command_results; + return $self->{data} +} + +1; diff --git a/lib/perl/centreon/script/centreon_health.pm b/lib/perl/centreon/script/centreon_health.pm new file mode 100644 index 00000000000..aa1aece29fd --- /dev/null +++ b/lib/perl/centreon/script/centreon_health.pm @@ -0,0 +1,89 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::script::centreon_health; + +use strict; +use warnings; +use POSIX; +use centreon::script; +use centreon::health::checkservers; +use centreon::health::checkrrd; +use centreon::health::checkdb; +use centreon::health::checkmodules; +use centreon::health::checksystems; +use centreon::health::checkbroker; +use centreon::health::checkengine; +use centreon::health::checklogs; + +use Data::Dumper; +use base qw(centreon::script); + +sub new { + my $class = shift; + my $self = $class->SUPER::new("centreon_health", + centreon_db_conn => 1, + centstorage_db_conn => 1, + noroot => 1, + ); + bless $self, $class; + + $self->add_options( + "centstorage-db=s" => \$self->{opt_csdb}, + "centreon-branch=s" => \$self->{opt_majorversion}, + "anonymous" => \$self->{opt_anonymous} + ); + + $self->{current_time} = time(); + $self->{global_output} = {}; + + $self->{opt_csdb} = 'centreon_storage' if (!defined $self->{opt_csdb}); + $self->{opt_majorversion} = '2.8' if (!defined $self->{opt_majorversion}); + return $self; +} + +sub run { + my $self = shift; + $self->SUPER::run(); + + $self->{logger}->writeLogInfo("Starting centreon_health check"); + $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}); + $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}); + $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); + $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); + $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, 'snmp'); + + print Dumper($self->{global_output}->{systems}); +} + +1; From fed4083b2492605a436f90d44196115d0e459c92 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 29 Dec 2017 18:02:52 +0100 Subject: [PATCH 02/17] + Still WIP - add partitioning check, rrd outdated, fix headers --- lib/perl/centreon/health/checkbroker.pm | 140 ++++++++++++++++++++ lib/perl/centreon/health/checkdb.pm | 75 ++++++----- lib/perl/centreon/health/checkglobal.pm | 49 +++---- lib/perl/centreon/health/checkmodules.pm | 53 +++----- lib/perl/centreon/health/checkrrd.pm | 61 ++++----- lib/perl/centreon/health/checkservers.pm | 68 +++++----- lib/perl/centreon/health/checksystems.pm | 69 ++++------ lib/perl/centreon/health/misc.pm | 49 +++---- lib/perl/centreon/health/ssh.pm | 56 ++++---- lib/perl/centreon/script/centreon_health.pm | 91 +++++++------ 10 files changed, 396 insertions(+), 315 deletions(-) create mode 100644 lib/perl/centreon/health/checkbroker.pm diff --git a/lib/perl/centreon/health/checkbroker.pm b/lib/perl/centreon/health/checkbroker.pm new file mode 100644 index 00000000000..3fb34024df1 --- /dev/null +++ b/lib/perl/centreon/health/checkbroker.pm @@ -0,0 +1,140 @@ +################################################################################ +# Copyright 2005-2013 Centreon +# Centreon is developped by : Julien Mathis and Romain Le Merlus under +# GPL Licence 2.0. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free Software +# Foundation ; either version 2 of the License. +# +# This program is distributed in the hope that it will be useful, but WITHOUT ANY +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. See the GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along with +# this program; if not, see . +# +# Linking this program statically or dynamically with other modules is making a +# combined work based on this program. Thus, the terms and conditions of the GNU +# General Public License cover the whole combination. +# +# As a special exception, the copyright holders of this program give Centreon +# permission to link this program with independent modules to produce an executable, +# regardless of the license terms of these independent modules, and to copy and +# distribute the resulting executable under terms of Centreon choice, provided that +# Centreon also meet, for each linked independent module, the terms and conditions +# of the license of that module. An independent module is a module which is not +# derived from this program. If you modify this program, you may extend this +# exception to your version of the program, but you are not obliged to do so. If you +# do not wish to do so, delete this exception statement from your version. +# +# +#################################################################################### + +package centreon::health::checksystems; + +use strict; +use warnings; +use centreon::common::misc; +use centreon::health::ssh; + +sub new { + my $class = shift; + my $self = {}; + $self->{cmd_system_health} = []; + $self->{output} = {}; + + bless $self, $class; + return $self; +} + +sub build_command_hash { + my ($self, %options) = @_; + + if ($options{medium} eq "snmp") { + $self->{cmd_system_health} = [ { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode cpu-detailed \\ + --hostname localhost \\ + --statefile-suffix='_diag-cpu' \\ + --filter-perfdata='^(?!(wait|guest|user|softirq|kernel|interrupt|guestnice|idle|steal|system|nice))' \\ + --snmp-community " . $options{community} , + callback => \¢reon::health::ssh::ssh_callback, + userdata => "cpu_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode load \\ + --hostname localhost \\ + --filter-perfdata='^(?!(load))' \\ + --snmp-community " . $options{community}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "load" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode memory \\ + --hostname localhost \\ + --filter-perfdata='^(?!(cached|buffer|used))' \\ + --snmp-community " . $options{community}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "mem_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode swap \\ + --hostname localhost \\ + --filter-perfdata='^(?!(used))' \\ + --snmp-community " . $options{community}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "swap_usage" }, + { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode storage \\ + --hostname localhost \\ + --storage='^(?!(/dev/shm|/sys/fs/cgroup|/boot|/run.*))' --name --regexp \\ + --filter-perfdata='^(?!(used))' --statefile-suffix='_diag-storage' \\ + --verbose \\ + --snmp-community " . $options{community}, + callback => \¢reon::health::ssh::ssh_callback, + userdata => "storage_usage" }, + ]; + } else { + return -1 + } + + +} + +sub get_remote_infos { + my ($self, %options) = @_; + + return centreon::health::ssh::new->main(host => $options{host}, port => $options{ssh_port}, command_pool => $self->{cmd_system_health}); + +} + +sub get_local_infos { + my ($self, %options) = @_; + my ($lerror, $stdout); + + my $results; + + foreach my $command (@{$self->{cmd_system_health}}) { + ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); + $results->{$command->{userdata}} = $stdout; + while ($stdout =~ m/Buffer creation/) { + # Replay command to bypass cache creation + sleep 1; + ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); + $results->{$command->{userdata}} = $stdout; + } + } + + return $results + +} + +sub run { + my $self = shift; + my ($server_list, $medium, $community, $logger) = @_; + + foreach my $server (keys $server_list) { + my $name = $server_list->{$server}->{name}; + if ($server_list->{$server}->{localhost} eq "NO") { + $self->{output}->{$name} = $self->get_remote_infos(host => $server_list->{$server}->{address}, ssh_port => $server_list->{$server}->{ssh_port}); + } else { + $self->{output}->{$name} = $self->get_local_infos(poller_name => $name); + } + } + + return $self->{output} +} + +1; diff --git a/lib/perl/centreon/health/checkdb.pm b/lib/perl/centreon/health/checkdb.pm index 14b9ca2a566..f80f5e088eb 100644 --- a/lib/perl/centreon/health/checkdb.pm +++ b/lib/perl/centreon/health/checkdb.pm @@ -1,40 +1,28 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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 { @@ -48,10 +36,18 @@ sub new { sub run { my $self = shift; - my ($centreon_db, $centstorage_db, $centstorage_db_name) = @_; + my ($centreon_db, $centstorage_db, $centstorage_db_name, $flag, $logger) = @_; my $size = 0; my ($sth, $status); + if ($flag == 1) { + $logger->writeLogDebug("[INFO] Skipping Database checks"); + return 0 + } + + $logger->writeLogDebug("[INFO] Gathering Databases informations"); + + 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 @@ -61,14 +57,27 @@ sub run { } 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 !"; + } + } + } return $self->{output}; diff --git a/lib/perl/centreon/health/checkglobal.pm b/lib/perl/centreon/health/checkglobal.pm index e8229b7f4ba..24a976b4cf7 100644 --- a/lib/perl/centreon/health/checkglobal.pm +++ b/lib/perl/centreon/health/checkglobal.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checkglobal; diff --git a/lib/perl/centreon/health/checkmodules.pm b/lib/perl/centreon/health/checkmodules.pm index c13322578e0..bb76c994a43 100644 --- a/lib/perl/centreon/health/checkmodules.pm +++ b/lib/perl/centreon/health/checkmodules.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checkmodules; @@ -48,10 +35,12 @@ sub new { sub run { my $self = shift; - my ($centreon_db) = @_; + my ($centreon_db, $logger) = @_; my $size = 0; my ($sth, $status); + $logger->writeLogDebug("[INFO] Gathering Modules informations"); + $sth = $centreon_db->query("SELECT name, rname, author, mod_release FROM modules_informations"); while (my $row = $sth->fetchrow_hashref) { $self->{output}->{$row->{name}}{full_name} = $row->{rname}; diff --git a/lib/perl/centreon/health/checkrrd.pm b/lib/perl/centreon/health/checkrrd.pm index 381f1a503d2..6f3fd5574d1 100644 --- a/lib/perl/centreon/health/checkrrd.pm +++ b/lib/perl/centreon/health/checkrrd.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checkrrd; @@ -70,18 +57,26 @@ sub get_rrd_infos { my ($lerror_cm, $count_metrics) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_metrics} . " | wc -l"); my ($lerror_cs, $count_status) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_status} . " | wc -l"); my ($lerror_lw, $count_last_written) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin 5 | wc -l"); + my ($lerror_od, $count_outdated_rrd) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin +288000 | wc -l"); $self->{output}->{$self->{rrd_metrics}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_metrics); $self->{output}->{$self->{rrd_status}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_status); $self->{output}->{rrd_written_last_5m} = $count_last_written; + $self->{output}->{rrd_not_updated_since_180d} = $count_outdated_rrd; $self->{output}->{$self->{rrd_metrics}}{count} = $count_metrics; $self->{output}->{$self->{rrd_status}}{count} = $count_status; } sub run { my $self = shift; - my ($centstorage_db) = @_; - + my ($centstorage_db, $flag, $logger) = @_; + + if ($flag == 1) { + $logger->writeLogDebug("[INFO] Skipping RRD checks"); + return 0 + } + + $logger->writeLogDebug("[INFO] Gathering RRD informations"); $self->get_rrd_path(csdb => $centstorage_db); $self->get_rrd_infos(); diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm index cbda214c47e..efa73c653db 100644 --- a/lib/perl/centreon/health/checkservers.pm +++ b/lib/perl/centreon/health/checkservers.pm @@ -1,41 +1,29 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checkservers; use strict; use warnings; use integer; +use POSIX qw(strftime); sub new { my $class = shift; @@ -62,7 +50,7 @@ sub get_servers_informations { FROM nagios_server"); while (my $row = $sth->fetchrow_hashref()) { $self->{output}->{poller}->{$row->{id}}{name} = $row->{name}; - $self->{output}->{poller}->{$row->{id}}{localhost} = $row->{localhost}; + $self->{output}->{poller}->{$row->{id}}{localhost} = ($row->{localhost} == 1) ? "YES" : "NO"; $self->{output}->{poller}->{$row->{id}}{address} = $row->{ns_ip_address}; $self->{output}->{poller}->{$row->{id}}{ssh_port} = $row->{ssh_port}; } @@ -95,14 +83,22 @@ sub get_servers_informations { while (my $row = $sth->fetchrow_hashref()) { $self->{output}->{poller}{$row->{instance_id}}{uptime} = centreon::health::misc::change_seconds(value => $row->{last_alive} - $row->{start_time}); - $self->{output}->{poller}{$row->{instance_id}}{running} = $row->{running}; - $self->{output}->{poller}{$row->{instance_id}}{start_time} = $row->{start_time}; - $self->{output}->{poller}{$row->{instance_id}}{last_alive} = $row->{last_alive}; - $self->{output}->{poller}{$row->{instance_id}}{last_command_check} = $row->{last_command_check}; + $self->{output}->{poller}{$row->{instance_id}}{running} = ($row->{running} == 1) ? "YES" : "NO"; + $self->{output}->{poller}{$row->{instance_id}}{start_time} = strftime("%m/%d/%Y %H:%M:%S",localtime($row->{start_time})); + $self->{output}->{poller}{$row->{instance_id}}{last_alive} = strftime("%m/%d/%Y %H:%M:%S",localtime($row->{last_alive})); + $self->{output}->{poller}{$row->{instance_id}}{last_command_check} = strftime("%m/%d/%Y %H:%M:%S",localtime($row->{last_command_check})); $self->{output}->{poller}{$row->{instance_id}}{engine} = $row->{engine}; $self->{output}->{poller}{$row->{instance_id}}{version} = $row->{version}; } + $sth = $options{csdb}->query("SELECT stat_key, stat_value, stat_label + FROM nagios_stats + WHERE instance_id = " . $options{cdb}->quote($id) . ""); + + while (my $row = $sth->fetchrow_hashref()) { + $self->{output}->{poller}->{$id}->{engine_stats}->{$row->{stat_label}}->{$row->{stat_key}} = $row->{stat_value}; + } + $self->{output}->{global}->{hosts_by_poller_avg} = $self->{output}->{global}->{count_hosts}/$self->{output}->{global}->{count_poller}; $self->{output}->{global}->{services_by_poller_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_poller}; $self->{output}->{global}->{services_by_host_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_hosts}; diff --git a/lib/perl/centreon/health/checksystems.pm b/lib/perl/centreon/health/checksystems.pm index 4693e160b62..20952527188 100644 --- a/lib/perl/centreon/health/checksystems.pm +++ b/lib/perl/centreon/health/checksystems.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checksystems; @@ -54,30 +41,29 @@ sub build_command_hash { my ($self, %options) = @_; if ($options{medium} eq "snmp") { - $options{snmp} = defined($options{snmp}) ? $options{snmp} : 'public'; $self->{cmd_system_health} = [ { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode cpu-detailed \\ --hostname localhost \\ --statefile-suffix='_diag-cpu' \\ --filter-perfdata='^(?!(wait|guest|user|softirq|kernel|interrupt|guestnice|idle|steal|system|nice))' \\ - --snmp-community " . $options{snmp} , + --snmp-community " . $options{community} , callback => \¢reon::health::ssh::ssh_callback, userdata => "cpu_usage" }, { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode load \\ --hostname localhost \\ --filter-perfdata='^(?!(load))' \\ - --snmp-community " . $options{snmp}, + --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "load" }, { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode memory \\ --hostname localhost \\ --filter-perfdata='^(?!(cached|buffer|used))' \\ - --snmp-community " . $options{snmp}, + --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "mem_usage" }, { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode swap \\ --hostname localhost \\ --filter-perfdata='^(?!(used))' \\ - --snmp-community " . $options{snmp}, + --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "swap_usage" }, { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode storage \\ @@ -85,7 +71,7 @@ sub build_command_hash { --storage='^(?!(/dev/shm|/sys/fs/cgroup|/boot|/run.*))' --name --regexp \\ --filter-perfdata='^(?!(used))' --statefile-suffix='_diag-storage' \\ --verbose \\ - --snmp-community " . $options{snmp}, + --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "storage_usage" }, ]; @@ -126,13 +112,14 @@ sub get_local_infos { sub run { my $self = shift; - my ($server_list, $medium) = @_; + my ($server_list, $medium, $community, $logger) = @_; + + $self->build_command_hash(medium => $medium, + community => $community); - $self->build_command_hash(medium => $medium); - foreach my $server (keys $server_list) { my $name = $server_list->{$server}->{name}; - if ($server_list->{$server}->{localhost} != 1) { + if ($server_list->{$server}->{localhost} eq "NO") { $self->{output}->{$name} = $self->get_remote_infos(host => $server_list->{$server}->{address}, ssh_port => $server_list->{$server}->{ssh_port}); } else { $self->{output}->{$name} = $self->get_local_infos(poller_name => $name); diff --git a/lib/perl/centreon/health/misc.pm b/lib/perl/centreon/health/misc.pm index 6b94a2e1361..8b9b9ac684c 100644 --- a/lib/perl/centreon/health/misc.pm +++ b/lib/perl/centreon/health/misc.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::misc; diff --git a/lib/perl/centreon/health/ssh.pm b/lib/perl/centreon/health/ssh.pm index f57fa5cd18f..ba3026d5ac4 100644 --- a/lib/perl/centreon/health/ssh.pm +++ b/lib/perl/centreon/health/ssh.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::ssh; @@ -91,7 +78,12 @@ sub main { my ($self, %options) = @_; $self->create_ssh_channel(host => $options{host}, port => $options{port}, user => 'centreon'); - $self->{session}->execute(commands => $options{command_pool}, timeout => 5000, timeout_nodata => 10, parallel => 5); + if (defined($options{command_pool})) { + $self->{session}->execute(commands => $options{command_pool}, timeout => 5000, timeout_nodata => 10, parallel => 5); + } else { + my $return = $self->{session}->execute_simple(cmd => $options{command_simple}, timeout => 10, timeout_nodata => 5); + $command_results->{$return->{userdata}} = defined($return->{stderr}) ? $return->{stderr} : $return->{stdout}; + } $self->{data} = $command_results; return $self->{data} } diff --git a/lib/perl/centreon/script/centreon_health.pm b/lib/perl/centreon/script/centreon_health.pm index aa1aece29fd..f56632184cf 100644 --- a/lib/perl/centreon/script/centreon_health.pm +++ b/lib/perl/centreon/script/centreon_health.pm @@ -1,35 +1,22 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::script::centreon_health; @@ -42,10 +29,10 @@ use centreon::health::checkrrd; use centreon::health::checkdb; use centreon::health::checkmodules; use centreon::health::checksystems; -use centreon::health::checkbroker; -use centreon::health::checkengine; -use centreon::health::checklogs; +#use centreon::health::checkbroker; +#use centreon::health::checklogs; +use JSON; use Data::Dumper; use base qw(centreon::script); @@ -59,16 +46,23 @@ sub new { bless $self, $class; $self->add_options( - "centstorage-db=s" => \$self->{opt_csdb}, - "centreon-branch=s" => \$self->{opt_majorversion}, - "anonymous" => \$self->{opt_anonymous} + "centstorage-db=s" => \$self->{opt_csdb}, + "centreon-branch=s" => \$self->{opt_majorversion}, + "check-protocol=s" => \$self->{opt_checkprotocol}, + "output-type=s" => \$self->{opt_output}, + "snmp-community=s" => \$self->{opt_community}, + "skip-rrd" => \$self->{opt_skiprrd}, + "skip-db" => \$self->{opt_skipdb}, + "anonymous" => \$self->{opt_anonymous} ); - $self->{current_time} = time(); $self->{global_output} = {}; + $self->{opt_checkprotocol} = 'snmp'; + $self->{opt_community} = 'public' if (!defined $self->{opt_community}); $self->{opt_csdb} = 'centreon_storage' if (!defined $self->{opt_csdb}); $self->{opt_majorversion} = '2.8' if (!defined $self->{opt_majorversion}); + return $self; } @@ -76,14 +70,19 @@ sub run { my $self = shift; $self->SUPER::run(); - $self->{logger}->writeLogInfo("Starting centreon_health check"); - $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}); - $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}); - $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); - $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); - $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, 'snmp'); - - print Dumper($self->{global_output}->{systems}); + $self->{logger}->writeLogDebug("[INFO] Starting centreon_health check"); + $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}, defined($self->{opt_skiprrd}), $self->{logger}); + $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}, defined($self->{opt_skipdb}), $self->{logger}); + $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}, $self->{logger}); + $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}, $self->{logger}); + $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}, $self->{logger}); + + + #print Dumper($self->{global_output}); + my $json = JSON->new->encode($self->{global_output}); + print $json ; } + + 1; From f0a1b5a914dee3e7b1055d40d1a9464b66523440 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Fri, 29 Dec 2017 18:03:50 +0100 Subject: [PATCH 03/17] + remove overkill checkglobal pm --- lib/perl/centreon/health/checkglobal.pm | 66 ------------------------- 1 file changed, 66 deletions(-) delete mode 100644 lib/perl/centreon/health/checkglobal.pm diff --git a/lib/perl/centreon/health/checkglobal.pm b/lib/perl/centreon/health/checkglobal.pm deleted file mode 100644 index 24a976b4cf7..00000000000 --- a/lib/perl/centreon/health/checkglobal.pm +++ /dev/null @@ -1,66 +0,0 @@ -# -# 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::checkglobal; - -use strict; -use warnings; -use centreon::common::misc; -use centreon::health::misc; - -sub new { - my $class = shift; - my $self = {}; - $self->{output} = {}; - - bless $self, $class; - return $self; -} - -sub query_misc { - my ($self, %options) = @_; - my ($sth, $status); - - $sth = $options{cdb}->query($options{query}); - - return $sth->fetchrow() -} - -sub run { - my $self = shift; - my ($centreon_db, $centstorage_db, $centreon_version) = @_; - - my $query_misc = { count_pp => [$centreon_db, $centreon_version eq '2.7' ? "SELECT count(*) FROM mod_pluginpack" : "SELECT count(*) FROM mod_ppm_pluginpack"], - count_downtime => [$centreon_db, "SELECT count(*) FROM downtime"], - count_modules => [$centreon_db, "SELECT count(*) FROM modules_informations"], - centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], - count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; - - foreach my $info (keys $query_misc) { - my $result = $self->query_misc(cdb => $query_misc->{$info}[0], - query => $query_misc->{$info}[1] ); - $self->{output}->{$info} = $result; - - } - - return $self->{output} -} - -1; From f1af89326c910688006e75f5d03d662e4d7f0ace Mon Sep 17 00:00:00 2001 From: Sims24 Date: Sat, 30 Dec 2017 15:07:34 +0100 Subject: [PATCH 04/17] + finish checkbroker initial module (subject to change) --- lib/perl/centreon/health/checkbroker.pm | 171 +++++++++--------------- 1 file changed, 64 insertions(+), 107 deletions(-) diff --git a/lib/perl/centreon/health/checkbroker.pm b/lib/perl/centreon/health/checkbroker.pm index 3fb34024df1..b5da2b04c44 100644 --- a/lib/perl/centreon/health/checkbroker.pm +++ b/lib/perl/centreon/health/checkbroker.pm @@ -1,139 +1,96 @@ -################################################################################ -# Copyright 2005-2013 Centreon -# Centreon is developped by : Julien Mathis and Romain Le Merlus under -# GPL Licence 2.0. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation ; either version 2 of the License. -# -# This program is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A -# PARTICULAR PURPOSE. See the GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, see . -# -# Linking this program statically or dynamically with other modules is making a -# combined work based on this program. Thus, the terms and conditions of the GNU -# General Public License cover the whole combination. -# -# As a special exception, the copyright holders of this program give Centreon -# permission to link this program with independent modules to produce an executable, -# regardless of the license terms of these independent modules, and to copy and -# distribute the resulting executable under terms of Centreon choice, provided that -# Centreon also meet, for each linked independent module, the terms and conditions -# of the license of that module. An independent module is a module which is not -# derived from this program. If you modify this program, you may extend this -# exception to your version of the program, but you are not obliged to do so. If you -# do not wish to do so, delete this exception statement from your version. -# # -#################################################################################### +# 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::checksystems; +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->{cmd_system_health} = []; $self->{output} = {}; bless $self, $class; return $self; } -sub build_command_hash { - my ($self, %options) = @_; - - if ($options{medium} eq "snmp") { - $self->{cmd_system_health} = [ { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode cpu-detailed \\ - --hostname localhost \\ - --statefile-suffix='_diag-cpu' \\ - --filter-perfdata='^(?!(wait|guest|user|softirq|kernel|interrupt|guestnice|idle|steal|system|nice))' \\ - --snmp-community " . $options{community} , - callback => \¢reon::health::ssh::ssh_callback, - userdata => "cpu_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode load \\ - --hostname localhost \\ - --filter-perfdata='^(?!(load))' \\ - --snmp-community " . $options{community}, - callback => \¢reon::health::ssh::ssh_callback, - userdata => "load" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode memory \\ - --hostname localhost \\ - --filter-perfdata='^(?!(cached|buffer|used))' \\ - --snmp-community " . $options{community}, - callback => \¢reon::health::ssh::ssh_callback, - userdata => "mem_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode swap \\ - --hostname localhost \\ - --filter-perfdata='^(?!(used))' \\ - --snmp-community " . $options{community}, - callback => \¢reon::health::ssh::ssh_callback, - userdata => "swap_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode storage \\ - --hostname localhost \\ - --storage='^(?!(/dev/shm|/sys/fs/cgroup|/boot|/run.*))' --name --regexp \\ - --filter-perfdata='^(?!(used))' --statefile-suffix='_diag-storage' \\ - --verbose \\ - --snmp-community " . $options{community}, - callback => \¢reon::health::ssh::ssh_callback, - userdata => "storage_usage" }, - ]; - } else { - return -1 - } - - -} - -sub get_remote_infos { +sub json_parsing { my ($self, %options) = @_; - - return centreon::health::ssh::new->main(host => $options{host}, port => $options{ssh_port}, command_pool => $self->{cmd_system_health}); -} - -sub get_local_infos { - my ($self, %options) = @_; - my ($lerror, $stdout); - - my $results; - - foreach my $command (@{$self->{cmd_system_health}}) { - ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); - $results->{$command->{userdata}} = $stdout; - while ($stdout =~ m/Buffer creation/) { - # Replay command to bypass cache creation - sleep 1; - ($lerror, $stdout) = centreon::common::misc::backtick(command => $command->{cmd}); - $results->{$command->{userdata}} = $stdout; + 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 $results - + return $self->{output} + } sub run { my $self = shift; - my ($server_list, $medium, $community, $logger) = @_; + my ($centreon_db, $server_list, $centreon_version, $logger) = @_; + + my $sth; foreach my $server (keys $server_list) { - my $name = $server_list->{$server}->{name}; - if ($server_list->{$server}->{localhost} eq "NO") { - $self->{output}->{$name} = $self->get_remote_infos(host => $server_list->{$server}->{address}, ssh_port => $server_list->{$server}->{ssh_port}); - } else { - $self->{output}->{$name} = $self->get_local_infos(poller_name => $name); - } + $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} } From 990d9a8255eb8d41e5b7a9dfcb6e91cc56a62285 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Sun, 31 Dec 2017 14:07:49 +0100 Subject: [PATCH 05/17] + wip - complete working version TODO: output class --- bin/centreon_health | 64 ++++++++++++ lib/perl/centreon/health/checkdb.pm | 8 -- lib/perl/centreon/health/checklogs.pm | 102 ++++++++++++++++++++ lib/perl/centreon/health/checkmodules.pm | 2 - lib/perl/centreon/health/checkrrd.pm | 6 -- lib/perl/centreon/health/checksystems.pm | 2 - lib/perl/centreon/health/ssh.pm | 12 ++- lib/perl/centreon/script/centreon_health.pm | 26 +++-- 8 files changed, 185 insertions(+), 37 deletions(-) create mode 100755 bin/centreon_health create mode 100644 lib/perl/centreon/health/checklogs.pm diff --git a/bin/centreon_health b/bin/centreon_health new file mode 100755 index 00000000000..16c50807070 --- /dev/null +++ b/bin/centreon_health @@ -0,0 +1,64 @@ +#!/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 + + "skip-rrd" => \$self->{opt_skiprrd}, + "skip-db" => \$self->{opt_skipdb}, + "skip-logs" => \$self->{opt_skiplogs}, + "anonymous" => \$self->{opt_anonymous} + + +=item B<--centstorage-db> + +Specify the name of your centstorage db if not standard +(default: centreon_storage). + +=item B<--centreon-branch> + +Specify centreon version (X.Y format) if older than 2.8 +(default: 2.8). + +=item B<--check-protocol> + +Specify favorite check protocol (will be implemented later, 'snmp' is forced for now) + +=item B<--snmp-community> + +Specify snmp community to execute plugins + +=item B<--output-type> + +Specify output type (will be implemented soon and will offer json, xml at least) + +=item B<--skip-*> + +Skip check that might be long (rrd and db e.g) on large setup. +Can be : --skip-rrd, --skip-db, --skip-logs + +=item B<--anonymous> + +Anonymize sensitive information from output for easier sharing (to implement - no effect for now) + +=head1 DESCRIPTION + +B will gather some informations about centreon global metrics +sample usage: su - centreon -c "/usr/share/centreon/bin/centreon_health --skip-logs" + +=cut diff --git a/lib/perl/centreon/health/checkdb.pm b/lib/perl/centreon/health/checkdb.pm index f80f5e088eb..0230aaf9c71 100644 --- a/lib/perl/centreon/health/checkdb.pm +++ b/lib/perl/centreon/health/checkdb.pm @@ -40,14 +40,6 @@ sub run { my $size = 0; my ($sth, $status); - if ($flag == 1) { - $logger->writeLogDebug("[INFO] Skipping Database checks"); - return 0 - } - - $logger->writeLogDebug("[INFO] Gathering Databases informations"); - - 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 diff --git a/lib/perl/centreon/health/checklogs.pm b/lib/perl/centreon/health/checklogs.pm new file mode 100644 index 00000000000..8541d123d46 --- /dev/null +++ b/lib/perl/centreon/health/checklogs.pm @@ -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; diff --git a/lib/perl/centreon/health/checkmodules.pm b/lib/perl/centreon/health/checkmodules.pm index bb76c994a43..0d749bbf7df 100644 --- a/lib/perl/centreon/health/checkmodules.pm +++ b/lib/perl/centreon/health/checkmodules.pm @@ -39,8 +39,6 @@ sub run { my $size = 0; my ($sth, $status); - $logger->writeLogDebug("[INFO] Gathering Modules informations"); - $sth = $centreon_db->query("SELECT name, rname, author, mod_release FROM modules_informations"); while (my $row = $sth->fetchrow_hashref) { $self->{output}->{$row->{name}}{full_name} = $row->{rname}; diff --git a/lib/perl/centreon/health/checkrrd.pm b/lib/perl/centreon/health/checkrrd.pm index 6f3fd5574d1..c589add4e24 100644 --- a/lib/perl/centreon/health/checkrrd.pm +++ b/lib/perl/centreon/health/checkrrd.pm @@ -71,12 +71,6 @@ sub run { my $self = shift; my ($centstorage_db, $flag, $logger) = @_; - if ($flag == 1) { - $logger->writeLogDebug("[INFO] Skipping RRD checks"); - return 0 - } - - $logger->writeLogDebug("[INFO] Gathering RRD informations"); $self->get_rrd_path(csdb => $centstorage_db); $self->get_rrd_infos(); diff --git a/lib/perl/centreon/health/checksystems.pm b/lib/perl/centreon/health/checksystems.pm index 20952527188..63077a8009d 100644 --- a/lib/perl/centreon/health/checksystems.pm +++ b/lib/perl/centreon/health/checksystems.pm @@ -31,8 +31,6 @@ sub new { $self->{cmd_system_health} = []; $self->{output} = {}; - $self->{cmd_system_health} = []; - bless $self, $class; return $self; } diff --git a/lib/perl/centreon/health/ssh.pm b/lib/perl/centreon/health/ssh.pm index ba3026d5ac4..0ea31159f8b 100644 --- a/lib/perl/centreon/health/ssh.pm +++ b/lib/perl/centreon/health/ssh.pm @@ -44,9 +44,9 @@ sub ssh_callback { if ($options{exit} == SSH_OK || $options{exit} == SSH_AGAIN) { # AGAIN means timeout chomp($options{stdout}); - $command_results->{$options{userdata}} = $options{stdout}; + $command_results->{multiple}->{$options{userdata}} = $options{stdout}; } else { - $command_results->{$options{userdata}} = "Failed action on ssh or plugin"; + $command_results->{multiple}->{$options{userdata}} = "Failed action on ssh or plugin"; return -1 } return 0 @@ -80,11 +80,13 @@ sub main { $self->create_ssh_channel(host => $options{host}, port => $options{port}, user => 'centreon'); if (defined($options{command_pool})) { $self->{session}->execute(commands => $options{command_pool}, timeout => 5000, timeout_nodata => 10, parallel => 5); + $self->{data} = $command_results->{multiple}; } else { - my $return = $self->{session}->execute_simple(cmd => $options{command_simple}, timeout => 10, timeout_nodata => 5); - $command_results->{$return->{userdata}} = defined($return->{stderr}) ? $return->{stderr} : $return->{stdout}; + my $return = $self->{session}->execute_simple(cmd => $options{command}, userdata => $options{userdata}, timeout => 10, timeout_nodata => 5); + $command_results->{simple} = $return->{stdout}; + $self->{data} = $command_results->{simple}; } - $self->{data} = $command_results; + return $self->{data} } diff --git a/lib/perl/centreon/script/centreon_health.pm b/lib/perl/centreon/script/centreon_health.pm index f56632184cf..18a192173d3 100644 --- a/lib/perl/centreon/script/centreon_health.pm +++ b/lib/perl/centreon/script/centreon_health.pm @@ -29,8 +29,8 @@ use centreon::health::checkrrd; use centreon::health::checkdb; use centreon::health::checkmodules; use centreon::health::checksystems; -#use centreon::health::checkbroker; -#use centreon::health::checklogs; +use centreon::health::checkbroker; +use centreon::health::checklogs; use JSON; use Data::Dumper; @@ -53,11 +53,11 @@ sub new { "snmp-community=s" => \$self->{opt_community}, "skip-rrd" => \$self->{opt_skiprrd}, "skip-db" => \$self->{opt_skipdb}, + "skip-logs" => \$self->{opt_skiplogs}, "anonymous" => \$self->{opt_anonymous} ); $self->{global_output} = {}; - $self->{opt_checkprotocol} = 'snmp'; $self->{opt_community} = 'public' if (!defined $self->{opt_community}); $self->{opt_csdb} = 'centreon_storage' if (!defined $self->{opt_csdb}); @@ -70,19 +70,17 @@ sub run { my $self = shift; $self->SUPER::run(); - $self->{logger}->writeLogDebug("[INFO] Starting centreon_health check"); - $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}, defined($self->{opt_skiprrd}), $self->{logger}); - $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}, defined($self->{opt_skipdb}), $self->{logger}); - $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}, $self->{logger}); - $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}, $self->{logger}); - $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}, $self->{logger}); - - - #print Dumper($self->{global_output}); + $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}) if (!defined($self->{opt_skiprrd})); + $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}) if (!defined($self->{opt_skipdb})); + $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); + $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); + $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}); + $self->{global_output}->{broker} = centreon::health::checkbroker->new->run($self->{cdb}, $self->{global_output}->{server}->{poller}, $self->{opt_majorversion}); + $self->{global_output}->{logs} = centreon::health::checklogs->new->run($self->{cdb}, $self->{global_output}->{server}->{poller}) if (!defined($self->{opt_skiplogs})); + + #print Dumper($self->{global_output}->{broker}); my $json = JSON->new->encode($self->{global_output}); print $json ; } - - 1; From 8d9c2ae89966f04602fe34999dfc1d59029a94af Mon Sep 17 00:00:00 2001 From: Sims24 Date: Sun, 31 Dec 2017 14:09:23 +0100 Subject: [PATCH 06/17] delete temp copy/paste --- bin/centreon_health | 6 ------ 1 file changed, 6 deletions(-) diff --git a/bin/centreon_health b/bin/centreon_health index 16c50807070..611d25d9b26 100755 --- a/bin/centreon_health +++ b/bin/centreon_health @@ -19,12 +19,6 @@ centreon_health [options] =over 8 - "skip-rrd" => \$self->{opt_skiprrd}, - "skip-db" => \$self->{opt_skipdb}, - "skip-logs" => \$self->{opt_skiplogs}, - "anonymous" => \$self->{opt_anonymous} - - =item B<--centstorage-db> Specify the name of your centstorage db if not standard From 11e2efb6884ae984740c4970c168865a10e8357c Mon Sep 17 00:00:00 2001 From: Sims24 Date: Sun, 31 Dec 2017 14:35:44 +0100 Subject: [PATCH 07/17] remove ssh from misc as it lives in a dedicated ssh module --- lib/perl/centreon/health/misc.pm | 27 --------------------------- 1 file changed, 27 deletions(-) diff --git a/lib/perl/centreon/health/misc.pm b/lib/perl/centreon/health/misc.pm index 8b9b9ac684c..9157bdd94f5 100644 --- a/lib/perl/centreon/health/misc.pm +++ b/lib/perl/centreon/health/misc.pm @@ -22,33 +22,6 @@ package centreon::health::misc; use strict; use warnings; -use Libssh::Session qw(:all); - -sub get_ssh_connection { - my %options = @_; - - my $session = Libssh::Session->new(); - if ($session->options(host => $options{host}, port => $options{port}, user => $options{user}) != SSH_OK) { - print $session->error() . "\n"; - return 1 - } - - if ($session->connect() != SSH_OK) { - print $session->error() . "\n"; - return 1 - } - - if ($session->auth_publickey_auto() != SSH_AUTH_SUCCESS) { - printf("auth issue pubkey: %s\n", $session->error(GetErrorSession => 1)); - if ($session->auth_password(password => $options{password}) != SSH_AUTH_SUCCESS) { - printf("auth issue: %s\n", $session->error(GetErrorSession => 1)); - return 1 - } - } - - return $session - -} sub change_seconds { my %options = @_; From 856129e2f00b50010313ec89b322a3a4a59ad139 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Tue, 2 Jan 2018 16:56:04 +0100 Subject: [PATCH 08/17] + initial release for centreon_health overview script v1 --- bin/centreon_health | 6 +- lib/perl/centreon/health/output.pm | 163 ++++++++++++++++++++ lib/perl/centreon/script/centreon_health.pm | 32 ++-- 3 files changed, 182 insertions(+), 19 deletions(-) create mode 100644 lib/perl/centreon/health/output.pm diff --git a/bin/centreon_health b/bin/centreon_health index 611d25d9b26..5e4197a3a07 100755 --- a/bin/centreon_health +++ b/bin/centreon_health @@ -31,7 +31,7 @@ Specify centreon version (X.Y format) if older than 2.8 =item B<--check-protocol> -Specify favorite check protocol (will be implemented later, 'snmp' is forced for now) +Specify favorite check protocol (will be implemented later, 'snmp' is the only one available for now) =item B<--snmp-community> @@ -39,11 +39,11 @@ Specify snmp community to execute plugins =item B<--output-type> -Specify output type (will be implemented soon and will offer json, xml at least) +Specify output type. Can be: 'TEXT', 'JSON' 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. +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 =item B<--anonymous> diff --git a/lib/perl/centreon/health/output.pm b/lib/perl/centreon/health/output.pm new file mode 100644 index 00000000000..1df9f6e7934 --- /dev/null +++ b/lib/perl/centreon/health/output.pm @@ -0,0 +1,163 @@ +# +# 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::output; + +use strict; +use warnings; +use JSON; + +sub new { + my $class = shift; + my $self = {}; + + bless $self, $class; + return $self; +} + +sub output_text { + my ($self, %options) = @_; + + my $output = "\t ===CENTREON_HEALTH TEXT OUTPUT=== \n\n"; + $output .= "\t\t CENTREON OVERVIEW\n\n"; + $output .= "Centreon Version: " . $options{data}->{server}->{global}->{centreon_version} . "\n"; + $output .= "Number of pollers: " . $options{data}->{server}->{global}->{count_poller} . "\n"; + $output .= "Number of hosts: " . $options{data}->{server}->{global}->{count_hosts} . "\n"; + $output .= "Number of services: " . $options{data}->{server}->{global}->{count_services} . "\n"; + $output .= "Number of metrics: " . $options{data}->{server}->{global}->{count_metrics} . "\n"; + $output .= "Number of modules: " . $options{data}->{server}->{global}->{count_modules} . "\n"; + $output .= defined($options{data}->{server}->{global}->{count_pp}) ? "Number of plugin-packs: " . $options{data}->{server}->{global}->{count_pp} . "\n" : " Number of plugin-packs: N/A\n"; + $output .= "Number of recurrent downtimes: " . $options{data}->{server}->{global}->{count_downtime} . "\n\n"; + + $output .= "\t\t AVERAGE METRICS\n\n"; + $output .= "Host / poller (avg): " . $options{data}->{server}->{global}->{hosts_by_poller_avg} . "\n"; + $output .= "Service / poller (avg): " . $options{data}->{server}->{global}->{services_by_poller_avg} . "\n"; + $output .= "Service / host (avg): " . $options{data}->{server}->{global}->{services_by_host_avg} . "\n"; + $output .= "Metrics / service (avg): " . $options{data}->{server}->{global}->{metrics_by_service_avg} . "\n\n"; + + if ($options{flag_rrd} != 1 || $options{flag_db} ne "") { + $output .= "\t\t RRD INFORMATIONS\n\n"; + $output .= "RRD not updated since more than 180 days: " . $options{data}->{rrd}->{rrd_not_updated_since_180d} . "\n"; + $output .= "RRD written during last 5 five minutes: " . $options{data}->{rrd}->{rrd_written_last_5m} . "\n"; + foreach my $key (sort keys $options{data}->{rrd}) { + next if ($key =~ m/^rrd_/); + $output .= "RRD files Count/Size in " . $key . " directory: " . $options{data}->{rrd}->{$key}->{count} . "/" . $options{data}->{rrd}->{$key}->{size} . "\n"; + } + $output .= "\n"; + } + + if ($options{flag_db} != 1 || $options{flag_db} ne "") { + $output .= "\t\t DATABASES INFORMATIONS\n\n"; + $output .= "\tDatabases size\n\n"; + foreach my $database (keys $options{data}->{database}->{db_size}) { + $output .= "Size of " . $database . " database: " . $options{data}->{database}->{db_size}->{$database} . "\n"; + } + $output .= "\n"; + $output .= "\tTables size (centreon_storage db)\n\n"; + foreach my $database (keys $options{data}->{database}->{table_size}) { + $output .= "Size of " . $database . " table: " . $options{data}->{database}->{table_size}->{$database} . "\n"; + } + $output .= "\n"; + $output .= "\tPartitioning check\n\n"; + foreach my $table (keys $options{data}->{database}->{partitioning_last_part}) { + $output .= "Last partition date for " . $table . " table: " . $options{data}->{database}->{partitioning_last_part}->{$table} . "\n"; + } + $output .= "\n"; + } + + $output .= "\t\t MODULE INFORMATIONS\n\n"; + foreach my $module_key (keys $options{data}->{module}) { + $output .= "Module " . $options{data}->{module}->{$module_key}->{full_name} . " is installed. (Author: " . $options{data}->{module}->{$module_key}->{author} . " # Codename: " . $module_key . " # Version: " . $options{data}->{module}->{$module_key}->{version} . ")\n"; + } + $output .= "\n"; + + $output .= "\t\t CENTREON NODES INFORMATIONS\n\n"; + + foreach my $poller_id (keys $options{data}->{server}->{poller}) { + $output .= "\t" . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; + $output .= "Identity: \n"; + $output .= " Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; + $output .= " IP Address (SSH port): " . $options{data}->{server}->{poller}->{$poller_id}->{address} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{ssh_port} . ")\n"; + $output .= " Localhost: " . $options{data}->{server}->{poller}->{$poller_id}->{localhost} . "\n"; + $output .= " Running: " . $options{data}->{server}->{poller}->{$poller_id}->{running} . "\n"; + $output .= " Start time: " . $options{data}->{server}->{poller}->{$poller_id}->{start_time} . "\n"; + $output .= " Last alive: " . $options{data}->{server}->{poller}->{$poller_id}->{last_alive} . "\n"; + $output .= " Uptime: " . $options{data}->{server}->{poller}->{$poller_id}->{uptime} . "\n"; + $output .= " Count Hosts/Services - (Last command check): " . $options{data}->{server}->{poller}->{$poller_id}->{hosts} . "/" . $options{data}->{server}->{poller}->{$poller_id}->{services} . " - (" . $options{data}->{server}->{poller}->{$poller_id}->{last_command_check} . ")\n\n"; + + $output .= "Engine stats: \n"; + foreach my $stat_key (sort keys $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}) { + foreach my $stat_value (sort keys $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}) { + $output .= " " . $stat_key . "(" . $stat_value . "): " . $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}->{$stat_value} . "\n"; + } + } + $output .= "\n"; + + $output .= "Broker stats: \n"; + foreach my $broker_stat_file (sort keys $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}) { + $output .= " \tFile: " . $broker_stat_file . "\n"; + $output .= " Version: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{version} . "\n"; + $output .= " State: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{state} . "\n"; + $output .= " Event proecessing speed " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{event_processing_speed} . "\n"; + $output .= " Queued events: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{queued_events} . "\n"; + $output .= " Last connection attempts: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{last_connection_attempt} . "\n"; + $output .= " Last connection success: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{last_connection_success} . "\n\n"; + } + + $output .= "System stats: \n"; + $output .= " CPU => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage} . "\n"; + $output .= " LOAD => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load} . "\n"; + $output .= " MEMORY => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage} . "\n"; + $output .= " SWAP => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage} . "\n"; + $output .= " STORAGE => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage} . "\n\n"; + + if ($options{flag_logs} != 1 || $options{flag_logs} eq "") { + $output .= "\t\t LOGS LAST LINES: \n\n"; + foreach my $log_topic (sort keys $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}) { + foreach my $log_file (keys $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}) { + $output .= " " . $log_file . " (" . $log_topic . ")\n\n"; + $output .= $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}->{$log_file} . "\n\n"; + } + $output .= "\n"; + } + } + } + + return $output; + +} + +sub run { + my $self = shift; + my ($data, $format, $flag_rrd, $flag_db, $flash_logs) = @_; + + if ($format eq "JSON") { + my $output = JSON->new->encode($data); + print $output; + } elsif ($format eq "TEXT") { + my $output = $self->output_text(data => $data, flag_rrd => $flag_rrd, flag_db => $flag_db, flag_logs => $flash_logs); + print $output; + } elsif ($format eq "DUMPER") { + use Data::Dumper; + print Dumper($data); + } +} + +1; diff --git a/lib/perl/centreon/script/centreon_health.pm b/lib/perl/centreon/script/centreon_health.pm index 18a192173d3..08862a78f5c 100644 --- a/lib/perl/centreon/script/centreon_health.pm +++ b/lib/perl/centreon/script/centreon_health.pm @@ -24,6 +24,8 @@ use strict; use warnings; use POSIX; use centreon::script; +use base qw(centreon::script); + use centreon::health::checkservers; use centreon::health::checkrrd; use centreon::health::checkdb; @@ -31,10 +33,7 @@ use centreon::health::checkmodules; use centreon::health::checksystems; use centreon::health::checkbroker; use centreon::health::checklogs; - -use JSON; -use Data::Dumper; -use base qw(centreon::script); +use centreon::health::output; sub new { my $class = shift; @@ -49,7 +48,7 @@ sub new { "centstorage-db=s" => \$self->{opt_csdb}, "centreon-branch=s" => \$self->{opt_majorversion}, "check-protocol=s" => \$self->{opt_checkprotocol}, - "output-type=s" => \$self->{opt_output}, + "output-format=s" => \$self->{opt_outputformat}, "snmp-community=s" => \$self->{opt_community}, "skip-rrd" => \$self->{opt_skiprrd}, "skip-db" => \$self->{opt_skipdb}, @@ -57,8 +56,10 @@ sub new { "anonymous" => \$self->{opt_anonymous} ); - $self->{global_output} = {}; + $self->{data} = {}; + $self->{final_output} = {}; $self->{opt_checkprotocol} = 'snmp'; + $self->{opt_outputformat} = 'JSON' if (!defined$self->{opt_outputformat}); $self->{opt_community} = 'public' if (!defined $self->{opt_community}); $self->{opt_csdb} = 'centreon_storage' if (!defined $self->{opt_csdb}); $self->{opt_majorversion} = '2.8' if (!defined $self->{opt_majorversion}); @@ -70,17 +71,16 @@ sub run { my $self = shift; $self->SUPER::run(); - $self->{global_output}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}) if (!defined($self->{opt_skiprrd})); - $self->{global_output}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}) if (!defined($self->{opt_skipdb})); - $self->{global_output}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); - $self->{global_output}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); - $self->{global_output}->{systems} = centreon::health::checksystems->new->run($self->{global_output}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}); - $self->{global_output}->{broker} = centreon::health::checkbroker->new->run($self->{cdb}, $self->{global_output}->{server}->{poller}, $self->{opt_majorversion}); - $self->{global_output}->{logs} = centreon::health::checklogs->new->run($self->{cdb}, $self->{global_output}->{server}->{poller}) if (!defined($self->{opt_skiplogs})); + $self->{data}->{rrd} = centreon::health::checkrrd->new->run($self->{csdb}) if (!defined($self->{opt_skiprrd})); + $self->{data}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}) if (!defined($self->{opt_skipdb})); + $self->{data}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); + $self->{data}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); + $self->{data}->{systems} = centreon::health::checksystems->new->run($self->{data}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}); + $self->{data}->{broker} = centreon::health::checkbroker->new->run($self->{cdb}, $self->{data}->{server}->{poller}, $self->{opt_majorversion}); + $self->{data}->{logs} = centreon::health::checklogs->new->run($self->{cdb}, $self->{data}->{server}->{poller}) if (!defined($self->{opt_skiplogs})); + + centreon::health::output->new->run($self->{data}, $self->{opt_outputformat}, defined($self->{opt_skiprrd}), defined($self->{opt_skipdb}), defined($self->{opt_skiplogs})); - #print Dumper($self->{global_output}->{broker}); - my $json = JSON->new->encode($self->{global_output}); - print $json ; } 1; From 7a2b905ee32ca7b9a79ba0a7f323484b9606a044 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Tue, 2 Jan 2018 18:12:28 +0100 Subject: [PATCH 09/17] + working with perl 5.10 and enhance el6/2.7 compatibility --- lib/perl/centreon/health/checkbroker.pm | 17 +++++++------ lib/perl/centreon/health/checklogs.pm | 2 +- lib/perl/centreon/health/checkservers.pm | 5 ++-- lib/perl/centreon/health/checksystems.pm | 16 ++++++------ lib/perl/centreon/health/misc.pm | 27 +++++++++++++++++++++ lib/perl/centreon/health/output.pm | 26 ++++++++++---------- lib/perl/centreon/script/centreon_health.pm | 2 +- 7 files changed, 63 insertions(+), 32 deletions(-) diff --git a/lib/perl/centreon/health/checkbroker.pm b/lib/perl/centreon/health/checkbroker.pm index b5da2b04c44..94a7665e7b9 100644 --- a/lib/perl/centreon/health/checkbroker.pm +++ b/lib/perl/centreon/health/checkbroker.pm @@ -40,9 +40,9 @@ sub json_parsing { my ($self, %options) = @_; my $json_content = JSON->new->decode($options{json_content}); - foreach my $key (keys $json_content) { + foreach my $key (keys %$json_content) { if ($key =~ m/^endpoint/) { - foreach my $broker_metric (keys $json_content->{$key}) { + 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})) @@ -60,11 +60,12 @@ sub json_parsing { sub run { my $self = shift; - my ($centreon_db, $server_list, $centreon_version, $logger) = @_; + my ($centreon_db, $server_list, $centreon_version) = @_; my $sth; - foreach my $server (keys $server_list) { + 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' @@ -74,8 +75,8 @@ sub run { 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"); + poller_name => $server_list->{$server}->{name}, + file_name => $row->{config_name}. "-stats.json"); } } else { while (my $row = $sth->fetchrow_hashref()) { @@ -84,8 +85,8 @@ sub run { 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"); + poller_name => $server_list->{$server}->{name}, + file_name => $row->{config_name}. "-stats.json"); } } diff --git a/lib/perl/centreon/health/checklogs.pm b/lib/perl/centreon/health/checklogs.pm index 8541d123d46..e54e1925e4f 100644 --- a/lib/perl/centreon/health/checklogs.pm +++ b/lib/perl/centreon/health/checklogs.pm @@ -44,7 +44,7 @@ sub run { my $sth; my ($lerror, $stdout); - foreach my $server (keys $server_list) { + foreach my $server (keys %$server_list) { $sth = $centreon_db->query("SELECT log_file FROM cfg_nagios WHERE nagios_id=" . $centreon_db->quote($server)); diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm index efa73c653db..212e6035da8 100644 --- a/lib/perl/centreon/health/checkservers.pm +++ b/lib/perl/centreon/health/checkservers.pm @@ -47,12 +47,13 @@ sub get_servers_informations { my ($self, %options) = @_; my $sth = $options{cdb}->query("SELECT id, name, localhost, ns_ip_address, ssh_port - FROM nagios_server"); + FROM nagios_server WHERE ns_activate='1'"); while (my $row = $sth->fetchrow_hashref()) { $self->{output}->{poller}->{$row->{id}}{name} = $row->{name}; $self->{output}->{poller}->{$row->{id}}{localhost} = ($row->{localhost} == 1) ? "YES" : "NO"; $self->{output}->{poller}->{$row->{id}}{address} = $row->{ns_ip_address}; $self->{output}->{poller}->{$row->{id}}{ssh_port} = $row->{ssh_port}; + $self->{output}->{poller}->{$row->{id}}{id} = $row->{id}; } foreach my $id (keys %{$self->{output}->{poller}}) { @@ -118,7 +119,7 @@ sub run { centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; - foreach my $info (keys $query_misc) { + foreach my $info (keys %$query_misc) { my $result = $self->query_misc(cdb => $query_misc->{$info}[0], query => $query_misc->{$info}[1] ); $self->{output}->{global}->{$info} = $result; diff --git a/lib/perl/centreon/health/checksystems.pm b/lib/perl/centreon/health/checksystems.pm index 63077a8009d..e5ef0cff0b9 100644 --- a/lib/perl/centreon/health/checksystems.pm +++ b/lib/perl/centreon/health/checksystems.pm @@ -39,32 +39,32 @@ sub build_command_hash { my ($self, %options) = @_; if ($options{medium} eq "snmp") { - $self->{cmd_system_health} = [ { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode cpu-detailed \\ + $self->{cmd_system_health} = [ { cmd => "/usr/lib/" . $options{plugin_path} . "/plugins/" . $options{plugins} . " --plugin os::linux::snmp::plugin --mode cpu-detailed \\ --hostname localhost \\ --statefile-suffix='_diag-cpu' \\ --filter-perfdata='^(?!(wait|guest|user|softirq|kernel|interrupt|guestnice|idle|steal|system|nice))' \\ --snmp-community " . $options{community} , callback => \¢reon::health::ssh::ssh_callback, userdata => "cpu_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode load \\ + { cmd => "/usr/lib/" . $options{plugin_path} . "/plugins/" . $options{plugins} . " --plugin os::linux::snmp::plugin --mode load \\ --hostname localhost \\ --filter-perfdata='^(?!(load))' \\ --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "load" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode memory \\ + { cmd => "/usr/lib/" . $options{plugin_path} . "/plugins/" . $options{plugins} . " --plugin os::linux::snmp::plugin --mode memory \\ --hostname localhost \\ --filter-perfdata='^(?!(cached|buffer|used))' \\ --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "mem_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode swap \\ + { cmd => "/usr/lib/" . $options{plugin_path} . "/plugins/" . $options{plugins} . " --plugin os::linux::snmp::plugin --mode swap \\ --hostname localhost \\ --filter-perfdata='^(?!(used))' \\ --snmp-community " . $options{community}, callback => \¢reon::health::ssh::ssh_callback, userdata => "swap_usage" }, - { cmd => "/usr/lib/centreon/plugins/centreon_linux_snmp.pl --plugin os::linux::snmp::plugin --mode storage \\ + { cmd => "/usr/lib/" . $options{plugin_path} . "/plugins/" . $options{plugins} . " --plugin os::linux::snmp::plugin --mode storage \\ --hostname localhost \\ --storage='^(?!(/dev/shm|/sys/fs/cgroup|/boot|/run.*))' --name --regexp \\ --filter-perfdata='^(?!(used))' --statefile-suffix='_diag-storage' \\ @@ -110,12 +110,14 @@ sub get_local_infos { sub run { my $self = shift; - my ($server_list, $medium, $community, $logger) = @_; + my ($server_list, $medium, $community, $centreon_ver) = @_; $self->build_command_hash(medium => $medium, + plugins => ($centreon_ver eq 2.8) ? 'centreon_linux_snmp.pl' : 'centreon_plugins.pl', + plugin_path => ($centreon_ver eq 2.8) ? 'centreon' : 'nagios', community => $community); - foreach my $server (keys $server_list) { + foreach my $server (keys %$server_list) { my $name = $server_list->{$server}->{name}; if ($server_list->{$server}->{localhost} eq "NO") { $self->{output}->{$name} = $self->get_remote_infos(host => $server_list->{$server}->{address}, ssh_port => $server_list->{$server}->{ssh_port}); diff --git a/lib/perl/centreon/health/misc.pm b/lib/perl/centreon/health/misc.pm index 9157bdd94f5..8b9b9ac684c 100644 --- a/lib/perl/centreon/health/misc.pm +++ b/lib/perl/centreon/health/misc.pm @@ -22,6 +22,33 @@ package centreon::health::misc; use strict; use warnings; +use Libssh::Session qw(:all); + +sub get_ssh_connection { + my %options = @_; + + my $session = Libssh::Session->new(); + if ($session->options(host => $options{host}, port => $options{port}, user => $options{user}) != SSH_OK) { + print $session->error() . "\n"; + return 1 + } + + if ($session->connect() != SSH_OK) { + print $session->error() . "\n"; + return 1 + } + + if ($session->auth_publickey_auto() != SSH_AUTH_SUCCESS) { + printf("auth issue pubkey: %s\n", $session->error(GetErrorSession => 1)); + if ($session->auth_password(password => $options{password}) != SSH_AUTH_SUCCESS) { + printf("auth issue: %s\n", $session->error(GetErrorSession => 1)); + return 1 + } + } + + return $session + +} sub change_seconds { my %options = @_; diff --git a/lib/perl/centreon/health/output.pm b/lib/perl/centreon/health/output.pm index 1df9f6e7934..1873a5d0fda 100644 --- a/lib/perl/centreon/health/output.pm +++ b/lib/perl/centreon/health/output.pm @@ -52,45 +52,45 @@ sub output_text { $output .= "Service / host (avg): " . $options{data}->{server}->{global}->{services_by_host_avg} . "\n"; $output .= "Metrics / service (avg): " . $options{data}->{server}->{global}->{metrics_by_service_avg} . "\n\n"; - if ($options{flag_rrd} != 1 || $options{flag_db} ne "") { + if ($options{flag_rrd} != 1 || $options{flag_db} eq "") { $output .= "\t\t RRD INFORMATIONS\n\n"; $output .= "RRD not updated since more than 180 days: " . $options{data}->{rrd}->{rrd_not_updated_since_180d} . "\n"; $output .= "RRD written during last 5 five minutes: " . $options{data}->{rrd}->{rrd_written_last_5m} . "\n"; - foreach my $key (sort keys $options{data}->{rrd}) { + foreach my $key (sort keys %{$options{data}->{rrd}}) { next if ($key =~ m/^rrd_/); $output .= "RRD files Count/Size in " . $key . " directory: " . $options{data}->{rrd}->{$key}->{count} . "/" . $options{data}->{rrd}->{$key}->{size} . "\n"; } $output .= "\n"; } - if ($options{flag_db} != 1 || $options{flag_db} ne "") { + if ($options{flag_db} != 1 || $options{flag_db} eq "") { $output .= "\t\t DATABASES INFORMATIONS\n\n"; $output .= "\tDatabases size\n\n"; - foreach my $database (keys $options{data}->{database}->{db_size}) { + foreach my $database (keys %{$options{data}->{database}->{db_size}}) { $output .= "Size of " . $database . " database: " . $options{data}->{database}->{db_size}->{$database} . "\n"; } $output .= "\n"; $output .= "\tTables size (centreon_storage db)\n\n"; - foreach my $database (keys $options{data}->{database}->{table_size}) { + foreach my $database (keys %{$options{data}->{database}->{table_size}}) { $output .= "Size of " . $database . " table: " . $options{data}->{database}->{table_size}->{$database} . "\n"; } $output .= "\n"; $output .= "\tPartitioning check\n\n"; - foreach my $table (keys $options{data}->{database}->{partitioning_last_part}) { + foreach my $table (keys %{$options{data}->{database}->{partitioning_last_part}}) { $output .= "Last partition date for " . $table . " table: " . $options{data}->{database}->{partitioning_last_part}->{$table} . "\n"; } $output .= "\n"; } $output .= "\t\t MODULE INFORMATIONS\n\n"; - foreach my $module_key (keys $options{data}->{module}) { + foreach my $module_key (keys %{$options{data}->{module}}) { $output .= "Module " . $options{data}->{module}->{$module_key}->{full_name} . " is installed. (Author: " . $options{data}->{module}->{$module_key}->{author} . " # Codename: " . $module_key . " # Version: " . $options{data}->{module}->{$module_key}->{version} . ")\n"; } $output .= "\n"; $output .= "\t\t CENTREON NODES INFORMATIONS\n\n"; - foreach my $poller_id (keys $options{data}->{server}->{poller}) { + foreach my $poller_id (keys %{$options{data}->{server}->{poller}}) { $output .= "\t" . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; $output .= "Identity: \n"; $output .= " Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; @@ -103,15 +103,15 @@ sub output_text { $output .= " Count Hosts/Services - (Last command check): " . $options{data}->{server}->{poller}->{$poller_id}->{hosts} . "/" . $options{data}->{server}->{poller}->{$poller_id}->{services} . " - (" . $options{data}->{server}->{poller}->{$poller_id}->{last_command_check} . ")\n\n"; $output .= "Engine stats: \n"; - foreach my $stat_key (sort keys $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}) { - foreach my $stat_value (sort keys $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}) { + foreach my $stat_key (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}}) { + foreach my $stat_value (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}}) { $output .= " " . $stat_key . "(" . $stat_value . "): " . $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}->{$stat_value} . "\n"; } } $output .= "\n"; $output .= "Broker stats: \n"; - foreach my $broker_stat_file (sort keys $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}) { + foreach my $broker_stat_file (sort keys %{$options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { $output .= " \tFile: " . $broker_stat_file . "\n"; $output .= " Version: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{version} . "\n"; $output .= " State: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{state} . "\n"; @@ -130,8 +130,8 @@ sub output_text { if ($options{flag_logs} != 1 || $options{flag_logs} eq "") { $output .= "\t\t LOGS LAST LINES: \n\n"; - foreach my $log_topic (sort keys $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}) { - foreach my $log_file (keys $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}) { + foreach my $log_topic (sort keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { + foreach my $log_file (keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}}) { $output .= " " . $log_file . " (" . $log_topic . ")\n\n"; $output .= $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}->{$log_file} . "\n\n"; } diff --git a/lib/perl/centreon/script/centreon_health.pm b/lib/perl/centreon/script/centreon_health.pm index 08862a78f5c..2a4884d5bda 100644 --- a/lib/perl/centreon/script/centreon_health.pm +++ b/lib/perl/centreon/script/centreon_health.pm @@ -75,7 +75,7 @@ sub run { $self->{data}->{database} = centreon::health::checkdb->new->run($self->{cdb}, $self->{csdb}, $self->{opt_csdb}) if (!defined($self->{opt_skipdb})); $self->{data}->{module} = centreon::health::checkmodules->new->run($self->{cdb}); $self->{data}->{server} = centreon::health::checkservers->new->run($self->{cdb}, $self->{csdb}, $self->{opt_majorversion}); - $self->{data}->{systems} = centreon::health::checksystems->new->run($self->{data}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}); + $self->{data}->{systems} = centreon::health::checksystems->new->run($self->{data}->{server}->{poller}, $self->{opt_checkprotocol}, $self->{opt_community}, $self->{opt_majorversion}); $self->{data}->{broker} = centreon::health::checkbroker->new->run($self->{cdb}, $self->{data}->{server}->{poller}, $self->{opt_majorversion}); $self->{data}->{logs} = centreon::health::checklogs->new->run($self->{cdb}, $self->{data}->{server}->{poller}) if (!defined($self->{opt_skiplogs})); From e71ea1436b0d8cbe1258fd6fb8c6f60bbd453aac Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 4 Jan 2018 08:22:27 +0100 Subject: [PATCH 10/17] + add markdown output (easier for github copy/paste) --- bin/centreon_health | 2 +- lib/perl/centreon/health/checkbroker.pm | 5 + lib/perl/centreon/health/checkrrd.pm | 26 +++- lib/perl/centreon/health/checksystems.pm | 3 +- lib/perl/centreon/health/output.pm | 174 +++++++++++++++++++++-- lib/perl/centreon/health/ssh.pm | 2 +- 6 files changed, 188 insertions(+), 24 deletions(-) diff --git a/bin/centreon_health b/bin/centreon_health index 5e4197a3a07..bf6441dfdc7 100755 --- a/bin/centreon_health +++ b/bin/centreon_health @@ -39,7 +39,7 @@ Specify snmp community to execute plugins =item B<--output-type> -Specify output type. Can be: 'TEXT', 'JSON' or 'DUMPER' (last if for debugging only, raw perl struct) +Specify output type. Can be: 'MARKDOWN', 'JSON', 'TEXT' or 'DUMPER' (last if for debugging only, raw perl struct) =item B<--skip-*> diff --git a/lib/perl/centreon/health/checkbroker.pm b/lib/perl/centreon/health/checkbroker.pm index 94a7665e7b9..7080877d983 100644 --- a/lib/perl/centreon/health/checkbroker.pm +++ b/lib/perl/centreon/health/checkbroker.pm @@ -64,6 +64,11 @@ sub run { 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 diff --git a/lib/perl/centreon/health/checkrrd.pm b/lib/perl/centreon/health/checkrrd.pm index c589add4e24..f0079972610 100644 --- a/lib/perl/centreon/health/checkrrd.pm +++ b/lib/perl/centreon/health/checkrrd.pm @@ -52,12 +52,26 @@ sub get_rrd_path { sub get_rrd_infos { my $self = shift; - my ($lerror_m, $size_metrics) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_metrics}); - my ($lerror_s, $size_status) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_status}); - my ($lerror_cm, $count_metrics) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_metrics} . " | wc -l"); - my ($lerror_cs, $count_status) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_status} . " | wc -l"); - my ($lerror_lw, $count_last_written) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin 5 | wc -l"); - my ($lerror_od, $count_outdated_rrd) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin +288000 | wc -l"); + my ($lerror, $size_metrics, $size_status, $count_last_written, $count_outdated_rrd, $count_metrics, $count_status); + + if (-d $self->{rrd_metrics}) { + ($lerror, $size_metrics) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_metrics}); + ($lerror, $count_metrics) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_metrics} . " | wc -l"); + ($lerror, $count_last_written) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin 5 | wc -l"); + ($lerror, $count_outdated_rrd) = centreon::common::misc::backtick(command => "find " . $self->{rrd_metrics} . " -type f -mmin +288000 | wc -l"); + } else { + $count_metrics = 0; + $size_metrics = 0; + $count_last_written = "ERROR, Directory " . $self->{rrd_metrics} . " does not exist !\n"; + $count_outdated_rrd = "ERROR, Directory " . $self->{rrd_metrics} . " does not exist !\n"; + } + if (-d $self->{rrd_status}) { + ($lerror, $size_status) = centreon::common::misc::backtick(command => "du -sb " . $self->{rrd_status}); + ($lerror, $count_status) = centreon::common::misc::backtick(command => "ls -l " . $self->{rrd_status} . " | wc -l"); + } else { + $count_status = 0; + $size_status = 0; + } $self->{output}->{$self->{rrd_metrics}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_metrics); $self->{output}->{$self->{rrd_status}}{size} = centreon::health::misc::format_bytes(bytes_value => $size_status); diff --git a/lib/perl/centreon/health/checksystems.pm b/lib/perl/centreon/health/checksystems.pm index e5ef0cff0b9..dfb4d7b9979 100644 --- a/lib/perl/centreon/health/checksystems.pm +++ b/lib/perl/centreon/health/checksystems.pm @@ -82,7 +82,8 @@ sub build_command_hash { sub get_remote_infos { my ($self, %options) = @_; - + + return centreon::health::ssh::new->main(host => $options{host}, port => $options{ssh_port}, command_pool => $self->{cmd_system_health}); } diff --git a/lib/perl/centreon/health/output.pm b/lib/perl/centreon/health/output.pm index 1873a5d0fda..3ca1f3f0c92 100644 --- a/lib/perl/centreon/health/output.pm +++ b/lib/perl/centreon/health/output.pm @@ -35,7 +35,9 @@ sub new { sub output_text { my ($self, %options) = @_; - my $output = "\t ===CENTREON_HEALTH TEXT OUTPUT=== \n\n"; + my $output; + + $output = "\t ===CENTREON_HEALTH TEXT OUTPUT=== \n\n"; $output .= "\t\t CENTREON OVERVIEW\n\n"; $output .= "Centreon Version: " . $options{data}->{server}->{global}->{centreon_version} . "\n"; $output .= "Number of pollers: " . $options{data}->{server}->{global}->{count_poller} . "\n"; @@ -93,14 +95,18 @@ sub output_text { foreach my $poller_id (keys %{$options{data}->{server}->{poller}}) { $output .= "\t" . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; $output .= "Identity: \n"; - $output .= " Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; - $output .= " IP Address (SSH port): " . $options{data}->{server}->{poller}->{$poller_id}->{address} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{ssh_port} . ")\n"; - $output .= " Localhost: " . $options{data}->{server}->{poller}->{$poller_id}->{localhost} . "\n"; - $output .= " Running: " . $options{data}->{server}->{poller}->{$poller_id}->{running} . "\n"; - $output .= " Start time: " . $options{data}->{server}->{poller}->{$poller_id}->{start_time} . "\n"; - $output .= " Last alive: " . $options{data}->{server}->{poller}->{$poller_id}->{last_alive} . "\n"; - $output .= " Uptime: " . $options{data}->{server}->{poller}->{$poller_id}->{uptime} . "\n"; - $output .= " Count Hosts/Services - (Last command check): " . $options{data}->{server}->{poller}->{$poller_id}->{hosts} . "/" . $options{data}->{server}->{poller}->{$poller_id}->{services} . " - (" . $options{data}->{server}->{poller}->{$poller_id}->{last_command_check} . ")\n\n"; + if (defined($options{data}->{server}->{poller}->{$poller_id}->{engine}) && defined($options{data}->{server}->{poller}->{$poller_id}->{version})) { + $output .= " Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; + $output .= " IP Address (SSH port): " . $options{data}->{server}->{poller}->{$poller_id}->{address} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{ssh_port} . ")\n"; + $output .= " Localhost: " . $options{data}->{server}->{poller}->{$poller_id}->{localhost} . "\n"; + $output .= " Running: " . $options{data}->{server}->{poller}->{$poller_id}->{running} . "\n"; + $output .= " Start time: " . $options{data}->{server}->{poller}->{$poller_id}->{start_time} . "\n"; + $output .= " Last alive: " . $options{data}->{server}->{poller}->{$poller_id}->{last_alive} . "\n"; + $output .= " Uptime: " . $options{data}->{server}->{poller}->{$poller_id}->{uptime} . "\n"; + $output .= " Count Hosts/Services - (Last command check): " . $options{data}->{server}->{poller}->{$poller_id}->{hosts} . "/" . $options{data}->{server}->{poller}->{$poller_id}->{services} . " - (" . $options{data}->{server}->{poller}->{$poller_id}->{last_command_check} . ")\n\n"; + } else { + $output .= " SKIP Identity for this poller, enabled but does not seems to work correctly ! \n\n"; + } $output .= "Engine stats: \n"; foreach my $stat_key (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}}) { @@ -122,11 +128,21 @@ sub output_text { } $output .= "System stats: \n"; - $output .= " CPU => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage} . "\n"; - $output .= " LOAD => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load} . "\n"; - $output .= " MEMORY => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage} . "\n"; - $output .= " SWAP => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage} . "\n"; - $output .= " STORAGE => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage} . "\n\n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage}) ? + " CPU => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage} . "\n" : + " CPU => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load}) ? + " LOAD => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load} . "\n" : + " LOAD => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage}) ? + " MEMORY => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage} . "\n" : + " MEMORY => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage}) ? + " SWAP => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage} . "\n" : + " SWAP => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage}) ? + " STORAGE => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage} . "\n\n" : + " STORAGE => Could not gather data \n\n"; if ($options{flag_logs} != 1 || $options{flag_logs} eq "") { $output .= "\t\t LOGS LAST LINES: \n\n"; @@ -139,11 +155,136 @@ sub output_text { } } } - return $output; +} + +sub output_markdown { + my ($self, %options) = @_; + + my $output; + + $output = "#CENTREON_HEALTH TEXT OUTPUT\n"; + $output .= "##CENTREON OVERVIEW\n\n"; + $output .= " + Centreon Version: " . $options{data}->{server}->{global}->{centreon_version} . "\n"; + $output .= " + Number of pollers: " . $options{data}->{server}->{global}->{count_poller} . "\n"; + $output .= " + Number of hosts: " . $options{data}->{server}->{global}->{count_hosts} . "\n"; + $output .= " + Number of services: " . $options{data}->{server}->{global}->{count_services} . "\n"; + $output .= " + Number of metrics: " . $options{data}->{server}->{global}->{count_metrics} . "\n"; + $output .= " + Number of modules: " . $options{data}->{server}->{global}->{count_modules} . "\n"; + $output .= defined($options{data}->{server}->{global}->{count_pp}) ? " + Number of plugin-packs: " . $options{data}->{server}->{global}->{count_pp} . "\n" : " + Number of plugin-packs: N/A\n"; + $output .= " + Number of recurrent downtimes: " . $options{data}->{server}->{global}->{count_downtime} . "\n\n"; + $output .= "##AVERAGE METRICS\n\n"; + $output .= " + Host / poller (avg): " . $options{data}->{server}->{global}->{hosts_by_poller_avg} . "\n"; + $output .= " + Service / poller (avg): " . $options{data}->{server}->{global}->{services_by_poller_avg} . "\n"; + $output .= " + Service / host (avg): " . $options{data}->{server}->{global}->{services_by_host_avg} . "\n"; + $output .= " + Metrics / service (avg): " . $options{data}->{server}->{global}->{metrics_by_service_avg} . "\n\n"; + + if ($options{flag_rrd} != 1 || $options{flag_db} eq "") { + $output .= "##RRD INFORMATIONS\n\n"; + $output .= " + RRD not updated since more than 180 days: " . $options{data}->{rrd}->{rrd_not_updated_since_180d} . "\n"; + $output .= " + RRD written during last 5 five minutes: " . $options{data}->{rrd}->{rrd_written_last_5m} . "\n"; + foreach my $key (sort keys %{$options{data}->{rrd}}) { + next if ($key =~ m/^rrd_/); + $output .= " + RRD files Count/Size in " . $key . " directory: " . $options{data}->{rrd}->{$key}->{count} . "/" . $options{data}->{rrd}->{$key}->{size} . "\n"; + } + $output .= "\n"; + } + + if ($options{flag_db} != 1 || $options{flag_db} eq "") { + $output .= "##DATABASES INFORMATIONS\n\n"; + $output .= "###Databases size\n\n"; + foreach my $database (keys %{$options{data}->{database}->{db_size}}) { + $output .= " + Size of " . $database . " database: " . $options{data}->{database}->{db_size}->{$database} . "\n"; + } + $output .= "\n"; + $output .= "###Tables size (centreon_storage db)\n\n"; + foreach my $database (keys %{$options{data}->{database}->{table_size}}) { + $output .= " + Size of " . $database . " table: " . $options{data}->{database}->{table_size}->{$database} . "\n"; + } + $output .= "\n"; + $output .= "###Partitioning check\n\n"; + foreach my $table (keys %{$options{data}->{database}->{partitioning_last_part}}) { + $output .= " + Last partition date for " . $table . " table: " . $options{data}->{database}->{partitioning_last_part}->{$table} . "\n"; + } + $output .= "\n"; + } + + $output .= "##MODULE INFORMATIONS\n\n"; + foreach my $module_key (keys %{$options{data}->{module}}) { + $output .= " + Module " . $options{data}->{module}->{$module_key}->{full_name} . " is installed. (Author: " . $options{data}->{module}->{$module_key}->{author} . " # Codename: " . $module_key . " # Version: " . $options{data}->{module}->{$module_key}->{version} . ")\n"; + } + $output .= "\n"; + $output .= "##CENTREON NODES INFORMATIONS\n\n"; + + foreach my $poller_id (keys %{$options{data}->{server}->{poller}}) { + $output .= "###" . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; + $output .= "####Identity: \n"; + if (defined($options{data}->{server}->{poller}->{$poller_id}->{engine}) && defined($options{data}->{server}->{poller}->{$poller_id}->{version})) { + $output .= " + Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; + $output .= " + IP Address (SSH port): " . $options{data}->{server}->{poller}->{$poller_id}->{address} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{ssh_port} . ")\n"; + $output .= " + Localhost: " . $options{data}->{server}->{poller}->{$poller_id}->{localhost} . "\n"; + $output .= " + Running: " . $options{data}->{server}->{poller}->{$poller_id}->{running} . "\n"; + $output .= " + Start time: " . $options{data}->{server}->{poller}->{$poller_id}->{start_time} . "\n"; + $output .= " + Last alive: " . $options{data}->{server}->{poller}->{$poller_id}->{last_alive} . "\n"; + $output .= " + Uptime: " . $options{data}->{server}->{poller}->{$poller_id}->{uptime} . "\n"; + $output .= " + Count Hosts/Services - (Last command check): " . $options{data}->{server}->{poller}->{$poller_id}->{hosts} . "/" . $options{data}->{server}->{poller}->{$poller_id}->{services} . " - (" . $options{data}->{server}->{poller}->{$poller_id}->{last_command_check} . ")\n\n"; + + } else { + $output .= " + SKIP Identity for this poller, enabled but does not seems to work correctly ! \n\n"; + } + + $output .= "####Engine stats: \n"; + foreach my $stat_key (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}}) { + foreach my $stat_value (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}}) { + $output .= " + " . $stat_key . "(" . $stat_value . "): " . $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}->{$stat_value} . "\n"; + } + } + $output .= "\n"; + + $output .= "####Broker stats: \n"; + foreach my $broker_stat_file (sort keys %{$options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { + $output .= "#####File: " . $broker_stat_file . "\n"; + $output .= " + Version: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{version} . "\n"; + $output .= " + State: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{state} . "\n"; + $output .= " + Event proecessing speed " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{event_processing_speed} . "\n"; + $output .= " + Queued events: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{queued_events} . "\n"; + $output .= " + Last connection attempts: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{last_connection_attempt} . "\n"; + $output .= " + Last connection success: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{last_connection_success} . "\n\n"; + } + + $output .= "####System stats: \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage}) ? + " + CPU => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage} . "\n" : + " + CPU => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load}) ? + " + LOAD => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{load} . "\n" : + " + LOAD => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage}) ? + " + MEMORY => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{mem_usage} . "\n" : + " + MEMORY => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage}) ? + " + SWAP => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{swap_usage} . "\n" : + " + SWAP => Could not gather data \n"; + $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage}) ? + " + STORAGE => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{storage_usage} . "\n\n" : + " + STORAGE => Could not gather data \n\n"; + + if ($options{flag_logs} != 1 || $options{flag_logs} eq "") { + $output .= "##LOGS LAST LINES: \n\n"; + foreach my $log_topic (sort keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { + foreach my $log_file (keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}}) { + $output .= " + " . $log_file . " (" . $log_topic . ")\n\n"; + $output .= $options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}->{$log_file} . "\n\n"; + } + $output .= "\n"; + } + } + } + return $output; } + sub run { my $self = shift; my ($data, $format, $flag_rrd, $flag_db, $flash_logs) = @_; @@ -154,6 +295,9 @@ sub run { } elsif ($format eq "TEXT") { my $output = $self->output_text(data => $data, flag_rrd => $flag_rrd, flag_db => $flag_db, flag_logs => $flash_logs); print $output; + } elsif ($format eq "MARKDOWN") { + my $output = $self->output_markdown(data => $data, flag_rrd => $flag_rrd, flag_db => $flag_db, flag_logs => $flash_logs); + print $output; } elsif ($format eq "DUMPER") { use Data::Dumper; print Dumper($data); diff --git a/lib/perl/centreon/health/ssh.pm b/lib/perl/centreon/health/ssh.pm index 0ea31159f8b..870c4c37e52 100644 --- a/lib/perl/centreon/health/ssh.pm +++ b/lib/perl/centreon/health/ssh.pm @@ -46,7 +46,7 @@ sub ssh_callback { chomp($options{stdout}); $command_results->{multiple}->{$options{userdata}} = $options{stdout}; } else { - $command_results->{multiple}->{$options{userdata}} = "Failed action on ssh or plugin"; + $command_results->{multiple}->{failed_action} = "Failed action on ssh or plugin"; return -1 } return 0 From c7ef3968f20dad4d3c7b31274e0b648bee8f13b2 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 4 Jan 2018 08:27:16 +0100 Subject: [PATCH 11/17] - remove unimplemented option --- bin/centreon_health | 4 ---- 1 file changed, 4 deletions(-) diff --git a/bin/centreon_health b/bin/centreon_health index bf6441dfdc7..32d26941661 100755 --- a/bin/centreon_health +++ b/bin/centreon_health @@ -46,10 +46,6 @@ Specify output type. Can be: 'MARKDOWN', 'JSON', 'TEXT' or 'DUMPER' (last if for 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 -=item B<--anonymous> - -Anonymize sensitive information from output for easier sharing (to implement - no effect for now) - =head1 DESCRIPTION B will gather some informations about centreon global metrics From 4d7fa58204583d2c75abc00fd618c07e0c168839 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 4 Jan 2018 09:13:12 +0100 Subject: [PATCH 12/17] + add few things to db check --- lib/perl/centreon/health/checkdb.pm | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/perl/centreon/health/checkdb.pm b/lib/perl/centreon/health/checkdb.pm index 0230aaf9c71..b097773900c 100644 --- a/lib/perl/centreon/health/checkdb.pm +++ b/lib/perl/centreon/health/checkdb.pm @@ -72,8 +72,23 @@ sub run { } - return $self->{output}; + 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}; } From 5562074271dba034d7e9e89cffb41b5d78de712a Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 4 Jan 2018 09:20:12 +0100 Subject: [PATCH 13/17] + make markdown github compliant --- lib/perl/centreon/health/output.pm | 34 +++++++++++++++--------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/lib/perl/centreon/health/output.pm b/lib/perl/centreon/health/output.pm index 3ca1f3f0c92..0b5ff667b74 100644 --- a/lib/perl/centreon/health/output.pm +++ b/lib/perl/centreon/health/output.pm @@ -163,8 +163,8 @@ sub output_markdown { my $output; - $output = "#CENTREON_HEALTH TEXT OUTPUT\n"; - $output .= "##CENTREON OVERVIEW\n\n"; + $output = "# CENTREON_HEALTH TEXT OUTPUT\n"; + $output .= "## CENTREON OVERVIEW\n\n"; $output .= " + Centreon Version: " . $options{data}->{server}->{global}->{centreon_version} . "\n"; $output .= " + Number of pollers: " . $options{data}->{server}->{global}->{count_poller} . "\n"; $output .= " + Number of hosts: " . $options{data}->{server}->{global}->{count_hosts} . "\n"; @@ -173,14 +173,14 @@ sub output_markdown { $output .= " + Number of modules: " . $options{data}->{server}->{global}->{count_modules} . "\n"; $output .= defined($options{data}->{server}->{global}->{count_pp}) ? " + Number of plugin-packs: " . $options{data}->{server}->{global}->{count_pp} . "\n" : " + Number of plugin-packs: N/A\n"; $output .= " + Number of recurrent downtimes: " . $options{data}->{server}->{global}->{count_downtime} . "\n\n"; - $output .= "##AVERAGE METRICS\n\n"; + $output .= "## AVERAGE METRICS\n\n"; $output .= " + Host / poller (avg): " . $options{data}->{server}->{global}->{hosts_by_poller_avg} . "\n"; $output .= " + Service / poller (avg): " . $options{data}->{server}->{global}->{services_by_poller_avg} . "\n"; $output .= " + Service / host (avg): " . $options{data}->{server}->{global}->{services_by_host_avg} . "\n"; $output .= " + Metrics / service (avg): " . $options{data}->{server}->{global}->{metrics_by_service_avg} . "\n\n"; if ($options{flag_rrd} != 1 || $options{flag_db} eq "") { - $output .= "##RRD INFORMATIONS\n\n"; + $output .= "## RRD INFORMATIONS\n\n"; $output .= " + RRD not updated since more than 180 days: " . $options{data}->{rrd}->{rrd_not_updated_since_180d} . "\n"; $output .= " + RRD written during last 5 five minutes: " . $options{data}->{rrd}->{rrd_written_last_5m} . "\n"; foreach my $key (sort keys %{$options{data}->{rrd}}) { @@ -191,35 +191,35 @@ sub output_markdown { } if ($options{flag_db} != 1 || $options{flag_db} eq "") { - $output .= "##DATABASES INFORMATIONS\n\n"; - $output .= "###Databases size\n\n"; + $output .= "## DATABASES INFORMATIONS\n\n"; + $output .= "### Databases size\n\n"; foreach my $database (keys %{$options{data}->{database}->{db_size}}) { $output .= " + Size of " . $database . " database: " . $options{data}->{database}->{db_size}->{$database} . "\n"; } $output .= "\n"; - $output .= "###Tables size (centreon_storage db)\n\n"; + $output .= "### Tables size (centreon_storage db)\n\n"; foreach my $database (keys %{$options{data}->{database}->{table_size}}) { $output .= " + Size of " . $database . " table: " . $options{data}->{database}->{table_size}->{$database} . "\n"; } $output .= "\n"; - $output .= "###Partitioning check\n\n"; + $output .= "### Partitioning check\n\n"; foreach my $table (keys %{$options{data}->{database}->{partitioning_last_part}}) { $output .= " + Last partition date for " . $table . " table: " . $options{data}->{database}->{partitioning_last_part}->{$table} . "\n"; } $output .= "\n"; } - $output .= "##MODULE INFORMATIONS\n\n"; + $output .= "## MODULE INFORMATIONS\n\n"; foreach my $module_key (keys %{$options{data}->{module}}) { $output .= " + Module " . $options{data}->{module}->{$module_key}->{full_name} . " is installed. (Author: " . $options{data}->{module}->{$module_key}->{author} . " # Codename: " . $module_key . " # Version: " . $options{data}->{module}->{$module_key}->{version} . ")\n"; } $output .= "\n"; - $output .= "##CENTREON NODES INFORMATIONS\n\n"; + $output .= "## CENTREON NODES INFORMATIONS\n\n"; foreach my $poller_id (keys %{$options{data}->{server}->{poller}}) { - $output .= "###" . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; - $output .= "####Identity: \n"; + $output .= "### " . $options{data}->{server}->{poller}->{$poller_id}->{name} . "\n\n"; + $output .= "#### Identity: \n"; if (defined($options{data}->{server}->{poller}->{$poller_id}->{engine}) && defined($options{data}->{server}->{poller}->{$poller_id}->{version})) { $output .= " + Engine (version): " . $options{data}->{server}->{poller}->{$poller_id}->{engine} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{version} . ")\n"; $output .= " + IP Address (SSH port): " . $options{data}->{server}->{poller}->{$poller_id}->{address} . " (" . $options{data}->{server}->{poller}->{$poller_id}->{ssh_port} . ")\n"; @@ -234,7 +234,7 @@ sub output_markdown { $output .= " + SKIP Identity for this poller, enabled but does not seems to work correctly ! \n\n"; } - $output .= "####Engine stats: \n"; + $output .= "#### Engine stats: \n"; foreach my $stat_key (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}}) { foreach my $stat_value (sort keys %{$options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}}) { $output .= " + " . $stat_key . "(" . $stat_value . "): " . $options{data}->{server}->{poller}->{$poller_id}->{engine_stats}->{$stat_key}->{$stat_value} . "\n"; @@ -242,9 +242,9 @@ sub output_markdown { } $output .= "\n"; - $output .= "####Broker stats: \n"; + $output .= "#### Broker stats: \n"; foreach my $broker_stat_file (sort keys %{$options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { - $output .= "#####File: " . $broker_stat_file . "\n"; + $output .= "##### File: " . $broker_stat_file . "\n"; $output .= " + Version: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{version} . "\n"; $output .= " + State: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{state} . "\n"; $output .= " + Event proecessing speed " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{event_processing_speed} . "\n"; @@ -253,7 +253,7 @@ sub output_markdown { $output .= " + Last connection success: " . $options{data}->{broker}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$broker_stat_file}->{last_connection_success} . "\n\n"; } - $output .= "####System stats: \n"; + $output .= "#### System stats: \n"; $output .= defined($options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage}) ? " + CPU => " . $options{data}->{systems}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{cpu_usage} . "\n" : " + CPU => Could not gather data \n"; @@ -271,7 +271,7 @@ sub output_markdown { " + STORAGE => Could not gather data \n\n"; if ($options{flag_logs} != 1 || $options{flag_logs} eq "") { - $output .= "##LOGS LAST LINES: \n\n"; + $output .= "## LOGS LAST LINES: \n\n"; foreach my $log_topic (sort keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}}) { foreach my $log_file (keys %{$options{data}->{logs}->{$options{data}->{server}->{poller}->{$poller_id}->{name}}->{$log_topic}}) { $output .= " + " . $log_file . " (" . $log_topic . ")\n\n"; From 465ce9b0059b179e1bdbf235d3e5ef63711103b3 Mon Sep 17 00:00:00 2001 From: Sims24 Date: Thu, 18 Jan 2018 17:08:02 +0100 Subject: [PATCH 14/17] handle query errors --- lib/perl/centreon/health/checkservers.pm | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm index 212e6035da8..3121b92ebde 100644 --- a/lib/perl/centreon/health/checkservers.pm +++ b/lib/perl/centreon/health/checkservers.pm @@ -38,7 +38,11 @@ sub query_misc { my ($self, %options) = @_; my ($sth, $status); - $sth = $options{cdb}->query($options{query}); + if ($status == -1) { + return "Query error - information is not available\n"; + } else { + return $sth->fetchrow() + } return $sth->fetchrow() } From 7595180e7c461c7e2d70cff6bf8e0447028b88c4 Mon Sep 17 00:00:00 2001 From: Simon Bomm Date: Fri, 19 Jan 2018 11:00:57 +0100 Subject: [PATCH 15/17] + really launch query .. --- lib/perl/centreon/health/checkservers.pm | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm index 3121b92ebde..85200da2f37 100644 --- a/lib/perl/centreon/health/checkservers.pm +++ b/lib/perl/centreon/health/checkservers.pm @@ -38,13 +38,14 @@ sub query_misc { my ($self, %options) = @_; my ($sth, $status); + ($status, $sth) = $options{cdb}->query($options{query}); + if ($status == -1) { return "Query error - information is not available\n"; } else { return $sth->fetchrow() } - return $sth->fetchrow() } sub get_servers_informations { From 67f3dd5172e9eea868ec13ac0d5f9d0855b51914 Mon Sep 17 00:00:00 2001 From: Simon Bomm Date: Fri, 19 Jan 2018 11:12:50 +0100 Subject: [PATCH 16/17] + better compat with old perl version --- lib/perl/centreon/health/checkdb.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/perl/centreon/health/checkdb.pm b/lib/perl/centreon/health/checkdb.pm index b097773900c..d3d23a00a72 100644 --- a/lib/perl/centreon/health/checkdb.pm +++ b/lib/perl/centreon/health/checkdb.pm @@ -82,7 +82,7 @@ sub run { 'read_rnd_buffer_size' => 1, 'max_allowed_packet' => 1 }; - foreach my $var (keys $var_list) { + 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; From c5e3b9df65f6f23ca118cd6255fc2b39254f2a63 Mon Sep 17 00:00:00 2001 From: cgagnaire Date: Thu, 12 Jul 2018 15:18:02 +0200 Subject: [PATCH 17/17] + fix division by zero, fix empty value --- lib/perl/centreon/health/checkservers.pm | 29 ++++++++++++------------ 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/perl/centreon/health/checkservers.pm b/lib/perl/centreon/health/checkservers.pm index 85200da2f37..bb81f9037c1 100644 --- a/lib/perl/centreon/health/checkservers.pm +++ b/lib/perl/centreon/health/checkservers.pm @@ -57,7 +57,7 @@ sub get_servers_informations { $self->{output}->{poller}->{$row->{id}}{name} = $row->{name}; $self->{output}->{poller}->{$row->{id}}{localhost} = ($row->{localhost} == 1) ? "YES" : "NO"; $self->{output}->{poller}->{$row->{id}}{address} = $row->{ns_ip_address}; - $self->{output}->{poller}->{$row->{id}}{ssh_port} = $row->{ssh_port}; + $self->{output}->{poller}->{$row->{id}}{ssh_port} = (defined($row->{ssh_port})) ? $row->{ssh_port} : "-"; $self->{output}->{poller}->{$row->{id}}{id} = $row->{id}; } @@ -98,18 +98,17 @@ sub get_servers_informations { } $sth = $options{csdb}->query("SELECT stat_key, stat_value, stat_label - FROM nagios_stats - WHERE instance_id = " . $options{cdb}->quote($id) . ""); + FROM nagios_stats + WHERE instance_id = " . $options{cdb}->quote($id) . ""); while (my $row = $sth->fetchrow_hashref()) { - $self->{output}->{poller}->{$id}->{engine_stats}->{$row->{stat_label}}->{$row->{stat_key}} = $row->{stat_value}; + $self->{output}->{poller}->{$id}->{engine_stats}->{$row->{stat_label}}->{$row->{stat_key}} = $row->{stat_value}; } - $self->{output}->{global}->{hosts_by_poller_avg} = $self->{output}->{global}->{count_hosts}/$self->{output}->{global}->{count_poller}; - $self->{output}->{global}->{services_by_poller_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_poller}; - $self->{output}->{global}->{services_by_host_avg} = $self->{output}->{global}->{count_services}/$self->{output}->{global}->{count_hosts}; - $self->{output}->{global}->{metrics_by_service_avg} = $self->{output}->{global}->{count_metrics}/$self->{output}->{global}->{count_services}; - + $self->{output}->{global}->{hosts_by_poller_avg} = ($self->{output}->{global}->{count_poller} != 0) ? $self->{output}->{global}->{count_hosts} / $self->{output}->{global}->{count_poller} : '0'; + $self->{output}->{global}->{services_by_poller_avg} = ($self->{output}->{global}->{count_poller} != 0) ? $self->{output}->{global}->{count_services} / $self->{output}->{global}->{count_poller} : '0'; + $self->{output}->{global}->{services_by_host_avg} = ($self->{output}->{global}->{count_hosts} != 0) ? $self->{output}->{global}->{count_services} / $self->{output}->{global}->{count_hosts} : '0'; + $self->{output}->{global}->{metrics_by_service_avg} = ($self->{output}->{global}->{count_services} != 0) ? $self->{output}->{global}->{count_metrics} / $self->{output}->{global}->{count_services} : '0'; } } @@ -119,19 +118,19 @@ sub run { my ($centreon_db, $centstorage_db, $centreon_version) = @_; my $query_misc = { count_pp => [$centreon_db, $centreon_version eq '2.7' ? "SELECT count(*) FROM mod_pluginpack" : "SELECT count(*) FROM mod_ppm_pluginpack"], - count_downtime => [$centreon_db, "SELECT count(*) FROM downtime"], - count_modules => [$centreon_db, "SELECT count(*) FROM modules_informations"], - centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], - count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; + count_downtime => [$centreon_db, "SELECT count(*) FROM downtime"], + count_modules => [$centreon_db, "SELECT count(*) FROM modules_informations"], + centreon_version => [$centreon_db, "SELECT value FROM informations LIMIT 1"], + count_metrics => [$centstorage_db, "SELECT count(*) FROM metrics"] }; foreach my $info (keys %$query_misc) { my $result = $self->query_misc(cdb => $query_misc->{$info}[0], - query => $query_misc->{$info}[1] ); + query => $query_misc->{$info}[1] ); $self->{output}->{global}->{$info} = $result; } $self->get_servers_informations(cdb => $centreon_db, csdb => $centstorage_db); - + return $self->{output} }