| 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::storages; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
|
| 28 |
sub custom_usage_output { |
| 29 |
my ($self, %options) = @_; |
| 30 |
|
| 31 |
my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}); |
| 32 |
my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}); |
| 33 |
my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}); |
| 34 |
return sprintf( |
| 35 |
'space usage total: %s used: %s (%.2f%%) free: %s (%.2f%%)', |
| 36 |
$total_size_value . " " . $total_size_unit, |
| 37 |
$total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used}, |
| 38 |
$total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free} |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
sub prefix_storage_output { |
| 43 |
my ($self, %options) = @_; |
| 44 |
|
| 45 |
return sprintf( |
| 46 |
"Storage '%s' [type: %s] ", |
| 47 |
$options{instance_value}->{name}, |
| 48 |
$options{instance_value}->{type} |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
sub set_counters { |
| 53 |
my ($self, %options) = @_; |
| 54 |
|
| 55 |
$self->{maps_counters_type} = [ |
| 56 |
{ name => 'global', type => 0 }, |
| 57 |
{ name => 'storages', type => 1, cb_prefix_output => 'prefix_storage_output', message_multiple => 'All storages are ok', skipped_code => { -10 => 1 } } |
| 58 |
]; |
| 59 |
|
| 60 |
$self->{maps_counters}->{global} = [ |
| 61 |
{ label => 'detected', nlabel => 'storages.detected.count', display_ok => 0, set => { |
| 62 |
key_values => [ { name => 'detected' } ], |
| 63 |
output_template => 'Storages detected: %d', |
| 64 |
perfdatas => [ |
| 65 |
{ template => '%d', min => 0 } |
| 66 |
] |
| 67 |
} |
| 68 |
} |
| 69 |
]; |
| 70 |
|
| 71 |
$self->{maps_counters}->{storages} = [ |
| 72 |
{ label => 'space-usage', nlabel => 'storage.space.usage.bytes', set => { |
| 73 |
key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 74 |
closure_custom_output => $self->can('custom_usage_output'), |
| 75 |
perfdatas => [ |
| 76 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 77 |
] |
| 78 |
} |
| 79 |
}, |
| 80 |
{ label => 'space-usage-free', nlabel => 'storage.space.free.bytes', display_ok => 0, set => { |
| 81 |
key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 82 |
closure_custom_output => $self->can('custom_usage_output'), |
| 83 |
perfdatas => [ |
| 84 |
{ template => '%d', min => 0, max => 'total', unit => 'B', cast_int => 1, label_extra_instance => 1 } |
| 85 |
] |
| 86 |
} |
| 87 |
}, |
| 88 |
{ label => 'space-usage-prct', nlabel => 'storage.space.usage.percentage', display_ok => 0, set => { |
| 89 |
key_values => [ { name => 'prct_used' }, { name => 'free' }, { name => 'used' }, { name => 'prct_free' }, { name => 'total' } ], |
| 90 |
closure_custom_output => $self->can('custom_usage_output'), |
| 91 |
perfdatas => [ |
| 92 |
{ template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 } |
| 93 |
] |
| 94 |
} |
| 95 |
} |
| 96 |
]; |
| 97 |
} |
| 98 |
|
| 99 |
sub new { |
| 100 |
my ($class, %options) = @_; |
| 101 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 102 |
bless $self, $class; |
| 103 |
|
| 104 |
$options{options}->add_options(arguments => { |
| 105 |
'filter-name:s' => { name => 'filter_name' }, |
| 106 |
'filter-type:s' => { name => 'filter_type', default => 'localDisk' } |
| 107 |
}); |
| 108 |
|
| 109 |
return $self; |
| 110 |
} |
| 111 |
|
| 112 |
my $map_types = { |
| 113 |
0 => 'unknown', |
| 114 |
1 => 'noRootDirectory', |
| 115 |
2 => 'removableDisk', |
| 116 |
3 => 'localDisk', |
| 117 |
4 => 'networkDrive', |
| 118 |
5 => 'compactDisc', |
| 119 |
6 => 'ramDisk' |
| 120 |
}; |
| 121 |
|
| 122 |
sub manage_selection { |
| 123 |
my ($self, %options) = @_; |
| 124 |
|
| 125 |
my $results = $options{wsman}->request( |
| 126 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 127 |
wql_filter => 'Select Capacity,DeviceID,DriveLetter,DriveType,FileSystem,FreeSpace,Label,Name from Win32_Volume', |
| 128 |
result_type => 'array' |
| 129 |
); |
| 130 |
|
| 131 |
# 'CLASS: Win32_Volume', |
| 132 |
# 'Capacity;DeviceID;DriveLetter;DriveType;FileSystem;FreeSpace;Label;Name', |
| 133 |
# '32210153472;\\\\?\\Volume{1952b268-0000-0000-0000-100000000000}\\;C:;3;NTFS;14982889472;(null);C:\\' |
| 134 |
|
| 135 |
$self->{global} = { detected => 0 }; |
| 136 |
$self->{storages} = {}; |
| 137 |
foreach (@$results) { |
| 138 |
my $type = $map_types->{ $_->{DriveType} }; |
| 139 |
|
| 140 |
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' && |
| 141 |
$_->{Name} !~ /$self->{option_results}->{filter_name}/); |
| 142 |
next if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' && |
| 143 |
$type !~ /$self->{option_results}->{filter_type}/); |
| 144 |
|
| 145 |
next if (!defined($_->{Capacity}) || $_->{Capacity} eq '' || $_->{Capacity} == 0); |
| 146 |
|
| 147 |
my ($total, $free) = ($_->{Capacity}, $_->{FreeSpace}); |
| 148 |
$self->{storages}->{ $_->{Name} } = { |
| 149 |
name => $_->{Name}, |
| 150 |
type => $type, |
| 151 |
total => $total, |
| 152 |
used => $total - $free, |
| 153 |
free => $free, |
| 154 |
prct_free => $free * 100 / $total, |
| 155 |
prct_used => 100 - ($free * 100 / $total) |
| 156 |
}; |
| 157 |
$self->{global}->{detected}++; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
1; |
| 162 |
|
| 163 |
__END__ |
| 164 |
|
| 165 |
=head1 MODE |
| 166 |
|
| 167 |
Check storages. |
| 168 |
|
| 169 |
=over 8 |
| 170 |
|
| 171 |
=item B<--filter-name> |
| 172 |
|
| 173 |
Filter storages by name (can be a regexp). |
| 174 |
|
| 175 |
=item B<--filter-type> |
| 176 |
|
| 177 |
Filter storages by type (can be a regexp) (default: 'localDisk'). |
| 178 |
|
| 179 |
=item B<--warning-*> B<--critical-*> |
| 180 |
|
| 181 |
Thresholds. |
| 182 |
Can be: 'space-usage' (B), 'space-usage-free' (B), 'space-usage-prct'. |
| 183 |
|
| 184 |
=back |
| 185 |
|
| 186 |
=cut |