Debian Codegraph @

src/network/silverpeak/snmp/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 network::silverpeak::snmp::mode::uptime;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use POSIX;
28 use centreon::plugins::misc;
29 use centreon::plugins::statefile;
30 use Time::HiRes qw(time);
31
32 my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 };
33 my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' };
34
35 sub custom_uptime_output {
36 my ($self, %options) = @_;
37
38 my $msg = sprintf(
39 'System uptime is: %s',
40 centreon::plugins::misc::change_seconds(value => $self->{result_values}->{uptime}, start => 'd')
41 );
42 if (defined($self->{instance_mode}->{option_results}->{add_sysdesc})) {
43 $msg .= sprintf(', %s', $self->{result_values}->{sysdesc});
44 }
45
46 return $msg;
47 }
48
49 sub custom_uptime_perfdata {
50 my ($self, %options) = @_;
51
52 $self->{output}->perfdata_add(
53 label => 'uptime', unit => $self->{instance_mode}->{option_results}->{unit},
54 nlabel => 'system.uptime.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} },
55 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
56 warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}),
57 critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}),
58 min => 0
59 );
60 }
61
62 sub custom_uptime_threshold {
63 my ($self, %options) = @_;
64
65 return $self->{perfdata}->threshold_check(
66 value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }),
67 threshold => [
68 { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' },
69 { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' },
70 { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' }
71 ]
72 );
73 }
74
75 sub set_counters {
76 my ($self, %options) = @_;
77
78 $self->{maps_counters_type} = [
79 { name => 'global', type => 0 }
80 ];
81
82 $self->{maps_counters}->{global} = [
83 { label => 'uptime', set => {
84 key_values => [ { name => 'uptime' }, { name => 'sysdesc' } ],
85 closure_custom_output => $self->can('custom_uptime_output'),
86 closure_custom_perfdata => $self->can('custom_uptime_perfdata'),
87 closure_custom_threshold_check => $self->can('custom_uptime_threshold')
88 }
89 }
90 ];
91 }
92
93 sub new {
94 my ($class, %options) = @_;
95 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
96 bless $self, $class;
97
98 $options{options}->add_options(arguments => {
99 'force-oid:s' => { name => 'force_oid' },
100 'check-overload' => { name => 'check_overload' },
101 'reboot-window:s' => { name => 'reboot_window', default => 5000 },
102 'unit:s' => { name => 'unit', default => 's' },
103 'add-sysdesc' => { name => 'add_sysdesc' }
104 });
105
106 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
107 return $self;
108 }
109
110 sub check_options {
111 my ($self, %options) = @_;
112 $self->SUPER::check_options(%options);
113
114 if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) {
115 $self->{option_results}->{unit} = 's';
116 }
117
118 $self->{statefile_cache}->check_options(%options);
119 }
120
121 sub check_overload {
122 my ($self, %options) = @_;
123
124 return $options{timeticks} if (!defined($self->{option_results}->{check_overload}));
125
126 my $current_time = floor(time() * 100);
127 $self->{new_datas} = { last_time => $current_time, uptime => $options{timeticks} };
128 $self->{statefile_cache}->read(statefile => 'cache_' . $options{snmp}->get_hostname() . '_' . $options{snmp}->get_port() . '_' . $self->{mode});
129 my $old_uptime = $self->{statefile_cache}->get(name => 'uptime');
130 my $last_time = $self->{statefile_cache}->get(name => 'last_time');
131 $self->{new_datas}->{overload} = $self->{statefile_cache}->get(name => 'overload') || 0;
132
133 if (defined($old_uptime) && $options{timeticks} < $old_uptime) {
134 my $diff_time = $current_time - $last_time;
135 my $overflow = ($old_uptime + $diff_time) % 4294967296;
136 my $division = ($old_uptime + $diff_time) / 4294967296;
137 if ($division >= 1 &&
138 $overflow >= ($options{timeticks} - ($self->{option_results}->{reboot_window} / 2)) &&
139 $overflow <= ($options{timeticks} + ($self->{option_results}->{reboot_window} / 2))) {
140 $self->{new_datas}->{overload}++;
141 } else {
142 $self->{new_datas}->{overload} = 0;
143 }
144 }
145 $options{timeticks} += ($self->{new_datas}->{overload} * 4294967296);
146
147 $self->{statefile_cache}->write(data => $self->{new_datas});
148 return $options{timeticks};
149 }
150
151 sub manage_selection {
152 my ($self, %options) = @_;
153
154 my $oid_sysDescr = '.1.3.6.1.2.1.1.1.0';
155 my @oids = ();
156 if (defined($self->{option_results}->{add_sysdesc})) {
157 @oids = ($oid_sysDescr);
158 }
159
160 # To be used first for OS
161 # spsSystemUptime from SILVERPEAK-MGMT-MIB 8.0
162 my $oid_hrSystemUptime = '.1.3.6.1.4.1.23867.3.1.1.1.5.0';
163 # For network equipment or others
164 my $oid_sysUpTime = '.1.3.6.1.2.1.1.3.0';
165 my ($result, $value);
166
167 if (defined($self->{option_results}->{force_oid})) {
168 $result = $options{snmp}->get_leef(oids => [ @oids, $self->{option_results}->{force_oid} ], nothing_quit => 1);
169 $value = $result->{ $self->{option_results}->{force_oid} };
170 } else {
171 $result = $options{snmp}->get_leef(oids => [ @oids, $oid_hrSystemUptime, $oid_sysUpTime ], nothing_quit => 1);
172 if (defined($result->{$oid_hrSystemUptime})) {
173 $value = $result->{$oid_hrSystemUptime};
174 } else {
175 $value = $result->{$oid_sysUpTime};
176 }
177 }
178
179 $value = $self->check_overload(timeticks => $value, snmp => $options{snmp});
180 $value = floor($value / 100);
181
182 $self->{global} = { uptime => $value, sysdesc => defined($result->{$oid_sysDescr}) ? $result->{$oid_sysDescr} : '-' };
183 }
184
185 1;
186
187 __END__
188
189 =head1 MODE
190
191 Check system uptime.
192
193 =over 8
194
195 =item B<--warning-uptime>
196
197 Warning threshold.
198
199 =item B<--critical-uptime>
200
201 Critical threshold.
202
203 =item B<--add-sysdesc>
204
205 Display system description.
206
207 =item B<--force-oid>
208
209 Can choose your oid (numeric format only).
210
211 =item B<--check-overload>
212
213 Uptime counter limit is 4294967296 and overflow.
214 With that option, we manage the counter going back. But there is a few chance we can miss a reboot.
215
216 =item B<--reboot-window>
217
218 To be used with check-overload option. Time in milliseconds (default: 5000)
219 You increase the chance of not missing a reboot if you decrease that value.
220
221 =item B<--unit>
222
223 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.
224
225 =back
226
227 =cut