| 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::liststorages; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::mode); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use Safe; |
| 28 |
|
| 29 |
sub new { |
| 30 |
my ($class, %options) = @_; |
| 31 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 32 |
bless $self, $class; |
| 33 |
|
| 34 |
$options{options}->add_options(arguments => { |
| 35 |
'display-transform-src:s' => { name => 'display_transform_src' }, |
| 36 |
'display-transform-dst:s' => { name => 'display_transform_dst' } |
| 37 |
}); |
| 38 |
|
| 39 |
$self->{safe} = Safe->new(); |
| 40 |
$self->{safe}->share('$assign_var'); |
| 41 |
|
| 42 |
return $self; |
| 43 |
} |
| 44 |
|
| 45 |
sub check_options { |
| 46 |
my ($self, %options) = @_; |
| 47 |
$self->SUPER::init(%options); |
| 48 |
|
| 49 |
} |
| 50 |
|
| 51 |
my @labels = ('size', 'name', 'label', 'type'); |
| 52 |
my $map_types = { |
| 53 |
0 => 'unknown', |
| 54 |
1 => 'noRootDirectory', |
| 55 |
2 => 'removableDisk', |
| 56 |
3 => 'localDisk', |
| 57 |
4 => 'networkDrive', |
| 58 |
5 => 'compactDisc', |
| 59 |
6 => 'ramDisk' |
| 60 |
}; |
| 61 |
|
| 62 |
sub get_display_value { |
| 63 |
my ($self, %options) = @_; |
| 64 |
|
| 65 |
our $assign_var = $options{name}; |
| 66 |
if (defined($self->{option_results}->{display_transform_src})) { |
| 67 |
$self->{option_results}->{display_transform_dst} = '' if (!defined($self->{option_results}->{display_transform_dst})); |
| 68 |
|
| 69 |
$self->{safe}->reval("\$assign_var =~ s{$self->{option_results}->{display_transform_src}}{$self->{option_results}->{display_transform_dst}}", 1); |
| 70 |
if ($@) { |
| 71 |
die 'Unsafe code evaluation: ' . $@; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return $assign_var; |
| 76 |
} |
| 77 |
|
| 78 |
sub manage_selection { |
| 79 |
my ($self, %options) = @_; |
| 80 |
|
| 81 |
my $entries = $options{wsman}->request( |
| 82 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 83 |
wql_filter => 'Select Capacity,DeviceID,DriveLetter,DriveType,FileSystem,FreeSpace,Label,Name from Win32_Volume', |
| 84 |
result_type => 'array' |
| 85 |
); |
| 86 |
|
| 87 |
my $results = {}; |
| 88 |
foreach my $entry (@$entries) { |
| 89 |
my $display_value = $self->get_display_value(name => $entry->{Name} ); |
| 90 |
$results->{ $entry->{DeviceID} } = { |
| 91 |
size => $entry->{Capacity}, |
| 92 |
name => $display_value, |
| 93 |
label => $entry->{Label}, |
| 94 |
type => $map_types->{ $entry->{DriveType} } |
| 95 |
}; |
| 96 |
} |
| 97 |
|
| 98 |
return $results; |
| 99 |
} |
| 100 |
|
| 101 |
sub run { |
| 102 |
my ($self, %options) = @_; |
| 103 |
|
| 104 |
my $results = $self->manage_selection(wsman => $options{wsman}); |
| 105 |
foreach my $instance (sort keys %$results) { |
| 106 |
$self->{output}->output_add(long_msg => |
| 107 |
join('', map("[$_: " . $results->{$instance}->{$_} . ']', @labels)) |
| 108 |
); |
| 109 |
} |
| 110 |
|
| 111 |
$self->{output}->output_add( |
| 112 |
severity => 'OK', |
| 113 |
short_msg => 'List storages:' |
| 114 |
); |
| 115 |
|
| 116 |
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1); |
| 117 |
$self->{output}->exit(); |
| 118 |
} |
| 119 |
|
| 120 |
sub disco_format { |
| 121 |
my ($self, %options) = @_; |
| 122 |
|
| 123 |
$self->{output}->add_disco_format(elements => [@labels]); |
| 124 |
} |
| 125 |
|
| 126 |
sub disco_show { |
| 127 |
my ($self, %options) = @_; |
| 128 |
|
| 129 |
my $results = $self->manage_selection(wsman => $options{wsman}); |
| 130 |
foreach (sort keys %$results) { |
| 131 |
$self->{output}->add_disco_entry( |
| 132 |
%{$results->{$_}} |
| 133 |
); |
| 134 |
} |
| 135 |
} |
| 136 |
1; |
| 137 |
|
| 138 |
__END__ |
| 139 |
|
| 140 |
=head1 MODE |
| 141 |
|
| 142 |
List storages. |
| 143 |
|
| 144 |
=over 8 |
| 145 |
|
| 146 |
=item B<--display-transform-src> B<--display-transform-dst> |
| 147 |
|
| 148 |
Modify the storage name displayed by using a regular expression. |
| 149 |
|
| 150 |
Example: adding --display-transform-src='dev' --display-transform-dst='run' will replace all occurrences of 'dev' with 'run' |
| 151 |
|
| 152 |
=back |
| 153 |
|
| 154 |
=cut |