fix(plugin) cifs,sftp,ftp plugin now check recursively folders (#5015)

REF:CTOR-258
This commit is contained in:
Evan-Adam 2024-05-14 10:37:40 +02:00 committed by GitHub
parent 1fb1647965
commit 30286395ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 52 additions and 47 deletions

View File

@ -106,27 +106,28 @@ sub countFiles {
# Cannot list we skip # Cannot list we skip
next; next;
} }
# this loop is recursive, when we find a directory we add it to the list used by the for loop.
# max_depth is used to limit the depth we search.
# same behaviour as ftp and sftp protocol.
foreach my $file (@$files) { foreach my $file (@$files) {
next if ($file->[0] != SMBC_FILE && $file->[0] != SMBC_DIR); next if ($file->[0] != SMBC_FILE && $file->[0] != SMBC_DIR);
next if ($file->[1] eq '.' || $file->[1] eq '..'); next if ($file->[1] eq '.' || $file->[1] eq '..');
my $name = $dir . '/' . $file->[1]; 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 ($file->[0] == SMBC_DIR) {
if (defined($self->{option_results}->{max_depth}) && $level + 1 <= $self->{option_results}->{max_depth}) { if (defined($self->{option_results}->{max_depth}) && $level + 1 <= $self->{option_results}->{max_depth}) {
push @$list, { name => $name, level => $level + 1 }; push @$list, { name => $name, level => $level + 1 };
} }
} else { next;
$self->{output}->output_add(long_msg => sprintf("Match '%s'", $name)); } elsif (!centreon::plugins::misc::is_empty($self->{option_results}->{filter_file})
$self->{global}->{detected}++; # if this is a file check the filter_file regex
&& $name !~ /$self->{option_results}->{filter_file}/) {
$self->{output}->output_add(long_msg => sprintf("skipping '%s'", $name), debug => 1);
next;
} }
$self->{output}->output_add(long_msg => sprintf("Match '%s'", $name));
$self->{global}->{detected}++;
} }
} }
} }

View File

