diff --git a/database/db2/dbi.pm b/database/db2/dbi.pm new file mode 100644 index 000000000..434c07221 --- /dev/null +++ b/database/db2/dbi.pm @@ -0,0 +1,42 @@ +# +# Copyright 2021 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 database::db2::dbi; + +use base qw(centreon::plugins::dbi); + +use strict; +use warnings; + +sub connect { + my ($self, %options) = @_; + + if ($self->{data_source} =~ /PROTOCOL=TCPIP/) { + $self->{data_source} .= 'UID=' . $self->{username} . ';' + if ($self->{data_source} !~ /UID/ && defined($self->{username})); + $self->{data_source} .= 'PWD=' . $self->{password} . ';' + if ($self->{data_source} !~ /PWD/ && defined($self->{password})); + } + $self->SUPER::connect(%options); +} + +1; + +__END__ diff --git a/database/db2/mode/connectedusers.pm b/database/db2/mode/connectedusers.pm new file mode 100644 index 000000000..761bafc13 --- /dev/null +++ b/database/db2/mode/connectedusers.pm @@ -0,0 +1,98 @@ +# +# Copyright 2021 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 database::db2::mode::connectedusers; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + "warning:s" => { name => 'warning', }, + "critical:s" => { name => 'critical', }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); + + if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); + $self->{output}->option_exit(); + } + if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { + $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); + $self->{output}->option_exit(); + } +} + +sub run { + my ($self, %options) = @_; + # $options{sql} = sqlmode object + $self->{sql} = $options{sql}; + + $self->{sql}->connect(); + $self->{sql}->query(query => q{SELECT COUNT(*) FROM v$session WHERE type = 'USER'}); + my $users = $self->{sql}->fetchrow_array(); + $self->{sql}->disconnect(); + + my $exit_code = $self->{perfdata}->threshold_check(value => $users, threshold => [ { label => 'critical', 'exit_litteral' => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); + $self->{output}->output_add(severity => $exit_code, + short_msg => sprintf("%i Connected user(s).", $users)); + $self->{output}->perfdata_add(label => 'connected_users', + value => $users, + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), + min => 0); + + $self->{output}->display(); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +Check Oracle connected users. + +=over 8 + +=item B<--warning> + +Threshold warning. + +=item B<--critical> + +Threshold critical. + +=back + +=cut diff --git a/database/db2/plugin.pm b/database/db2/plugin.pm new file mode 100644 index 000000000..5b2ac4790 --- /dev/null +++ b/database/db2/plugin.pm @@ -0,0 +1,107 @@ +# +# Copyright 2021 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 database::db2::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_sql); + +sub new { + my ($class, %options) = @_; + + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + $self->{modes} = { + 'collection' => 'centreon::common::protocols::sql::mode::collection', + 'connection-time' => 'centreon::common::protocols::sql::mode::connectiontime', + 'connected-users' => 'database::db2::mode::connectedusers' + }; + + $self->{sql_modes}->{dbi} = 'database::db2::dbi'; + + return $self; +} + +sub init { + my ($self, %options) = @_; + + $self->{options}->add_options( + arguments => { + 'server:s@' => { name => 'server' }, + 'port:s@' => { name => 'port' }, + 'database:s' => { name => 'database' } + } + ); + $self->{options}->parse_options(); + my $options_result = $self->{options}->get_options(); + $self->{options}->clean(); + + if (defined($options_result->{server})) { + $self->{sqldefault}->{dbi} = []; + for (my $i = 0; $i < scalar(@{$options_result->{server}}); $i++) { + next if ($options_result->{server}->[$i] eq ''); + + $self->{sqldefault}->{dbi}->[$i] = { data_source => 'DB2:PROTOCOL=TCPIP;hostname=' . $options_result->{server}->[$i] }; + if (!defined($options_result->{database}) || $options_result->{database} eq '') { + $self->{output}->add_option_msg(short_msg => 'Need to specify --database option'); + $self->{output}->option_exit(); + } + if (!defined($options_result->{port}->[$i]) || $options_result->{port}->[$i] eq '') { + $self->{output}->add_option_msg(short_msg => 'Need to specify --port option'); + $self->{output}->option_exit(); + } + $self->{sqldefault}->{dbi}->[$i]->{data_source} .= ';PORT=' . $options_result->{port}->[$i]; + $self->{sqldefault}->{dbi}->[$i]->{data_source} .= ';DATABASE=' . $options_result->{database} . ';'; + } + } elsif (defined($options_result->{database}) && $options_result->{database} ne '') { + $self->{sqldefault}->{dbi} = [ { data_source => 'DB2;' . $options_result->{database} } ]; + } + + $self->SUPER::init(%options); +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check DB2 Server. + +=over 8 + +=item B<--server> + +Domain name or IP address of the Db2 database system (Uncataloged database connections) + +=item B<--port> + +TCP/IP server port number that is assigned to the Db2 database system + +=item B<--database> + +Name for the Db2 database system. If --server is not set, it's a cataloged connection (database alias). + +=back + +=cut