Debian Codegraph @

src/hardware/devices/cisco/ces/restapi/mode/peripherals.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 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 hardware::devices::cisco::ces::restapi::mode::peripherals;
22
23 use base qw(centreon::plugins::templates::counter);
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 { label => 'peripherals-connected', nlabel => 'system.peripherals.connected.count', set => {
37 key_values => [ { name => 'connected' } ],
38 output_template => 'peripherals connected: %d',
39 perfdatas => [
40 { template => '%d', min => 0 }
41 ]
42 }
43 }
44 ];
45 }
46
47 sub new {
48 my ($class, %options) = @_;
49 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
50 bless $self, $class;
51
52 $self->{version} = '1.0';
53 $options{options}->add_options(arguments => {
54 'filter-since:s' => { name => 'filter_since', default => 86400 }
55 });
56
57 centreon::plugins::misc::mymodule_load(
58 output => $self->{output},
59 module => 'Date::Parse',
60 error_msg => "Cannot load module 'Date::Parse'."
61 );
62 return $self;
63 }
64
65 sub manage_selection {
66 my ($self, %options) = @_;
67
68 my $result = $options{custom}->request_api(
69 method => 'POST',
70 url_path => '/putxml',
71 query_form_post => '<Command><Peripherals><List/></Peripherals></Command>',
72 ForceArray => ['Device']
73 );
74
75 $self->{global} = { connected => 0 };
76
77 return if (!defined($result->{PeripheralsListResult}->{Device}));
78
79 foreach (@{$result->{PeripheralsListResult}->{Device}}) {
80 if (defined($self->{option_results}->{filter_since}) && $self->{option_results}->{filter_since} =~ /\d+/) {
81 my $last_seen = Date::Parse::str2time($_->{LastSeen});
82 if (!defined($last_seen)) {
83 $self->{output}->output_add(
84 severity => 'UNKNOWN',
85 short_msg => "can't parse date '" . $_->{LastSeen} . "'"
86 );
87 next;
88 }
89 next if ($last_seen < time() - $self->{option_results}->{filter_since});
90 }
91
92 $self->{output}->output_add(
93 long_msg => sprintf(
94 'peripheral [name: %s] [Last Seen: %s] [NetworkAddress: %s] [SerialNumber: %s]',
95 $_->{Name},
96 $_->{LastSeen},
97 $_->{NetworkAddress},
98 $_->{SerialNumber}
99 )
100 );
101 $self->{global}->{connected}++;
102 }
103 }
104
105 1;
106
107 __END__
108
109 =head1 MODE
110
111 Check peripherals device connected (since TC 7.2).
112
113 =over 8
114
115 =item B<--filter-since>
116
117 Filter by since X seconds (default: 86400).
118
119 =item B<--warning-*> B<--critical-*>
120
121 Thresholds.
122 Can be: 'peripherals-connected'.
123
124 =back
125
126 =cut