Debian Codegraph @

src/storage/hp/3par/ssh/mode/uptime.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::hp::3par::ssh::mode::uptime;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use DateTime;
28 use POSIX;
29 use centreon::plugins::misc;
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_uptime_output {
35 my ($self, %options) = @_;
36
37 return sprintf(
38 'uptime is: %s',
39 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{uptime}, start => 'd')
40 );
41 }
42
43 sub custom_uptime_perfdata {
44 my ($self, %options) = @_;
45
46 $self->{output}->perfdata_add(
47 nlabel => 'node.uptime.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
48 unit => $self->{instance_mode}->{option_results}->{unit},
49 instances => 'node' . $self->{result_values}->{id},
50 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
51 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
52 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
53 min => 0
54 );
55 }
56
57 sub custom_uptime_threshold {
58 my ($self, %options) = @_;
59
60 return $self->{perfdata}->threshold_check(
61 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
62 threshold => [
63 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
64 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
65 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
66 ]
67 );
68 }
69
70 sub prefix_node_output {
71 my ($self, %options) = @_;
72
73 return "Node '" . $options{instance_value}->{id} . "' ";
74 }
75
76 sub set_counters {
77 my ($self, %options) = @_;
78
79 $self->{maps_counters_type} = [
80 { name => 'nodes', type => 1, cb_prefix_output => 'prefix_node_output', message_multiple => 'All nodes uptime are ok' }
81 ];
82
83 $self->{maps_counters}->{nodes} = [
84 { label => 'uptime', set => {
85 key_values => [ { name => 'uptime' }, { name => 'id' } ],
86 closure_custom_output => $self->can('custom_uptime_output'),
87 closure_custom_perfdata => $self->can('custom_uptime_perfdata'),
88 closure_custom_threshold_check => $self->can('custom_uptime_threshold')
89 }
90 }
91 ];
92 }
93
94 sub new {
95 my ($class, %options) = @_;
96 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
97 bless $self, $class;
98
99 $options{options}->add_options(arguments => {
100 'filter-node-id:s' => { name => 'filter_node_id' },
101 'unit:s' => { name => 'unit', default => 's' },
102 'timezone:s' => { name => 'timezone' }
103 });
104
105 return $self;
106 }
107
108 sub check_options {
109 my ($self, %options) = @_;
110 $self->SUPER::check_options(%options);
111
112 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
113 $self->{option_results}->{unit} = 's';
114 }
115 $self->{option_results}->{timezone} = 'UTC' if (!defined($self->{option_results}->{timezone}) || $self->{option_results}->{timezone} eq '');
116 }
117
118 sub get_diff_time {
119 my ($self, %options) = @_;
120
121 # 2021-09-16 10:50:15 MSK
122 return undef if ($options{date} !~ /^\s*(\d{4})-(\d{2})-(\d{2})\s+(\d+):(\d+):(\d+)/);
123
124 my $tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone});
125 my $dt = DateTime->new(
126 year => $1,
127 month => $2,
128 day => $3,
129 hour => $4,
130 minute => $5,
131 second => $6,
132 %$tz
133 );
134 return (time() - $dt->epoch());
135 }
136
137 sub manage_selection {
138 my ($self, %options) = @_;
139
140 my ($content) = $options{custom}->execute_command(commands => ['shownode -uptime']);
141
142 #Node -------Up Since--------
143 # 0 2021-09-16 10:50:15 MSK
144 # 1 2021-09-15 13:21:39 MSK
145 $self->{nodes} = {};
146 foreach my $line (split(/\n/, $content)) {
147 next if ($line !~ /^\s*(\d+)\s+(.*)\s*$/);
148 my ($node_id, $date) = ($1, $2);
149
150 next if (defined($self->{option_results}->{filter_node_id}) && $self->{option_results}->{filter_node_id} ne '' &&
151 $node_id !~ /$self->{option_results}->{filter_node_id}/);
152 my $uptime = $self->get_diff_time(date => $date);
153 next if (!defined($uptime));
154
155 $self->{nodes}->{$node_id} = {
156 id => $node_id,
157 uptime => $uptime
158 };
159 }
160
161 if (scalar(keys %{$self->{nodes}}) <= 0) {
162 $self->{output}->add_option_msg(short_msg => "Couldn't get nodes uptime");
163 $self->{output}->option_exit();
164 }
165 }
166
167 1;
168
169 __END__
170
171 =head1 MODE
172
173 Check nodes uptime.
174
175 =over 8
176
177 =item B<--filter-node-id>
178
179 Filter nodes by ID (can be a regexp).
180
181 =item B<--timezone>
182
183 Timezone options. Default is 'UTC'.
184
185 =item B<--unit>
186
187 Select the time unit for the performance data and thresholds. May be 's' for seconds, 'm' for minutes, 'h' for hours, 'd' for days, 'w' for weeks. Default is seconds.
188
189 =item B<--warning-*> B<--critical-*>
190
191 Thresholds.
192 Can be: 'uptime'.
193
194 =back
195
196 =cut