| 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 os::windows::wsman::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 Time::HiRes qw(time); |
| 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 |
'System 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 => 'system.uptime.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} }, |
| 48 |
unit => $self->{instance_mode}->{option_results}->{unit}, |
| 49 |
value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), |
| 50 |
warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), |
| 51 |
critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), |
| 52 |
min => 0 |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
sub custom_uptime_threshold { |
| 57 |
my ($self, %options) = @_; |
| 58 |
|
| 59 |
return $self->{perfdata}->threshold_check( |
| 60 |
value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), |
| 61 |
threshold => [ |
| 62 |
{ label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, |
| 63 |
{ label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }, |
| 64 |
{ label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' } |
| 65 |
] |
| 66 |
); |
| 67 |
} |
| 68 |
|
| 69 |
sub set_counters { |
| 70 |
my ($self, %options) = @_; |
| 71 |
|
| 72 |
$self->{maps_counters_type} = [ |
| 73 |
{ name => 'global', type => 0 } |
| 74 |
]; |
| 75 |
|
| 76 |
$self->{maps_counters}->{global} = [ |
| 77 |
{ label => 'uptime', set => { |
| 78 |
key_values => [ { name => 'uptime' } ], |
| 79 |
closure_custom_output => $self->can('custom_uptime_output'), |
| 80 |
closure_custom_perfdata => $self->can('custom_uptime_perfdata'), |
| 81 |
closure_custom_threshold_check => $self->can('custom_uptime_threshold') |
| 82 |
} |
| 83 |
} |
| 84 |
]; |
| 85 |
} |
| 86 |
|
| 87 |
sub new { |
| 88 |
my ($class, %options) = @_; |
| 89 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); |
| 90 |
bless $self, $class; |
| 91 |
|
| 92 |
$options{options}->add_options(arguments => { |
| 93 |
'unit:s' => { name => 'unit', default => 's' } |
| 94 |
}); |
| 95 |
|
| 96 |
return $self; |
| 97 |
} |
| 98 |
|
| 99 |
sub check_options { |
| 100 |
my ($self, %options) = @_; |
| 101 |
$self->SUPER::check_options(%options); |
| 102 |
|
| 103 |
if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) { |
| 104 |
$self->{option_results}->{unit} = 's'; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
sub manage_selection { |
| 109 |
my ($self, %options) = @_; |
| 110 |
|
| 111 |
my $results = $options{wsman}->request( |
| 112 |
uri => 'http://schemas.microsoft.com/wbem/wsman/1/wmi/root/cimv2/*', |
| 113 |
wql_filter => "Select Frequency_Sys100NS,SystemUpTime,Timestamp_Object from Win32_PerfRawData_PerfOS_System", |
| 114 |
result_type => 'array' |
| 115 |
); |
| 116 |
|
| 117 |
# |
| 118 |
#CLASS: Win32_PerfRawData_PerfOS_System |
| 119 |
#Frequency_Sys100NS;SystemUpTime;Timestamp_Object |
| 120 |
#10000000;132847344565000000;132847576466519223 |
| 121 |
# |
| 122 |
my $uptime = 0; |
| 123 |
foreach my $result (@$results) { |
| 124 |
my $Frequency_Sys100NS = $result->{Frequency_Sys100NS}; |
| 125 |
my $SystemUpTime = $result->{SystemUpTime}; |
| 126 |
my $Timestamp_Object = $result->{Timestamp_Object}; |
| 127 |
if (!defined($result->{SystemUpTime}) || !defined($result->{Timestamp_Object}) || !defined($result->{Frequency_Sys100NS})) { |
| 128 |
$self->{output}->add_option_msg(short_msg => 'Some informations missing.'); |
| 129 |
$self->{output}->option_exit(); |
| 130 |
} |
| 131 |
|
| 132 |
$self->{global} = { uptime => ($result->{Timestamp_Object} - $result->{SystemUpTime}) / $result->{Frequency_Sys100NS} }; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
1; |
| 137 |
|
| 138 |
__END__ |
| 139 |
|
| 140 |
=head1 MODE |
| 141 |
|
| 142 |
Check system uptime. |
| 143 |
|
| 144 |
=over 8 |
| 145 |
|
| 146 |
=item B<--warning-uptime> |
| 147 |
|
| 148 |
Warning threshold. |
| 149 |
|
| 150 |
=item B<--critical-uptime> |
| 151 |
|
| 152 |
Critical threshold. |
| 153 |
|
| 154 |
=item B<--unit> |
| 155 |
|
| 156 |
Select the unit for performance data and thresholds. May be 's' for seconds, 'm' for minutes, |
| 157 |
'h' for hours, 'd' for days, 'w' for weeks. Default is seconds |
| 158 |
|
| 159 |
=back |
| 160 |
|
| 161 |
=cut |