Debian Codegraph @

src/network/enterasys/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::enterasys::snmp::mode::cpu;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub prefix_cpu_avg_output {
29 my ($self, %options) = @_;
30
31 return $self->{cpu_avg}->{count} . " CPU(s) average usage is ";
32 }
33
34 sub prefix_cpu_core_output {
35 my ($self, %options) = @_;
36
37 return sprintf(
38 "CPU '%s' [%s] usage ",
39 $options{instance_value}->{cpu_num},
40 $options{instance_value}->{physicalName}
41 );
42 }
43
44 sub set_counters {
45 my ($self, %options) = @_;
46
47 $self->{maps_counters_type} = [
48 { name => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } },
49 { 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 } }
50 ];
51
52 $self->{maps_counters}->{cpu_avg} = [
53 { label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => {
54 key_values => [ { name => 'average_5s' } ],
55 output_template => '%.2f %% (5s)',
56 perfdatas => [
57 { template => '%.2f', min => 0, max => 100, unit => '%' }
58 ]
59 }
60 },
61 { label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => {
62 key_values => [ { name => 'average_1m' } ],
63 output_template => '%.2f %% (1m)',
64 perfdatas => [
65 { template => '%.2f', min => 0, max => 100, unit => '%' }
66 ]
67 }
68 },
69 { label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => {
70 key_values => [ { name => 'average_5m' } ],
71 output_template => '%.2f %% (5m)',
72 perfdatas => [
73 { template => '%.2f', min => 0, max => 100, unit => '%' }
74 ]
75 }
76 }
77 ];
78
79 $self->{maps_counters}->{cpu_core} = [
80 { label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => {
81 key_values => [ { name => 'cpu_5s' }, { name => 'cpu_num' }, { name => 'physicalName' } ],
82 output_template => '%.2f %% (5s)',
83 closure_custom_perfdata => sub {
84 my ($self, %options) = @_;
85
86 $self->{output}->perfdata_add(
87 nlabel => $self->{nlabel},
88 unit => '%',
89 instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{cpu_num}],
90 value => sprintf('%.2f', $self->{result_values}->{cpu_5s}),
91 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
92 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
93 min => 0,
94 max => 100
95 );
96 }
97 }
98 },
99 { label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => {
100 key_values => [ { name => 'cpu_1m' }, { name => 'cpu_num' }, { name => 'physicalName' } ],
101 output_template => '%.2f %% (1m)',
102 closure_custom_perfdata => sub {
103 my ($self, %options) = @_;
104
105 $self->{output}->perfdata_add(
106 nlabel => $self->{nlabel},
107 unit => '%',
108 instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{cpu_num}],
109 value => sprintf('%.2f', $self->{result_values}->{cpu_1m}),
110 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
111 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
112 min => 0,
113 max => 100
114 );
115 }
116 }
117 },
118 { label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => {
119 key_values => [ { name => 'cpu_5m' }, { name => 'cpu_num' }, { name => 'physicalName' } ],
120 output_template => '%.2f %% (5m)',
121 closure_custom_perfdata => sub {
122 my ($self, %options) = @_;
123
124 $self->{output}->perfdata_add(
125 nlabel => $self->{nlabel},
126 unit => '%',
127 instances => [$self->{result_values}->{physicalName}, $self->{result_values}->{cpu_num}],
128 value => sprintf('%.2f', $self->{result_values}->{cpu_5m}),
129 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
130 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
131 min => 0,
132 max => 100
133 );
134 }
135 }
136 }
137 ];
138 }
139
140 sub new {
141 my ($class, %options) = @_;
142 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
143 bless $self, $class;
144
145 $options{options}->add_options(arguments => {
146 'filter-entity-name:s' => { name => 'filter_entity_name' },
147 });
148
149 return $self;
150 }
151
152 sub check_table_cpu {
153 my ($self, %options) = @_;
154
155 my $instances = {};
156 foreach my $oid (keys %{$options{snmp_result}}) {
157 $oid =~ /\.(\d+)\.(\d+)$/;
158 my $display = 'unit' . $1 . $self->{output}->get_instance_perfdata_separator() . $2;
159 my $instance = $1 . '.' . $2;
160 next if (defined($instances->{$instance}));
161 $instances->{$instance} = 1;
162
163 my $cpu5sec = defined($options{snmp_result}->{$options{sec5} . '.' . $instance}) ? $options{snmp_result}->{$options{sec5} . '.' . $instance} : undef;
164 my $cpu1min = defined($options{snmp_result}->{$options{min1} . '.' . $instance}) ? $options{snmp_result}->{$options{min1} . '.' . $instance} : undef;
165 my $cpu5min = defined($options{snmp_result}->{$options{min5} . '.' . $instance}) ? $options{snmp_result}->{$options{min5} . '.' . $instance} : undef;
166
167 # Case that it's maybe other CPU oid in table for datas.
168 next if (!defined($cpu5sec) && !defined($cpu1min) && !defined($cpu5min));
169
170 $self->{checked_cpu} = 1;
171 $self->{cpu_core}->{$instance} = {
172 display => $display,
173 cpu_5s => $cpu5sec,
174 cpu_1m => $cpu1min,
175 cpu_5m => $cpu5min
176 };
177 }
178 }
179
180 sub check_cpu_average {
181 my ($self, %options) = @_;
182
183 my $count = scalar(keys %{$self->{cpu_core}});
184 my ($avg_5s, $avg_1m, $avg_5m);
185 foreach (values %{$self->{cpu_core}}) {
186 $avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s}
187 if (defined($_->{cpu_5s}));
188 $avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m}
189 if (defined($_->{cpu_1m}));
190 $avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m}
191 if (defined($_->{cpu_5m}));
192 }
193
194 $self->{cpu_avg} = {
195 average_5s => defined($avg_5s) ? $avg_5s / $count : undef,
196 average_1m => defined($avg_1m) ? $avg_1m / $count : undef,
197 average_5m => defined($avg_5m) ? $avg_5m / $count : undef,
198 count => $count
199 };
200 }
201
202 my $mapping = {
203 cpu_5s => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.1.1.1.2' }, # etsysResourceCpuLoad5sec
204 cpu_1m => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.1.1.1.3' }, # etsysResourceCpuLoad1min
205 cpu_5m => { oid => '.1.3.6.1.4.1.5624.1.2.49.1.1.1.1.4' } # etsysResourceCpuLoad5min
206 };
207 my $oid_etsysResourceCpuTable = '.1.3.6.1.4.1.5624.1.2.49.1.1.1';
208 my $oid_entPhysicalName = '.1.3.6.1.2.1.47.1.1.1.1.7';
209
210 sub manage_selection {
211 my ($self, %options) = @_;
212
213 my $snmp_result = $options{snmp}->get_table(
214 oid => $oid_etsysResourceCpuTable,
215 nothing_quit => 1
216 );
217
218 $self->{cpu_avg} = {};
219 $self->{cpu_core} = {};
220
221 my $indexes = {};
222 foreach (keys %$snmp_result) {
223 next if (! /^$mapping->{cpu_5m}->{oid}\.(\d+).(\d+)$/);
224 my ($physicalIndex, $cpu_num) = ($1, $2);
225 my $instance = $1 . '.' . $2;
226
227 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
228
229 $options{snmp}->load(oids => [ $oid_entPhysicalName . '.' . $physicalIndex ]) if (!defined($indexes->{$physicalIndex}));
230 $indexes->{$physicalIndex} = 1;
231
232 $self->{cpu_core}->{$instance} = {
233 physicalIndex => $physicalIndex,
234 cpu_num => $cpu_num,
235 cpu_5s => $result->{cpu_5s} / 10,
236 cpu_1m => $result->{cpu_1m} / 10,
237 cpu_5m => $result->{cpu_5m} / 10
238 };
239 }
240
241 if (scalar(keys %$indexes) > 0) {
242 $snmp_result = $options{snmp}->get_leef();
243 foreach (keys %{$self->{cpu_core}}) {
244 my $entity_name = $snmp_result->{ $oid_entPhysicalName . '.' . $self->{cpu_core}->{$_}->{physicalIndex} };
245 if (defined($self->{option_results}->{filter_entity_name}) && $self->{option_results}->{filter_entity_name} ne '' &&
246 $entity_name !~ /$self->{option_results}->{filter_entity_name}/) {
247 delete $self->{cpu_core}->{$_};
248 next;
249 }
250 $self->{cpu_core}->{$_}->{physicalName} = $entity_name;
251 }
252 }
253
254 $self->check_cpu_average();
255 }
256
257 1;
258
259 __END__
260
261 =head1 MODE
262
263 Check CPU usage.
264
265 =over 8
266
267 =item B<--filter-entity-name>
268
269 Filter entity name (can be a regexp).
270
271 =item B<--warning-*> B<--critical-*>
272
273 Thresholds.
274 Can be: 'core-5s', 'core-1m', 'core-5m', 'average-5s', 'average-1m', 'average-5m'.
275
276 =back
277
278 =cut