Debian Codegraph @

src/network/dlink/standard/snmp/mode/cpu.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 network::dlink::standard::snmp::mode::cpu;
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 => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } },
33 { name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output', message_separator => ' ', message_multiple => 'All core cpu are ok', skipped_code => { -10 => 1 } }
34 ];
35
36 $self->{maps_counters}->{cpu_avg} = [
37 { label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => {
38 key_values => [ { name => 'average_5s' } ],
39 output_template => '%.2f %% (5s)',
40 perfdatas => [
41 { template => '%.2f', min => 0, max => 100, unit => '%' }
42 ]
43 }
44 },
45 { label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => {
46 key_values => [ { name => 'average_1m' } ],
47 output_template => '%.2f %% (1m)',
48 perfdatas => [
49 { template => '%.2f', min => 0, max => 100, unit => '%' }
50 ]
51 }
52 },
53 { label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => {
54 key_values => [ { name => 'average_5m' } ],
55 output_template => '%.2f %% (5m)',
56 perfdatas => [
57 { template => '%.2f', min => 0, max => 100, unit => '%' }
58 ]
59 }
60 }
61 ];
62
63 $self->{maps_counters}->{cpu_core} = [
64 { label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => {
65 key_values => [ { name => 'cpu_5s' }, { name => 'display' } ],
66 output_template => '%.2f %% (5s)',
67 perfdatas => [
68 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }
69 ]
70 }
71 },
72 { label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => {
73 key_values => [ { name => 'cpu_1m' }, { name => 'display' } ],
74 output_template => '%.2f %% (1m)',
75 perfdatas => [
76 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }
77 ]
78 }
79 },
80 { label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => {
81 key_values => [ { name => 'cpu_5m' }, { name => 'display' } ],
82 output_template => '%.2f %% (5m)',
83 perfdatas => [
84 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'display' }
85 ]
86 }
87 }
88 ];
89 }
90
91 sub prefix_cpu_avg_output {
92 my ($self, %options) = @_;
93
94 return $self->{cpu_avg}->{count} . " CPU(s) average usage is ";
95 }
96
97 sub prefix_cpu_core_output {
98 my ($self, %options) = @_;
99
100 return "CPU '" . $options{instance_value}->{display} . "' usage ";
101 }
102
103 sub new {
104 my ($class, %options) = @_;
105 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
106 bless $self, $class;
107
108 $options{options}->add_options(arguments => {
109 'check-order:s' => { name => 'check_order', default => 'common,industrial,agent' },
110 });
111
112 return $self;
113 }
114
115 sub check_table_cpu {
116 my ($self, %options) = @_;
117
118 my $instances = {};
119 foreach my $oid (keys %{$options{snmp_result}}) {
120 $oid =~ /\.(\d+)\.(\d+)$/;
121 my $display = 'unit' . $1 . $self->{output}->get_instance_perfdata_separator() . $2;
122 my $instance = $1 . '.' . $2;
123 next if (defined($instances->{$instance}));
124 $instances->{$instance} = 1;
125
126 my $cpu5sec = defined($options{snmp_result}->{$options{sec5} . '.' . $instance}) ? $options{snmp_result}->{$options{sec5} . '.' . $instance} : undef;
127 my $cpu1min = defined($options{snmp_result}->{$options{min1} . '.' . $instance}) ? $options{snmp_result}->{$options{min1} . '.' . $instance} : undef;
128 my $cpu5min = defined($options{snmp_result}->{$options{min5} . '.' . $instance}) ? $options{snmp_result}->{$options{min5} . '.' . $instance} : undef;
129
130 # Case that it's maybe other CPU oid in table for datas.
131 next if (!defined($cpu5sec) && !defined($cpu1min) && !defined($cpu5min));
132
133 $self->{checked_cpu} = 1;
134 $self->{cpu_core}->{$instance} = {
135 display => $display,
136 cpu_5s => $cpu5sec,
137 cpu_1m => $cpu1min,
138 cpu_5m => $cpu5min
139 };
140 }
141 }
142
143 sub check_cpu_average {
144 my ($self, %options) = @_;
145
146 my $count = scalar(keys %{$self->{cpu_core}});
147 my ($avg_5s, $avg_1m, $avg_5m);
148 foreach (values %{$self->{cpu_core}}) {
149 $avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s}
150 if (defined($_->{cpu_5s}));
151 $avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m}
152 if (defined($_->{cpu_1m}));
153 $avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m}
154 if (defined($_->{cpu_5m}));
155 }
156
157 $self->{cpu_avg} = {
158 average_5s => defined($avg_5s) ? $avg_5s / $count : undef,
159 average_1m => defined($avg_1m) ? $avg_1m / $count : undef,
160 average_5m => defined($avg_5m) ? $avg_5m / $count : undef,
161 count => $count,
162 };
163 }
164
165 sub check_cpu_industrial {
166 my ($self, %options) = @_;
167
168 return if ($self->{checked_cpu} == 1);
169
170 my $oid_dEntityExtCpuUtilEntry = '.1.3.6.1.4.1.171.14.5.1.7.1';
171 my $oid_dEntityExtCpuUtilFiveSeconds = '.1.3.6.1.4.1.171.14.5.1.7.1.3';
172 my $oid_dEntityExtCpuUtilOneMinute = '.1.3.6.1.4.1.171.14.5.1.7.1.4';
173 my $oid_dEntityExtCpuUtilFiveMinutes = '.1.3.6.1.4.1.171.14.5.1.7.1.5';
174
175 my $snmp_result = $options{snmp}->get_table(
176 oid => $oid_dEntityExtCpuUtilEntry, start => $oid_dEntityExtCpuUtilFiveSeconds, end => $oid_dEntityExtCpuUtilFiveMinutes
177 );
178 $self->check_table_cpu(
179 snmp_result => $snmp_result,
180 sec5 => $oid_dEntityExtCpuUtilFiveSeconds,
181 min1 => $oid_dEntityExtCpuUtilOneMinute,
182 min5 => $oid_dEntityExtCpuUtilFiveMinutes
183 );
184 }
185
186 sub check_cpu_agent {
187 my ($self, %options) = @_;
188
189 return if ($self->{checked_cpu} == 1);
190
191 my $oid_agentCPUutilizationIn5sec = '.1.3.6.1.4.1.171.12.1.1.6.1.0';
192 my $oid_agentCPUutilizationIn1min = '.1.3.6.1.4.1.171.12.1.1.6.2.0';
193 my $oid_agentCPUutilizationIn5min = '.1.3.6.1.4.1.171.12.1.1.6.3.0';
194
195 my $snmp_result = $options{snmp}->get_leef(
196 oids => [$oid_agentCPUutilizationIn5sec, $oid_agentCPUutilizationIn1min, $oid_agentCPUutilizationIn5min]
197 );
198
199 if (defined($snmp_result->{$oid_agentCPUutilizationIn5min})) {
200 $self->{checked_cpu} = 1;
201 $self->{cpu_core}->{0} = {
202 display => 0,
203 cpu_5s => $snmp_result->{$oid_agentCPUutilizationIn5sec},
204 cpu_1m => $snmp_result->{$oid_agentCPUutilizationIn1min},
205 cpu_5m => $snmp_result->{$oid_agentCPUutilizationIn5min}
206 };
207 }
208 }
209
210 sub check_cpu_common {
211 my ($self, %options) = @_;
212
213 return if ($self->{checked_cpu} == 1);
214
215 my $oid_esEntityExtCpuUtilEntry = '.1.3.6.1.4.1.171.17.5.1.7.1';
216 my $oid_esEntityExtCpuUtilFiveSeconds = '.1.3.6.1.4.1.171.17.5.1.7.1.3';
217 my $oid_esEntityExtCpuUtilOneMinute = '.1.3.6.1.4.1.171.17.5.1.7.1.4';
218 my $oid_esEntityExtCpuUtilFiveMinutes = '.1.3.6.1.4.1.171.17.5.1.7.1.5';
219
220 my $snmp_result = $options{snmp}->get_table(
221 oid => $oid_esEntityExtCpuUtilEntry, start => $oid_esEntityExtCpuUtilFiveSeconds, end => $oid_esEntityExtCpuUtilFiveMinutes
222 );
223 $self->check_table_cpu(
224 snmp_result => $snmp_result,
225 sec5 => $oid_esEntityExtCpuUtilFiveSeconds,
226 min1 => $oid_esEntityExtCpuUtilOneMinute,
227 min5 => $oid_esEntityExtCpuUtilFiveMinutes
228 );
229 }
230
231 sub manage_selection {
232 my ($self, %options) = @_;
233
234 $self->{cpu_avg} = {};
235 $self->{cpu_core} = {};
236 $self->{checked_cpu} = 0;
237
238 foreach (split /,/, $self->{option_results}->{check_order}) {
239 my $method = $self->can('check_cpu_' . $_);
240 if ($method) {
241 $self->$method(snmp => $options{snmp});
242 }
243 }
244
245 if ($self->{checked_cpu} == 0) {
246 $self->{output}->add_option_msg(short_msg => "Cannot find CPU informations");
247 $self->{output}->option_exit();
248 }
249
250 $self->check_cpu_average();
251 }
252
253 1;
254
255 __END__
256
257 =head1 MODE
258
259 Check CPU usage.
260
261 =over 8
262
263 =item B<--check-order>
264
265 Check CPU in standard dlink mib. If you have some issue (wrong CPU information in a specific mib), you can change the order
266 (default: 'common,industrial,agent').
267
268 =item B<--warning-*> B<--critical-*>
269
270 Thresholds.
271 Can be: 'core-5s', 'core-1m', 'core-5m', 'average-5s', 'average-1m', 'average-5m'.
272
273 =back
274
275 =cut