diff --git a/src/apps/monitoring/latencetech/restapi/mode/discovery.pm b/src/apps/monitoring/latencetech/restapi/mode/discovery.pm new file mode 100644 index 000000000..0e5681d02 --- /dev/null +++ b/src/apps/monitoring/latencetech/restapi/mode/discovery.pm @@ -0,0 +1,93 @@ +# +# Copyright 2025 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package apps::monitoring::latencetech::restapi::mode::discovery; + +use base qw(centreon::plugins::mode); + +use strict; +use warnings; +use JSON::XS; + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'prettify' => { name => 'prettify' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::init(%options); +} + +sub run { + my ($self, %options) = @_; + + my $disco_stats; + $disco_stats->{start_time} = time(); + + my @get_param = [ 'metadata=true' ]; + my $results = $options{custom}->request_api( + endpoint => '/agents', + get_param => @get_param, + method => 'GET', + ); + + $disco_stats->{end_time} = time(); + $disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time}; + $disco_stats->{discovered_items} = scalar(@{$results->{agents}}); + $disco_stats->{results} = $results->{agents}; + + my $encoded_data; + eval { + if (defined($self->{option_results}->{prettify})) { + $encoded_data = JSON::XS->new->utf8->pretty->encode($disco_stats); + } else { + $encoded_data = JSON::XS->new->utf8->encode($disco_stats); + } + }; + if ($@) { + $encoded_data = '{"code":"encode_error","message":"Cannot encode discovered data into JSON format"}'; + } + + $self->{output}->output_add(short_msg => $encoded_data); + $self->{output}->display(nolabel => 1, force_ignore_perfdata => 1); + $self->{output}->exit(); +} + +1; + +__END__ + +=head1 MODE + +LatenceTech agents discovery. + +=over 8 + +=back + +=cut diff --git a/src/apps/monitoring/latencetech/restapi/plugin.pm b/src/apps/monitoring/latencetech/restapi/plugin.pm index bfe0861b4..c98de8451 100644 --- a/src/apps/monitoring/latencetech/restapi/plugin.pm +++ b/src/apps/monitoring/latencetech/restapi/plugin.pm @@ -32,7 +32,8 @@ sub new { $self->{version} = '1.0'; $self->{modes} = { - 'connectivity' => 'apps::monitoring::latencetech::restapi::mode::connectivity' + 'connectivity' => 'apps::monitoring::latencetech::restapi::mode::connectivity', + 'discovery' => 'apps::monitoring::latencetech::restapi::mode::discovery' }; $self->{custom_modes}->{api} = 'apps::monitoring::latencetech::restapi::custom::api'; diff --git a/tests/apps/monitoring/latencetech/restapi/discovery.robot b/tests/apps/monitoring/latencetech/restapi/discovery.robot new file mode 100644 index 000000000..97addb83c --- /dev/null +++ b/tests/apps/monitoring/latencetech/restapi/discovery.robot @@ -0,0 +1,37 @@ +*** Settings *** +Documentation Check the LatenceTech discovery mode with api custom mode + +Resource ${CURDIR}${/}..${/}..${/}..${/}..${/}resources/import.resource + +Suite Setup Start Mockoon ${MOCKOON_JSON} +Suite Teardown Stop Mockoon +Test Timeout 120s + +*** Variables *** +${MOCKOON_JSON} ${CURDIR}${/}mockoon.json + +${cmd} ${CENTREON_PLUGINS} +... --plugin=apps::monitoring::latencetech::restapi::plugin +... --custommode=api +... --mode=discovery +... --hostname=${HOSTNAME} +... --api-key=key +... --port=${APIPORT} +... --proto=http + +*** Test Cases *** +Discovery ${tc} + [Documentation] Check LatenceTech discovery + [Tags] apps monitoring latencetech restapi + + ${command} Catenate + ... ${cmd} + ... --customer-id=0 + ... --agent-id=0 + ... ${extraoptions} + Log ${cmd} + Ctn Run Command And Check Result As Json ${command} ${expected_result} + + Examples: tc extraoptions expected_result -- + ... 1 ${EMPTY} + ... {"duration":3716,"start_time":1753278888,"discovered_items":3,"results":[{"gpsPosition":"48.864716,2.349014","networkType":"Outscale","hardware":"Not-Applicable","networkName":"OutScale-Network","id":"0","details":"Generic QoSAgent preinstalled with OMI","name":"GenericQoSAgent"},{"gpsPosition":",undefined","networkType":"Azure Server Network","id":"2","details":"Azure Agent used for demonstration purposes","name":"Germany WEST","hardware":"Azure Server","networkName":"Azure Net 2"},{"details":"Uses india beacon reflector","id":"3","name":"South Central US","hardware":"Azure Server","networkName":"","networkType":"Azure Server Network","gpsPosition":",undefined"}],"end_time":1753282604} diff --git a/tests/apps/monitoring/latencetech/restapi/mockoon.json b/tests/apps/monitoring/latencetech/restapi/mockoon.json index 2f9b33dbb..d67c601e1 100644 --- a/tests/apps/monitoring/latencetech/restapi/mockoon.json +++ b/tests/apps/monitoring/latencetech/restapi/mockoon.json @@ -98,12 +98,45 @@ } ], "responseMode": null + }, + { + "uuid": "a8045b6a-886a-4ba0-a2b4-9c9d425f9124", + "type": "http", + "documentation": "", + "method": "get", + "endpoint": "api/v1/agents", + "responses": [ + { + "uuid": "0dfb8c64-6ee8-40aa-8f42-aefb39d9dd9d", + "body": "{\n \"CustomerID\": \"0\",\n \"count\": 3,\n \"agents\": [\n {\n \"id\": \"0\",\n \"name\": \"GenericQoSAgent\",\n \"networkName\": \"OutScale-Network\",\n \"networkType\": \"Outscale\",\n \"hardware\": \"Not-Applicable\",\n \"details\": \"Generic QoSAgent preinstalled with OMI\",\n \"gpsPosition\": \"48.864716,2.349014\"\n },\n {\n \"id\": \"2\",\n \"name\": \"Germany WEST\",\n \"networkName\": \"Azure Net 2\",\n \"networkType\": \"Azure Server Network\",\n \"hardware\": \"Azure Server\",\n \"details\": \"Azure Agent used for demonstration purposes\",\n \"gpsPosition\": \",undefined\"\n },\n {\n \"id\": \"3\",\n \"name\": \"South Central US\",\n \"networkName\": \"\",\n \"networkType\": \"Azure Server Network\",\n \"hardware\": \"Azure Server\",\n \"details\": \"Uses india beacon reflector\",\n \"gpsPosition\": \",undefined\"\n }\n ]\n}", + "latency": 0, + "statusCode": 200, + "label": "", + "headers": [], + "bodyType": "INLINE", + "filePath": "", + "databucketID": "", + "sendFileAsBody": false, + "rules": [], + "rulesOperator": "OR", + "disableTemplating": false, + "fallbackTo404": false, + "default": true, + "crudKey": "id", + "callbacks": [] + } + ], + "responseMode": null } ], "rootChildren": [ { "type": "route", "uuid": "2a664365-e4a5-470d-a21e-08006280ba55" + }, + { + "type": "route", + "uuid": "a8045b6a-886a-4ba0-a2b4-9c9d425f9124" } ], "proxyMode": false,