Debian Codegraph @

src/network/citrix/sdx/snmp/mode/xenusage.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::citrix::sdx::snmp::mode::xenusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_usage_perfdata {
29 my ($self, %options) = @_;
30
31 my $label = 'memory_used';
32 my $value_perf = $self->{result_values}->{used};
33 if (defined($self->{instance_mode}->{option_results}->{free})) {
34 $label = 'memory_free';
35 $value_perf = $self->{result_values}->{free};
36 }
37
38 my %total_options = ();
39 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
40 $total_options{total} = $self->{result_values}->{total};
41 $total_options{cast_int} = 1;
42 }
43
44 $self->{output}->perfdata_add(
45 label => $label, unit => 'B',
46 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
47 value => $value_perf,
48 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
49 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
50 min => 0, max => $self->{result_values}->{total}
51 );
52 }
53
54 sub custom_usage_threshold {
55 my ($self, %options) = @_;
56
57 my ($exit, $threshold_value);
58 $threshold_value = $self->{result_values}->{used};
59 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
60 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
61 $threshold_value = $self->{result_values}->{prct_used};
62 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
63 }
64 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
65 return $exit;
66 }
67
68 sub custom_usage_output {
69 my ($self, %options) = @_;
70
71 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
72 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
73 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
74 my $msg = sprintf("Memory Usage Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
75 $total_size_value . " " . $total_size_unit,
76 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
77 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
78 return $msg;
79 }
80
81 sub custom_usage_calc {
82 my ($self, %options) = @_;
83
84 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
85 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_mem_total'};
86 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_mem_free'};
87 $self->{result_values}->{used} = $self->{result_values}->{total} - $self->{result_values}->{free};
88 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
89 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
90
91 return 0;
92 }
93
94 sub set_counters {
95 my ($self, %options) = @_;
96
97 $self->{maps_counters_type} = [
98 { name => 'xen', type => 1, cb_prefix_output => 'prefix_xen_output', message_multiple => 'All xen hypervisors are ok' }
99 ];
100
101 $self->{maps_counters}->{xen} = [
102 { label => 'memory-usage', set => {
103 key_values => [ { name => 'display' }, { name => 'mem_free' }, { name => 'mem_total' } ],
104 closure_custom_calc => $self->can('custom_usage_calc'),
105 closure_custom_output => $self->can('custom_usage_output'),
106 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
107 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
108 }
109 },
110 { label => 'cpu-usage', set => {
111 key_values => [ { name => 'xenCpuUsage' }, { name => 'display' } ],
112 output_template => 'CPU Usage : %.2f %%', output_error_template => "CPU Usage : %s",
113 perfdatas => [
114 { label => 'cpu_usage', value => 'xenCpuUsage', template => '%.2f',
115 unit => '%', min => 0, max => 100, label_extra_instance => 1, instance_use => 'display' },
116 ],
117 }
118 },
119 ];
120 }
121
122 sub new {
123 my ($class, %options) = @_;
124 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
125 bless $self, $class;
126
127 $options{options}->add_options(arguments => {
128 "filter-name:s" => { name => 'filter_name' },
129 "units:s" => { name => 'units', default => '%' },
130 "free" => { name => 'free' },
131 });
132
133 return $self;
134 }
135
136 sub prefix_disk_output {
137 my ($self, %options) = @_;
138
139 return "Xen '" . $options{instance_value}->{display} . "' ";
140 }
141
142 my $mapping = {
143 xenHostname => { oid => '.1.3.6.1.4.1.5951.6.3.1.1.3' },
144 xenCpuUsage => { oid => '.1.3.6.1.4.1.5951.6.3.1.1.8' },
145 xenMemoryTotal => { oid => '.1.3.6.1.4.1.5951.6.3.1.1.9' },
146 xenMemoryFree => { oid => '.1.3.6.1.4.1.5951.6.3.1.1.10' },
147 };
148
149 my $oid_xenEntry = '.1.3.6.1.4.1.5951.6.3.1.1';
150
151 sub manage_selection {
152 my ($self, %options) = @_;
153
154 $self->{xen} = {};
155 my $snmp_result = $options{snmp}->get_table(oid => $oid_xenEntry,
156 nothing_quit => 1);
157
158
159 foreach my $oid (keys %{$snmp_result}) {
160 next if ($oid !~ /^$mapping->{xenHostname}->{oid}\.(.*)$/);
161 my $instance = $1;
162 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
163
164 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
165 $result->{xenHostname} !~ /$self->{option_results}->{filter_name}/) {
166 $self->{output}->output_add(long_msg => "skipping '" . $result->{xenHostname} . "': no matching filter.", debug => 1);
167 next;
168 }
169
170 $self->{xen}->{$instance} = {
171 display => $result->{xenHostname},
172 mem_total => $result->{xenMemoryTotal} * 1024 * 1024,
173 mem_free => $result->{xenMemoryFree} * 1024 * 1024,
174 %$result
175 };
176 }
177
178 if (scalar(keys %{$self->{xen}}) <= 0) {
179 $self->{output}->add_option_msg(short_msg => "No xen hypervisor found.");
180 $self->{output}->option_exit();
181 }
182 }
183
184 1;
185
186 __END__
187
188 =head1 MODE
189
190 Check xen hypervisors.
191
192 =over 8
193
194 =item B<--filter-counters>
195
196 Only display some counters (regexp can be used).
197 Example: --filter-counters='^memory-usage$'
198
199 =item B<--filter-name>
200
201 Filter xen name (can be a regexp).
202
203 =item B<--warning-*>
204
205 Warning threshold.
206 Can be: 'cpu-usage', 'memory-usage'.
207
208 =item B<--critical-*>
209
210 Critical threshold.
211 Can be: 'cpu-usage', 'memory-usage'.
212
213 =item B<--units>
214
215 Units of thresholds (default: '%') ('%', 'B').
216
217 =item B<--free>
218
219 Thresholds are on free space left.
220
221 =back
222
223 =cut