diff --git a/centreon-plugins/apps/centreon/sql/mode/countservices.pm b/centreon-plugins/apps/centreon/sql/mode/countservices.pm index 796c40714..e3f38f773 100644 --- a/centreon-plugins/apps/centreon/sql/mode/countservices.pm +++ b/centreon-plugins/apps/centreon/sql/mode/countservices.pm @@ -25,39 +25,92 @@ use base qw(centreon::plugins::templates::counter); use strict; use warnings; -sub set_counters { +sub poller_long_output { my ($self, %options) = @_; - - $self->{maps_counters_type} = [ - { name => 'poller', type => 1, cb_prefix_output => 'prefix_poller_output', message_multiple => 'All poller hosts/services are ok' } - ]; - - $self->{maps_counters}->{poller} = [ - { label => 'host', nlabel => 'centreon.hosts.count', set => { - key_values => [ { name => 'hosts' }, { name => 'display' } ], - output_template => 'Number of hosts : %s', - perfdatas => [ - { label => 'total_hosts', value => 'hosts', template => '%s', - min => 0, label_extra_instance => 1 }, - ], - } - }, - { label => 'service', nlabel => 'centreon.services.count', set => { - key_values => [ { name => 'services' }, { name => 'display' } ], - output_template => 'Number of services : %s', - perfdatas => [ - { label => 'total_services', value => 'services', template => '%s', - min => 0, label_extra_instance => 1 }, - ], - } - }, - ]; + + return sprintf( + "checking poller '%s'", + $options{instance} + ); } sub prefix_poller_output { my ($self, %options) = @_; - - return "Poller '" . $options{instance_value}->{display} . "' "; + + return sprintf( + "poller '%s' ", + $options{instance} + ); +} + +sub prefix_host_output { + my ($self, %options) = @_; + + return "number of hosts "; +} + +sub prefix_service_output { + my ($self, %options) = @_; + + return "number of services "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { + name => 'pollers', type => 3, cb_prefix_output => 'prefix_poller_output', cb_long_output => 'poller_long_output', indent_long_output => ' ', message_multiple => 'All pollers are ok', + group => [ + { name => 'host', type => 0, cb_prefix_output => 'prefix_host_output', skipped_code => { -10 => 1 } }, + { name => 'service', type => 0, cb_prefix_output => 'prefix_service_output', skipped_code => { -10 => 1 } } + ] + } + ]; + + $self->{maps_counters}->{host} = [ + { label => 'host', nlabel => 'centreon.hosts.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + } + ]; + foreach ('up', 'down', 'unreachable', 'pending') { + push @{$self->{maps_counters}->{host}}, { + label => 'hosts-' . $_, nlabel => 'centreon.hosts.' . $_ . '.count', set => { + key_values => [ { name => $_ } ], + output_template => $_ . ': %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }; + } + + $self->{maps_counters}->{service} = [ + { label => 'service', nlabel => 'centreon.services.count', set => { + key_values => [ { name => 'total' } ], + output_template => 'total: %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + } + ]; + foreach ('ok', 'warning', 'critical', 'unknown', 'pending') { + push @{$self->{maps_counters}->{service}}, { + label => 'services-' . $_, nlabel => 'centreon.services.' . $_ . '.count', set => { + key_values => [ { name => $_ } ], + output_template => $_ . ': %s', + perfdatas => [ + { template => '%s', min => 0, label_extra_instance => 1 } + ] + } + }; + } } sub new { @@ -79,22 +132,64 @@ sub manage_selection { my $query = "SELECT instances.name, COUNT(DISTINCT hosts.host_id) as num_hosts, count(DISTINCT services.host_id, services.service_id) as num_services FROM " . $self->{option_results}->{centreon_storage_database} . ".instances, " . $self->{option_results}->{centreon_storage_database} . ".hosts, " . $self->{option_results}->{centreon_storage_database} . ".services WHERE instances.running = '1' AND instances.instance_id = hosts.instance_id AND hosts.enabled = '1' AND hosts.host_id = services.host_id AND services.enabled = '1' GROUP BY hosts.instance_id"; $options{sql}->connect(); - $options{sql}->query(query => $query); + $options{sql}->query( + query => 'SELECT + i.name, + COALESCE(SUM(CASE WHEN h.state = 0 THEN 1 ELSE 0 END), 0) AS up, + COALESCE(SUM(CASE WHEN h.state = 1 THEN 1 ELSE 0 END), 0) AS down, + COALESCE(SUM(CASE WHEN h.state = 2 THEN 1 ELSE 0 END), 0) AS unreachable, + COALESCE(SUM(CASE WHEN h.state = 4 THEN 1 ELSE 0 END), 0) AS pending, + count(*) as total + FROM ' . $self->{option_results}->{centreon_storage_database} . '.hosts h, ' . $self->{option_results}->{centreon_storage_database} . '.instances i + WHERE i.running = 1 + AND h.instance_id = i.instance_id + AND h.enabled = 1 + AND h.name NOT LIKE "\_Module\_%" + GROUP BY h.instance_id' + ); # check by poller - $self->{poller} = {}; + $self->{pollers} = {}; while ((my $row = $options{sql}->fetchrow_hashref())) { if (defined($self->{option_results}->{filter_poller}) && $self->{option_results}->{filter_poller} ne '' && $row->{name} !~ /$self->{option_results}->{filter_poller}/) { - $self->{output}->output_add(long_msg => "Skipping '" . $row->{name} . "': no matching filter.", debug => 1); + $self->{output}->output_add(long_msg => "skipping '" . $row->{name} . "': no matching filter.", debug => 1); next; } - - $self->{poller}->{$row->{name}} = { display => $row->{name}, hosts => $row->{num_hosts}, services => $row->{num_services} }; + + $self->{pollers}->{ $row->{name} } = { host => $row }; + } + + $options{sql}->query( + query => 'SELECT + i.name, + COALESCE(SUM(CASE WHEN s.state = 0 THEN 1 ELSE 0 END), 0) AS ok, + COALESCE(SUM(CASE WHEN s.state = 1 THEN 1 ELSE 0 END), 0) AS warning, + COALESCE(SUM(CASE WHEN s.state = 2 THEN 1 ELSE 0 END), 0) AS critical, + COALESCE(SUM(CASE WHEN s.state = 3 THEN 1 ELSE 0 END), 0) AS unknown, + COALESCE(SUM(CASE WHEN s.state = 4 THEN 1 ELSE 0 END), 0) AS pending, + count(*) as total + FROM ' . $self->{option_results}->{centreon_storage_database} . '.hosts h, ' . $self->{option_results}->{centreon_storage_database} . '.services s, ' . $self->{option_results}->{centreon_storage_database} . '.instances i + WHERE i.running = 1 + AND h.instance_id = i.instance_id + AND h.enabled = 1 + AND (h.name NOT LIKE "\_Module\_%" OR h.name LIKE "\_Module\_Meta%") + AND s.enabled = 1 + AND h.host_id = s.host_id + GROUP BY h.instance_id' + ); + while ((my $row = $options{sql}->fetchrow_hashref())) { + if (defined($self->{option_results}->{filter_poller}) && $self->{option_results}->{filter_poller} ne '' && + $row->{name} !~ /$self->{option_results}->{filter_poller}/) { + $self->{output}->output_add(long_msg => "skipping '" . $row->{name} . "': no matching filter.", debug => 1); + next; + } + + $self->{pollers}->{ $row->{name} }->{service} = $row; } - if (scalar(keys %{$self->{poller}}) == 0) { - $self->{output}->add_option_msg(short_msg => "No poller found."); + if (scalar(keys %{$self->{pollers}}) == 0) { + $self->{output}->add_option_msg(short_msg => "No poller found"); $self->{output}->option_exit(); } } @@ -109,29 +204,25 @@ Check the number of hosts/services by poller. =over 8 -=item B<--centreon-storage-database> - -Centreon storage database name (default: 'centreon_storage'). - =item B<--filter-counters> Only display some counters (regexp can be used). -Example: --filter-counters='^services$' +Example: --filter-counters='service' -=item B<--warning-*> +=item B<--centreon-storage-database> -Threshold warning. -Can be: 'host', 'service'. - -=item B<--critical-*> - -Threshold critical. -Can be: Can be: 'host', 'service'. +Centreon storage database name (default: 'centreon_storage'). =item B<--filter-poller> Filter by poller name (regexp can be used). +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'service', 'services-ok', 'services-warning', 'services-critical', 'services-unknown', 'services-pending', +'host', 'hosts-up', 'hosts-down', 'hosts-unreachable', 'hosts-pending'. + =back =cut