Debian Codegraph @

src/network/fortinet/fortiadc/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::fortinet::fortiadc::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-2s', nlabel => 'cpu.utilization.2s.percentage', set => {
50 key_values => [ { name => 'average_2s' } ],
51 output_template => '%.2f %% (2s)',
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-2s', nlabel => 'core.cpu.utilization.2s.percentage', set => {
77 key_values => [ { name => 'cpu_2s' } ],
78 output_template => '%.2f %% (2s)',
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_2s, $avg_1m, $avg_5m);
119 foreach (values %{$self->{cpu_core}}) {
120 $avg_2s = defined($avg_2s) ? $avg_2s + $_->{cpu_2s} : $_->{cpu_2s}
121 if (defined($_->{cpu_2s}));
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_2s => defined($avg_2s) ? $avg_2s / $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 name => { oid => '.1.3.6.1.4.1.12356.112.1.40.1.2' }, # fadcCpuName
138 cpu_2s => { oid => '.1.3.6.1.4.1.12356.112.1.40.1.3' }, # fadcCpu2secAvgUsage
139 cpu_1m => { oid => '.1.3.6.1.4.1.12356.112.1.40.1.4' }, # fadcCpu1minAvgUsage
140 cpu_5m => { oid => '.1.3.6.1.4.1.12356.112.1.40.1.5' } # fadcCpu5minAvgUsage
141 };
142 my $oid_cpu_table = '.1.3.6.1.4.1.12356.112.1.40.1'; # fadcSysCpuUsageTable
143
144 sub manage_selection {
145 my ($self, %options) = @_;
146
147 my $snmp_result = $options{snmp}->get_table(
148 oid => $oid_cpu_table,
149 nothing_quit => 1
150 );
151
152 $self->{cpu_avg} = {};
153 $self->{cpu_core} = {};
154 foreach my $oid (keys %$snmp_result) {
155 next if ($oid !~ /^$mapping->{cpu_5m}->{oid}\.(.*)$/);
156 my $instance = $1;
157 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
158
159 $self->{cpu_core}->{ $result->{name} } = { core_id => $instance, %$result };
160 }
161
162 $self->check_cpu_average();
163 }
164
165 1;
166
167 __END__
168
169 =head1 MODE
170
171 Check CPU usage.
172
173 =over 8
174
175 =item B<--warning-*> B<--critical-*>
176
177 Thresholds.
178 Can be: 'core-2s', 'core-1m', 'core-5m', 'average-2s', 'average-1m', 'average-5m'.
179
180 =back
181
182 =cut