Debian Codegraph @

src/apps/backup/rapidrecovery/snmp/mode/repository.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 apps::backup::rapidrecovery::snmp::mode::repository;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold catalog_status_calc);
28
29 sub custom_status_output {
30 my ($self, %options) = @_;
31
32 my $msg = 'status : ' . $self->{result_values}->{status};
33 return $msg;
34 }
35
36 sub custom_space_output {
37 my ($self, %options) = @_;
38
39 my $msg = sprintf(
40 "space total: %s %s used: %s %s (%.2f%%) free: %s %s (%.2f%%)",
41 $self->{perfdata}->change_bytes(value => $self->{result_values}->{total}),
42 $self->{perfdata}->change_bytes(value => $self->{result_values}->{used}),
43 $self->{result_values}->{prct_used},
44 $self->{perfdata}->change_bytes(value => $self->{result_values}->{free}),
45 $self->{result_values}->{prct_free}
46 );
47 return $msg;
48 }
49
50 sub set_counters {
51 my ($self, %options) = @_;
52
53 $self->{maps_counters_type} = [
54 { name => 'repository', type => 1, cb_prefix_output => 'prefix_repository_output', message_multiple => 'All repositories are ok', skipped_code => { -10 => 1 } }
55 ];
56
57 $self->{maps_counters}->{repository} = [
58 { label => 'status', threshold => 0, set => {
59 key_values => [ { name => 'status' }, { name => 'display' } ],
60 closure_custom_calc => \&catalog_status_calc,
61 closure_custom_output => $self->can('custom_status_output'),
62 closure_custom_perfdata => sub { return 0; },
63 closure_custom_threshold_check => \&catalog_status_threshold,
64 }
65 },
66 { label => 'space-usage', nlabel => 'repository.space.usage.bytes', set => {
67 key_values => [ { name => 'used' }, { name => 'free' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
68 closure_custom_output => $self->can('custom_space_output'),
69 perfdatas => [
70 { value => 'used', template => '%d', min => 0, max => 'total',
71 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' },
72 ],
73 }
74 },
75 { label => 'space-usage-free', display_ok => 0, nlabel => 'repository.space.free.bytes', set => {
76 key_values => [ { name => 'free' }, { name => 'used' }, { name => 'prct_used' }, { name => 'prct_free' }, { name => 'total' }, { name => 'display' } ],
77 closure_custom_output => $self->can('custom_space_output'),
78 perfdatas => [
79 { value => 'free', template => '%d', min => 0, max => 'total',
80 unit => 'B', cast_int => 1, label_extra_instance => 1, instance_use => 'display' },
81 ],
82 }
83 },
84 { label => 'space-usage-prct', display_ok => 0, nlabel => 'repository.space.usage.percentage', set => {
85 key_values => [ { name => 'prct_used' }, { name => 'display' } ],
86 output_template => 'space used: %.2f %%',
87 perfdatas => [
88 { value => 'prct_used', template => '%.2f', min => 0, max => 100, unit => '%',
89 label_extra_instance => 1, instance_use => 'display' },
90 ],
91 }
92 },
93 ];
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 'filter-name:s' => { name => 'filter_name' },
103 'unknown-status:s' => { name => 'unknown_status', default => '%{status} =~ /unknown/i' },
104 'warning-status:s' => { name => 'warning_status', default => '' },
105 'critical-status:s' => { name => 'critical_status', default => '%{status} =~ /error/i' },
106 });
107
108 return $self;
109 }
110
111 sub check_options {
112 my ($self, %options) = @_;
113 $self->SUPER::check_options(%options);
114
115 $self->change_macros(macros => ['warning_status', 'critical_status', 'unknown_status']);
116 }
117
118 sub prefix_repository_output {
119 my ($self, %options) = @_;
120
121 return "Repository '" . $options{instance_value}->{display} . "' ";
122 }
123
124 my $map_status = {
125 0 => 'unknown', 1 => 'unmounting',
126 2 => 'unmounted', 3 => 'mounting',
127 4 => 'mounted', 5 => 'maintenance',
128 6 => 'error'
129 };
130
131 my $mapping = {
132 repositoryName => { oid => '.1.3.6.1.4.1.674.11000.1000.200.100.200.1.3' },
133 repositoryStatus => { oid => '.1.3.6.1.4.1.674.11000.1000.200.100.200.1.5', map => $map_status },
134 repositorySizeMB => { oid => '.1.3.6.1.4.1.674.11000.1000.200.100.200.1.6' },
135 repositoryFreeMB => { oid => '.1.3.6.1.4.1.674.11000.1000.200.100.200.1.7' },
136 };
137
138 sub manage_selection {
139 my ($self, %options) = @_;
140
141 my $oid_repositoryEntry = '.1.3.6.1.4.1.674.11000.1000.200.100.200.1';
142 my $snmp_result = $options{snmp}->get_table(
143 oid => $oid_repositoryEntry,
144 start => $mapping->{repositoryName}->{oid},
145 nothing_quit => 1
146 );
147
148 $self->{repository} = {};
149 foreach my $oid (keys %$snmp_result) {
150 next if ($oid !~ /^$mapping->{repositoryName}->{oid}\.(.*)/);
151 my $instance = $1;
152 my $result = $options{snmp}->map_instance(mapping => $mapping, results => $snmp_result, instance => $instance);
153 if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne '' &&
154 $result->{repositoryName} !~ /$self->{option_results}->{filter_name}/) {
155 $self->{output}->output_add(long_msg => "skipping '" . $result->{repositoryName} . "': no matching filter.", debug => 1);
156 next;
157 }
158
159 my $total = $result->{repositorySizeMB} * 1024 * 1024;
160 my $free = $result->{repositoryFreeMB} * 1024 * 1024;
161 $self->{repository}->{$instance} = {
162 display => $result->{repositoryName},
163 status => $result->{repositoryStatus},
164 free => $free,
165 used => $total - $free,
166 prct_used => ($total - $free) * 100 / $total,
167 prct_free => $free * 100 / $total,
168 total => $total,
169 };
170 }
171
172 if (scalar(keys %{$self->{repository}}) <= 0) {
173 $self->{output}->add_option_msg(short_msg => 'No repository found.');
174 $self->{output}->option_exit();
175 }
176 }
177
178 1;
179
180 __END__
181
182 =head1 MODE
183
184 Check repositories.
185
186 =over 8
187
188 =item B<--unknown-status>
189
190 Define the conditions to match for the status to be UNKNOWN (default: '%{status} =~ /unknown/i').
191 You can use the following variables: %{status}, %{display}
192
193 =item B<--warning-status>
194
195 Define the conditions to match for the status to be WARNING (default: '').
196 You can use the following variables: %{status}, %{display}
197
198 =item B<--critical-status>
199
200 Define the conditions to match for the status to be CRITICAL (default: '%{status} =~ /error/i').
201 You can use the following variables: %{status}, %{display}
202
203 =item B<--warning-*> B<--critical-*>
204
205 Thresholds.
206 Can be: 'space-usage' (B), 'space-usage-free' (B), 'space-usage-prct' (%).
207
208 =item B<--filter-name>
209
210 Filter repository name (can be a regexp).
211
212 =back
213
214 =cut