Debian Codegraph @

src/network/tplink/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::tplink::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 "CPU '" . $options{instance_value}->{core_id} . "' usage ";
38 }
39
40 sub set_counters {
41 my ($self, %options) = @_;
42
43 $self->{maps_counters_type} = [
44 { name => 'cpu_avg', type => 0, cb_prefix_output => 'prefix_cpu_avg_output', message_separator => ' ', skipped_code => { -10 => 1 } },
45 { 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 } }
46 ];
47
48 $self->{maps_counters}->{cpu_avg} = [
49 { label => 'average-5s', nlabel => 'cpu.utilization.5s.percentage', set => {
50 key_values => [ { name => 'average_5s' } ],
51 output_template => '%.2f %% (5s)',
52 perfdatas => [
53 { template => '%.2f', min => 0, max => 100, unit => '%' }
54 ]
55 }
56 },
57 { label => 'average-1m', nlabel => 'cpu.utilization.1m.percentage', set => {
58 key_values => [ { name => 'average_1m' } ],
59 output_template => '%.2f %% (1m)',
60 perfdatas => [
61 { template => '%.2f', min => 0, max => 100, unit => '%' }
62 ]
63 }
64 },
65 { label => 'average-5m', nlabel => 'cpu.utilization.5m.percentage', set => {
66 key_values => [ { name => 'average_5m' } ],
67 output_template => '%.2f %% (5m)',
68 perfdatas => [
69 { template => '%.2f', min => 0, max => 100, unit => '%' }
70 ]
71 }
72 }
73 ];
74
75 $self->{maps_counters}->{cpu_core} = [
76 { label => 'core-5s', nlabel => 'core.cpu.utilization.5s.percentage', set => {
77 key_values => [ { name => 'cpu_5s' } ],
78 output_template => '%.2f %% (5s)',
79 perfdatas => [
80 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
81 ]
82 }
83 },
84 { label => 'core-1m', nlabel => 'core.cpu.utilization.1m.percentage', set => {
85 key_values => [ { name => 'cpu_1m' } ],
86 output_template => '%.2f %% (1m)',
87 perfdatas => [
88 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
89 ]
90 }
91 },
92 { label => 'core-5m', nlabel => 'core.cpu.utilization.5m.percentage', set => {
93 key_values => [ { name => 'cpu_5m' } ],
94 output_template => '%.2f %% (5m)',
95 perfdatas => [
96 { template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1 }
97 ]
98 }
99 }
100 ];
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 });
110
111 return $self;
112 }
113
114 sub check_cpu_average {
115 my ($self, %options) = @_;
116
117 my $count = scalar(keys %{$self->{cpu_core}});
118 my ($avg_5s, $avg_1m, $avg_5m);
119 foreach (values %{$self->{cpu_core}}) {
120 $avg_5s = defined($avg_5s) ? $avg_5s + $_->{cpu_5s} : $_->{cpu_5s}
121 if (defined($_->{cpu_5s}));
122 $avg_1m = defined($avg_1m) ? $avg_1m + $_->{cpu_1m} : $_->{cpu_1m}
123 if (defined($_->{cpu_1m}));
124 $avg_5m = defined($avg_5m) ? $avg_5m + $_->{cpu_5m} : $_->{cpu_5m}
125 if (defined($_->{cpu_5m}));
126 }
127
128 $self->{cpu_avg} = {
129 average_5s => defined($avg_5s) ? $avg_5s / $count : undef,
130 average_1m => defined($avg_1m) ? $avg_1m / $count : undef,
131 average_5m => defined($avg_5m) ? $avg_5m / $count : undef,
132 count => $count
133 };
134 }
135
136 my $mapping = {
137 cpu_5s => { oid => '.1.3.6.1.4.1.11863.6.4.1.1.1.1.2' }, # tpSysMonitorCpu5Seconds
138 cpu_1m => { oid => '.1.3.6.1.4.1.11863.6.4.1.1.1.1.3' }, # tpSysMonitorCpu1Minute
139 cpu_5m => { oid => '.1.3.6.1.4.1.11863.6.4.1.1.1.1.4' } # tpSysMonitorCpu5Minutes
140 };
141 my $oid_cpu_table = '.1.3.6.1.4.1.11863.6.4.1.1.1'; # tpSysMonitorCpuTable
142
143 sub manage_selection {
144 my ($self, %options) = @_;
145
146 my $snmp_result = $options{snmp}->get_table(
147 oid => $oid_cpu_table,
148 nothing_quit => 1
149 );
150
151 $self->{cpu_avg} = {};
152 $self->{cpu_core} = {};
153 foreach my $oid (keys %$snmp_result) {
154 next if ($oid !~ /^$mapping->{cpu_5m}->{oid}\.(.*)$/);
155 my $instance = $1;
156 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
157
158 $self->{cpu_core}->{$instance} = { core_id => $instance, %$result };
159 }
160
161 $self->check_cpu_average();
162 }
163
164 1;
165
166 __END__
167
168 =head1 MODE
169
170 Check CPU usage.
171
172 =over 8
173
174 =item B<--warning-*> B<--critical-*>
175
176 Thresholds.
177 Can be: 'core-5s', 'core-1m', 'core-5m', 'average-5s', 'average-1m', 'average-5m'.
178
179 =back
180
181 =cut