| 1 |
# |
| 2 |
# Copyright 2024 Centreon (http://www.centreon.com/) |
| 3 |
# |
| 4 |
# Centreon is a full-fledged industry-strength solution that meets |
| 5 |
# the needs in IT infrastructure and application monitoring for |
| 6 |
# service performance. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
|
| 21 |
package os::windows::wsman::mode::filessize; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
|
| 29 |
sub new { |
| 30 |
my ($class, %options) = @_; |
| 31 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 32 |
bless $self, $class; |
| 33 |
|
| 34 |
$options{options}->add_options(arguments => { |
| 35 |
'warning-one:s' => { name => 'warning_one' }, |
| 36 |
'critical-one:s' => { name => 'critical_one' }, |
| 37 |
'warning-total:s' => { name => 'warning_total' }, |
| 38 |
'critical-total:s' => { name => 'critical_total' }, |
| 39 |
'all-files' => { name => 'all_files' }, |
| 40 |
'filter-filename:s' => { name => 'filter_filename' }, |
| 41 |
'folder:s' => { name => 'folder' } |
| 42 |
}); |
| 43 |
|
| 44 |
return $self; |
| 45 |
} |
| 46 |
|
| 47 |
sub check_options { |
| 48 |
my ($self, %options) = @_; |
| 49 |
$self->SUPER::init(%options); |
| 50 |
|
| 51 |
if (($self->{perfdata}->threshold_validate(label => 'warning_one', value => $self->{option_results}->{warning_one})) == 0) { |
| 52 |
$self->{output}->add_option_msg(short_msg => "Wrong warning-one threshold '" . $self->{warning_one} . "'."); |
| 53 |
$self->{output}->option_exit(); |
| 54 |
} |
| 55 |
if (($self->{perfdata}->threshold_validate(label => 'critical_one', value => $self->{option_results}->{critical_one})) == 0) { |
| 56 |
$self->{output}->add_option_msg(short_msg => "Wrong critical-one threshold '" . $self->{critical_one} . "'."); |
| 57 |
$self->{output}->option_exit(); |
| 58 |
} |
| 59 |
if (($self->{perfdata}->threshold_validate(label => 'warning_total', value => $self->{option_results}->{warning_total})) == 0) { |
| 60 |
$self->{output}->add_option_msg(short_msg => "Wrong warning-total threshold '" . $self->{warning_total} . "'."); |
| 61 |
$self->{output}->option_exit(); |
| 62 |
} |
| 63 |
if (($self->{perfdata}->threshold_validate(label => 'critical_total', value => $self->{option_results}->{critical_total})) == 0) { |
| 64 |
$self->{output}->add_option_msg(short_msg => "Wrong critical-total threshold '" . $self->{critical_total} . "'."); |
| 65 |
$self->{output}->option_exit(); |
| 66 |
} |
| 67 |
if (!defined($self->{option_results}->{folder}) || $self->{option_results}->{folder} eq '') { |
| 68 |
$self->{output}->add_option_msg(short_msg => "Need to specify folder option."); |
| 69 |
$self->{output}->option_exit(); |
| 70 |
} |
| 71 |
|
| 72 |
#### Create file path |
| 73 |
$self->{option_results}->{folder} =~ s/\//\\\\/g; |
| 74 |
} |
| 75 |
|
| 76 |
sub run { |
| 77 |
my ($self, %options) = @_; |
| 78 |
|
| 79 |
my ($total_size, $exit_code) = (0); |
| 80 |
|
| 81 |
$self->{option_results}->{folder} =~ /^(..)(.*)$/; |
| 82 |
my ($drive, $path) = ($1, $2); |
| 83 |
my $WQL = 'Select name,filesize from CIM_DataFile where drive = "' . $drive . '" AND path = "' . $path . '"'; |
| 84 |
|
| 85 |
my $results = $options{wsman}->request( |
| 86 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 87 |
wql_filter => $WQL, |
| 88 |
result_type => 'array' |
| 89 |
); |
| 90 |
# |
| 91 |
#CLASS: CIM_DataFile |
| 92 |
#FileSize|Name |
| 93 |
#1092|C:\Users\Administrator\.bash_history |
| 94 |
#52|C:\Users\Administrator\.gitconfig |
| 95 |
#37|C:\Users\Administrator\.lesshst |
| 96 |
#1038|C:\Users\Administrator\.viminfo |
| 97 |
#20|C:\Users\Administrator\ntuser.ini |
| 98 |
# |
| 99 |
|
| 100 |
$self->{output}->output_add( |
| 101 |
severity => 'OK', |
| 102 |
short_msg => "All file sizes are ok." |
| 103 |
); |
| 104 |
if (!defined($results) || scalar(@$results) <= 0) { |
| 105 |
$self->{output}->output_add( |
| 106 |
severity => 'UNKNOWN', |
| 107 |
short_msg => 'No file found.' |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
foreach (@$results) { |
| 112 |
my $size = $_->{FileSize}; |
| 113 |
my $name = centreon::plugins::misc::trim($_->{Name}); |
| 114 |
|
| 115 |
next if (defined($self->{option_results}->{filter_filename}) && $self->{option_results}->{filter_filename} ne '' && |
| 116 |
$name !~ /$self->{option_results}->{filter_filename}/); |
| 117 |
|
| 118 |
$total_size += $size; |
| 119 |
my $exit_code = $self->{perfdata}->threshold_check( |
| 120 |
value => $size, |
| 121 |
threshold => [ { label => 'critical_one', exit_litteral => 'critical' }, { label => 'warning_one', exit_litteral => 'warning' } ] |
| 122 |
); |
| 123 |
my ($size_value, $size_unit) = $self->{perfdata}->change_bytes(value => $size); |
| 124 |
$self->{output}->output_add(long_msg => sprintf("%s: %s", $name, $size_value . ' ' . $size_unit)); |
| 125 |
if (!$self->{output}->is_status(litteral => 1, value => $exit_code, compare => 'ok')) { |
| 126 |
$self->{output}->output_add( |
| 127 |
severity => $exit_code, |
| 128 |
short_msg => sprintf("'%s' size is %s", $name, $size_value . ' ' . $size_unit) |
| 129 |
); |
| 130 |
} |
| 131 |
$self->{output}->perfdata_add( |
| 132 |
nlabel => 'file.size.bytes', |
| 133 |
unit => 'B', |
| 134 |
instances => $name, |
| 135 |
value => $size, |
| 136 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_one'), |
| 137 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_one'), |
| 138 |
min => 0 |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
# Total Size |
| 143 |
$exit_code = $self->{perfdata}->threshold_check( |
| 144 |
value => $total_size, |
| 145 |
threshold => [ { label => 'critical_total', exit_litteral => 'critical' }, { label => 'warning_total', exit_litteral => 'warning' } ] |
| 146 |
); |
| 147 |
my ($size_value, $size_unit) = $self->{perfdata}->change_bytes(value => $total_size); |
| 148 |
$self->{output}->output_add(long_msg => sprintf("Total: %s", $size_value . ' ' . $size_unit)); |
| 149 |
if (!$self->{output}->is_status(litteral => 1, value => $exit_code, compare => 'ok')) { |
| 150 |
$self->{output}->output_add( |
| 151 |
severity => $exit_code, |
| 152 |
short_msg => sprintf('Total size is %s', $size_value . ' ' . $size_unit) |
| 153 |
); |
| 154 |
} |
| 155 |
$self->{output}->perfdata_add( |
| 156 |
nlabel => 'files.size.bytes', |
| 157 |
unit => 'B', |
| 158 |
value => $total_size, |
| 159 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning_total'), |
| 160 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical_total'), |
| 161 |
min => 0 |
| 162 |
); |
| 163 |
|
| 164 |
$self->{output}->display(); |
| 165 |
$self->{output}->exit(); |
| 166 |
} |
| 167 |
|
| 168 |
1; |
| 169 |
|
| 170 |
__END__ |
| 171 |
|
| 172 |
=head1 MODE |
| 173 |
|
| 174 |
Check size of files/directories. |
| 175 |
|
| 176 |
=over 8 |
| 177 |
|
| 178 |
=item B<--folder> |
| 179 |
|
| 180 |
Folder to check (no WQL wildcard allowed). |
| 181 |
Example: 'C:/Users/Administrator/'. |
| 182 |
|
| 183 |
=item B<--filter-filename> |
| 184 |
|
| 185 |
Filter files by name. |
| 186 |
|
| 187 |
=item B<--warning-one> |
| 188 |
|
| 189 |
Warning threshold in bytes for each files/directories. |
| 190 |
|
| 191 |
=item B<--critical-one> |
| 192 |
|
| 193 |
Critical threshold in bytes for each files/directories. |
| 194 |
|
| 195 |
=item B<--warning-total> |
| 196 |
|
| 197 |
Warning threshold in bytes for all files/directories. |
| 198 |
|
| 199 |
=item B<--critical-total> |
| 200 |
|
| 201 |
Critical threshold in bytes for all files/directories. |
| 202 |
|
| 203 |
=back |
| 204 |
|
| 205 |
=cut |