|
| 1 | +# |
| 2 | +# Copyright 2025 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 apps::vmware::vsphere8::vcenter::mode::vmcount; |
| 22 | + |
| 23 | +use base qw(apps::vmware::vsphere8::vcenter::mode); |
| 24 | + |
| 25 | +use strict; |
| 26 | +use warnings; |
| 27 | + |
| 28 | +sub set_counters { |
| 29 | + my ($self, %options) = @_; |
| 30 | + |
| 31 | + $self->{maps_counters_type} = [ |
| 32 | + { name => 'global', type => 0 } |
| 33 | + ]; |
| 34 | + |
| 35 | + $self->{maps_counters}->{global} = [ |
| 36 | + { |
| 37 | + label => 'on-count', |
| 38 | + nlabel => 'vm.poweredon.count', |
| 39 | + type => 1, |
| 40 | + set => { |
| 41 | + key_values => [ { name => 'POWERED_ON' }, { name => 'total' } ], |
| 42 | + output_template => '%s VM(s) powered on', |
| 43 | + perfdatas => [ |
| 44 | + { label => 'POWERED_ON', template => '%s', min => 0, max => 'total' } |
| 45 | + ] |
| 46 | + } |
| 47 | + }, |
| 48 | + { |
| 49 | + label => 'off-count', |
| 50 | + nlabel => 'vm.poweredoff.count', |
| 51 | + type => 1, |
| 52 | + set => { |
| 53 | + key_values => [ { name => 'POWERED_OFF' }, { name => 'total' } ], |
| 54 | + output_template => '%s VM(s) powered off', |
| 55 | + perfdatas => [ |
| 56 | + { label => 'POWERED_OFF', template => '%s', min => 0, max => 'total' } |
| 57 | + ] |
| 58 | + } |
| 59 | + }, |
| 60 | + { |
| 61 | + label => 'suspended-count', |
| 62 | + nlabel => 'vm.suspended.count', |
| 63 | + type => 1, |
| 64 | + set => { |
| 65 | + key_values => [ { name => 'SUSPENDED' }, { name => 'total' } ], |
| 66 | + output_template => '%s VM(s) suspended', |
| 67 | + perfdatas => [ |
| 68 | + { label => 'SUSPENDED', template => '%s', min => 0, max => 'total' } |
| 69 | + ] |
| 70 | + } |
| 71 | + }, |
| 72 | + { |
| 73 | + label => 'total-count', |
| 74 | + nlabel => 'vm.total.count', |
| 75 | + type => 1, |
| 76 | + warning_default => '1:', |
| 77 | + set => { |
| 78 | + key_values => [ { name => 'total' } ], |
| 79 | + output_template => '%s VM(s) in total', |
| 80 | + perfdatas => [ |
| 81 | + { label => 'total', template => '%s', min => 0 } |
| 82 | + ] |
| 83 | + } |
| 84 | + } |
| 85 | + ]; |
| 86 | +} |
| 87 | + |
| 88 | +sub new { |
| 89 | + my ($class, %options) = @_; |
| 90 | + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 91 | + |
| 92 | + $options{options}->add_options( |
| 93 | + arguments => { |
| 94 | + 'include-name:s' => { name => 'include_name', default => '' }, |
| 95 | + 'exclude-name:s' => { name => 'exclude_name', default => '' }, |
| 96 | + 'include-state:s' => { name => 'include_state', default => '' }, |
| 97 | + 'exclude-state:s' => { name => 'exclude_state', default => '' }, |
| 98 | + } |
| 99 | + ); |
| 100 | + $options{options}->add_help(package => __PACKAGE__, sections => 'VMWARE 8 VCENTER OPTIONS', once => 1); |
| 101 | + |
| 102 | + return $self; |
| 103 | +} |
| 104 | + |
| 105 | +sub manage_selection { |
| 106 | + my ($self, %options) = @_; |
| 107 | + |
| 108 | + # get the response from /api/vcenter/vm endpoint |
| 109 | + my $response = $self->get_vms(%options); |
| 110 | + |
| 111 | + $self->{global} = { |
| 112 | + 'POWERED_ON' => 0, |
| 113 | + 'POWERED_OFF' => 0, |
| 114 | + 'SUSPENDED' => 0, |
| 115 | + 'total' => 0 |
| 116 | + }; |
| 117 | + |
| 118 | + for my $vm (@{$response}) { |
| 119 | + # avoid undef values |
| 120 | + my $entry = { |
| 121 | + vm => defined($vm->{vm}) ? $vm->{vm} : '', |
| 122 | + name => defined($vm->{name}) ? $vm->{name} : '', |
| 123 | + cpu_count => defined($vm->{cpu_count}) ? $vm->{cpu_count} : '', |
| 124 | + power_state => defined($vm->{power_state}) ? $vm->{power_state} : '', |
| 125 | + memory_size_MiB => defined($vm->{memory_size_MiB}) ? $vm->{memory_size_MiB} : '' |
| 126 | + }; |
| 127 | + |
| 128 | + my $entry_desc = sprintf( |
| 129 | + "VM '%s' (%s) which is %s, has %d CPUs and %d MiB of RAM", |
| 130 | + $entry->{name}, |
| 131 | + $entry->{vm}, |
| 132 | + $entry->{power_state}, |
| 133 | + $entry->{cpu_count}, |
| 134 | + $entry->{memory_size_MiB} |
| 135 | + ); |
| 136 | + |
| 137 | + # include only whitelisted VMs |
| 138 | + if ($self->{option_results}->{include_name} ne '' || $self->{option_results}->{include_state} ne '' ) { |
| 139 | + my $whitelist = 0; |
| 140 | + |
| 141 | + $whitelist = 1 if ($self->{option_results}->{include_name} ne '' && $entry->{name} =~ $self->{option_results}->{include_name}); |
| 142 | + $whitelist = 1 if ($self->{option_results}->{include_state} ne '' && $entry->{state} =~ $self->{option_results}->{include_state}); |
| 143 | + |
| 144 | + if ($whitelist == 0) { |
| 145 | + $self->{output}->output_add(long_msg => "skipping not whitelisted " .$entry_desc, debug => 1); |
| 146 | + next; |
| 147 | + } |
| 148 | + } |
| 149 | + # exclude blacklisted VMs |
| 150 | + if ( |
| 151 | + ($self->{option_results}->{exclude_name} ne '' && $entry->{name} =~ /$self->{option_results}->{exclude_name}/ ) |
| 152 | + || ($self->{option_results}->{include_state} ne '' && $entry->{state} =~ /$self->{option_results}->{include_state}/ ) |
| 153 | + ) { |
| 154 | + $self->{output}->output_add(long_msg => "skipping blacklisted " .$entry_desc, debug => 1); |
| 155 | + next; |
| 156 | + } |
| 157 | + $self->{output}->output_add(long_msg => $entry_desc); |
| 158 | + $self->{global}->{ $entry->{power_state} }++; |
| 159 | + $self->{global}->{total}++; |
| 160 | + |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +1; |
| 165 | + |
| 166 | +__END__ |
| 167 | +
|
| 168 | +=head1 MODE |
| 169 | +
|
| 170 | +Monitor the number of VMware VMs through vSphere 8 REST API. |
| 171 | +
|
| 172 | +=over 8 |
| 173 | +
|
| 174 | +=item B<--include-name> |
| 175 | +
|
| 176 | +Filter by including only the VMs whose name matches the regular expression provided after this parameter. |
| 177 | +
|
| 178 | +Example : C<--include-name='^prod.*'> |
| 179 | +
|
| 180 | +=item B<--exclude-name> |
| 181 | +
|
| 182 | +Filter by excluding the VMs whose name matches the regular expression provided after this parameter. |
| 183 | +
|
| 184 | +Example : C<--exclude-name='^sandbox.*'> |
| 185 | +
|
| 186 | +=item B<--include-state> |
| 187 | +
|
| 188 | +Filter by including only the VMs whose power state matches the regular expression provided after this parameter. |
| 189 | +
|
| 190 | +Example : C<--include-name='^POWERED_ON$'> |
| 191 | +
|
| 192 | +=item B<--exclude-state> |
| 193 | +
|
| 194 | +Filter by excluding the VMs whose state matches the regular expression provided after this parameter. |
| 195 | +
|
| 196 | +Example : C<--exclude-name='^POWERED_OFF|SUSPENDED$'> |
| 197 | +
|
| 198 | +=item B<--warning-on-count> |
| 199 | +
|
| 200 | +Threshold. |
| 201 | +
|
| 202 | +=item B<--critical-on-count> |
| 203 | +
|
| 204 | +Threshold. |
| 205 | +
|
| 206 | +=item B<--warning-off-count> |
| 207 | +
|
| 208 | +Threshold. |
| 209 | +
|
| 210 | +=item B<--critical-off-count> |
| 211 | +
|
| 212 | +Threshold. |
| 213 | +
|
| 214 | +=item B<--warning-suspended-count> |
| 215 | +
|
| 216 | +Threshold. |
| 217 | +
|
| 218 | +=item B<--critical-suspended-count> |
| 219 | +
|
| 220 | +Threshold. |
| 221 | +
|
| 222 | +=item B<--warning-total-count> |
| 223 | +
|
| 224 | +Threshold. |
| 225 | +
|
| 226 | +=item B<--critical-total-count> |
| 227 | +
|
| 228 | +Threshold. |
| 229 | +
|
| 230 | +=back |
| 231 | +
|
| 232 | +=cut |
0 commit comments