Debian Codegraph @

src/centreon/common/dell/fastpath/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::dell::fastpath::snmp::mode::cpu;
22
23 use base qw(centreon::plugins::mode);
24
25 use strict;
26 use warnings;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
31 bless $self, $class;
32
33 $options{options}->add_options(arguments => {
34 'warning:s' => { name => 'warning', default => '' },
35 'critical:s' => { name => 'critical', default => '' }
36 });
37
38 return $self;
39 }
40
41 sub check_options {
42 my ($self, %options) = @_;
43 $self->SUPER::init(%options);
44
45 ($self->{warn5s}, $self->{warn1m}, $self->{warn5m}) = split /,/, $self->{option_results}->{warning};
46 ($self->{crit5s}, $self->{crit1m}, $self->{crit5m}) = split /,/, $self->{option_results}->{critical};
47
48 if (($self->{perfdata}->threshold_validate(label => 'warn5s', value => $self->{warn5s})) == 0) {
49 $self->{output}->add_option_msg(short_msg => "Wrong warning (5sec) threshold '" . $self->{warn5s} . "'.");
50 $self->{output}->option_exit();
51 }
52 if (($self->{perfdata}->threshold_validate(label => 'warn1m', value => $self->{warn1m})) == 0) {
53 $self->{output}->add_option_msg(short_msg => "Wrong warning (1min) threshold '" . $self->{warn1m} . "'.");
54 $self->{output}->option_exit();
55 }
56 if (($self->{perfdata}->threshold_validate(label => 'warn5m', value => $self->{warn5m})) == 0) {
57 $self->{output}->add_option_msg(short_msg => "Wrong warning (5min) threshold '" . $self->{warn5m} . "'.");
58 $self->{output}->option_exit();
59 }
60 if (($self->{perfdata}->threshold_validate(label => 'crit5s', value => $self->{crit5s})) == 0) {
61 $self->{output}->add_option_msg(short_msg => "Wrong critical (5sec) threshold '" . $self->{crit5s} . "'.");
62 $self->{output}->option_exit();
63 }
64 if (($self->{perfdata}->threshold_validate(label => 'crit1m', value => $self->{crit1m})) == 0) {
65 $self->{output}->add_option_msg(short_msg => "Wrong critical (1min) threshold '" . $self->{crit1m} . "'.");
66 $self->{output}->option_exit();
67 }
68 if (($self->{perfdata}->threshold_validate(label => 'crit5m', value => $self->{crit5m})) == 0) {
69 $self->{output}->add_option_msg(short_msg => "Wrong critical (5min) threshold '" . $self->{crit5m} . "'.");
70 $self->{output}->option_exit();
71 }
72 }
73
74 sub run {
75 my ($self, %options) = @_;
76 $self->{snmp} = $options{snmp};
77
78 my $oid_agentSwitchCpuProcessTotalUtilization1 = '.1.3.6.1.4.1.674.10895.5000.2.6132.1.1.1.1.4.9.0';
79 my $oid_agentSwitchCpuProcessTotalUtilization2 = '.1.3.6.1.4.1.674.10895.5000.2.6132.1.1.1.1.4.4.0'; # oid for 6200 series
80 $self->{result} = $self->{snmp}->get_leef(
81 oids => [ $oid_agentSwitchCpuProcessTotalUtilization1, $oid_agentSwitchCpuProcessTotalUtilization2 ],
82 nothing_quit => 1
83 );
84
85 my $cpu_usage;
86 if ((defined($self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization1})) && ($self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization1} =~ /sec.*(sec|min).*(sec|min)/i)) {
87 $cpu_usage = $self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization1};
88 } elsif ((defined($self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization2})) && ($self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization2} =~ /sec.*(sec|min).*(sec|min)/i)) {
89 $cpu_usage = $self->{result}->{$oid_agentSwitchCpuProcessTotalUtilization2};
90 } else {
91 $self->{output}->output_add(
92 severity => 'UNKNOWN',
93 short_msg => sprintf("Can't get CPU information.")
94 );
95 $self->{output}->display();
96 $self->{output}->exit();
97 }
98 if ($cpu_usage =~ /^.*\(\s*(\S+)%\).*\(\s*(\S+)%\).*\(\s*(\S+)%\)/) {
99 my $cpu5sec = $1;
100 my $cpu1min = $2;
101 my $cpu5min = $3;
102
103 my $exit1 = $self->{perfdata}->threshold_check(
104 value => $cpu5sec,
105 threshold => [ { label => 'crit5s', 'exit_litteral' => 'critical' }, { label => 'warn5s', exit_litteral => 'warning' } ]
106 );
107 my $exit2 = $self->{perfdata}->threshold_check(
108 value => $cpu1min,
109 threshold => [ { label => 'crit1m', 'exit_litteral' => 'critical' }, { label => 'warn1m', exit_litteral => 'warning' } ]
110 );
111 my $exit3 = $self->{perfdata}->threshold_check(
112 value => $cpu5min,
113 threshold => [ { label => 'crit5m', 'exit_litteral' => 'critical' }, { label => 'warn5m', exit_litteral => 'warning' } ]
114 );
115 my $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2, $exit3 ]);
116
117 $self->{output}->output_add(
118 severity => $exit,
119 short_msg => sprintf(
120 "CPU Usage: %.2f%% (5sec), %.2f%% (1min), %.2f%% (5min)",
121 $cpu5sec, $cpu1min, $cpu5min
122 )
123 );
124
125 $self->{output}->perfdata_add(
126 nlabel => 'cpu.utilization.5s.percentage',
127 unit => '%',
128 value => $cpu5sec,
129 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5s'),
130 critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5s'),
131 min => 0, max => 100
132 );
133 $self->{output}->perfdata_add(
134 nlabel => 'cpu.utilization.1m.percentage',
135 unit => '%',
136 value => $cpu1min,
137 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn1m'),
138 critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit1m'),
139 min => 0, max => 100
140 );
141 $self->{output}->perfdata_add(
142 nlabel => 'cpu.utilization.5m.percentage',
143 unit => '%',
144 value => $cpu5min,
145 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warn5m'),
146 critical => $self->{perfdata}->get_perfdata_for_output(label => 'crit5m'),
147 min => 0, max => 100
148 );
149 } else {
150 $self->{output}->output_add(severity => 'UNKNOWN',
151 short_msg => sprintf("Can't parse CPU usage."));
152 }
153
154 $self->{output}->display();
155 $self->{output}->exit();
156 }
157
158 1;
159
160 __END__
161
162 =head1 MODE
163
164 Check CPU usage (FASTPATH-SWITCHING-MIB).
165
166 =over 8
167
168 =item B<--warning>
169
170 Warning threshold in percent (5s,1min,5min).
171
172 =item B<--critical>
173
174 Critical threshold in percent (5s,1min,5min).
175
176 =back
177
178 =cut