Debian Codegraph @

src/centreon/common/ingrian/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::ingrian::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(label => 'used', unit => 'B',
32 value => $self->{result_values}->{used},
33 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
34 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{label}, total => $self->{result_values}->{total}, cast_int => 1),
35 min => 0, max => $self->{result_values}->{total});
36 }
37
38 sub custom_usage_threshold {
39 my ($self, %options) = @_;
40
41 my $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{prct_used}, threshold => [ { label => 'critical-' . $self->{label}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{label}, exit_litteral => 'warning' } ]);
42 return $exit;
43 }
44
45 sub custom_usage_output {
46 my ($self, %options) = @_;
47
48 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
49 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
50 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
51
52 my $msg = sprintf("Memory Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
53 $total_size_value . " " . $total_size_unit,
54 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
55 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
56 return $msg;
57 }
58
59 sub custom_usage_calc {
60 my ($self, %options) = @_;
61
62 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
63 $self->{result_values}->{prct_used} = $options{new_datas}->{$self->{instance} . '_prct_used'};
64 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
65 $self->{result_values}->{used} = $self->{result_values}->{total} * $self->{result_values}->{prct_used} / 100;
66 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
67
68 return 0;
69 }
70
71 sub set_counters {
72 my ($self, %options) = @_;
73
74 $self->{maps_counters_type} = [
75 { name => 'memory', type => 0 }
76 ];
77
78 $self->{maps_counters}->{memory} = [
79 { label => 'usage', set => {
80 key_values => [ { name => 'prct_used' }, { name => 'total' } ],
81 closure_custom_calc => $self->can('custom_usage_calc'),
82 closure_custom_output => $self->can('custom_usage_output'),
83 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
84 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
85 }
86 },
87 ];
88 }
89
90 sub new {
91 my ($class, %options) = @_;
92 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
93 bless $self, $class;
94
95 $options{options}->add_options(arguments =>
96 {
97 });
98
99 return $self;
100 }
101
102 sub manage_selection {
103 my ($self, %options) = @_;
104
105 my $oid_naeSystemStatUsedMem = '.1.3.6.1.4.1.5595.3.2.5.0';
106 my $oid_naeSystemStatTotalMem = '.1.3.6.1.4.1.5595.3.2.6.0'; # in Bytes
107 my $snmp_result = $options{snmp}->get_leef(oids => [$oid_naeSystemStatUsedMem, $oid_naeSystemStatTotalMem],
108 nothing_quit => 1);
109
110 $self->{memory} = { prct_used => $snmp_result->{$oid_naeSystemStatUsedMem}, total => $snmp_result->{$oid_naeSystemStatTotalMem} };
111
112 }
113
114 1;
115
116 __END__
117
118 =head1 MODE
119
120 Check memory usages.
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