Debian Codegraph @

src/hardware/devices/hms/netbiter/argos/restapi/mode/sensors.pm

raw
1 #
2 # Copyright 2024 Centreon (http://www.centreon.com/)
3 #
4 # Centreon is a full-fledged industry-strength solution that meets
5 # the needs in IT infrastructure and cluster 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 hardware::devices::hms::netbiter::argos::restapi::mode::sensors;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Date::Parse;
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 return sprintf('Device: %s, Name: %s, Value: %s%s (%s)',
33 $self->{result_values}->{device_name},
34 $self->{result_values}->{display},
35 $self->{result_values}->{value},
36 $self->{result_values}->{unit},
37 $self->{result_values}->{timestamp}
38 );
39 }
40
41 sub set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'sensors', type => 1, message_multiple => 'All sensors are OK' },
46 ];
47
48 $self->{maps_counters}->{sensors} = [
49 { label => 'sensor-value', nlabel => 'sensor.reading.count', set => {
50 key_values => [ { name => 'value' }, { name => 'display' }, { name => 'device_name' }, { name => 'timestamp' }, { name => 'unit' } ],
51 closure_custom_output => $self->can('custom_status_output'),
52 output_template => "value : %s",
53 closure_custom_perfdata => sub {
54 my ($self, %options) = @_;
55
56 $self->{output}->perfdata_add(
57 nlabel => $self->{nlabel},
58 unit => $self->{result_values}->{unit},
59 instances => $self->{result_values}->{display},
60 value => sprintf('%.1f', $self->{result_values}->{value}),
61 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
62 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel})
63 );
64 }
65 }
66 }
67 ];
68 }
69
70 sub new {
71 my ($class, %options) = @_;
72 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
73 bless $self, $class;
74
75 $options{options}->add_options(arguments => {
76 'filter-device:s' => { name => 'filter_device' },
77 'filter-name:s' => { name => 'filter_name' },
78 'filter-id:s' => { name => 'filter_id' },
79 'system-id:s' => { name => 'system_id' }
80 });
81
82 return $self;
83 }
84
85 sub check_options {
86 my ($self, %options) = @_;
87 $self->SUPER::check_options(%options);
88
89 if (!(defined($self->{option_results}->{system_id})) || $self->{option_results}->{system_id} eq '') {
90 $self->{output}->add_option_msg(short_msg => "Need to specify --system-id option.");
91 $self->{output}->option_exit();
92 }
93 }
94
95 sub manage_selection {
96 my ($self, %options) = @_;
97
98 my $sensors = $options{custom}->list_sensors(system_id => $self->{option_results}->{system_id});
99
100 foreach (@{$sensors}) {
101 next if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
102 $_->{id} !~ /$self->{option_results}->{filter_id}/);
103 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
104 $_->{name} !~ /$self->{option_results}->{filter_name}/);
105 next if (defined($self->{option_results}->{filter_device}) && $self->{option_results}->{filter_device} ne '' &&
106 $_->{deviceName} !~ /$self->{option_results}->{filter_device}/);
107
108 my $url = '/system/' . $self->{option_results}->{system_id} . '/log/' . $_->{id};
109 my $result = $options{custom}->request_api(
110 request => $url,
111 get_params => [ 'limitrows=1' ]
112 );
113
114 if (scalar(@{$result}) <= 0) {
115 $self->{output}->add_option_msg(short_msg => 'Skipping ' . $_->{name} . ' (no value)');
116 next
117 }
118
119 foreach my $entry (@{$result}) {
120 my $timestamp = $options{custom}->convert_iso8601_to_epoch(time_string => $entry->{timestamp});
121 $self->{sensors}->{$_->{id}} = {
122 device_name => $_->{deviceName},
123 display => $_->{name},
124 timestamp => POSIX::strftime('%Y-%m-%d %H:%M:%S %Z', localtime($timestamp)),
125 unit => $_->{unit},
126 value => $entry->{value}
127 }
128 }
129 }
130
131 if (scalar(keys %{$self->{sensors}}) <= 0) {
132 $self->{output}->add_option_msg(short_msg => "No sensor found.");
133 $self->{output}->option_exit();
134 }
135 }
136
137 1;
138
139 __END__
140
141 =head1 MODE
142
143 Check Netbiter sensors values using Argos RestAPI.
144
145 Example:
146 perl centreon_plugins.pl --plugin=hardware::devices::hms::netbiter::argos::restapi::plugin --mode=sensors
147 --access-key='ABCDEFG1234567890' --system-id='XYZ123' --filter-name='My Sensor' --verbose
148
149 More information on'https://apidocs.netbiter.net/?page=methods&show=getSystemLoggedValues'.
150
151 =over 8
152
153 =item B<--system-id>
154
155 Set the Netbiter Argos System ID (mandatory).
156
157 =item B<--filter-id>
158
159 Filter by sensor ID (regexp can be used).
160 Example: --filter-id='^1234.5678$'
161
162 =item B<--filter-device>
163
164 Filter by device name (regexp can be used).
165 Example: --filter-device='^ZONE(1|2)$'
166
167 =item B<--filter-name>
168
169 Filter by sensor name (regexp can be used).
170 Example: --filter-name='^temperature_(in|out)$'
171
172 =item B<--warning-sensor-value>
173
174 Warning threshold.
175
176 =item B<--critical-sensor-value>
177
178 Critical threshold.
179
180 =back
181
182 =cut