Debian Codegraph @

src/contrib/tutorial/network/mysnmpplugin/snmp/mode/cpu.pm

raw
1 #
2 # Copyright 2023 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 # Path to the plugin
22 package network::mysnmpplugin::snmp::mode::cpu;
23
24 # Consider this as mandatory when writing a new mode.
25 use base qw(centreon::plugins::templates::counter);
26
27 # Needed libraries
28 use strict;
29 use warnings;
30
31 sub prefix_cpu_output {
32 my ($self, %options) = @_;
33
34 return "CPU '" . $options{instance_value}->{name} . "' usage: ";
35 }
36
37 sub set_counters {
38 my ($self, %options) = @_;
39
40 $self->{maps_counters_type} = [
41 # app_metrics groups connections and errors and each will receive value for both instances (my-awesome-frontend and my-awesome-db)
42
43 #A compléter
44
45 # the type => 1 explicits that
46 # You can define a callback (cb) function to manage the output prefix. This function is called
47 # each time a value is passed to the counter and can be shared across multiple counters.
48 { name => 'cpu', type => 1, cb_prefix_output => 'prefix_cpu_output', message_multiple => 'All CPUs are ok' }
49 ];
50
51 $self->{maps_counters}->{cpu} = [
52 { label => 'cpu-usage-prct', nlabel => 'cpu.usage.percentage', set => {
53 key_values => [ { name => 'cpu_usage' }, { name => 'name' } ],
54 output_template => '%.2f %%',
55 perfdatas => [
56 # we add the label_extra_instance option to have one perfdata per instance
57 { label => 'cpu', template => '%.2f', min => 0, max => 100, unit => '%', label_extra_instance => 1, instance_use => 'name' }
58 ]
59 }
60 }
61 ];
62 }
63
64 sub new {
65 my ($class, %options) = @_;
66 # All options/properties of this mode, always add the force_new_perfdata => 1 to enable new metric/performance data naming.
67 # It also where you can specify that the plugin uses a cache file for example
68 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
69 bless $self, $class;
70
71 # Declare options
72 $options{options}->add_options(arguments => {
73 # One the left it's the option name that will be used in the command line. The ':s' at the end is to
74 # define that this options takes a value.
75 # On the right, it's the code name for this option, optionnaly you can define a default value so the user
76 # doesn't have to set it.
77 # option name => variable name
78 'filter-id:s' => { name => 'filter_id' }
79 });
80
81 return $self;
82 }
83
84 sub manage_selection {
85 my ($self, %options) = @_;
86 ###################################################
87 ##### Load SNMP informations to a result hash #####
88 ###################################################
89
90 # Select relevant oids for CPU monitoring
91 my $mapping = {
92 # hashKey => { oid => 'oid_number_path'}
93 hrProcessorID => { oid => '.1.3.6.1.2.1.25.3.3.1.1' },
94 hrProcessorLoad => { oid => '.1.3.6.1.2.1.25.3.3.1.2' }
95 #
96 };
97
98 # Point at the begining of the SNMP table
99 # Oid to point the table ahead all the oids given in mapping
100 my $oid_hrProcessorTable = '.1.3.6.1.2.1.25.3.3.1';
101
102 # Use SNMP Centreon plugins tools to push SNMP result in hash to handle with.
103 # $cpu_result is a hash table where keys are oids
104 my $cpu_result = $options{snmp}->get_table(
105 oid => $oid_hrProcessorTable,
106 nothing_quit => 1
107 );
108
109 ###################################################
110 ##### SNMP Result table to browse #####
111 ###################################################
112 foreach my $oid (keys %{$cpu_result}) {
113 next if ($oid !~ /^$mapping->{hrProcessorID}->{oid}\.(.*)$/);
114
115 # Catch table instance if exist :
116 # Instance is a number availible for a same oid refering to different target
117 my $instance = $1;
118 # Uncomment the lines below to see what instance looks like :
119
120 # use Data::Dumper;
121 # print Dumper($oid);
122 # print Dumper($instance);
123
124 # Data Dumper returns : with oid = hrProcessorID.instance
125 # $VAR1 = '.1.3.6.1.2.1.25.3.3.1.1.769';
126 # $VAR1 = '769';
127 # $VAR1 = '.1.3.6.1.2.1.25.3.3.1.1.768';
128 # $VAR1 = '768';
129
130 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $cpu_result, instance => $instance);
131
132 # Here is the way to handle with basic name/id filter.
133 # This filter is compare with hrProcessorID and in case of no match the oid is skipped
134 if (defined($self->{option_results}->{filter_id}) && $self->{option_results}->{filter_id} ne '' &&
135 $result->{hrProcessorID} !~ /$self->{option_results}->{filter_id}/) {
136 $self->{output}->output_add(long_msg => "skipping '" . $result->{hrProcessorID} . "': no matching filter.", debug => 1);
137 next;
138 }
139
140 # If the oid is not skipped above, here is convert the target values in result hash.
141 # Here is where the counter magic happens.
142 # $self->{cpu} is your counter definition (see $self->{maps_counters}->{<name>})
143 # Here, we map the obtained string $result->{hrProcessorLoad} with the cpu_usage key_value in the counter.
144 $self->{cpu}->{$instance} = {
145 name => $result->{hrProcessorID},
146 cpu_usage => $result->{hrProcessorLoad}
147 };
148 }
149
150 # IMPORTANT !
151 # If you use a way to filter the values set in result hash,
152 # check if at the end of parsing the result table isn't empty.
153 # If it's the case, add a message for user to explain the filter doesn't match.
154 if (scalar(keys %{$self->{cpu}}) <= 0) {
155 $self->{output}->add_option_msg(short_msg => "No processor ID matching with filter found.");
156 $self->{output}->option_exit();
157 }
158 }
159
160 1;
161
162 __END__
163
164 =head1 MODE
165
166 Check system CPUs.
167
168 =over 8
169
170 =item B<--filter-id>
171
172 Filter on one ID name.
173
174 =item B<--warning>
175
176 Warning threshold for CPU.
177
178 =item B<--critical>
179
180 Critical threshold for CPU.
181
182 =back
183
184 =cut