(plugin) apps::protocols::sftp - new (#3634)

This commit is contained in:
qgarnier 2022-05-03 14:26:26 +02:00 committed by GitHub
parent 8babb51486
commit bddbcf8c5f
8 changed files with 1158 additions and 3 deletions

View File

@ -0,0 +1,254 @@
#
# 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::sftp::custom::libsftp;
use strict;
use warnings;
use Libssh::Session qw(:all);
use Libssh::Sftp qw(:all);
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' },
'port:s' => { name => 'port' },
'timeout:s' => { name => 'timeout' },
'ssh-username:s' => { name => 'ssh_username' },
'ssh-password:s' => { name => 'ssh_password' },
'ssh-dir:s' => { name => 'ssh_dir' },
'ssh-identity:s' => { name => 'ssh_identity' },
'ssh-skip-serverkey-issue' => { name => 'ssh_skip_serverkey_issue' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'SFTP 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} : 22;
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{ssh_username} = (defined($self->{option_results}->{ssh_username})) ? $self->{option_results}->{ssh_username} : undef;
$self->{ssh_password} = (defined($self->{option_results}->{ssh_password})) ? $self->{option_results}->{ssh_password} : '';
$self->{ssh_dir} = (defined($self->{option_results}->{ssh_dir})) ? $self->{option_results}->{ssh_dir} : undef;
$self->{ssh_identity} = (defined($self->{option_results}->{ssh_identity})) ? $self->{option_results}->{ssh_identity} : undef;
$self->{ssh_skip_serverkey_issue} = defined($self->{option_results}->{ssh_skip_serverkey_issue}) ? 1 : 0;
if ($self->{hostname} eq '') {
$self->{output}->add_option_msg(short_msg => "Please set option --hostname.");
$self->{output}->option_exit();
}
return 0;
}
sub connect {
my ($self, %options) = @_;
$self->{ssh} = Libssh::Session->new();
foreach (['hostname', 'host'], ['port', 'port'], ['timeout', 'timeout'], ['ssh_username', 'user'],
['ssh_dir', 'sshdir'], ['ssh_identity', 'identity']) {
next if (!defined($self->{$_->[0]}) || $self->{$_->[0]} eq '');
if ($self->{ssh}->options($_->[1] => $self->{$_->[0]}) != SSH_OK) {
return (1, $self->{ssh}->error());
}
}
if ($self->{ssh}->connect(SkipKeyProblem => $self->{ssh_skip_serverkey_issue}) != SSH_OK) {
return (1, $self->{ssh}->error());
}
if ($self->{ssh}->auth_publickey_auto() == SSH_AUTH_SUCCESS ||
($self->{ssh_password} ne '' && $self->{ssh}->auth_password(password => $self->{ssh_password}) == SSH_AUTH_SUCCESS)) {
$self->{sftp} = Libssh::Sftp->new(session => $self->{ssh});
if (!defined($self->{sftp})) {
return (1, Libssh::Sftp::error());
}
} else {
my $msg_error = $self->{ssh}->error(GetErrorSession => 1);
my $message = sprintf("auth issue: %s", defined($msg_error) && $msg_error ne '' ? $msg_error : 'pubkey issue');
return (1, $message);
}
return (0, 'authentication succeeded');
}
sub read_file {
my ($self, %options) = @_;
my $file = $self->{sftp}->open(file => $options{file}, accesstype => O_RDONLY, mode => 0600);
if (!defined($file)) {
return (1, $self->{sftp}->error());
}
my ($rv, $data) = $self->{sftp}->read(handle_file => $file);
if ($rv != SSH_OK) {
return (1, $self->{sftp}->error());
}
return (0, '', $data);
}
sub write_file {
my ($self, %options) = @_;
my $file = $self->{sftp}->open(file => $options{file}, accesstype => O_WRONLY|O_CREAT|O_TRUNC, mode => 0600);
if (!defined($file)) {
return (1, $self->{sftp}->error());
}
my $data = defined($options{content}) ? $options{content} : '';
if ($self->{sftp}->write(handle_file => $file, data => $data) != SSH_OK) {
return (1, $self->{sftp}->error());
}
return (0, '');
}
sub delete_file {
my ($self, %options) = @_;
if ($self->{sftp}->unlink(file => $options{file}) != SSH_OK) {
return (1, $self->{sftp}->error());
}
return (0, '');
}
sub list_directory {
my ($self, %options) = @_;
if ($options{dir} !~ /^\//) {
$options{dir} = $self->{sftp}->canonicalize_path(path => $options{dir});
}
my $rv = $self->{sftp}->list_dir(dir => $options{dir});
if ($rv->{code} != SSH_OK) {
$rv->{message} = $self->{sftp}->error();
}
$rv->{dir} = $options{dir};
return $rv;
}
sub stat_file {
my ($self, %options) = @_;
my $file = $options{file};
if ($options{file} !~ /^\//) {
$file = $self->{sftp}->canonicalize_path(path => $file);
if (!defined($file)) {
return { code => 1, fullname => $options{file}, message => $self->{sftp}->get_msg_error() };
}
}
my $rv = $self->{sftp}->stat_file(file => $file);
if (!defined($rv)) {
return { code => 1, fullname => $file, message => $self->{sftp}->get_msg_error() };
}
return { code => 0, fullname => $file, %$rv };
}
1;
__END__
=head1 NAME
SSH connector library
=head1 SYNOPSIS
my ssh connector
=head1 SFTP OPTIONS
=over 8
=item B<--hostname>
SSH server hostname (required).
=item B<--port>
SSH port.
=item B<--timeout>
Timeout in seconds for connection (Defaults: 10 seconds)
=item B<--ssh-username>
SSH username.
=item B<--ssh-password>
SSH password.
=item B<--ssh-dir>
Set the ssh directory.
=item B<--ssh-identity>
Set the identity file name (default: id_dsa and id_rsa are checked).
=item B<--ssh-skip-serverkey-issue>
Connection will be OK even if there is a problem (server known changed or server found other) with the ssh server.
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,119 @@
#
# 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::sftp::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 => {});
return $self;
}
sub manage_selection {
my ($self, %options) = @_;
my $timing0 = [gettimeofday];
my ($rv, $message) = $options{custom}->connect();
my $timeelapsed = tv_interval($timing0, [gettimeofday]);
$self->{global} = {
status => $rv,
message => $message,
time_elapsed => $timeelapsed
};
}
1;
__END__
=head1 MODE
Check sftp connection.
=over 8
=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

