Debian Codegraph @

src/centreon/common/fortinet/fortigate/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 centreon::common::fortinet::fortigate::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 },
33 { name => 'cpu_core', type => 1, cb_prefix_output => 'prefix_cpu_core_output' },
34 { name => 'cluster', type => 1, cb_prefix_output => 'prefix_cluster_output' }
35 ];
36
37 $self->{maps_counters}->{cpu_avg} = [
38 { label => 'average', nlabel => 'cpu.utilization.percentage', set => {
39 key_values => [ { name => 'average' } ],
40 output_template => 'CPU(s) average usage is: %.2f %%',
41 perfdatas => [
42 { label => 'total_cpu_avg', value => 'average', template => '%.2f',
43 min => 0, max => 100, unit => '%' },
44 ]
45 }
46 }
47 ];
48
49 $self->{maps_counters}->{cpu_core} = [
50 { label => 'core', nlabel => 'core.cpu.utilization.percentage', set => {
51 key_values => [ { name => 'cpu' } ],
52 output_template => 'usage: %.2f %%',
53 perfdatas => [
54 { value => 'cpu', template => '%.2f',
55 min => 0, max => 100, unit => '%', label_extra_instance => 1 },
56 ]
57 }
58 }
59 ];
60
61 $self->{maps_counters}->{cluster} = [
62 { label => 'cluster-average', nlabel => 'cluster.cpu.utilization.percentage', display_ok => 0, set => {
63 key_values => [ { name => 'cpu' } ],
64 output_template => 'CPU usage: %.2f %%',
65 perfdatas => [
66 { value => 'cpu', template => '%.2f',
67 min => 0, max => 100, unit => '%', label_extra_instance => 1 },
68 ]
69 }
70 }
71 ];
72 }
73
74 sub prefix_cluster_output {
75 my ($self, %options) = @_;
76
77 return "Cluster '" . $options{instance_value}->{display} . "' ";
78 }
79
80 sub prefix_cpu_core_output {
81 my ($self, %options) = @_;
82
83 return "CPU '" . $options{instance_value}->{display} . "' ";
84 }
85
86 sub new {
87 my ($class, %options) = @_;
88 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
89 bless $self, $class;
90
91 $options{options}->add_options(arguments => {
92 'cluster' => { name => 'cluster' },
93 'filter-core:s' => { name => 'filter_core' },
94 });
95
96 return $self;
97 }
98
99 my $oid_fgSysCpuUsage = '.1.3.6.1.4.1.12356.101.4.1.3';
100 my $oid_fgProcessorUsage = '.1.3.6.1.4.1.12356.101.4.4.2.1.2'; # some not have
101 my $oid_fgHaSystemMode = '.1.3.6.1.4.1.12356.101.13.1.1'; # '.0' to have the mode
102 my $oid_fgHaStatsCpuUsage = '.1.3.6.1.4.1.12356.101.13.2.1.1.3';
103 my $oid_fgHaStatsHostname = '.1.3.6.1.4.1.12356.101.13.2.1.1.11';
104
105 my $maps_ha_mode = { 1 => 'standalone', 2 => 'activeActive', 3 => 'activePassive' };
106
107 sub cpu_ha {
108 my ($self, %options) = @_;
109
110 $self->{cluster} = {};
111 foreach ($options{snmp}->oid_lex_sort(keys %{$options{snmp_result}->{$oid_fgHaStatsCpuUsage}})) {
112 /\.(\d+)$/;
113
114 $self->{cluster}->{ $options{snmp_result}->{$oid_fgHaStatsHostname}->{$oid_fgHaStatsHostname . '.' . $1} } = {
115 display => $options{snmp_result}->{$oid_fgHaStatsHostname}->{$oid_fgHaStatsHostname . '.' . $1},
116 cpu => $options{snmp_result}->{$oid_fgHaStatsCpuUsage}->{$_}
117 };
118 }
119 }
120
121 sub manage_selection {
122 my ($self, %options) = @_;
123
124 my $table_oids = [ { oid => $oid_fgProcessorUsage }, { oid => $oid_fgSysCpuUsage } ];
125 if (defined($self->{option_results}->{cluster})) {
126 push @$table_oids, { oid => $oid_fgHaSystemMode },
127 { oid => $oid_fgHaStatsCpuUsage },
128 { oid => $oid_fgHaStatsHostname };
129 }
130
131 my $snmp_result = $options{snmp}->get_multiple_table(
132 oids => $table_oids,
133 nothing_quit => 1
134 );
135
136 my ($cpu, $i) = (0, -1);
137 $self->{cpu_core} = {};
138 foreach ($options{snmp}->oid_lex_sort(keys %{$snmp_result->{$oid_fgProcessorUsage}})) {
139 $i++;
140 if (defined($self->{option_results}->{filter_core}) && $self->{option_results}->{filter_core} ne '' &&
141 $i !~ /$self->{option_results}->{filter_core}/) {
142 $self->{output}->output_add(long_msg => "skipping core cpu '" . $i . "': no matching filter.", debug => 1);
143 next;
144 }
145
146 $self->{cpu_core}->{$i} = { display => $i, cpu => $snmp_result->{$oid_fgProcessorUsage}->{$_} };
147 $cpu += $snmp_result->{$oid_fgProcessorUsage}->{$_};
148 }
149
150 my $num_core = scalar(keys %{$self->{cpu_core}});
151 $self->{cpu_avg} = {
152 average => $num_core > 0 ? $cpu / $num_core : $snmp_result->{$oid_fgSysCpuUsage}->{$oid_fgSysCpuUsage . '.0'}
153 };
154
155 if (defined($self->{option_results}->{cluster})) {
156 my $ha_mode = $snmp_result->{$oid_fgHaSystemMode}->{$oid_fgHaSystemMode . '.0'};
157 my $ha_mode_str = defined($maps_ha_mode->{$ha_mode}) ? $maps_ha_mode->{$ha_mode} : 'unknown';
158 $self->{output}->output_add(long_msg => 'high availabily mode is ' . $ha_mode_str);
159 if ($ha_mode_str =~ /active/) {
160 $self->cpu_ha(snmp => $options{snmp}, snmp_result => $snmp_result);
161 }
162 }
163 }
164
165 1;
166
167 __END__
168
169 =head1 MODE
170
171 Check system CPU usage (FORTINET-FORTIGATE-MIB).
172
173 =over 8
174
175 =item B<--warning-*> B<--critical-*>
176
177 Thresholds.
178 Can be: 'core', 'average', 'cluster-average'.
179
180 =item B<--cluster>
181
182 Add cluster CPU informations.
183
184 =item B<--filter-core>
185
186 Core CPU to monitor (can be a regexp).
187
188 =back
189
190 =cut