(plugin) hardware::devices::hms::netbiter::argos::restapi - new (#4352)

hardware::devices::hms::netbiter::argos::restapi - new
This commit is contained in:
Thibault S 2023-04-27 10:00:47 +02:00 committed by GitHub
parent cbd3818393
commit ab787eb543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 1014 additions and 0 deletions

View File

@ -0,0 +1,6 @@
{
"dependencies": [
"libdatetime-perl",
"libdatetime-format-dateparse-perl"
]
}

View File

@ -0,0 +1,9 @@
{
"pkg_name": "centreon-plugin-Hardware-Devices-Hms-Netbiter-Argos-Restapi",
"pkg_summary": "Centreon Plugin to monitor HMS/Ewon Netbiter devices using Argos Rest Api",
"plugin_name": "centreon_hms_netbiter_argos_restapi.pl",
"files": [
"centreon/plugins/script_custom.pm",
"hardware/devices/hms/netbiter/argos/restapi"
]
}

View File

@ -0,0 +1,6 @@
{
"dependencies": [
"perl(DateTime)",
"perl(Date::Parse)"
]
}

View File

@ -0,0 +1,306 @@
#
# Copyright 2023 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 hardware::devices::hms::netbiter::argos::restapi::custom::api;
use strict;
use warnings;
use DateTime;
use centreon::plugins::http;
use centreon::plugins::statefile;
use JSON::XS;
use Digest::MD5 qw(md5_hex);
sub new {
my ($class, %options) = @_;
my $self = {};
bless $self, $class;
if (!defined($options{output})) {
print "Class Custom: Need to specify 'output' argument.\n";
exit 3;
}
if (!defined($options{options})) {
$options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
$options{output}->option_exit();
}
if (!defined($options{noptions})) {
$options{options}->add_options(arguments => {
'access-key:s' => { name => 'access_key' },
'api-endpoint:s' => { name => 'api_endpoint' },
'api-password:s' => { name => 'api_password' },
'api-username:s' => { name => 'api_username' },
'force-cache-reload' => { name => 'force_cache_reload' },
'hostname:s' => { name => 'hostname' },
'port:s' => { name => 'port' },
'proto:s' => { name => 'proto' },
'reload-cache-time:s' => { name => 'reload_cache_time' },
'timeout:s' => { name => 'timeout' }
});
}
$options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1);
$self->{output} = $options{output};
$self->{http} = centreon::plugins::http->new(%options, default_backend => 'curl');
$self->{cache_auth} = centreon::plugins::statefile->new(%options);
$self->{cache_sensors} = centreon::plugins::statefile->new(%options);
return $self;
}
sub set_options {
my ($self, %options) = @_;
$self->{option_results} = $options{option_results};
}
sub set_defaults {}
sub check_options {
my ($self, %options) = @_;
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : 'api.netbiter.net';
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 443;
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'https';
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 10;
$self->{api_endpoint} = (defined($self->{option_results}->{api_endpoint})) ? $self->{option_results}->{api_endpoint} : '/operation/v1/rest/json';
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : '';
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : '';
$self->{access_key} = (defined($self->{option_results}->{access_key})) ? $self->{option_results}->{access_key} : '';
$self->{reload_cache_time} = (defined($self->{option_results}->{reload_cache_time})) ? $self->{option_results}->{reload_cache_time} : 3600;
$self->{force_cache_reload} = (defined($self->{option_results}->{force_cache_reload})) ? $self->{option_results}->{force_cache_reload} : undef;
if ( !(($self->{api_username} ne '' && $self->{api_password} ne '') || ($self->{access_key} ne '')) ) {
$self->{output}->add_option_msg(short_msg => 'At least one of --api-username / --api-password (login) ; or --access-key must be set');
$self->{output}->option_exit();
}
$self->{cache_auth}->check_options(option_results => $self->{option_results});
$self->{cache_sensors}->check_options(option_results => $self->{option_results});
return 0;
}
sub build_options_for_httplib {
my ($self, %options) = @_;
$self->{option_results}->{hostname} = $self->{hostname};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{port} = $self->{port};
$self->{option_results}->{proto} = $self->{proto};
$self->{option_results}->{timeout} = $self->{timeout};
$self->{option_results}->{warning_status} = '';
$self->{option_results}->{critical_status} = '';
$self->{option_results}->{unknown_status} = '%{http_code} < 200 or %{http_code} > 400';
}
sub settings {
my ($self, %options) = @_;
$self->build_options_for_httplib();
$self->{http}->add_header(key => 'Accept', value => 'application/json');
$self->{http}->set_options(%{$self->{option_results}});
}
sub convert_iso8601_to_epoch {
my ($self, %options) = @_;
if ($options{time_string} =~ /(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/) {
my $dt = DateTime->new(
year => $1,
month => $2,
day => $3,
hour => $4,
minute => $5,
second => $6
);
my $epoch_time = $dt->epoch();
return $epoch_time;
}
$self->{output}->add_option_msg(short_msg => "Wrong date format: $options{time_string}");
$self->{output}->option_exit();
}
sub get_access_key {
my ($self, %options) = @_;
my $has_cache_file = $self->{cache_auth}->read(statefile => 'netbiter_argos_api_' . md5_hex($self->{hostname}) . '_' . $self->{cache_key});
my $expires_on = $self->{cache_auth}->get(name => 'expires_on');
my $access_key = $self->{cache_auth}->get(name => 'access_key');
if ( $has_cache_file == 0 || !defined($access_key) || (($expires_on - time()) < 10) ) {
my $login;
if ($self->{api_username} ne '') {
$login = { userName => $self->{api_username}, password => $self->{api_password} };
}
my $post_json = JSON::XS->new->utf8->encode($login);
$self->settings();
my $content = $self->{http}->request(
method => 'POST',
header => ['Content-type: application/json'],
query_form_post => $post_json,
url_path => $self->{api_endpoint} . '/user/authenticate'
);
if (!defined($content) || $content eq '') {
$self->{output}->add_option_msg(short_msg => "Authentication endpoint returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->utf8->decode($content);
};
if ($@) {
$self->{output}->output_add(long_msg => $content, debug => 1);
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
if (defined($decoded->{error_code})) {
$self->{output}->output_add(long_msg => "Error message : " . $decoded->{error}, debug => 1);
$self->{output}->add_option_msg(short_msg => "Authentication endpoint returns error code '" . $decoded->{error_code} . "' (add --debug option for detailed message)");
$self->{output}->option_exit();
}
$access_key = $decoded->{access_key};
my $expires_on = $self->convert_iso8601_to_epoch(time_string => $decoded->{expires});
my $datas = { last_timestamp => time(), access_key => $decoded->{accessKey}, expires_on => $expires_on };
$self->{cache_auth}->write(data => $datas);
}
return $access_key;
}
sub request_api {
my ($self, %options) = @_;
if (!defined($self->{access_key})) {
$self->{access_key} = $self->get_access_key(statefile => $self->{cache});
}
$self->settings();
my $url_path = $self->{api_endpoint} . $options{request};
my $parameters = defined($options{get_params}) ? $options{get_params} : [];
my $response = $self->{http}->request(
method => 'GET',
get_param => [ 'accesskey=' . $self->{access_key}, @$parameters ],
url_path => $url_path
);
# check rate limit => to do
#my $rate_limit = $self->{http}->get_header(name => 'argos-ratelimit-remaining');
if (!defined($response) || $response eq '') {
$self->{output}->add_option_msg(short_msg => "API returns empty content [code: '" . $self->{http}->get_code() . "'] [message: '" . $self->{http}->get_message() . "']");
$self->{output}->option_exit();
}
my $decoded;
eval {
$decoded = JSON::XS->new->decode($response);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode response (add --debug option to display returned content)");
$self->{output}->option_exit();
}
return $decoded;
}
sub list_sensors {
my ($self, %options) = @_;
# Results are cached to avoid too many API calls
my $has_cache_file = $self->{cache_sensors}->read(statefile => 'netbiter_cache_sensors_' . md5_hex($options{system_id}));
my $response = $self->{cache_sensors}->get(name => 'response');
my $freshness = defined($self->{cache_sensors}->get(name => 'update_time')) ? time() - $self->{cache_sensors}->get(name => 'update_time') : undef;
$self->{force_cache_reload} = 1 if defined($options{force});
if ( $has_cache_file == 0 || !defined($response) || (defined($freshness)) && ($freshness > $self->{reload_cache_time}) || defined($self->{force_cache_reload}) ) {
my $request = '/system/' . $options{system_id} . '/log/config';
$response = $self->request_api(request => $request);
}
$self->{cache_sensors}->write(data => {
update_time => time(),
response => $response
});
return $response
}
1;
__END__
=head1 NAME
Netbiter Argos RestAPI
=head1 REST API OPTIONS
Netbiter Argos RestAPI
=over 8
=item B<--hostname>
Argos API hostname (Default: api.netbiter.net).
=item B<--port>
Port used (Default: 443)
=item B<--proto>
Specify https if needed (Default: 'https')
=item B<--api-endpoint>
Argos API requests endpoint (Default: '/operation/v1/rest/json')
=item B<--access-key>
For Access Key "direct" authentication method.
Example: --access-key='ABCDEFG1234567890'
=item B<--api-username>
For Username/Password authentication method.
Must be used with --api-password option.
=item B<--api-password>
For Username/Password authentication method.
Must be used with --api-username option.
=item B<--timeout>
Set timeout in seconds (Default: 10).
=back
=head1 DESCRIPTION
B<custom>.
=cut

View File

@ -0,0 +1,247 @@
#
# Copyright 2023 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and cluster 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 hardware::devices::hms::netbiter::argos::restapi::mode::alarms;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Date::Parse;
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
sub prefix_global_output {
my ($self, %options) = @_;
return 'Alarms: ';
}
sub prefix_alarms_output {
my ($self, %options) = @_;
return sprintf('[%s] Alarm name: "%s", Device name: "%s" ', $options{instance_value}->{timestamp}, $options{instance_value}->{display}, $options{instance_value}->{device_name});
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'global', type => 0, cb_prefix_output => 'prefix_global_output', skipped_code => { -10 => 1 } },
{ name => 'alarms', type => 1, cb_prefix_output => 'prefix_alarms_output' }
];
$self->{maps_counters}->{global} = [
{ label => 'alarms-total', nlabel => 'alarms.total.count', set => {
key_values => [ { name => 'total' } ],
output_template => 'total current: %s',
perfdatas => [ { template => '%d', min => 0 } ]
}
}
];
$self->{maps_counters}->{alarms} = [
{ label => 'alarm-active',
type => 2,
critical_default => '%{active} =~ "true"',
set => {
key_values => [ { name => 'active' } ],
output_template => 'active: %s',
display_ok => 0,
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'alarm-acked',
type => 2,
warning_default => '%{acked} =~ "false"',
set => {
key_values => [ { name => 'acked' } ],
output_template => 'acknowledged: %s',
display_ok => 0,
closure_custom_perfdata => sub { return 0; },
closure_custom_threshold_check => \&catalog_status_threshold_ng
}
},
{ label => 'alarm-duration', nlabel => 'alarm.duration.seconds',
type => 1,
set => {
key_values => [ { name => 'duration' }, { name => 'display' } ],
output_template => 'duration: %ds',
display_ok => 0,
perfdatas => [ { template => '%d', min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display' } ]
}
},
{ label => 'alarm-severity',
type => 1,
set => {
key_values => [ { name => 'severity' } ],
output_template => 'severity: %s',
display_ok => 0,
closure_custom_perfdata => sub { return 0; }
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-acked' => { name => 'filter_acked' },
'filter-active' => { name => 'filter_active' },
'filter-severity:s' => { name => 'filter_severity' },
'system-id:s' => { name => 'system_id' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (defined($self->{option_results}->{filter_severity}) && $self->{option_results}->{filter_severity} ne '') {
if (lc($self->{option_results}->{filter_severity}) !~ '/critical|major|minor|warning|cleared/') {
$self->{output}->add_option_msg(short_msg => 'Unknown severity "' . $self->{option_results}->{filter_severity} . '"');
$self->{output}->option_exit();
}
$self->{severity} = lc($self->{option_results}->{filter_severity});
}
if (!(defined($self->{option_results}->{system_id})) || $self->{option_results}->{system_id} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --system-id option.");
$self->{output}->option_exit();
}
}
sub manage_selection {
my ($self, %options) = @_;
$self->{global}->{total} = 0;
my $severity_mapping = {
critical => '1',
major => '2',
minor => '3',
warning => '4',
cleared => '5'
};
my $active_alarms = 'false';
my $url = '/system/' . $self->{option_results}->{system_id} . '/alarm';
my $get_params;
if (defined($self->{severity})) {
my $severity_level = $severity_mapping->{$self->{severity}};
push @$get_params, 'severity=' . $severity_level;
}
if (defined($self->{option_results}->{filter_acked})) {
push @$get_params, 'acked=false';
}
if (defined($self->{option_results}->{filter_active})) {
$active_alarms = 'true';
}
push @$get_params, 'active=' . $active_alarms;
my $result = $options{custom}->request_api(
request => $url,
get_params => $get_params
);
foreach (@{$result}) {
my $timestamp = str2time($_->{timestamp}, 'GMT');
$self->{alarms}->{$_->{id}} = {
acked => ($_->{acked}) ? 'true' : 'false',
active => ($_->{active}) ? 'true' : 'false',
device_name => $_->{deviceName},
duration => time() - $timestamp,
display => $_->{name},
id => $_->{id},
severity => $_->{severity},
timestamp => POSIX::strftime('%d-%m-%Y %H:%M:%S %Z', localtime($timestamp))
};
}
$self->{global}->{total} = scalar (keys %{$self->{alarms}});
}
1;
__END__
=head1 MODE
Check Netbiter alarms using Argos RestAPI.
Example:
perl centreon_plugins.pl --plugin=hardware::devices::hms::netbiter::argos::restapi::plugin --mode=alarms
--access-key='ABCDEFG1234567890' --system-id='XYZ123' --filter-active --verbose
More information on'https://apidocs.netbiter.net/?page=methods&show=getSystemAlarms'.
=over 8
=item B<--system-id>
Set the Netbiter Argos System ID (Mandatory).
=item B<--filter-acked>
Hide acknowledged alarms.
=item B<--filter-active>
Only show active alarms.
=item B<--filter-severity>
Only show alarms with a given severity level.
Can be: 'critical', 'major', 'minor', 'warning', 'cleared'.
Only one value can be set (no multiple values).
=item B<--warning-active-status>
Set warning threshold for active status (Default: '').
Typical syntax: --warning-active-status='%{active} =~ "true"'
=item B<--critical-active-status>
Set critical threshold for active status (Default: '%{active} =~ "true"').
Typical syntax: --critical-active-status='%{active} =~ "true"'
=item B<--warning-acked-status>
Set warning threshold for acked status (Default: '%{acked} =~ "false"').
Typical syntax: --warning-acked-status='%{acked} =~ "false"'
=item B<--critical-acked-status>
Set critical threshold for acked status (Default: '').
Typical syntax: --critical-acked-status='%{acked} =~ "false"'
=item B<--warning-*> B<--critical-*>
Thresholds.
Can be: 'warning-alarms-total' (count) 'critical-alarms-total' (count),
'warning-alarm-duration' (s), 'critical-alarm-duration' (s),
'warning-alarm-severity' (level from 0 to 5), critical-alarm-severity (level from 0 to 5).
=back
=cut

View File

@ -0,0 +1,88 @@
#
# Copyright 2023 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 hardware::devices::hms::netbiter::argos::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 $results = $options{custom}->request_api(request => '/system');
$disco_stats->{results} = $results;
$disco_stats->{end_time} = time();
$disco_stats->{duration} = $disco_stats->{end_time} - $disco_stats->{start_time};
$disco_stats->{discovered_items} = scalar(@{$disco_stats->{results}});
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
Resources discovery.
=over 8
=back
=cut

View File

@ -0,0 +1,118 @@
#
# Copyright 2023 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 hardware::devices::hms::netbiter::argos::restapi::mode::listsensors;
use base qw(centreon::plugins::mode);
use strict;
use warnings;
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$options{options}->add_options(arguments => {
"system-id:s" => { name => 'system_id' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::init(%options);
if (!(defined($self->{option_results}->{system_id})) || $self->{option_results}->{system_id} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --system-id option.");
$self->{output}->option_exit();
}
}
sub manage_selection {
my ($self, %options) = @_;
return $options{custom}->list_sensors(system_id => $self->{option_results}->{system_id}, force => 1);
}
sub run {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (@$results) {
next if ($_->{pointType} ne 'R');
$self->{output}->output_add(
long_msg => sprintf(
'[id: %s][name: %s][device name: %s][unit: %s][log interval: %s]',
$_->{id},
$_->{name},
$_->{deviceName},
$_->{unit},
$_->{logInterval}
));
}
$self->{output}->output_add(
severity => 'OK',
short_msg => 'List sensors:'
);
$self->{output}->display(nolabel => 1, force_ignore_perfdata => 1, force_long_output => 1);
$self->{output}->exit();
}
sub disco_format {
my ($self, %options) = @_;
$self->{output}->add_disco_format(elements => ['id', 'name', 'deviceName', 'unit', 'logInterval']);
}
sub disco_show {
my ($self, %options) = @_;
my $results = $self->manage_selection(%options);
foreach (@$results) {
$self->{output}->add_disco_entry(
deviceName => $_->{deviceName},
id => $_->{id},
logInterval => $_->{logInterval},
name => $_->{name},
unit => defined($_->{unit}) ? $_->{unit} : ''
);
}
}
1;
__END__
=head1 MODE
List Netbiter log sensors using Argos RestAPI.
=over 8
=item B<--system-id>
Set the Netbiter Argos System ID (Mandatory).
=back
=cut

View File

@ -0,0 +1,182 @@
#
# Copyright 2023 Centreon (http://www.centreon.com/)
#
# Centreon is a full-fledged industry-strength solution that meets
# the needs in IT infrastructure and cluster 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 hardware::devices::hms::netbiter::argos::restapi::mode::sensors;
use base qw(centreon::plugins::templates::counter);
use strict;
use warnings;
use Date::Parse;
sub custom_status_output {
my ($self, %options) = @_;
return sprintf('Device: %s, Name: %s, Value: %s%s (%s)',
$self->{result_values}->{device_name},
$self->{result_values}->{display},
$self->{result_values}->{value},
$self->{result_values}->{unit},
$self->{result_values}->{timestamp}
);
}
sub set_counters {
my ($self, %options) = @_;
$self->{maps_counters_type} = [
{ name => 'sensors', type => 1, message_multiple => 'All sensors are OK' },
];
$self->{maps_counters}->{sensors} = [
{ label => 'sensor-value', nlabel => 'sensor.reading.count', set => {
key_values => [ { name => 'value' }, { name => 'display' }, { name => 'device_name' }, { name => 'timestamp' }, { name => 'unit' } ],
closure_custom_output => $self->can('custom_status_output'),
output_template => "value : %s",
closure_custom_perfdata => sub {
my ($self, %options) = @_;
$self->{output}->perfdata_add(
nlabel => $self->{nlabel},
unit => $self->{result_values}->{unit},
instances => $self->{result_values}->{display},
value => sprintf('%.1f', $self->{result_values}->{value}),
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
);
}
}
}
];
}
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
bless $self, $class;
$options{options}->add_options(arguments => {
'filter-device:s' => { name => 'filter_device' },
'filter-name:s' => { name => 'filter_name' },
'filter-id:s' => { name => 'filter_id' },
'system-id:s' => { name => 'system_id' }
});
return $self;
}
sub check_options {
my ($self, %options) = @_;
$self->SUPER::check_options(%options);
if (!(defined($self->{option_results}->{system_id})) || $self->{option_results}->{system_id} eq '') {
$self->{output}->add_option_msg(short_msg => "Need to specify --system-id option.");
$self->{output}->option_exit();
}
}
sub manage_selection {
my ($self, %options) = @_;
my $sensors = $options{custom}->list_sensors(system_id => $self->{option_results}->{system_id});
foreach (@{$sensors}) {
next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
$_->{id} !~ /$self->{option_results}->{filter_id}/);
next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
$_->{name} !~ /$self->{option_results}->{filter_name}/);
next if (defined($self->{option_results}->{filter_device}) && $self->{option_results}->{filter_device} ne '' &&
$_->{deviceName} !~ /$self->{option_results}->{filter_device}/);
my $url = '/system/' . $self->{option_results}->{system_id} . '/log/' . $_->{id};
my $result = $options{custom}->request_api(
request => $url,
get_params => [ 'limitrows=1' ]
);
if (scalar(@{$result}) <= 0) {
$self->{output}->add_option_msg(short_msg => 'Skipping ' . $_->{name} . ' (no value)');
next
}
foreach my $entry (@{$result}) {
my $timestamp = $options{custom}->convert_iso8601_to_epoch(time_string => $entry->{timestamp});
$self->{sensors}->{$_->{id}} = {
device_name => $_->{deviceName},
display => $_->{name},
timestamp => POSIX::strftime('%Y-%m-%d %H:%M:%S %Z', localtime($timestamp)),
unit => $_->{unit},
value => $entry->{value}
}
}
}
if (scalar(keys %{$self->{sensors}}) <= 0) {
$self->{output}->add_option_msg(short_msg => "No sensor found.");
$self->{output}->option_exit();
}
}
1;
__END__
=head1 MODE
Check Netbiter sensors values using Argos RestAPI.
Example:
perl centreon_plugins.pl --plugin=hardware::devices::hms::netbiter::argos::restapi::plugin --mode=sensors
--access-key='ABCDEFG1234567890' --system-id='XYZ123' --filter-name='My Sensor' --verbose
More information on'https://apidocs.netbiter.net/?page=methods&show=getSystemLoggedValues'.
=over 8
=item B<--system-id>
Set the Netbiter Argos System ID (Mandatory).
=item B<--filter-id>
Filter by sensor ID (Regexp can be used).
Example: --filter-id='^1234.5678$'
=item B<--filter-device>
Filter by device name (Regexp can be used).
Example: --filter-device='^ZONE(1|2)$'
=item B<--filter-name>
Filter by sensor name (Regexp can be used).
Example: --filter-name='^temperature_(in|out)$'
=item B<--warning-sensor-value>
Warning threshold.
=item B<--critical-sensor-value>
Critical threshold.
=back
=cut

View File

@ -0,0 +1,52 @@
#
# Copyright 2023 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 hardware::devices::hms::netbiter::argos::restapi::plugin;
use strict;
use warnings;
use base qw(centreon::plugins::script_custom);
sub new {
my ($class, %options) = @_;
my $self = $class->SUPER::new(package => __PACKAGE__, %options);
bless $self, $class;
$self->{version} = '1.0';
$self->{modes} = {
'alarms' => 'hardware::devices::hms::netbiter::argos::restapi::mode::alarms',
'discovery' => 'hardware::devices::hms::netbiter::argos::restapi::mode::discovery',
'list-sensors' => 'hardware::devices::hms::netbiter::argos::restapi::mode::listsensors',
'sensors' => 'hardware::devices::hms::netbiter::argos::restapi::mode::sensors'
};
$self->{custom_modes}->{restapi} = 'hardware::devices::hms::netbiter::argos::restapi::custom::api';
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Netbiter IoT devices using Argos RestAPI.
=cut