View File

@ -0,0 +1,167 @@
#
# 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::sftp::mode::filescount;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
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) = @_;
my ($rv, $message) = $options{custom}->connect();
if ($rv != 0) {
$self->{output}->add_option_msg(short_msg => $message);
$self->{output}->option_exit();
}
$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 = $options{custom}->list_directory(dir => $dir);
if ($rv->{code} != 0) {
# Cannot list we skip
next;
}
foreach my $file (@{$rv->{files}}) {
next if ($file->{name} eq '.' || $file->{name} eq '..');
my $name = $dir . '/' . $file->{name};
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->{type} == 2) {
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

View File

@ -0,0 +1,224 @@
#
# 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::sftp::mode::filesdate;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
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 ($rv, $message) = $options{custom}->connect();
if ($rv != 0) {
$self->{output}->add_option_msg(short_msg => $message);
$self->{output}->option_exit();
}
my $ctime = time();
$self->{files} = {};
foreach my $dir (@{$self->{option_results}->{directory}}) {
my $rv = $options{custom}->list_directory(dir => $dir);
if ($rv->{code} != 0) {
$self->{output}->add_option_msg(short_msg => "cannot read directory '" . $dir . "': " . $rv->{message});
$self->{output}->option_exit();
}
foreach my $file (@{$rv->{files}}) {
next if ($file->{name} eq '.' || $file->{name} eq '..');
my $name = $rv->{dir} . '/' . $file->{name};
$self->{files}->{$name} = {
name => $name,
mtime_seconds => $ctime - $file->{mtime},
mtime_human => centreon::plugins::misc::change_seconds(
value => $ctime - $file->{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}->{ $rv->{fullname} } = {
name => $rv->{fullname},
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

View File

@ -0,0 +1,337 @@
#
# 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::sftp::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.txt", "content": "my string 1" } },
# {"cmd": "read", "options": { "file": "test.txt", "match": "string" } },
# {"cmd": "delete", "options": { "file": "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();
my $timing0 = [gettimeofday];
my ($rv, $message) = $options{custom}->connect();
if ($rv != 0) {
$self->{output}->add_option_msg(short_msg => $message);
$self->{output}->option_exit();
}
$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

View File

@ -0,0 +1,54 @@
#
# 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::sftp::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::sftp::mode::connection',
'files-count' => 'apps::protocols::sftp::mode::filescount',
'files-date' => 'apps::protocols::sftp::mode::filesdate',
'scenario' => 'apps::protocols::sftp::mode::scenario'
};
$self->{custom_modes}->{libsftp} = 'apps::protocols::sftp::custom::libsftp';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check a SFTP server.
Need Libssh perl:
https://github.com/garnier-quentin/perl-libssh
=cut

View File

@ -43,7 +43,7 @@ sub set_counters {
{ {
label => 'status', label => 'status',
type => 2, type => 2,
critical_default => '%{message} !~ /authentification succeeded/i', critical_default => '%{message} !~ /authentication succeeded/i',
set => { set => {
key_values => [ { name => 'status' }, { name => 'message' } ], key_values => [ { name => 'status' }, { name => 'message' } ],
closure_custom_output => $self->can('custom_status_output'), closure_custom_output => $self->can('custom_status_output'),
@ -99,7 +99,7 @@ Can used special variables like: %{status}, %{message}
=item B<--critical-status> =item B<--critical-status>
Set critical threshold for status (Default: '%{message} !~ /authentification succeeded/i' Set critical threshold for status (Default: '%{message} !~ /authentication succeeded/i'
Can used special variables like: %{status}, %{message} Can used special variables like: %{status}, %{message}
=item B<--warning-time> =item B<--warning-time>

View File

@ -93,7 +93,7 @@ sub check_options {
sub login { sub login {
my ($self, %options) = @_; my ($self, %options) = @_;
my $result = { status => 0, message => 'authentification succeeded' }; my $result = { status => 0, message => 'authentication succeeded' };
$self->{ssh} = Libssh::Session->new(); $self->{ssh} = Libssh::Session->new();
foreach (['hostname', 'host'], ['port', 'port'], ['timeout', 'timeout'], ['ssh_username', 'user'], foreach (['hostname', 'host'], ['port', 'port'], ['timeout', 'timeout'], ['ssh_username', 'user'],