Skip to content

Commit 164e359

Browse files
committed
feat(apps::vmware::vsphere8::vcenter::plugin): new plugin
Modes: - vm-count
1 parent 0151f65 commit 164e359

9 files changed

Lines changed: 736 additions & 3 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": [
3+
"libjson-perl"
4+
]
5+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"pkg_name": "centreon-plugin-Virtualization-Vmware8-Vcenter-Restapi",
3+
"pkg_summary": "Centreon Plugin to monitor VMware vCenter using vSphere 8 REST API",
4+
"plugin_name": "centreon_vmware8_vcenter_restapi.pl",
5+
"files": [
6+
"centreon/plugins/script_custom.pm",
7+
"apps/vmware/vsphere8/vcenter/",
8+
"apps/vmware/vsphere8/custom/"
9+
]
10+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"dependencies": [
3+
"perl(JSON)"
4+
]
5+
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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;
22+
use strict;
23+
use warnings FATAL => 'all';
24+
25+
use base qw(centreon::plugins::templates::counter);
26+
27+
sub new {
28+
my ($class, %options) = @_;
29+
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
30+
31+
$options{options}->add_options(
32+
arguments => {}
33+
);
34+
$options{options}->add_help(package => __PACKAGE__, sections => 'VMWARE 8 VCENTER OPTIONS', once => 1);
35+
36+
return $self;
37+
}
38+
39+
sub get_vms {
40+
my ($self, %options) = @_;
41+
# Retrieve the data
42+
return $options{custom}->request_api('endpoint' => '/vcenter/vm', 'method' => 'GET');
43+
}
44+
45+
sub request_api {
46+
my ($self, %options) = @_;
47+
48+
return $options{custom}->request_api(%options);
49+
}
50+
51+
sub check_options {
52+
my ($self, %options) = @_;
53+
54+
$self->SUPER::check_options(%options);
55+
56+
}
57+
58+
1;
59+
60+
__END__
61+
62+
=head1 VMWARE 8 HOST OPTIONS
63+
64+
=over 4
65+
66+
=item B<--esx-id>
67+
68+
Define which physical server to monitor based on its resource ID (example: C<host-16>).
69+
70+
=item B<--esx-name>
71+
72+
Define which physical server to monitor based on its name (example: C<esx01.mydomain.tld>).
73+
When possible, it is recommended to use C<--esx-id> instead.
74+
75+
=back
76+
77+
=cut
78+
79+
=head1 NAME
80+
81+
apps::vmware::vsphere8::esx::mode - Template for modes monitoring VMware physical hosts
82+
83+
=head1 SYNOPSIS
84+
85+
use base apps::vmware::vsphere8::esx::mode;
86+
87+
sub set_counters {...}
88+
sub manage_selection {
89+
my ($self, %options) = @_;
90+
91+
92+
93+
$api->set_options(option_results => $option_results);
94+
$api->check_options();
95+
my $response = $api->request_api(endpoint => '/vcenter/host');
96+
my $host_cpu_capacity = $self->get_esx_stats(
97+
cid => 'cpu.capacity.provisioned.HOST',
98+
rsrc_id => 'host-18');
99+
100+
=head1 DESCRIPTION
101+
102+
This module provides methods to interact with the VMware vSphere 8 REST API. It handles authentication, caching, and API requests.
103+
104+
=head1 METHODS
105+
106+
=head2 get_esx_stats
107+
108+
$self->get_esx_stats(%options);
109+
110+
Retrieves the ESX statistics for the given options using package apps::vmware::vsphere8::custom::api::get_stats()
111+
112+
=over 4
113+
114+
=item * C<%options> - A hash of options. The following keys are supported:
115+
116+
=over 8
117+
118+
=item * C<cid> - The C<cid> (counter id) of the desired metric.
119+
120+
=item * C<esx_id> - The ESX's C<rsrc_id> (resource ID) for which to retrieve the statistics. This option is optional if C<esx_name> is provided.
121+
122+
=item * C<esx_name> - The ESX's name for which to retrieve the statistics. This option is not used if C<esx_id> is provided, which is the nominal usage of this function.
123+
124+
=back
125+
126+
=back
127+
128+
Returns the statistics for the specified ESX.
129+
130+
=cut
131+
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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

Comments
 (0)