Debian Codegraph @

src/database/sap/hana/mode/diskusage.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::diskusage;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub set_counters {
29 my ($self, %options) = @_;
30
31 $self->{maps_counters_type} = [
32 { name => 'disk', type => 1, cb_prefix_output => 'prefix_volume_output', message_multiple => 'All disks are ok' },
33 ];
34
35 $self->{maps_counters}->{disk} = [
36 { label => 'usage', nlabel => 'disk.usage.bytes', set => {
37 key_values => [ { name => 'used' }, { name => 'total' }, { name => 'display' } ],
38 closure_custom_calc => $self->can('custom_usage_calc'),
39 closure_custom_output => $self->can('custom_usage_output'),
40 closure_custom_perfdata => $self->can('custom_usage_perfdata'),
41 closure_custom_threshold_check => $self->can('custom_usage_threshold'),
42 }
43 },
44 ];
45 }
46
47 sub custom_usage_perfdata {
48 my ($self, %options) = @_;
49
50 my ($label, $nlabel) = ('used', $self->{nlabel});
51 my $value_perf = $self->{result_values}->{used};
52 if (defined($self->{instance_mode}->{option_results}->{free})) {
53 ($label, $nlabel) = ('free', 'disk.usage.free.bytes');
54 $value_perf = $self->{result_values}->{free};
55 }
56
57 my %total_options = ();
58 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
59 $total_options{total} = $self->{result_values}->{total};
60 $total_options{cast_int} = 1;
61 }
62
63 $self->{output}->perfdata_add(
64 label => $label, unit => 'B',
65 nlabel => $self->{nlabel},
66 instances => $self->use_instances(extra_instance => $options{extra_instance}) ? $self->{result_values}->{display} : undef,
67 value => $value_perf,
68 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, %total_options),
69 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, %total_options),
70 min => 0, max => $self->{result_values}->{total}
71 );
72 }
73
74 sub custom_usage_threshold {
75 my ($self, %options) = @_;
76
77 my ($exit, $threshold_value);
78 $threshold_value = $self->{result_values}->{used};
79 $threshold_value = $self->{result_values}->{free} if (defined($self->{instance_mode}->{option_results}->{free}));
80 if ($self->{instance_mode}->{option_results}->{units} eq '%') {
81 $threshold_value = $self->{result_values}->{prct_used};
82 $threshold_value = $self->{result_values}->{prct_free} if (defined($self->{instance_mode}->{option_results}->{free}));
83 }
84 $exit = $self->{perfdata}->threshold_check(value => $threshold_value, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' } ]);
85 return $exit;
86 }
87
88 sub custom_usage_output {
89 my ($self, %options) = @_;
90
91 my ($total_size_value, $total_size_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{total});
92 my ($total_used_value, $total_used_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{used});
93 my ($total_free_value, $total_free_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{free});
94 my $msg = sprintf("Total: %s Used: %s (%.2f%%) Free: %s (%.2f%%)",
95 $total_size_value . " " . $total_size_unit,
96 $total_used_value . " " . $total_used_unit, $self->{result_values}->{prct_used},
97 $total_free_value . " " . $total_free_unit, $self->{result_values}->{prct_free});
98 return $msg;
99 }
100
101 sub custom_usage_calc {
102 my ($self, %options) = @_;
103
104 $self->{result_values}->{display} = $options{new_datas}->{$self->{instance} . '_display'};
105 $self->{result_values}->{total} = $options{new_datas}->{$self->{instance} . '_total'};
106 $self->{result_values}->{used} = $options{new_datas}->{$self->{instance} . '_used'};
107 $self->{result_values}->{free} = $self->{result_values}->{total} - $self->{result_values}->{used};
108
109 $self->{result_values}->{prct_used} = $self->{result_values}->{used} * 100 / $self->{result_values}->{total};
110 $self->{result_values}->{prct_free} = 100 - $self->{result_values}->{prct_used};
111
112 return 0;
113 }
114
115 sub new {
116 my ($class, %options) = @_;
117 my $self = $class->SUPER::new(package => __PACKAGE__, %options);
118 bless $self, $class;
119
120 $options{options}->add_options(arguments => {
121 "filter-name:s" => { name => 'filter_name' },
122 "units:s" => { name => 'units', default => '%' },
123 "free" => { name => 'free' },
124 });
125
126 return $self;
127 }
128
129 sub prefix_disk_output {
130 my ($self, %options) = @_;
131
132 return "Disk '" . $options{instance_value}->{display} . "' ";
133 }
134
135 sub manage_selection {
136 my ($self, %options) = @_;
137 $options{sql}->connect();
138
139 my $query = q{
140 SELECT * FROM SYS.M_DISKS
141 };
142 $options{sql}->query(query => $query);
143 $self->{disk} = {};
144
145 while ((my $row = $options{sql}->fetchrow_hashref())) {
146 my $name = $row->{HOST} . ':' . $row->{PATH};
147
148 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
149 $name !~ /$self->{option_results}->{filter_name}/) {
150 $self->{output}->output_add(long_msg => "skipping '" . $name . "': no matching filter.", debug => 1);
151 next;
152 }
153
154 $self->{disk}->{$name} = {
155 used => $row->{USED_SIZE},
156 total => $row->{TOTAL_SIZE},
157 display => $name
158 };
159 }
160
161 if (scalar(keys %{$self->{disk}}) <= 0) {
162 $self->{output}->add_option_msg(short_msg => "No disk found.");
163 $self->{output}->option_exit();
164 }
165 }
166
167 1;
168
169 __END__
170
171 =head1 MODE
172
173 Check disk usages.
174
175 =over 8
176
177 =item B<--warning-usage>
178
179 Warning threshold.
180
181 =item B<--critical-usage>
182
183 Critical threshold.
184
185 =item B<--filter-name>
186
187 Filter disk name. (can be a regex)
188
189 =item B<--units>
190
191 Default is '%', can be 'B'
192
193 =item B<--free>
194
195 Thresholds are on free space left.
196
197 =back
198
199 =cut