Debian Codegraph @

src/apps/backup/arcserve/udp/mssql/mode/jobstatus.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 apps::backup::arcserve::udp::mssql::mode::jobstatus;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27
28 sub custom_status_threshold {
29 my ($self, %options) = @_;
30
31 my $status = 'ok';
32 # To exclude some OK
33 if (defined($self->{instance_mode}->{option_results}->{ok_status}) && $self->{instance_mode}->{option_results}->{ok_status} ne '' &&
34 $self->eval(value => $self->{instance_mode}->{option_results}->{ok_status})) {
35 $status = 'ok';
36 } elsif (defined($self->{instance_mode}->{option_results}->{critical_status}) && $self->{instance_mode}->{option_results}->{critical_status} ne '' &&
37 $self->eval(value => $self->{instance_mode}->{option_results}->{critical_status})) {
38 $status = 'critical';
39 } elsif (defined($self->{instance_mode}->{option_results}->{warning_status}) && $self->{instance_mode}->{option_results}->{warning_status} ne '' &&
40 $self->eval(value => $self->{instance_mode}->{option_results}->{warning_status})) {
41 $status = 'warning';
42 }
43
44 return $status;
45 }
46
47 sub custom_status_output {
48 my ($self, %options) = @_;
49
50 return sprintf(
51 'status : %s (%s) [type: %s] [remote hostname: %s] [vmname: %s] [plan name: %s] [end time: %s]',
52 $self->{result_values}->{status} == 1 ? 'ok' : 'failed',
53 $self->{result_values}->{status},
54 $self->{result_values}->{type},
55 $self->{result_values}->{rhostname},
56 $self->{result_values}->{vmname},
57 $self->{result_values}->{plan_name},
58 scalar(localtime($self->{result_values}->{end_time}))
59 );
60 }
61
62 sub set_counters {
63 my ($self, %options) = @_;
64
65 $self->{maps_counters_type} = [
66 { name => 'global', type => 0 },
67 { name => 'job', type => 1, cb_prefix_output => 'prefix_job_output', message_multiple => 'All jobs are ok' },
68 ];
69
70 $self->{maps_counters}->{global} = [
71 { label => 'total', nlabel => 'jobs.total.count', set => {
72 key_values => [ { name => 'total' } ],
73 output_template => 'total jobs : %s',
74 perfdatas => [
75 { label => 'total', value => 'total', template => '%s', min => 0 },
76 ],
77 }
78 },
79 ];
80
81 $self->{maps_counters}->{job} = [
82 { label => 'status', threshold => 0, set => {
83 key_values => [
84 { name => 'status' }, { name => 'display' },
85 { name => 'type' }, { name => 'rhostname' }, { name => 'vmname' }, { name => 'plan_name' },
86 { name => 'elapsed_time' }, { name => 'end_time' }
87 ],
88 closure_custom_output => $self->can('custom_status_output'),
89 closure_custom_perfdata => sub { return 0; },
90 closure_custom_threshold_check => $self->can('custom_status_threshold')
91 }
92 },
93 ];
94 }
95
96 sub new {
97 my ($class, %options) = @_;
98 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
99 bless $self, $class;
100
101 $options{options}->add_options(arguments => {
102 'filter-server-name:s' => { name => 'filter_server_name' },
103 'filter-type:s' => { name => 'filter_type' },
104 'filter-start-time:s' => { name => 'filter_start_time' },
105 'filter-end-time:s' => { name => 'filter_end_time', default => 86400 },
106 'ok-status:s' => { name => 'ok_status', default => '%{status} == 1' },
107 'warning-status:s' => { name => 'warning_status', default => '' },
108 'critical-status:s' => { name => 'critical_status', default => '%{status} != 1' },
109 'timezone:s' => { name => 'timezone' }
110 });
111
112 return $self;
113 }
114
115 sub check_options {
116 my ($self, %options) = @_;
117 $self->SUPER::check_options(%options);
118
119 $self->change_macros(macros => [
120 'ok_status', 'warning_status', 'critical_status'
121 ]
122 );
123
124 if (defined($self->{option_results}->{timezone}) && $self->{option_results}->{timezone} ne '') {
125 $ENV{TZ} = $self->{option_results}->{timezone};
126 }
127 }
128
129 sub prefix_job_output {
130 my ($self, %options) = @_;
131
132 return "job '" . $options{instance_value}->{display} . "' ";
133 }
134
135 sub manage_selection {
136 my ($self, %options) = @_;
137
138 my $query = q{
139 SELECT
140 lj.jobId,
141 lj.jobStatus,
142 rhostname,
143 vmname,
144 DATEDIFF(s, '1970-01-01 00:00:00', lj.jobUTCStartTime) as start_time,
145 DATEDIFF(s, '1970-01-01 00:00:00', lj.jobUTCEndTime) as end_time,
146 ep.name,
147 lj.jobType,
148 lj.jobStatus
149 FROM as_edge_d2dJobHistory_lastJob lj LEFT OUTER JOIN as_edge_policy ep ON lj.planUUID = ep.uuid
150 LEFT JOIN as_edge_host h on lj.agentId = h.rhostid
151 LEFT JOIN as_edge_vsphere_entity_host_map entityHostMap ON h.rhostid = entityHostMap.hostId
152 LEFT JOIN as_edge_vsphere_vm_detail vmDetail ON entityHostMap.entityId=vmDetail.entityId
153 };
154 $options{sql}->connect();
155 $options{sql}->query(query => $query);
156
157 $self->{global} = { total => 0 };
158 $self->{job} = {};
159 my ($count, $current_time) = (0, time());
160 while ((my $row = $options{sql}->fetchrow_hashref())) {
161 my $rhostname = defined($row->{rhostname}) && $row->{rhostname} ne '' ? $row->{rhostname} : 'unknown';
162 my $vmname = defined($row->{vmname}) && $row->{vmname} ne '' ? $row->{vmname} : '-';
163 my $plan_name = defined($row->{name}) && $row->{name} ne '' ? $row->{name} : 'unknown';
164 if (defined($self->{option_results}->{filter_type}) && $self->{option_results}->{filter_type} ne '' &&
165 $row->{jobType} !~ /$self->{option_results}->{filter_type}/) {
166 $self->{output}->output_add(long_msg => "skipping job '" . $row->{jobId} . "': no matching filter type.", debug => 1);
167 next;
168 }
169 if (defined($self->{option_results}->{filter_end_time}) && $self->{option_results}->{filter_end_time} =~ /[0-9]+/ &&
170 defined($row->{end_time}) && $row->{end_time} =~ /[0-9]+/ && $row->{end_time} < ($current_time - $self->{option_results}->{filter_end_time})) {
171 $self->{output}->output_add(long_msg => "skipping job '" . $row->{jobId} . "': end time too old.", debug => 1);
172 next;
173 }
174 if (defined($self->{option_results}->{filter_start_time}) && $self->{option_results}->{filter_start_time} =~ /[0-9]+/ &&
175 defined($row->{start_time}) && $row->{start_time} =~ /[0-9]+/ && $row->{start_time} < ($current_time - $self->{option_results}->{filter_start_time})) {
176 $self->{output}->output_add(long_msg => "skipping job '" . $row->{jobId} . "': start time too old.", debug => 1);
177 next;
178 }
179 if (defined($self->{option_results}->{filter_server_name}) && $self->{option_results}->{filter_server_name} ne '' &&
180 ($row->{rhostname} !~ /$self->{option_results}->{filter_server_name}/ && $vmname !~ /$self->{option_results}->{filter_server_name}/)) {
181 $self->{output}->output_add(long_msg => "skipping job '" . $row->{jobId} . "': no matching filter type.", debug => 1);
182 next;
183 }
184
185 my $elapsed_time = defined($row->{start_time}) ? $current_time - $row->{start_time} : -1;
186 $self->{job}->{$row->{jobId}} = {
187 display => $row->{jobId},
188 elapsed_time => $elapsed_time,
189 status => $row->{jobStatus},
190 type => $row->{jobType},
191 rhostname => $rhostname,
192 vmname => $vmname,
193 plan_name => $plan_name,
194 end_time => $row->{end_time},
195 };
196
197 $self->{global}->{total}++;
198 }
199 }
200
201 1;
202
203 __END__
204
205 =head1 MODE
206
207 Check job status.
208
209 =over 8
210
211 =item B<--filter-server-name>
212
213 Filter job server name (can be a regexp).
214
215 =item B<--filter-type>
216
217 Filter job type (can be a regexp).
218
219 =item B<--filter-start-time>
220
221 Filter job with start time greater than current time less value in seconds.
222
223 =item B<--filter-end-time>
224
225 Filter job with end time greater than current time less value in seconds (default: 86400).
226
227 =item B<--timezone>
228
229 Timezone of mssql server (if not set, we use current server execution timezone).
230
231 =item B<--ok-status>
232
233 Define the conditions to match for the status to be OK (default: '%{status} == 1')
234 You can use the following variables: %{display}, %{status}
235
236 =item B<--warning-status>
237
238 Define the conditions to match for the status to be WARNING (default: '%{status} == 1')
239 You can use the following variables: %{display}, %{status}, %{type}
240
241 =item B<--critical-status>
242
243 Define the conditions to match for the status to be CRITICAL (default: '%{status} != 1').
244 You can use the following variables: %{display}, %{status}, %{type}
245
246 =item B<--warning-total>
247
248 Set warning threshold for total jobs.
249
250 =item B<--critical-total>
251
252 Set critical threshold for total jobs.
253
254 =back
255
256 =cut