| 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::toshiba::storemate::sql::mode::maintenanceplan; |
| 22 |
|
| 23 |
use base qw(centreon::plugins::templates::counter); |
| 24 |
|
| 25 |
use strict; |
| 26 |
use warnings; |
| 27 |
use centreon::plugins::misc; |
| 28 |
use centreon::plugins::statefile; |
| 29 |
use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold); |
| 30 |
use DateTime; |
| 31 |
|
| 32 |
sub custom_status_output { |
| 33 |
my ($self, %options) = @_; |
| 34 |
|
| 35 |
my $msg = sprintf("alarm [workstation: %s] [text: %s] %s", $self->{result_values}->{workstation_id}, |
| 36 |
$self->{result_values}->{description}, $self->{result_values}->{generation_time}); |
| 37 |
return $msg; |
| 38 |
} |
| 39 |
|
| 40 |
sub custom_status_calc { |
| 41 |
my ($self, %options) = @_; |
| 42 |
|
| 43 |
$self->{result_values}->{description} = $options{new_datas}->{$self->{instance} . '_description'}; |
| 44 |
$self->{result_values}->{workstation_id} = $options{new_datas}->{$self->{instance} . '_workstation_id'}; |
| 45 |
$self->{result_values}->{since} = $options{new_datas}->{$self->{instance} . '_since'}; |
| 46 |
$self->{result_values}->{generation_time} = $options{new_datas}->{$self->{instance} . '_generation_time'}; |
| 47 |
return 0; |
| 48 |
} |
| 49 |
|
| 50 |
sub set_counters { |
| 51 |
my ($self, %options) = @_; |
| 52 |
|
| 53 |
$self->{maps_counters_type} = [ |
| 54 |
{ name => 'alarms', type => 2, message_multiple => '0 problem(s) detected', display_counter_problem => { label => 'alerts', min => 0 }, |
| 55 |
group => [ { name => 'alarm', skipped_code => { -11 => 1 } } ] |
| 56 |
} |
| 57 |
]; |
| 58 |
|
| 59 |
$self->{maps_counters}->{alarm} = [ |
| 60 |
{ label => 'status', threshold => 0, set => { |
| 61 |
key_values => [ { name => 'description' }, { name => 'workstation_id' }, { name => 'since' }, { name => 'generation_time' } ], |
| 62 |
closure_custom_calc => $self->can('custom_status_calc'), |
| 63 |
closure_custom_output => $self->can('custom_status_output'), |
| 64 |
closure_custom_perfdata => sub { return 0; }, |
| 65 |
closure_custom_threshold_check => \&catalog_status_threshold, |
| 66 |
} |
| 67 |
}, |
| 68 |
]; |
| 69 |
} |
| 70 |
|
| 71 |
sub new { |
| 72 |
my ($class, %options) = @_; |
| 73 |
my $self = $class->SUPER::new(package => __PACKAGE__, %options); |
| 74 |
bless $self, $class; |
| 75 |
|
| 76 |
$options{options}->add_options(arguments => |
| 77 |
{ |
| 78 |
"database:s" => { name => 'database', default => 'Framework' }, |
| 79 |
"warning-status:s" => { name => 'warning_status', default => '' }, |
| 80 |
"critical-status:s" => { name => 'critical_status', default => '1 == 1' }, |
| 81 |
"memory" => { name => 'memory' }, |
| 82 |
"timezone:s" => { name => 'timezone' }, |
| 83 |
}); |
| 84 |
|
| 85 |
centreon::plugins::misc::mymodule_load(output => $self->{output}, module => 'DateTime', |
| 86 |
error_msg => "Cannot load module 'DateTime'."); |
| 87 |
$self->{statefile_cache} = centreon::plugins::statefile->new(%options); |
| 88 |
return $self; |
| 89 |
} |
| 90 |
|
| 91 |
sub check_options { |
| 92 |
my ($self, %options) = @_; |
| 93 |
$self->SUPER::check_options(%options); |
| 94 |
|
| 95 |
$self->change_macros(macros => ['warning_status', 'critical_status']); |
| 96 |
if (defined($self->{option_results}->{memory})) { |
| 97 |
$self->{statefile_cache}->check_options(%options); |
| 98 |
} |
| 99 |
|
| 100 |
$self->{option_results}->{timezone} = 'GMT' if (!defined($self->{option_results}->{timezone}) || $self->{option_results}->{timezone} eq ''); |
| 101 |
} |
| 102 |
|
| 103 |
sub manage_selection { |
| 104 |
my ($self, %options) = @_; |
| 105 |
|
| 106 |
$self->{sql} = $options{sql}; |
| 107 |
$self->{sql}->connect(); |
| 108 |
$self->{sql}->query(query => "SELECT CONVERT(varchar, LOGDATE, 120) as LOGDATE, WORKSTATION_ID, USER_ID, DESCRIPTION, DESCRIPTION_PARAMETERS |
| 109 |
FROM " . $self->{option_results}->{database} . ".dbo.log |
| 110 |
WHERE (TYPE <> 1) AND (DESCRIPTION_PARAMETERS LIKE N'\%FO\%') AND (TASKNAME = 'FOCleanup')"); |
| 111 |
$self->{alarms}->{global} = { alarm => {} }; |
| 112 |
my $last_time; |
| 113 |
if (defined($self->{option_results}->{memory})) { |
| 114 |
$self->{statefile_cache}->read(statefile => 'cache_toshiba_storemate_' . $self->{mode} . '_' . $self->{sql}->get_unique_id4save()); |
| 115 |
$last_time = $self->{statefile_cache}->get(name => 'last_time'); |
| 116 |
} |
| 117 |
|
| 118 |
my ($i, $current_time) = (1, time()); |
| 119 |
my $tz = centreon::plugins::misc::set_timezone(name => $self->{option_results}->{timezone}); |
| 120 |
while (my $row = $self->{sql}->fetchrow_hashref()) { |
| 121 |
# date form: 2017-09-22 01:01:08.133 |
| 122 |
$row->{LOGDATE} =~ /^(\d+)-(\d+)-(\d+)\s+(\d+)[:\/](\d+)[:\/](\d+)/; |
| 123 |
|
| 124 |
my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6, |
| 125 |
%$tz); |
| 126 |
|
| 127 |
next if (defined($self->{option_results}->{memory}) && defined($last_time) && $last_time > $dt->epoch); |
| 128 |
|
| 129 |
my $diff_time = $current_time - $dt->epoch; |
| 130 |
|
| 131 |
$self->{alarms}->{global}->{alarm}->{$i} = { |
| 132 |
description => $row->{DESCRIPTION}, |
| 133 |
workstation_id => $row->{WORKSTATION_ID}, |
| 134 |
since => $diff_time, generation_time => centreon::plugins::misc::change_seconds(value => $diff_time) |
| 135 |
}; |
| 136 |
$i++; |
| 137 |
} |
| 138 |
|
| 139 |
if (defined($self->{option_results}->{memory})) { |
| 140 |
$self->{statefile_cache}->write(data => { last_time => $current_time }); |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
1; |
| 145 |
|
| 146 |
__END__ |
| 147 |
|
| 148 |
=head1 MODE |
| 149 |
|
| 150 |
Check the maintenance plan error logs. |
| 151 |
|
| 152 |
=over 8 |
| 153 |
|
| 154 |
=item B<--database> |
| 155 |
|
| 156 |
Database name (default: 'Framework'). |
| 157 |
|
| 158 |
=item B<--warning-status> |
| 159 |
|
| 160 |
Define the conditions to match for the status to be WARNING (default: '') |
| 161 |
You can use the following variables: %{description}, %{workstation_id}, %{since} |
| 162 |
|
| 163 |
=item B<--critical-status> |
| 164 |
|
| 165 |
Define the conditions to match for the status to be CRITICAL (default: '1 == 1'. We match all errors). |
| 166 |
You can use the following variables: %{description}, %{workstation_id}, %{since} |
| 167 |
|
| 168 |
=item B<--timezone> |
| 169 |
|
| 170 |
Timezone of time options. Default is 'GMT'. |
| 171 |
|
| 172 |
=item B<--memory> |
| 173 |
|
| 174 |
Only check new alarms. |
| 175 |
|
| 176 |
=back |
| 177 |
|
| 178 |
=cut |
| 179 |
|