Debian Codegraph @

src/network/bluecoat/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::bluecoat::snmp::mode::cpu;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Digest::MD5 qw(md5_hex);
28
29 sub set_counters {
30 my ($self, %options) = @_;
31
32 $self->{maps_counters_type} = [
33 { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All cpu usages are ok' },
34 ];
35 $self->{maps_counters}->{cpu} = [
36 { label => 'usage', set => {
37 key_values => [ { name => 'idle', diff => 1 }, { name => 'busy', diff => 1 }, { name => 'display' } ],
38 closure_custom_calc => $self->can('custom_data_calc'),
39 output_template => '%.2f %%', output_use => 'used_prct', threshold_use => 'used_prct',
40 perfdatas => [
41 { label => 'cpu', value => 'used_prct', template => '%.2f', min => 0, max => 100, unit => '%',
42 label_extra_instance => 1, instance_use => 'display' },
43 ],
44 }
45 },
46 ];
47 }
48
49 sub custom_data_calc {
50 my ($self, %options) = @_;
51
52 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
53 my $delta_busy = $options{new_datas}->{$self->{instance} . '_busy'} - $options{old_datas}->{$self->{instance} . '_busy'};
54 my $delta_idle = $options{new_datas}->{$self->{instance} . '_idle'} - $options{old_datas}->{$self->{instance} . '_idle'};
55 my $total = $delta_busy + $delta_idle;
56 $self->{result_values}->{used_prct} = 0;
57 if ($total > 0) {
58 $self->{result_values}->{used_prct} = ($delta_busy) * 100 / $total;
59 }
60 return 0;
61 }
62
63 sub prefix_cpu_output {
64 my ($self, %options) = @_;
65
66 return "CPU '" . $options{instance_value}->{display} . "' Usage : ";
67 }
68
69 sub new {
70 my ($class, %options) = @_;
71 my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1);
72 bless $self, $class;
73
74 $options{options}->add_options(arguments =>
75 {
76 });
77
78 return $self;
79 }
80
81 my $mapping = {
82 sgProxyCpuCoreBusyTime => { oid => '.1.3.6.1.4.1.3417.2.11.2.4.1.3' },
83 sgProxyCpuCoreIdleTime => { oid => '.1.3.6.1.4.1.3417.2.11.2.4.1.4' },
84 };
85 my $oid_table = '.1.3.6.1.4.1.3417.2.11.2.4.1';
86
87 sub manage_selection {
88 my ($self, %options) = @_;
89
90 if ($options{snmp}->is_snmpv1()) {
91 $self->{output}->add_option_msg(short_msg => "Need to use SNMP v2c or v3.");
92 $self->{output}->option_exit();
93 }
94
95 $self->{cache_name} = 'bluecoat_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode}. '_' .
96 (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all'));
97
98 $self->{cpu} = {};
99 my $snmp_result = $options{snmp}->get_table(oid => $oid_table, start => $mapping->{sgProxyCpuCoreBusyTime}, end => $mapping->{sgProxyCpuCoreIdleTime}, nothing_quit => 1);
100 my $i = 0;
101 foreach my $oid ($options{snmp}->oid_lex_sort(keys %{$snmp_result})) {
102 next if ($oid !~ /^$mapping->{sgProxyCpuCoreBusyTime}->{oid}\.(.*)$/);
103 my $instance = $1;
104 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
105
106 $self->{cpu}->{$i} = {
107 display => $i,
108 idle => $result->{sgProxyCpuCoreIdleTime}, busy => $result->{sgProxyCpuCoreBusyTime}
109 };
110 $i++;
111 }
112 }
113
114 1;
115
116 __END__
117
118 =head1 MODE
119
120 Check CPU Usage
121
122 =over 8
123
124 =item B<--warning-usage>
125
126 Warning threshold in percent.
127
128 =item B<--critical-usage>
129
130 Critical threshold in percent.
131
132 =back
133
134 =cut