Debian Codegraph @

src/centreon/common/avaya/snmp/mode/memory.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::avaya::snmp::mode::memory;
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 $self->{output}->perfdata_add(
32 label => 'used', unit => 'B',
33 nlabel => $self->{nlabel},
34 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
35 value => $self->{result_values}->{used},
36 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
37 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{total}, cast_int => 1),
38 min => 0, max => $self->{result_values}->{total}
39 );
40 }
41
42 sub custom_usage_threshold {
43 my ($self, %options) = @_;
44
45 my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]);
46 return $exit;
47 }
48
49 sub custom_usage_output {
50 my ($self, %options) = @_;
51
52 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
53 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
54 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
55
56 my $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
57 $total_size_value . " " . $total_size_unit,
58 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
59 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
60 return $msg;
61 }
62
63 sub custom_usage_calc {
64 my ($self, %options) = @_;
65
66 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
67 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
68 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
69 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
70 $self->{result_values}->{prct_free} = $self->{result_values}->{free} * 100 / $self->{result_values}->{total};
71 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
72 return 0;
73 }
74
75 sub set_counters {
76 my ($self, %options) = @_;
77
78 $self->{maps_counters_type} = [
79 { name => 'memory', type => 1, cb_prefix_output => 'prefix_memory_output', message_multiple => 'All memory usages are ok' }
80 ];
81
82 $self->{maps_counters}->{memory} = [
83 { label => 'usage', nlabel => 'memory.usage.bytes', set => {
84 key_values => [ { name => 'display' }, { name => 'total' }, { name => 'used' } ],
85 closure_custom_calc => $self->can('custom_usage_calc'),
86 closure_custom_output => $self->can('custom_usage_output'),
87 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
88 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
89 }
90 },
91 ];
92 }
93
94 sub prefix_memory_output {
95 my ($self, %options) = @_;
96
97 if ($self->{multiple} == 1) {
98 return "Memory '" . $options{instance_value}->{display} . "' ";
99 }
100 return '';
101 }
102
103 sub new {
104 my ($class, %options) = @_;
105 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
106 bless $self, $class;
107
108 $options{options}->add_options(arguments => {
109 });
110
111 return $self;
112 }
113
114 my $mapping = {
115 genMemUtilizationPhyRam => { oid => '.1.3.6.1.4.1.6889.2.1.11.1.2.6.1.2' }, # in Bytes
116 genMemUtilizationPercentUsed => { oid => '.1.3.6.1.4.1.6889.2.1.11.1.2.6.1.3' },
117 };
118
119 sub manage_selection {
120 my ($self, %options) = @_;
121
122 my $oid_genMemUtilizationEntry = '.1.3.6.1.4.1.6889.2.1.11.1.2.6.1';
123 my $results = $options{snmp}->get_table(oid => $oid_genMemUtilizationEntry, nothing_quit => 1);
124 $self->{memory} = {};
125 foreach my $oid (keys %{$results}) {
126 next if ($oid !~ /^$mapping->{genMemUtilizationPercentUsed}->{oid}\.(.*)/);
127 my $instance = $1;
128 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $results, instance => $instance);
129
130 my $total = $result->{genMemUtilizationPhyRam};
131 my $used = sprintf('%d', $total * $result->{genMemUtilizationPercentUsed} / 100);
132 $self->{memory}->{$instance} = {
133 display => $instance,
134 used => $used,
135 total => $total
136 };
137 }
138
139 if (scalar(keys %{$self->{memory}}) <= 0) {
140 $self->{output}->add_option_msg(short_msg => "No entry found.");
141 $self->{output}->option_exit();
142 }
143 }
144
145 1;
146
147 __END__
148
149 =head1 MODE
150
151 Check memory usages.
152
153 =over 8
154
155 =item B<--warning-usage>
156
157 Warning threshold (in percent).
158
159 =item B<--critical-usage>
160
161 Critical threshold (in percent).
162
163 =back
164
165 =cut