diff --git a/centreon-plugins/apps/protocols/cifs/custom/libcifs.pm b/centreon-plugins/apps/protocols/cifs/custom/libcifs.pm new file mode 100644 index 000000000..5b5e8b692 --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/custom/libcifs.pm @@ -0,0 +1,230 @@ +# +# Copyright 2022 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 apps::protocols::cifs::custom::libcifs; + +use strict; +use warnings; +use Filesys::SmbClient; +use POSIX; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'timeout:s' => { name => 'timeout' }, + 'port:s' => { name => 'port' }, + 'workgroup:s' => { name => 'workgroup' }, + 'cifs-username:s' => { name => 'cifs_username' }, + 'cifs-password:s' => { name => 'cifs_password' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'CIFS OPTIONS', once => 1); + + $self->{output} = $options{output}; + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 139; + $self->{timeout} = (defined($self->{option_results}->{timeout})) && $self->{option_results}->{timeout} =~ /^\d+$/? $self->{option_results}->{timeout} : 30; + $self->{cifs_username} = (defined($self->{option_results}->{cifs_username})) ? $self->{option_results}->{cifs_username} : ''; + $self->{cifs_password} = (defined($self->{option_results}->{cifs_password})) ? $self->{option_results}->{cifs_password} : ''; + $self->{workgroup} = (defined($self->{option_results}->{workgroup})) ? $self->{option_results}->{workgroup} : ''; + + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Please set option --hostname."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub init_cifs { + my ($self, %options) = @_; + + if (!defined($self->{cifs})) { + $self->{cifs} = new Filesys::SmbClient( + username => $self->{cifs_username}, + password => $self->{cifs_password}, + workgroup => $self->{workgroup}, + debug => $self->{output}->is_debug() ? 10 : 0 + ); + } +} + +sub list_directory { + my ($self, %options) = @_; + + $self->init_cifs(); + + my $fd = $self->{cifs}->opendir('smb://' . $self->{hostname} . (defined($options{directory}) ? $options{directory} : '')); + if (!defined($fd)) { + return (1, $!); + } + + my $files = []; + while (my $file = $self->{cifs}->readdir_struct($fd)) { + push @$files, $file; + } + $self->{cifs}->close($fd); + + return (0, '', $files); +} + +sub read_file { + my ($self, %options) = @_; + + $self->init_cifs(); + + my $fd = $self->{cifs}->open('smb://' . $self->{hostname} . $options{file}, 0600); + if (!defined($fd)) { + return (1, "Can't read file: $!"); + } + + my $data = ''; + while (1) { + my $buffer = $self->{cifs}->read($fd); + last if (!defined($buffer) || $buffer eq ''); + + $data .= $buffer; + } + + $self->{cifs}->close($fd); + return (0, '', $data); +} + +sub write_file { + my ($self, %options) = @_; + + $self->init_cifs(); + + my $fd = $self->{cifs}->open('>smb://' . $self->{hostname} . $options{file}, 0600); + if (!defined($fd)) { + return (1, "Can't create file: $!"); + } + + my $data = defined($options{content}) ? $options{content} : ''; + if (!$self->{cifs}->write($fd, $data)) { + return (1, $!); + } + + $self->{cifs}->close($fd); + + return (0, ''); +} + +sub delete_file { + my ($self, %options) = @_; + + $self->init_cifs(); + + if ($self->{cifs}->unlink('smb://' . $self->{hostname} . $options{file}) == 0) { + return (1, "Can't unlink file: $!"); + } + + return (0, ''); +} + +sub stat_file { + my ($self, %options) = @_; + + $self->init_cifs(); + + my @stat = $self->{cifs}->stat('smb://' . $self->{hostname} .$options{file}); + if ($#stat == 0) { + return { code => 1, message => "Stat error: $!" }; + } + + return { code => 0, mtime => $stat[11] }; +} + +1; + +__END__ + +=head1 NAME + +CIFS connector library + +=head1 SYNOPSIS + +my cifs connector + +=head1 CIFS OPTIONS + +=over 8 + +=item B<--hostname> + +Set server hostname (required). + +=item B<--port> + +Set port. + +=item B<--timeout> + +Timeout in seconds for connection (Defaults: 30) + +=item B<--workgroup> + +Set workgroup. + +=item B<--cifs-username> + +Set username. + +=item B<--cifs-password> + +Set password. + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/apps/protocols/cifs/mode/connection.pm b/centreon-plugins/apps/protocols/cifs/mode/connection.pm new file mode 100644 index 000000000..8d2f9d3a5 --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/mode/connection.pm @@ -0,0 +1,129 @@ +# +# Copyright 2022 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 apps::protocols::cifs::mode::connection; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Time::HiRes qw(gettimeofday tv_interval); +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf('%s', $self->{result_values}->{message}); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, message_separator => ' - ' } + ]; + + $self->{maps_counters}->{global} = [ + { + label => 'status', + type => 2, + critical_default => '%{message} !~ /authentication succeeded/i', + set => { + key_values => [ { name => 'status' }, { name => 'message' } ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'time', nlabel => 'connection.time.seconds' , set => { + key_values => [ { name => 'time_elapsed' } ], + output_template => 'connection time: %.3fs', + perfdatas => [ + { template => '%.3f', unit => 's', min => 0 } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'directory:s' => { name => 'directory' } + }); + + return $self; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $timing0 = [gettimeofday]; + my ($rv, $message) = $options{custom}->list_directory(share => $self->{option_results}->{share}); + if ($rv == 0) { + $message = 'authentication succeeded'; + } + + my $timeelapsed = tv_interval($timing0, [gettimeofday]); + $self->{global} = { + status => $rv, + message => $message, + time_elapsed => $timeelapsed + }; +} + +1; + +__END__ + +=head1 MODE + +Check cifs connection and read directory. + +=over 8 + +=item B<--directory> + +Set the share directory. + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{message} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{message} !~ /authentication succeeded/i' +Can used special variables like: %{status}, %{message} + +=item B<--warning-time> + +Threshold warning in seconds. + +=item B<--critical-time> + +Threshold critical in seconds. + +=back + +=cut diff --git a/centreon-plugins/apps/protocols/cifs/mode/filescount.pm b/centreon-plugins/apps/protocols/cifs/mode/filescount.pm new file mode 100644 index 000000000..c979663f8 --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/mode/filescount.pm @@ -0,0 +1,164 @@ +# +# Copyright 2022 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 apps::protocols::cifs::mode::filescount; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Filesys::SmbClient; + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, skipped_code => { -10 => 1 } } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'files-detected', nlabel => 'files.detected.count', set => { + key_values => [ { name => 'detected' } ], + output_template => 'number of files: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'directory:s@' => { name => 'directory' }, + 'max-depth:s' => { name => 'max_depth', default => 0 }, + 'filter-file:s' => { name => 'filter_file' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + my $dirs = []; + if (defined($self->{option_results}->{directory})) { + foreach my $dir (@{$self->{option_results}->{directory}}) { + push @$dirs, $dir if ($dir ne ''); + } + } + + if (scalar(@$dirs) == 0) { + $self->{output}->add_option_msg(short_msg => 'Set --directory option'); + $self->{output}->option_exit(); + } + + $self->{option_results}->{directory} = $dirs; +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global} = { detected => 0 }; + $self->countFiles(custom => $options{custom}); +} + +sub countFiles { + my ($self, %options) = @_; + my @listings; + + foreach my $dir (@{$self->{option_results}->{directory}}) { + push @listings, [ { name => $dir, level => 0 } ]; + } + + my @build_name = (); + foreach my $list (@listings) { + while (@$list) { + my @files; + my $hash = pop(@$list); + my $dir = $hash->{name}; + my $level = $hash->{level}; + + my ($rv, $message, $files) = $options{custom}->list_directory(directory => $dir); + if ($rv != 0) { + # Cannot list we skip + next; + } + + foreach my $file (@$files) { + next if ($file->[0] != SMBC_FILE && $file->[0] != SMBC_DIR); + next if ($file->[1] eq '.' || $file->[1] eq '..'); + + my $name = $dir . '/' . $file->[1]; + + if (defined($self->{option_results}->{filter_file}) && $self->{option_results}->{filter_file} ne '' && + $name !~ /$self->{option_results}->{filter_file}/) { + $self->{output}->output_add(long_msg => sprintf("skipping '%s'", $name), debug => 1); + next; + } + + if ($file->[0] == SMBC_DIR) { + if (defined($self->{option_results}->{max_depth}) && $level + 1 <= $self->{option_results}->{max_depth}) { + push @$list, { name => $name, level => $level + 1 }; + } + } else { + $self->{output}->output_add(long_msg => sprintf("Match '%s'", $name)); + $self->{global}->{detected}++; + } + } + } + } +} + +1; + +__END__ + +=head1 MODE + +Count files in a directory (can be recursive). + +=over 8 + +=item B<--directory> + +Check files in the directory (Multiple option) + +=item B<--max-depth> + +Don't check fewer levels (Default: '0'. Means current dir only). + +=item B<--filter-file> + +Filter files (can be a regexp. Directory in the name). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'mtime-last'. + +=back + +=cut diff --git a/centreon-plugins/apps/protocols/cifs/mode/filesdate.pm b/centreon-plugins/apps/protocols/cifs/mode/filesdate.pm new file mode 100644 index 000000000..ebf37b811 --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/mode/filesdate.pm @@ -0,0 +1,226 @@ +# +# Copyright 2022 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 apps::protocols::cifs::mode::filesdate; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Filesys::SmbClient; +use centreon::plugins::misc; +use POSIX; + +my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 }; +my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' }; + +sub custom_mtime_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} }, + instances => $self->{result_values}->{name}, + unit => $self->{instance_mode}->{option_results}->{unit}, + value => floor($self->{result_values}->{mtime_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), + min => 0 + ); +} + +sub custom_mtime_threshold { + my ($self, %options) = @_; + + return $self->{perfdata}->threshold_check( + value => floor($self->{result_values}->{mtime_seconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), + threshold => [ + { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, + { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }, + { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' } + ] + ); +} + +sub prefix_file_output { + my ($self, %options) = @_; + + return "File '" . $options{instance} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'files', type => 1, cb_prefix_output => 'prefix_file_output', message_multiple => 'All files are ok' } + ]; + + $self->{maps_counters}->{files} = [ + { label => 'mtime-last', nlabel => 'file.mtime.last', set => { + key_values => [ { name => 'mtime_seconds' }, { name => 'mtime_human' }, { name => 'name' } ], + output_template => 'last modified %s', + output_use => 'mtime_human', + closure_custom_perfdata => $self->can('custom_mtime_perfdata'), + closure_custom_threshold_check => $self->can('custom_mtime_threshold') + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'directory:s@' => { name => 'directory' }, + 'file:s@' => { name => 'file' }, + 'timezone:s' => { name => 'timezone' }, + 'unit:s' => { name => 'unit', default => 's' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') { + centreon::plugins::misc::mymodule_load( + module => 'DateTime', + error_msg => "Cannot load module 'DateTime'." + ); + } + + if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) { + $self->{option_results}->{unit} = 's'; + } + + my $dirs = []; + if (defined($self->{option_results}->{directory})) { + foreach my $dir (@{$self->{option_results}->{directory}}) { + push @$dirs, $dir if ($dir ne ''); + } + } + + my $files = []; + if (defined($self->{option_results}->{file})) { + foreach my $file (@{$self->{option_results}->{file}}) { + push @$files, $file if ($file ne ''); + } + } + + if (scalar(@$files) == 0 && scalar(@$dirs) == 0) { + $self->{output}->add_option_msg(short_msg => 'Set --file and/or --directory option'); + $self->{output}->option_exit(); + } + + $self->{option_results}->{directory} = $dirs; + $self->{option_results}->{file} = $files; +} + +sub manage_selection { + my ($self, %options) = @_; + + my $ctime = time(); + + $self->{files} = {}; + foreach my $dir (@{$self->{option_results}->{directory}}) { + my ($rv, $message, $files) = $options{custom}->list_directory(directory => $dir); + if ($rv != 0) { + $self->{output}->add_option_msg(short_msg => "cannot read directory '" . $dir . "': " . $message); + $self->{output}->option_exit(); + } + + foreach my $file (@$files) { + next if ($file->[0] != SMBC_FILE && $file->[0] != SMBC_DIR); + next if ($file->[1] eq '.' || $file->[1] eq '..'); + + my $name = $dir . '/' . $file->[1]; + + $rv = $options{custom}->stat_file(file => $name); + if ($rv->{code} != 0) { + $self->{output}->add_option_msg(short_msg => "cannot stat file '" . $name . "': " . $rv->{message}); + $self->{output}->option_exit(); + } + + $self->{files}->{$name} = { + name => $name, + mtime_seconds => $ctime - $rv->{mtime}, + mtime_human => centreon::plugins::misc::change_seconds( + value => $ctime - $rv->{mtime} + ) + }; + } + } + + foreach my $file (@{$self->{option_results}->{file}}) { + my $rv = $options{custom}->stat_file(file => $file); + if ($rv->{code} != 0) { + $self->{output}->add_option_msg(short_msg => "cannot stat file '" . $file . "': " . $rv->{message}); + $self->{output}->option_exit(); + } + + $self->{files}->{$file} = { + name => $file, + mtime_seconds => $ctime - $rv->{mtime}, + mtime_human => centreon::plugins::misc::change_seconds( + value => $ctime - $rv->{mtime} + ) + }; + } +} + +1; + +__END__ + +=head1 MODE + +Check modified time of files. + +=over 8 + +=item B<--directory> + +Check files in the directory (no recursive) (Multiple option) + +=item B<--file> + +Check file (Multiple option) + +=item B<--timezone> + +Set the timezone of display date. +Can use format: 'Europe/London' or '+0100'. + +=item B<--unit> + +Select the unit for modified time threshold. May be 's' for seconds, 'm' for minutes, +'h' for hours, 'd' for days, 'w' for weeks. Default is seconds. + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'mtime-last'. + +=back + +=cut diff --git a/centreon-plugins/apps/protocols/cifs/mode/scenario.pm b/centreon-plugins/apps/protocols/cifs/mode/scenario.pm new file mode 100644 index 000000000..1f76fa283 --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/mode/scenario.pm @@ -0,0 +1,330 @@ +# +# Copyright 2022 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 apps::protocols::cifs::mode::scenario; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); +use Time::HiRes qw(gettimeofday tv_interval); +use JSON::XS; + +sub custom_step_status_output { + my ($self, %options) = @_; + + return sprintf( + 'status: %s [message: %s]', + $self->{result_values}->{status}, + $self->{result_values}->{message} + ); +} + +sub prefix_global_output { + my ($self, %options) = @_; + + return 'Scenario '; +} + +sub prefix_step_output { + my ($self, %options) = @_; + + return "Step '" . $options{instance_value}->{label} . "' "; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } }, + { name => 'steps', type => 1, cb_prefix_output => 'prefix_step_output', message_multiple => 'All steps are ok', sort_method => 'num' } + ]; + + $self->{maps_counters}->{global} = [ + { + label => 'status', + type => 2, + critical_default => '%{status} ne "success"', + set => { + key_values => [ { name => 'status' } ], + output_template => 'status: %s', + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'total-time', nlabel => 'scenario.execution.time.milliseconds', set => { + key_values => [ { name => 'time_taken' } ], + output_template => 'execution time: %d ms', + perfdatas => [ + { template => '%d', min => 0, unit => 'ms' } + ] + } + }, + { label => 'total-steps', nlabel => 'scenario.steps.count', display_ok => 0, set => { + key_values => [ { name => 'total_steps' } ], + output_template => 'total steps: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + }, + { label => 'errors', nlabel => 'scenario.errors.count', set => { + key_values => [ { name => 'errors' } ], + output_template => 'errors: %s', + perfdatas => [ + { template => '%s', min => 0 } + ] + } + } + ]; + + $self->{maps_counters}->{steps} = [ + { label => 'step-time', nlabel => 'step.execution.time.milliseconds', set => { + key_values => [ { name => 'time_taken' }, { name => 'label' } ], + output_template => 'execution time: %d ms', + perfdatas => [ + { template => '%d', min => 0, unit => 'ms', label_extra_instance => 1, instance_use => 'label' } + ] + } + }, + { + label => 'step-status', + type => 2, + set => { + key_values => [ { name => 'status' }, { name => 'message' } ], + closure_custom_output => $self->can('custom_step_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'scenario:s' => { name => 'scenario' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + # Example of a scenario file: + # [ + # {"cmd": "write", "label": "write", "options": { "file": "/test/test.txt", "content": "my string 1" } }, + # {"cmd": "read", "options": { "file": "/test/test.txt", "match": "string" } }, + # {"cmd": "delete", "options": { "file": "/test/test.txt" } } + #] + if (!defined($self->{option_results}->{scenario})) { + $self->{output}->add_option_msg(short_msg => 'Please specify scenario option'); + $self->{output}->option_exit(); + } +} + +sub slurp_file { + my ($self, %options) = @_; + + my $content = do { + local $/ = undef; + if (!open my $fh, '<', $options{file}) { + $self->{output}->add_option_msg(short_msg => "Could not open file $options{file}: $!"); + $self->{output}->option_exit(); + } + <$fh>; + }; + + return $content; +} + +sub read_scenario { + my ($self, %options) = @_; + + my $content; + if ($self->{option_results}->{scenario} =~ /\n/m || ! -f "$self->{option_results}->{scenario}") { + $content = $self->{option_results}->{scenario}; + } else { + $content = $self->slurp_file(file => $self->{option_results}->{scenario}); + } + + eval { + $self->{scenario} = JSON::XS->new->decode($content); + }; + if ($@) { + $self->{output}->output_add(long_msg => "json config error: $@", debug => 1); + $self->{output}->add_option_msg(short_msg => 'Cannot decode json config'); + $self->{output}->option_exit(); + } + + if (ref($self->{scenario}) ne 'ARRAY') { + $self->{output}->add_option_msg(short_msg => 'scenario format error: expected an array'); + $self->{output}->option_exit(); + } + foreach (@{$self->{scenario}}) { + if ($_->{cmd} =~ /^(?:read|write|delete)$/) { + if (!defined($_->{options}->{file}) || $_->{options}->{file} eq '') { + $self->{output}->add_option_msg(short_msg => 'scenario format error: set file option'); + $self->{output}->option_exit(); + } + } else { + $self->{output}->add_option_msg(short_msg => 'scenario format error: unknown command ' . $_->{cmd}); + $self->{output}->option_exit(); + } + } +} + +sub failed { + my ($self, %options) = @_; + + $self->{global}->{status} = 'failed'; + $self->{global}->{errors}++; + $self->{steps}->{ $options{num} }->{status} = 'failed'; + $self->{steps}->{ $options{num} }->{message} = $options{message}; +} + +sub read { + my ($self, %options) = @_; + + my ($rv, $message, $data) = $options{custom}->read_file(file => $options{file}); + if ($rv) { + $self->failed(num => $options{num}, message => $message); + return ; + } + + if (defined($options{match}) && $data !~ /$options{match}/) { + $self->failed(num => $options{num}, message => 'matching failed'); + } +} + +sub write { + my ($self, %options) = @_; + + my ($rv, $message) = $options{custom}->write_file( + file => $options{file}, + content => $options{content} + ); + if ($rv) { + $self->failed(num => $options{num}, message => $message); + return ; + } +} + +sub delete { + my ($self, %options) = @_; + + my ($rv, $message) = $options{custom}->delete_file(file => $options{file}); + if ($rv) { + $self->failed(num => $options{num}, message => $message); + return ; + } +} + +sub execute_scenario { + my ($self, %options) = @_; + + $self->{steps} = {}; + my $num = 1; + foreach my $step (@{$self->{scenario}}) { + my $label = defined($step->{label}) && $step->{label} ne '' ? $step->{label} : $num; + $self->{steps}->{$num} = { label => $label, status => 'success', message => '-' }; + + my $timing0 = [gettimeofday]; + if ($step->{cmd} eq 'read') { + $self->read(num => $num, custom => $options{custom}, %{$step->{options}}); + } elsif ($step->{cmd} eq 'write') { + $self->write(num => $num, custom => $options{custom}, %{$step->{options}}); + } elsif ($step->{cmd} eq 'delete') { + $self->delete(num => $num, custom => $options{custom}, %{$step->{options}}); + } + $self->{steps}->{$num}->{time_taken} = tv_interval($timing0, [gettimeofday]) * 1000; + + $num++; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->read_scenario(); + + $self->{global} = { + status => 'success', + total_steps => scalar(@{$self->{scenario}}), + errors => 0, + time_taken => 0 + }; + + $self->execute_scenario(custom => $options{custom}); + + foreach (values %{$self->{steps}}) { + $self->{global}->{time_taken} += $_->{time_taken}; + } +} + +1; + +__END__ + +=head1 MODE + +Execute sftp commands. + +=over 8 + +=item B<--scenario> + +Scenario used (Required). +Can be a file or json content. + +=item B<--warning-status> + +Set warning threshold for status. +Can used special variables like: %{status} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{status} ne "success"') +Can used special variables like: %{status} + +=item B<--warning-step-status> + +Set warning threshold for status. +Can used special variables like: %{status}, %{message} + +=item B<--critical-step-status> + +Set critical threshold for status. +Can used special variables like: %{status}, %{message} + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'total-time', 'total-steps', 'errors', 'step-time'. + +=back + +=cut diff --git a/centreon-plugins/apps/protocols/cifs/plugin.pm b/centreon-plugins/apps/protocols/cifs/plugin.pm new file mode 100644 index 000000000..d6b4a222c --- /dev/null +++ b/centreon-plugins/apps/protocols/cifs/plugin.pm @@ -0,0 +1,53 @@ +# +# Copyright 2022 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 apps::protocols::cifs::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{modes} = { + 'connection' => 'apps::protocols::cifs::mode::connection', + 'files-count' => 'apps::protocols::cifs::mode::filescount', + 'files-date' => 'apps::protocols::cifs::mode::filesdate', + 'scenario' => 'apps::protocols::cifs::mode::scenario' + }; + + $self->{custom_modes}->{libcifs} = 'apps::protocols::cifs::custom::libcifs'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check a CIFS/SMB server. + +Need: https://github.com/garnier-quentin/Filesys-SmbClient + +=cut