Debian Codegraph @

src/database/sap/hana/mode/hostmemory.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 database::sap::hana::mode::hostmemory;
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}->{label} = $options{extra_options}->{label_ref};
67 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_used'} + $options{new_datas}->{$self->{instance} . '_free'};
68 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
69 $self->{result_values}->{free} = $options{new_datas}->{$self->{instance} . '_free'};
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 physical memories are ok' },
80 { name => 'swap', type => 1, cb_prefix_output => 'prefix_swap_output', message_multiple => 'All swap memories are ok' },
81 ];
82
83 $self->{maps_counters}->{memory} = [
84 { label => 'physical-usage', nlabel => 'host.memory.usage.bytes', set => {
85 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'display' } ],
86 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'physical' },
87 closure_custom_output => $self->can('custom_usage_output'),
88 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
89 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
90 }
91 },
92 ];
93 $self->{maps_counters}->{swap} = [
94 { label => 'swap-usage', nlabel => 'host.swap.usage.bytes', set => {
95 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'display' } ],
96 closure_custom_calc => $self->can('custom_usage_calc'), closure_custom_calc_extra_options => { label_ref => 'swap' },
97 closure_custom_output => $self->can('custom_usage_output'),
98 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
99 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
100 }
101 },
102 ];
103 }
104
105 sub prefix_memory_output {
106 my ($self, %options) = @_;
107
108 return "Physical memory '" . $options{instance_value}->{display} . "' ";
109 }
110
111 sub prefix_swap_output {
112 my ($self, %options) = @_;
113
114 return "Swap memory '" . $options{instance_value}->{display} . "' ";
115 }
116
117 sub new {
118 my ($class, %options) = @_;
119 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
120 bless $self, $class;
121
122 $options{options}->add_options(arguments =>
123 {
124 });
125
126 return $self;
127 }
128
129 sub manage_selection {
130 my ($self, %options) = @_;
131 $options{sql}->connect();
132
133 my $query = q{
134 SELECT * FROM SYS.M_HOST_RESOURCE_UTILIZATION
135 };
136 $options{sql}->query(query => $query);
137 $self->{swap} = {};
138 $self->{memory} = {};
139
140 while ((my $row = $options{sql}->fetchrow_hashref())) {
141 my $name = $row->{HOST};
142
143 $self->{memory}->{$name} = {
144 display => $name,
145 free => $row->{FREE_PHYSICAL_MEMORY},
146 used => $row->{USED_PHYSICAL_MEMORY},
147 };
148 $self->{swap}->{$name} = {
149 display => $name,
150 free => $row->{FREE_SWAP_SPACE},
151 used => $row->{USED_SWAP_SPACE},
152 };
153 }
154 }
155
156 1;
157
158 __END__
159
160 =head1 MODE
161
162 Check memory usages.
163
164 =over 8
165
166 =item B<--filter-counters>
167
168 Only display some counters (regexp can be used).
169 Example : --filter-counters='^physical-usage$'
170
171 =item B<--warning-*>
172
173 Warning threshold.
174 Can be: 'physical-usage' (%), 'swap-usage' (%).
175
176 =item B<--critical-*>
177
178 Critical threshold.
179 Can be: 'physical-usage' (%), 'swap-usage' (%).
180
181 =back
182
183 =cut