Debian Codegraph @

src/apps/automation/ansible/tower/mode/jobs.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::automation::ansible::tower::mode::jobs;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use Date::Parse;
28 use centreon::plugins::statefile;
29
30 sub prefix_output_global {
31 my ($self, %options) = @_;
32
33 return 'Jobs ';
34 }
35
36 sub set_counters {
37 my ($self, %options) = @_;
38
39 $self->{maps_counters_type} = [
40 { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' }
41 ];
42
43 $self->{maps_counters}->{global} = [
44 { label => 'total', nlabel => 'jobs.total.count', set => {
45 key_values => [ { name => 'total' } ],
46 output_template => 'total: %d',
47 perfdatas => [
48 { value => 'total', template => '%d', min => 0 }
49 ]
50 }
51 }
52 ];
53
54 foreach (([ 'successful', 1 ], [ 'failed', 1 ], [ 'running', 1 ], [ 'canceled', 0 ], [ 'pending', 0 ], [ 'default', 0 ])) {
55 push @{$self->{maps_counters}->{global}}, {
56 label => $_->[0], nlabel => 'jobs.' . $_->[0] . '.count', display_ok => $_->[1], set => {
57 key_values => [ { name => $_->[0] }, { name => 'total' } ],
58 output_template => $_->[0] . ': %d',
59 perfdatas => [
60 { template => '%d', min => 0, max => 'total' }
61 ]
62 }
63 };
64 }
65 }
66
67 sub new {
68 my ($class, %options) = @_;
69 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
70 bless $self, $class;
71
72 $options{options}->add_options(arguments => {
73 'filter-name:s' => { name => 'filter_name' },
74 'filter-time:s' => { name => 'filter_time' },
75 'display-failed-jobs' => { name => 'display_failed_jobs' },
76 'memory' => { name => 'memory' }
77 });
78
79 $self->{statefile_cache} = centreon::plugins::statefile->new(%options);
80 return $self;
81 }
82
83 sub check_options {
84 my ($self, %options) = @_;
85 $self->SUPER::check_options(%options);
86
87 if (defined($self->{option_results}->{memory})) {
88 $self->{statefile_cache}->check_options(%options);
89 centreon::plugins::misc::mymodule_load(
90 output => $self->{output},
91 module => 'Date::Parse',
92 error_msg => "Cannot load module 'Date::Parse'."
93 );
94 }
95 }
96
97 sub manage_selection {
98 my ($self, %options) = @_;
99
100 my $last_time;
101 if (defined($self->{option_results}->{memory})) {
102 $self->{statefile_cache}->read(statefile => 'cache_ansible_tower_' . $self->{mode} . '_' . $options{custom}->get_hostname());
103 $last_time = $self->{statefile_cache}->get(name => 'last_time');
104 }
105
106 my $jobs = $options{custom}->tower_list_unified_jobs();
107
108 $self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0 };
109
110 my $current_time = time();
111 my $failed_jobs = {};
112 foreach my $job (@$jobs) {
113 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
114 && $job->{name} !~ /$self->{option_results}->{filter_name}/);
115
116 next if (defined($self->{option_results}->{filter_time})
117 && defined($job->{finished})
118 && $current_time - Date::Parse::str2time($job->{finished}) > $self->{option_results}->{filter_time} * 3600);
119
120 if (defined($self->{option_results}->{memory}) && defined($job->{finished})) {
121 my $finished_time = Date::Parse::str2time($job->{finished});
122 if (!defined($finished_time)) {
123 $self->{output}->output_add(
124 severity => 'UNKNOWN',
125 short_msg => "Can't parse date '" . $job->{finished} . "'"
126 );
127 next;
128 }
129 next if (defined($last_time) && $last_time > $finished_time);
130 }
131
132 $self->{global}->{ $job->{status} }++;
133 $self->{global}->{total}++;
134 if ($job->{status} eq 'failed') {
135 $failed_jobs->{ $job->{name} } = 1;
136 }
137 }
138
139 if (defined($self->{option_results}->{memory})) {
140 $self->{statefile_cache}->write(data => { last_time => $current_time });
141 }
142
143 if (defined($self->{option_results}->{display_failed_jobs})) {
144 $self->{output}->output_add(long_msg => 'Failed jobs list: ' . join(', ', keys %$failed_jobs));
145 }
146 }
147
148 1;
149
150 __END__
151
152 =head1 MODE
153
154 Check jobs.
155
156 =over 8
157
158 =item B<--filter-name>
159
160 Define which jobs should be monitored based on their names. This option will be treated as a regular expression.
161
162 =item B<--filter-time>
163
164 Define which jobs should be monitored based on the age of their last execution. Jobs that finished longer than X hours ago will be ignored.
165
166 =item B<--display-failed-jobs>
167
168 Display failed jobs list in verbose output.
169
170 =item B<--memory>
171
172 Only check new jobs.
173
174 =item B<--warning-total>
175
176 Threshold warning for total jobs.
177
178 =item B<--critical-total>
179
180 Threshold critical for total jobs.
181
182 =item B<--warning-successful>
183
184 Threshold warning for successful jobs.
185
186 =item B<--critical-successful>
187
188 Threshold critical for successful jobs.
189
190 =item B<--warning-failed>
191
192 Threshold warning for failed jobs.
193
194 =item B<--critical-failed>
195
196 Threshold critical for failed jobs.
197
198 =item B<--warning-running>
199
200 Threshold warning for running jobs.
201
202 =item B<--critical-running>
203
204 Threshold critical for running jobs.
205
206 =item B<--warning-canceled>
207
208 Threshold warning for canceled jobs.
209
210 =item B<--critical-canceled>
211
212 Threshold critical for canceled jobs.
213
214 =item B<--warning-pending>
215
216 Threshold warning for pending jobs.
217
218 =item B<--critical-pending>
219
220 Threshold critical for pending jobs.
221
222 =item B<--warning-default>
223
224 Threshold warning for default jobs.
225
226 =item B<--critical-default>
227
228 Threshold critical for default jobs.
229
230 =back
231
232 =cut