Debian Codegraph @

src/hardware/kvm/adder/aim/snmp/mode/deviceusage.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::kvm::adder::aim::snmp::mode::deviceusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = 'status : ' . $self->{result_values}->{status};
33 return $msg;
34 }
35
36 sub custom_status_calc {
37 my ($self, %options) = @_;
38
39 $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_device_status'};
40 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
41 return 0;
42 }
43
44 sub set_counters {
45 my ($self, %options) = @_;
46
47 $self->{maps_counters_type} = [
48 { name => 'global', type => 0, message_separator => ' - ' },
49 { name => 'device', type => 1, cb_prefix_output => 'prefix_device_output', message_multiple => 'All devices are ok' }
50 ];
51
52 $self->{maps_counters}->{global} = [
53 { label => 'total', set => {
54 key_values => [ { name => 'total' } ],
55 output_template => 'Total devices : %s',
56 perfdatas => [
57 { label => 'devices_total', value => 'total', template => '%s', min => 0 },
58 ],
59 }
60 },
61 { label => 'online', set => {
62 key_values => [ { name => 'online' }, { name => 'total' } ],
63 output_template => 'Online devices : %s',
64 perfdatas => [
65 { label => 'devices_online', value => 'online', template => '%s', min => 0, max => 'total' },
66 ],
67 }
68 },
69 ];
70 $self->{maps_counters}->{device} = [
71 { label => 'status', threshold => 0, set => {
72 key_values => [ { name => 'device_status' }, { name => 'display' } ],
73 closure_custom_calc => $self->can('custom_status_calc'),
74 closure_custom_output => $self->can('custom_status_output'),
75 closure_custom_perfdata => sub { return 0; },
76 closure_custom_threshold_check => \&catalog_status_threshold,
77 }
78 },
79 ];
80 }
81
82 sub prefix_device_output {
83 my ($self, %options) = @_;
84
85 return "Device '" . $options{instance_value}->{display} . "' ";
86 }
87
88 sub new {
89 my ($class, %options) = @_;
90 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
91 bless $self, $class;
92
93 $options{options}->add_options(arguments =>
94 {
95 "filter-name:s" => { name => 'filter_name' },
96 "warning-status:s" => { name => 'warning_status', default => '' },
97 "critical-status:s" => { name => 'critical_status', default => 'not %{status} =~ /online|rebooting|upgrading/i' },
98 });
99
100 return $self;
101 }
102
103 sub check_options {
104 my ($self, %options) = @_;
105 $self->SUPER::check_options(%options);
106
107 $self->change_macros(macros => ['warning_status', 'critical_status']);
108 }
109
110 my %map_status = (
111 1 => 'offline', 2 => 'online', 3 => 'rebooting', 4 => 'resetting',
112 5 => 'upgrading', 6 => 'unconfigured', 7 => 'backup', 8 => 'unknown',
113 );
114 my $oid_deviceName = '.1.3.6.1.4.1.25119.1.1.1.4';
115 my $mapping = {
116 deviceStatus => { oid => '.1.3.6.1.4.1.25119.1.1.1.20', map => \%map_status },
117 };
118
119 sub manage_selection {
120 my ($self, %options) = @_;
121
122 my $snmp_result = $options{snmp}->get_table(oid => $oid_deviceName, nothing_quit => 1);
123 $self->{device} = {};
124 $self->{global} = { total => 0, online => 0 };
125 foreach my $oid (keys %{$snmp_result}) {
126 $oid =~ /^$oid_deviceName\.(.*)$/;
127 my $instance = $1;
128 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
129 $snmp_result->{$oid} !~ /$self->{option_results}->{filter_name}/) {
130 $self->{output}->output_add(long_msg => "skipping device '" . $snmp_result->{$oid} . "'.", debug => 1);
131 next;
132 }
133
134 $self->{device}->{$instance} = { display => $snmp_result->{$oid} };
135 }
136
137 $options{snmp}->load(oids => [$mapping->{deviceStatus}->{oid}],
138 instances => [keys %{$self->{device}}], instance_regexp => '^(.*)$');
139 $snmp_result = $options{snmp}->get_leef(nothing_quit => 1);
140
141 foreach (keys %{$self->{device}}) {
142 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $_);
143 $self->{device}->{$_}->{device_status} = $result->{deviceStatus};
144 $self->{global}->{$result->{deviceStatus}}++ if (defined($self->{global}->{$result->{deviceStatus}}));
145 $self->{global}->{total}++;
146 }
147
148 if (scalar(keys %{$self->{device}}) <= 0) {
149 $self->{output}->add_option_msg(short_msg => "No device found.");
150 $self->{output}->option_exit();
151 }
152 }
153
154 1;
155
156 __END__
157
158 =head1 MODE
159
160 Check device usage.
161
162 =over 8
163
164 =item B<--filter-counters>
165
166 Only display some counters (regexp can be used).
167 Example: --filter-counters='^total$'
168
169 =item B<--filter-name>
170
171 Filter by device name (can be a regexp).
172
173 =item B<--warning-status>
174
175 Define the conditions to match for the status to be WARNING.
176 You can use the following variables: %{status}, %{display}
177
178 =item B<--critical-status>
179
180 Define the conditions to match for the status to be CRITICAL (default: 'not %{status} =~ /online|rebooting|upgrading/i').
181 You can use the following variables: %{status}, %{display}
182
183 =item B<--warning-*>
184
185 Warning threshold.
186 Can be: 'total', 'online'.
187
188 =item B<--critical-*>
189
190 Critical threshold.
191 Can be: 'total', 'online'.
192
193 =back
194
195 =cut