@ -89,7 +89,7 @@ sub run {
my $exit_code = $self->{perfdata}->threshold_check(value => $count, my $exit_code = $self->{perfdata}->threshold_check(value => $count,
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]); threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ]);
$self->{output}->output_add(severity => $exit_code, $self->{output}->output_add(severity => $exit_code,
short_msg => sprintf("Number of files : %s", $count)); short_msg => sprintf("Number of files : %s", $count));
$self->{output}->perfdata_add(label => 'files', $self->{output}->perfdata_add(label => 'files',
@ -105,7 +105,7 @@ sub countFiles {
my ($self) = @_; my ($self) = @_;
my @listings; my @listings;
my $count = 0; my $count = 0;
if (!defined($self->{option_results}->{directory}) || scalar(@{$self->{option_results}->{directory}}) == 0) { if (!defined($self->{option_results}->{directory}) || scalar(@{$self->{option_results}->{directory}}) == 0) {
push @listings, [ { name => '.', level => 0 } ]; push @listings, [ { name => '.', level => 0 } ];
} else { } else {
@ -121,36 +121,39 @@ sub countFiles {
my $hash = pop @$list; my $hash = pop @$list;
my $dir = $hash->{name}; my $dir = $hash->{name};
my $level = $hash->{level}; my $level = $hash->{level};
if (!(@files = apps::protocols::ftp::lib::ftp::execute($self, command => $map_commands{ls}->{$self->{ssl_or_not}}->{name}, command_args => [$dir]))) { if (!(@files = apps::protocols::ftp::lib::ftp::execute($self, command => $map_commands{ls}->{$self->{ssl_or_not}}->{name}, command_args => [$dir]))) {
# Cannot list we skip # Cannot list we skip
next; next;
} }
# this loop is recursive, when we find a directory we add it to the list used by the for loop.
# max_depth is used to limit the depth we search.
# same behaviour as cifs(samba) and sftp protocol.
foreach my $line (@files) { foreach my $line (@files) {
# IIS: 05-13-15 10:59AM 1184403 test.jpg # IIS: 05-13-15 10:59AM 1184403 test.jpg
next if ($line !~ /(\S+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(.*)/ && next if ($line !~ /(\S+)\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(.*)/ &&
$line !~ /^\s*\S+\s*\S+\s*(\S+)\s+(.*)/); $line !~ /^\s*\S+\s*\S+\s*(\S+)\s+(.*)/);
my ($rights, $filename) = ($1, $2); my ($rights, $filename) = ($1, $2);
my $bname = basename($filename); my $bname = basename($filename);
next if ($bname eq '.' || $bname eq '..'); next if ($bname eq '.' || $bname eq '..');
my $name = $dir . '/' . $bname; my $name = $dir . '/' . $bname;
if (defined($self->{option_results}->{filter_file}) && $self->{option_results}->{filter_file} ne '' && if ($rights =~ /^(d|<DIR>)/i) {
$name !~ /$self->{option_results}->{filter_file}/) { # in the case of a directory and the max level is not reached yet, we add it to the recursive browsing
if (defined($self->{option_results}->{max_depth})
&& $level + 1 <= $self->{option_results}->{max_depth}) {
push @$list, { name => $name, level => $level + 1 };
}
next;
} elsif (!centreon::plugins::misc::is_empty($self->{option_results}->{filter_file})
&& $name !~ /$self->{option_results}->{filter_file}/) {
$self->{output}->output_add(long_msg => sprintf("Skipping '%s'", $name)); $self->{output}->output_add(long_msg => sprintf("Skipping '%s'", $name));
# in the case of a file that does not match the filter, we skip
next; next;
} }
$self->{output}->output_add(long_msg => sprintf("Match '%s'", $name));
if ($rights =~ /^(d|<DIR>)/i) { $count++;
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));
$count++;
}
}
} }
} }
return $count; return $count;

View File

@ -34,12 +34,12 @@ sub set_counters {
$self->{maps_counters}->{global} = [ $self->{maps_counters}->{global} = [
{ label => 'files-detected', nlabel => 'files.detected.count', set => { { label => 'files-detected', nlabel => 'files.detected.count', set => {
key_values => [ { name => 'detected' } ], key_values => [ { name => 'detected' } ],
output_template => 'number of files: %s', output_template => 'number of files: %s',
perfdatas => [ perfdatas => [
{ template => '%s', min => 0 } { template => '%s', min => 0 }
] ]
} }
} }
]; ];
} }
@ -93,7 +93,7 @@ sub manage_selection {
sub countFiles { sub countFiles {
my ($self, %options) = @_; my ($self, %options) = @_;
my @listings; my @listings;
foreach my $dir (@{$self->{option_results}->{directory}}) { foreach my $dir (@{$self->{option_results}->{directory}}) {
push @listings, [ { name => $dir, level => 0 } ]; push @listings, [ { name => $dir, level => 0 } ];
} }
@ -111,26 +111,27 @@ sub countFiles {
# Cannot list we skip # Cannot list we skip
next; next;
} }
# this loop is recursive, when we find a directory we add it to the list used by the for loop.
# max_depth is used to limit the depth we search.
# same behaviour as cifs(samba) and ftp protocol.
foreach my $file (@{$rv->{files}}) { foreach my $file (@{$rv->{files}}) {
next if ($file->{name} eq '.' || $file->{name} eq '..'); next if ($file->{name} eq '.' || $file->{name} eq '..');
my $name = $dir . '/' . $file->{name}; 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 ($file->{type} == 2) {
# case of a directory
if (defined($self->{option_results}->{max_depth}) && $level + 1 <= $self->{option_results}->{max_depth}) { if (defined($self->{option_results}->{max_depth}) && $level + 1 <= $self->{option_results}->{max_depth}) {
push @$list, { name => $name, level => $level + 1 }; push @$list, { name => $name, level => $level + 1 };
} }
} else { next;
$self->{output}->output_add(long_msg => sprintf("Match '%s'", $name)); } elsif (!centreon::plugins::misc::is_empty($self->{option_results}->{filter_file})
$self->{global}->{detected}++; && $name !~ /$self->{option_results}->{filter_file}/) {
$self->{output}->output_add(long_msg => sprintf("skipping '%s'", $name), debug => 1);
next;
} }
} $self->{output}->output_add(long_msg => sprintf("Match '%s'", $name));
$self->{global}->{detected}++;
}
} }
} }
} }