Debian Codegraph @

src/storage/emc/datadomain/snmp/mode/cleaning.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 storage::emc::datadomain::snmp::mode::cleaning;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::misc;
28 use POSIX;
29 use DateTime;
30
31 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
32 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
33
34 sub custom_last_exec_perfdata {
35 my ($self, %options) = @_;
36
37 $self->{output}->perfdata_add(
38 nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
39 unit => $self->{instance_mode}->{option_results}->{unit},
40 value => $self->{result_values}->{lastExecSeconds} >= 0 ? floor($self->{result_values}->{lastExecSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }) : $self->{result_values}->{lastExecSeconds},
41 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
42 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
43 min => 0
44 );
45 }
46
47 sub custom_last_exec_threshold {
48 my ($self, %options) = @_;
49
50 return $self->{perfdata}->threshold_check(
51 value => $self->{result_values}->{lastExecSeconds} >= 0 ? floor($self->{result_values}->{lastExecSeconds} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }) : $self->{result_values}->{lastExecSeconds},
52 threshold => [
53 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
54 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
55 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
56 ]
57 );
58 }
59
60 sub set_counters {
61 my ($self, %options) = @_;
62
63 $self->{maps_counters_type} = [
64 { name => 'global', type => 0 },
65 ];
66
67 $self->{maps_counters}->{global} = [
68 { label => 'last-cleaning-execution', nlabel => 'filesystems.cleaning.execution.last', set => {
69 key_values => [ { name => 'lastExecSeconds' }, { name => 'lastExecHuman' } ],
70 output_template => 'cleaning last execution: %s',
71 output_use => 'lastExecHuman',
72 closure_custom_perfdata => $self->can('custom_last_exec_perfdata'),
73 closure_custom_threshold_check => $self->can('custom_last_exec_threshold')
74 }
75 }
76 ];
77 }
78
79 sub new {
80 my ($class, %options) = @_;
81 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
82 bless $self, $class;
83
84 $options{options}->add_options(arguments => {
85 'unit:s' => { name => 'unit', default => 'd' },
86 'timezone:s' => { name => 'timezone'}
87 });
88
89 return $self;
90 }
91
92 sub check_options {
93 my ($self, %options) = @_;
94 $self->SUPER::check_options(%options);
95
96 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
97 $self->{option_results}->{unit} = 'd';
98 }
99 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '' && $self->{option_results}->{timezone} !~ /^[A-Za-z_\/0-9-]+$/) {
100 $self->{output}->add_option_msg(short_msg => "Wrong timezone format '" . $self->{option_results}->{timezone} . "'. (Example format: 'America/Los_Angeles')");
101 $self->{output}->option_exit();
102 }
103 }
104
105 sub manage_selection {
106 my ($self, %options) = @_;
107
108 # "Cleaning finished at 2024/08/27 13:58:59."
109 my $oid_fileSystemCleanStatus = '.1.3.6.1.4.1.19746.1.3.5.1.1.2';
110 my $snmp_result = $options{snmp}->get_table(
111 oid => $oid_fileSystemCleanStatus
112 );
113
114 my $ctime = time();
115
116 $self->{global} = { lastExecHuman => 'never', lastExecSeconds => -1 };
117 foreach my $oid (keys %$snmp_result) {
118 if ($snmp_result->{$oid} =~ /\s+(\d+)\/(\d+)\/(\d+)\s+(\d+):(\d+):(\d+)/) {
119 my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
120 # if the equipment check is on another timezone than the system where the plugin is executed.
121 if (defined($self->{option_results}->{timezone})){
122 eval {
123 $dt = $dt->set_time_zone($self->{option_results}->{timezone});
124 };
125 if ($@) {
126 $self->{output}->add_option_msg(short_msg => "Invalid timezone provided: '" . $self->{option_results}->{timezone} . "'. Check in /usr/share/zoneinfo for valid time zones.");
127 $self->{output}->option_exit();
128 }
129 }
130 my $lastExecSeconds = $ctime - $dt->epoch();
131 if ($self->{global}->{lastExecSeconds} == -1 || $self->{global}->{lastExecSeconds} > $lastExecSeconds) {
132 $self->{global}->{lastExecSeconds} = $lastExecSeconds;
133 }
134 } elsif ($snmp_result->{$oid} =~ /Cleaning: phase (\d+) of (\d+) \(([^)]+)\)/) {
135 $self->{global}->{lastExecHuman} = "running (phase $1 of $2 : $3)";
136 $self->{global}->{lastExecSeconds} = 0;
137 }
138 }
139
140 # If there is a lastExecSeconds set (if above in the looop) and this is not a cleaning running (elsif above)
141 if ($self->{global}->{lastExecSeconds} > 0 || ($self->{global}->{lastExecSeconds} == 0 && $self->{global}->{lastExecHuman} eq "never")) {
142 $self->{global}->{lastExecHuman} = centreon::plugins::misc::change_seconds(
143 value => $self->{global}->{lastExecSeconds}
144 );
145 }
146 }
147
148 1;
149
150 __END__
151
152 =head1 MODE
153
154 Check last time filesystems had been cleaned.
155
156 =over 8
157
158 =item B<--unit>
159
160 Select the time unit for thresholds. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks (default: 'd').
161
162 =item B<--timezone>
163
164 Set equipment timezone if different from Europe/London. Valid time zones can be found in C</usr/share/zoneinfo>.
165 Format example : Europe/Paris and America/Los_Angeles
166
167 =item B<--warning-*> B<--critical-*>
168
169 Thresholds.
170 Can be: 'last-cleaning-execution'.
171
172 =back
173
174 =cut