enh(apps::vmware::vsphere8): handle pagination for acq-specs results (#5752)

Refs: CTOR-1514
This commit is contained in:
omercier 2025-09-22 14:02:25 +02:00 committed by GitHub
parent 2671325c46
commit 41bdc18f2a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
15 changed files with 154 additions and 31 deletions

View File

@ -202,7 +202,8 @@ sub try_request_api {
$self->{output}->option_exit();
}
return {} if ($method ne 'GET');
return {} if ($method eq 'PATCH' && $self->{http}->get_code() == 204
|| $method eq 'POST' && $self->{http}->get_code() == 201);
my $decoded = centreon::plugins::misc::json_decode($content, booleans_as_strings => 1);
if (!defined($decoded)) {
@ -213,6 +214,11 @@ sub try_request_api {
$self->{output}->option_exit();
}
if (ref($decoded) eq "HASH" && defined($decoded->{error_type})) {
$self->{output}->add_option_msg(short_msg => "API returned an error: " . $decoded->{error_type} . " - " . $decoded->{messages}->[0]->{default_message});
$self->{output}->option_exit();
}
return $decoded;
}
@ -241,6 +247,8 @@ sub request_api {
$self->{output}->add_option_msg(short_msg => "API returns error of type " . $api_response->{error_type} . ": " . $full_message);
$self->{output}->option_exit();
}
return $api_response;
}
@ -269,11 +277,19 @@ sub get_vm_guest_identity {
sub get_all_acq_specs {
my ($self, %options) = @_;
# If we have already requested it, we return what we have
return $self->{all_acq_specs} if ($self->{all_acq_specs} && @{$self->{all_acq_specs}});
# Get all acq specs and store them in cache
# FIXME: cache management
# FIXME: any pagination issue ?
$self->{all_acq_specs} = $self->request_api(endpoint => '/stats/acq-specs')->{acq_specs} if ( !defined($self->{all_acq_specs}));
my $response = $self->request_api(endpoint => '/stats/acq-specs') ;
$self->{all_acq_specs} = $response->{acq_specs};
# If the whole acq-specs takes more than one page, the API will return a "next" value
while ($response->{next}) {
$response = $self->request_api(endpoint => '/stats/acq-specs', get_param => [ 'page=' . $response->{next} ] );
push @{$self->{all_acq_specs}}, @{$response->{acq_specs}};
}
return $self->{all_acq_specs};
}
@ -373,7 +389,6 @@ sub get_acq_spec {
# If it is not available in cache call get_all_acq_specs()
my $acq_specs = $self->get_all_acq_specs();
# FIXME: opt exit if centreon::plugins::misc::is_empty($options{cid})
for my $spec (@$acq_specs) {
# Ignore acq_specs not related to the counter_id
next if ($options{cid} ne $spec->{counters}->{cid_mid}->{cid});

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=cpu

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=cpu

View File

@ -9,7 +9,7 @@ Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --password=C3POR2P2

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=disk-io

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=host-status

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=memory

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=network

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=power

View File

@ -8,7 +8,7 @@ Suite Teardown Stop Mockoon
Test Timeout 120s
*** Variables ***
${MOCKOON_JSON} ${CURDIR}${/}vmware8-restapi.mockoon.json
${MOCKOON_JSON} ${CURDIR}${/}mockoon.json
${CMD} ${CENTREON_PLUGINS} --plugin=apps::vmware::vsphere8::esx::plugin
... --mode=swap

View File

@ -27,7 +27,7 @@ Cpu ${tc}
Ctn Run Command And Check Result As Strings ${command} ${expected_result}
Examples: tc extraoptions expected_result --
... 1 ${EMPTY} UNKNOWN: no data for host vm-7722 counter cpu.capacity.entitlement.VM at the moment. - get_vm_stats function failed to retrieve stats The counter cpu.capacity.entitlement.VM was not recorded for resource vm-7722 before. It will now (creating acq_spec). The counter cpu.capacity.usage.VM was not recorded for resource vm-7722 before. It will now (creating acq_spec).
... 1 ${EMPTY} UNKNOWN: no data for host vm-7722 counter cpu.capacity.entitlement.VM at the moment. - get_vm_stats function failed to retrieve stats The counter cpu.capacity.entitlement.VM was not recorded for resource vm-7722 before. It will now (creating acq_spec).
... 2 ${EMPTY} OK: CPU average usage is 11.13 %, used frequency is 81.56 kHz | 'cpu.capacity.usage.percentage'=11.13%;;;0;100 'cpu.capacity.usage.hertz'=81560000Hz;;;0;733000000
... 3 --warning-usage-prct=5 WARNING: CPU average usage is 11.13 % | 'cpu.capacity.usage.percentage'=11.13%;0:5;;0;100 'cpu.capacity.usage.hertz'=81560000Hz;;;0;733000000
... 4 --critical-usage-prct=5 CRITICAL: CPU average usage is 11.13 % | 'cpu.capacity.usage.percentage'=11.13%;;0:5;0;100 'cpu.capacity.usage.hertz'=81560000Hz;;;0;733000000

View File

@ -1,7 +1,7 @@
{
"uuid": "dd7d9589-c42b-42e9-8790-f11c8a0f344d",
"lastMigration": 33,
"name": "Vmware8 restapi.mockoon",
"name": "Vmware8 VMs",
"endpointPrefix": "",
"latency": 0,
"port": 3000,
@ -1317,10 +1317,10 @@
},
{
"uuid": "2f2fb518-6bd4-47b3-a127-55b26faa8500",
"body": "{\n \"acq_specs\": [\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"cpu.capacity.entitlement.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-7722\"\n }\n ],\n \"expiration\": 1760105513,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"542\",\n \"status\": \"ENABLED\"\n },\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"cpu.capacity.usage.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-7722\"\n }\n ],\n \"expiration\": 1760105513,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"543\",\n \"status\": \"ENABLED\"\n },\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"mem.capacity.entitlement.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-5175\"\n }\n ],\n \"expiration\": 1760109594,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"788\",\n \"status\": \"ENABLED\"\n },\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"mem.capacity.usage.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-5175\"\n }\n ],\n \"expiration\": 1760109594,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"789\",\n \"status\": \"ENABLED\"\n }\n ]\n}",
"body": "{\n \"next\": 36,\n \"acq_specs\": [\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"cpu.capacity.entitlement.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-7722\"\n }\n ],\n \"expiration\": 1760105513,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"542\",\n \"status\": \"ENABLED\"\n },\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"mem.capacity.entitlement.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-5175\"\n }\n ],\n \"expiration\": 1760109594,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"788\",\n \"status\": \"ENABLED\"\n }\n ]\n}",
"latency": 0,
"statusCode": 200,
"label": "Scond response",
"label": "Scond response page 1",
"headers": [
{
"key": "access-control-allow-headers",
@ -1358,9 +1358,75 @@
"value": "1",
"invert": true,
"operator": "equals"
},
{
"target": "query",
"modifier": "page",
"value": "",
"invert": false,
"operator": "null"
}
],
"rulesOperator": "OR",
"rulesOperator": "AND",
"disableTemplating": false,
"fallbackTo404": false,
"default": false,
"crudKey": "id",
"callbacks": []
},
{
"uuid": "b4c155f2-02d3-4c28-82e5-4530f359e781",
"body": "{\n \"acq_specs\": [\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"cpu.capacity.usage.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-7722\"\n }\n ],\n \"expiration\": 1760105513,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"543\",\n \"status\": \"ENABLED\"\n },\n {\n \"counters\": {\n \"cid_mid\": {\n \"mid\": \"\",\n \"cid\": \"mem.capacity.usage.VM\"\n },\n \"set_id\": \"\"\n },\n \"resources\": [\n {\n \"predicate\": \"EQUAL\",\n \"scheme\": \"moid\",\n \"type\": \"VM\",\n \"id_value\": \"vm-5175\"\n }\n ],\n \"expiration\": 1760109594,\n \"interval\": 60,\n \"memo_\": \"\",\n \"id\": \"789\",\n \"status\": \"ENABLED\"\n }\n ]\n}",
"latency": 0,
"statusCode": 200,
"label": "Scond response page 2",
"headers": [
{
"key": "access-control-allow-headers",
"value": "Content-Type, Origin, Accept, Authorization, Content-Length, X-Requested-With"
},
{
"key": "access-control-allow-methods",
"value": "GET,POST,PUT,PATCH,DELETE,HEAD,OPTIONS"
},
{
"key": "access-control-allow-origin",
"value": "*"
},
{
"key": "content-security-policy",
"value": "default-src 'none'"
},
{
"key": "content-type",
"value": "text/html; charset=utf-8"
},
{
"key": "x-content-type-options",
"value": "nosniff"
}
],
"bodyType": "INLINE",
"filePath": "",
"databucketID": "",
"sendFileAsBody": false,
"rules": [
{
"target": "request_number",
"modifier": "",
"value": "1",
"invert": true,
"operator": "equals"
},
{
"target": "query",
"modifier": "page",
"value": "36",
"invert": false,
"operator": "equals"
}
],
"rulesOperator": "AND",
"disableTemplating": false,
"fallbackTo404": false,
"default": false,

View File

@ -32,10 +32,11 @@ Vm-Status ${tc}
... 3 --vm-name=vm3.acme.com ${EMPTY} UNKNOWN: No VM found.
... 4 --vm-id=vm-7722 ${EMPTY} OK: VM 'db-server-01', id: 'vm-7722': power state is POWERED_ON
... 5 --vm-id=vm-7657 ${EMPTY} CRITICAL: VM 'web-server-01', id: 'vm-7657': power state is POWERED_OFF
... 6 --vm-id=vm-3 ${EMPTY} UNKNOWN: No VM found.
... 7 --vm-id=vm-3000000 --warning-power-status='\\\%{power_state} =~ /^powered_on$/i' UNKNOWN: No VM found.
... 6 --vm-id=vm-3 ${EMPTY} UNKNOWN: API returned an error: UNAUTHORIZED - The following (object: vm-3:c186dc36-76b6-4435-b5f3-cb1e9678a67e privileges: System.Read) privileges are insufficient to user
... 7 --vm-id=vm-3000000 --warning-power-status='\\\%{power_state} =~ /^powered_on$/i' UNKNOWN: API returned an error: UNAUTHORIZED - The following (object: vm-3000000:c186dc36-76b6-4435-b5f3-cb1e9678a67e privileges: System.Read) privileges are insufficient to user
... 8 --vm-id=vm-7722 --critical-power-status='\\\%{power_state} =~ /^powered_on$/i' CRITICAL: VM 'db-server-01', id: 'vm-7722': power state is POWERED_ON
... 9 --vm-id=vm-7657 --critical-power-status='\\\%{power_state} =~ /^powered_on$/i' OK: VM 'web-server-01', id: 'vm-7657': power state is POWERED_OFF
... 10 --vm-id=vm-7722 --warning-power-status='\\\%{power_state} =~ /^powered_on$/i' WARNING: VM 'db-server-01', id: 'vm-7722': power state is POWERED_ON
... 11 --vm-id=vm-7657 --warning-power-status='\\\%{power_state} =~ /^powered_on$/i' CRITICAL: VM 'web-server-01', id: 'vm-7657': power state is POWERED_OFF
... 12 --vm-id=vm-3 --vm-name=web-server-01 ${EMPTY} CRITICAL: VM 'web-server-01', id: 'vm-7657': power state is POWERED_OFF
... 12 --vm-id=vm-3 --vm-name=web-server-01 ${EMPTY} UNKNOWN: API returned an error: UNAUTHORIZED - The following (object: vm-3:c186dc36-76b6-4435-b5f3-cb1e9678a67e privileges: System.Read) privileges are insufficient to user

View File

@ -34,7 +34,7 @@ Vm-Tools ${tc}
... 5 --vm-id=vm-7722 ${EMPTY} OK: vm-7722 had 1 install attempts - version is UNMANAGED (v12.2.0) - updates are MANUAL (auto-updates not allowed) - tools are RUNNING | 'tools.install.attempts.count'=1;;;0;
... 6 --vm-id=vm-7657 ${EMPTY} OK: vm-7657 had 4 install attempts - version is CURRENT (v12.3.0) - updates are MANUAL (auto-updates allowed) - tools are RUNNING | 'tools.install.attempts.count'=4;;;0;
... 7 --vm-id=vm-1234 ${EMPTY} WARNING: vm-1234 tools are NOT_RUNNING | 'tools.install.attempts.count'=4;;;0;
... 8 --vm-id=vm-3000000 ${EMPTY} UNKNOWN: API returns error of type NOT_FOUND: [Id: com.vmware.api.vcenter.vm.not_found - Msg: Virtual machine with identifier 'vm-3000000:c186dc36-76b6-4435-b5f3-cb1e9678a67e' does not exist. (vm-3000000:c186dc36-76b6-4435-b5f3-cb1e9678a67e)]
... 8 --vm-id=vm-3000000 ${EMPTY} UNKNOWN: API returned an error: NOT_FOUND - Virtual machine with identifier 'vm-3000000:c186dc36-76b6-4435-b5f3-cb1e9678a67e' does not exist.
... 9 --vm-id=vm-7722 --warning-install-attempts=0 WARNING: vm-7722 had 1 install attempts | 'tools.install.attempts.count'=1;0:0;;0;
... 10 --vm-id=vm-7657 --warning-install-attempts=0 WARNING: vm-7657 had 4 install attempts | 'tools.install.attempts.count'=4;0:0;;0;
... 11 --vm-id=vm-1234 --warning-install-attempts=0 WARNING: vm-1234 had 4 install attempts (error messages available in long output with --verbose option) - tools are NOT_RUNNING | 'tools.install.attempts.count'=4;0:0;;0;