| 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::filesdate; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use DateTime; |
| 28 |
use centreon::plugins::misc; |
| 29 |
|
| 30 |
sub new { |
| 31 |
my ($class, %options) = @_; |
| 32 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 33 |
bless $self, $class; |
| 34 |
|
| 35 |
$options{options}->add_options(arguments => { |
| 36 |
'warning:s' => { name => 'warning' }, |
| 37 |
'critical:s' => { name => 'critical' }, |
| 38 |
'filter-filename:s' => { name => 'filter_filename' }, |
| 39 |
'folder:s' => { name => 'folder' } |
| 40 |
}); |
| 41 |
|
| 42 |
return $self; |
| 43 |
} |
| 44 |
|
| 45 |
sub check_options { |
| 46 |
my ($self, %options) = @_; |
| 47 |
$self->SUPER::init(%options); |
| 48 |
|
| 49 |
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { |
| 50 |
$self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); |
| 51 |
$self->{output}->option_exit(); |
| 52 |
} |
| 53 |
if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { |
| 54 |
$self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); |
| 55 |
$self->{output}->option_exit(); |
| 56 |
} |
| 57 |
if (!defined($self->{option_results}->{folder}) || $self->{option_results}->{folder} eq '') { |
| 58 |
$self->{output}->add_option_msg(short_msg => "Need to specify folder option."); |
| 59 |
$self->{output}->option_exit(); |
| 60 |
} |
| 61 |
|
| 62 |
#### Create file path |
| 63 |
$self->{option_results}->{folder} =~ s/\//\\\\/g; |
| 64 |
} |
| 65 |
|
| 66 |
sub wmi_to_seconds { |
| 67 |
my ($self, %options) = @_; |
| 68 |
|
| 69 |
# pass in a WMI Timestamp like 2021-11-04T21:24:11.871719Z |
| 70 |
my $sec = ''; |
| 71 |
my $age_sec = ''; |
| 72 |
my $current_dt = ''; |
| 73 |
my $current_sec = ''; |
| 74 |
my $tz = ''; |
| 75 |
my $timezone_direction = '+'; |
| 76 |
my $timezone_offset = 0; |
| 77 |
|
| 78 |
# 1 2 3 4 5 6 7 8 9 |
| 79 |
if ($options{ts} =~ /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).(\d*)(\S+)$/) { |
| 80 |
my %ts_info= ( |
| 81 |
year => $1, |
| 82 |
month => $2, |
| 83 |
day => $3, |
| 84 |
hour => $4, |
| 85 |
minute => $5, |
| 86 |
second => $6, |
| 87 |
nanosecond => $7, |
| 88 |
time_zone => $8 # set later |
| 89 |
); |
| 90 |
|
| 91 |
|
| 92 |
my $dt = DateTime->new(%ts_info); |
| 93 |
$sec = $dt->epoch(); |
| 94 |
# force the current time into the same timezone as the queried system |
| 95 |
$current_dt = DateTime->now(time_zone => $dt->time_zone()); |
| 96 |
$current_sec = $current_dt->epoch(); |
| 97 |
$age_sec = $current_sec - $sec; |
| 98 |
} else { |
| 99 |
$self->{output}->add_option_msg(short_msg => 'Wrong time format'); |
| 100 |
$self->{output}->option_exit(); |
| 101 |
} |
| 102 |
|
| 103 |
return ($sec, $age_sec); |
| 104 |
} |
| 105 |
|
| 106 |
sub run { |
| 107 |
my ($self, %options) = @_; |
| 108 |
|
| 109 |
my $total_size = 0; |
| 110 |
my $current_time = time(); |
| 111 |
|
| 112 |
$self->{option_results}->{folder} =~ /^(..)(.*)$/; |
| 113 |
my ($drive, $path) = ($1, $2); |
| 114 |
my $WQL = 'Select name, lastmodified from CIM_DataFile where drive = "' . $drive . '" AND path = "' . $path . '"'; |
| 115 |
|
| 116 |
my $results = $options{wsman}->request( |
| 117 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 118 |
wql_filter => $WQL, |
| 119 |
result_type => 'array' |
| 120 |
); |
| 121 |
|
| 122 |
# |
| 123 |
#$VAR1 = 'CLASS: CIM_DataFile |
| 124 |
#LastModified;Name |
| 125 |
#20211224000036.262291+000;C:\\Users\\Administrator\\NTUSER.DAT'; |
| 126 |
# |
| 127 |
$self->{output}->output_add( |
| 128 |
severity => 'OK', |
| 129 |
short_msg => 'All files times are ok.' |
| 130 |
); |
| 131 |
|
| 132 |
if (!defined($results) || scalar(@$results) <= 0) { |
| 133 |
$self->{output}->output_add( |
| 134 |
severity => 'UNKNOWN', |
| 135 |
short_msg => 'No file found.' |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
foreach (@$results) { |
| 140 |
my $last_modified = $_->{LastModified}; |
| 141 |
my $name = centreon::plugins::misc::trim($_->{Name}); |
| 142 |
my ($time, $diff_time) = $self->wmi_to_seconds(ts => $last_modified); |
| 143 |
|
| 144 |
next if (defined($self->{option_results}->{filter_filename}) && $self->{option_results}->{filter_filename} ne '' && |
| 145 |
$name !~ /$self->{option_results}->{filter_filename}/); |
| 146 |
|
| 147 |
my $exit_code = $self->{perfdata}->threshold_check( |
| 148 |
value => $diff_time, |
| 149 |
threshold => [ { label => 'critical', exit_litteral => 'critical' }, { label => 'warning', exit_litteral => 'warning' } ] |
| 150 |
); |
| 151 |
$self->{output}->output_add(long_msg => sprintf("%s: %s seconds (time: %s)", $name, $diff_time, scalar(localtime($time)))); |
| 152 |
if (!$self->{output}->is_status(litteral => 1, value => $exit_code, compare => 'ok')) { |
| 153 |
$self->{output}->output_add( |
| 154 |
severity => $exit_code, |
| 155 |
short_msg => sprintf('%s: %s seconds (time: %s)', $name, $diff_time, scalar(localtime($time))) |
| 156 |
); |
| 157 |
} |
| 158 |
$self->{output}->perfdata_add( |
| 159 |
label => $name, |
| 160 |
nlabel => 'file.mtime.last.seconds', |
| 161 |
unit => 's', |
| 162 |
instances => $name, |
| 163 |
value => $diff_time, |
| 164 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), |
| 165 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical') |
| 166 |
); |
| 167 |
} |
| 168 |
|
| 169 |
$self->{output}->display(); |
| 170 |
$self->{output}->exit(); |
| 171 |
} |
| 172 |
|
| 173 |
1; |
| 174 |
|
| 175 |
__END__ |
| 176 |
|
| 177 |
=head1 MODE |
| 178 |
|
| 179 |
Check files modified time. |
| 180 |
|
| 181 |
=over 8 |
| 182 |
|
| 183 |
=item B<--folder> |
| 184 |
|
| 185 |
Folder to check (no WQL wildcard allowed). |
| 186 |
Example: 'C:/Users/Administrator/'. |
| 187 |
|
| 188 |
=item B<--filter-filename> |
| 189 |
|
| 190 |
Filter files by name. |
| 191 |
|
| 192 |
=item B<--warning> |
| 193 |
|
| 194 |
Warning threshold in seconds for each files (diff time). |
| 195 |
|
| 196 |
=item B<--critical> |
| 197 |
|
| 198 |
Critical threshold in seconds for each files (diff time). |
| 199 |
|
| 200 |
=back |
| 201 |
|
| 202 |
=cut |