Debian Codegraph @

src/apps/automation/ansible/tower/mode/jobtemplates.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::jobtemplates;
22
23 use base qw(centreon::plugins::templates::counter);
24
25 use strict;
26 use warnings;
27 use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng);
28
29 sub prefix_output_global {
30 my ($self, %options) = @_;
31
32 return 'Job templates ';
33 }
34
35 sub prefix_output_jobtpl {
36 my ($self, %options) = @_;
37
38 return "Job template '" . $options{instance_value}->{display} . "' ";
39 }
40
41 sub set_counters {
42 my ($self, %options) = @_;
43
44 $self->{maps_counters_type} = [
45 { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' },
46 { name => 'jobtpl', type => 1, cb_prefix_output => 'prefix_output_jobtpl', message_multiple => 'All job templates are ok', skipped_code => { -10 => 1 } }
47 ];
48
49 $self->{maps_counters}->{global} = [
50 { label => 'total', nlabel => 'jobtemplates.total.count', set => {
51 key_values => [ { name => 'total' } ],
52 output_template => 'total: %d',
53 perfdatas => [
54 { value => 'total', template => '%d', min => 0 }
55 ]
56 }
57 }
58 ];
59
60 foreach ((['successful', 1], ['failed', 1], ['running', 1], ['canceled', 0], ['pending', 0], ['default', 0], ['never', 0])) {
61 push @{$self->{maps_counters}->{global}}, {
62 label => $_->[0], nlabel => 'jobtemplates.' . $_->[0] . '.count', display_ok => $_->[1], set => {
63 key_values => [ { name => $_->[0] }, { name => 'total' } ],
64 output_template => $_->[0] . ': %d',
65 perfdatas => [
66 { template => '%d', min => 0, max => 'total' }
67 ]
68 }
69 };
70 }
71
72 $self->{maps_counters}->{jobtpl} = [
73 {
74 label => 'job-status', type => 2,
75 unknown_default => '%{last_job_status} =~ /default/',
76 critical_default => '%{last_job_status} =~ /failed/',
77 set => {
78 key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
79 output_template => "last job status is '%s'",
80 closure_custom_perfdata => sub { return 0; },
81 closure_custom_threshold_check => \&catalog_status_threshold_ng
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 'filter-name:s' => { name => 'filter_name' },
94 'launch-job-template-id:s' => { name => 'launch_job_template_id' },
95 'launch-extra-vars:s' => { name => 'launch_extra_vars' },
96 'launch-tags:s' => { name => 'launch_tags' },
97 'launch-limit:s' => { name => 'launch_limit' },
98 'launch-inventory:s' => { name => 'launch_inventory' },
99 'launch-credential:s' => { name => 'launch_credential' },
100 'launch-max-retries:s' => { name => 'launch_max_retries', default => 5 },
101 'launch-retry-interval:s' => { name => 'launch_retry_interval', default => 10 }
102 });
103
104 return $self;
105 }
106
107 sub launch_job {
108 my ($self, %options) = @_;
109
110 my $job = $options{custom}->tower_launch_job_template(
111 launch_job_template_id => $self->{option_results}->{launch_job_template_id},
112 launch_extra_vars => $self->{option_results}->{launch_extra_vars},
113 launch_tags => $self->{option_results}->{launch_tags},
114 launch_limit => $self->{option_results}->{launch_limit},
115 launch_inventory => $self->{option_results}->{launch_inventory},
116 launch_credential => $self->{option_results}->{launch_credential}
117 );
118
119 my $job_id = $job->{id};
120 my ($job_completed, $retry_idx, $job_result);
121 $retry_idx = 0;
122 while (!defined($job_completed) && $retry_idx < $self->{option_results}->{launch_max_retries}) {
123 sleep($self->{option_results}->{launch_retry_interval}) if ($retry_idx > 0);
124 $job_result = $options{custom}->tower_get_job(job_id => $job_id);
125
126 $job_completed = $job_result->{finished};
127 $retry_idx++;
128 }
129
130 if ($retry_idx >= $self->{option_results}->{launch_max_retries}) {
131 $self->{output}->add_option_msg(short_msg => 'cannot get the last job result');
132 $self->{output}->option_exit();
133 }
134
135 $job->{summary_fields}->{last_job} = $job_result;
136 return [$job];
137 }
138
139 sub manage_selection {
140 my ($self, %options) = @_;
141
142 my $jobs;
143 if (defined($self->{option_results}->{launch_job_template_id}) && $self->{option_results}->{launch_job_template_id} =~ /\d+/) {
144 $jobs = $self->launch_job(custom => $options{custom});
145 } else {
146 $jobs = $options{custom}->tower_list_job_templates();
147 }
148
149 $self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0, never => 0 };
150 $self->{jobtpl} = {};
151
152 foreach my $job (@$jobs) {
153 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
154 && $job->{name} !~ /$self->{option_results}->{filter_name}/);
155
156 $self->{jobtpl}->{ $job->{id} } = {
157 display => $job->{name},
158 last_job_status => defined($job->{summary_fields}->{last_job}->{status}) ? $job->{summary_fields}->{last_job}->{status} : 'never'
159 };
160 $self->{global}->{total}++;
161
162 if (defined($job->{summary_fields}->{last_job}->{status})) {
163 $self->{global}->{ $job->{summary_fields}->{last_job}->{status} }++;
164 } else {
165 $self->{global}->{never}++;
166 }
167 }
168 }
169
170 1;
171
172 __END__
173
174 =head1 MODE
175
176 Check job templates.
177
178 =over 8
179
180 =item B<--filter-name>
181
182 Filter job template name (can use regexp).
183
184 =item B<--launch-job-template-id>
185
186 The job_template ID to launch.
187
188 =item B<--launch-inventory>
189
190 Specify inventory for job template to run.
191
192 =item B<--launch-credential>
193
194 Specify machine credential for job template to run.
195
196 =item B<--launch-tags>
197
198 Specify tagged actions in the playbook to run.
199
200 =item B<--launch-limit>
201
202 Specify host limit for job template to run.
203
204 =item B<--launch-extra-vars>
205
206 yaml format text that contains extra variables to pass on.
207
208 =item B<--launch-max-retries>
209
210 Number of retries to get job result once launched (default: 5).
211
212 =item B<--launch-retry-interval>
213
214 Number of seconds between retries (default : 10).
215
216 =item B<--unknown-job-status>
217
218 Define the conditions to match for the status to be UNKNOWN (default: '%{last_job_status} =~ /default/').
219 You can use the following variables: %{last_job_status}, %{display}
220
221 =item B<--warning-job-status>
222
223 Define the conditions to match for the status to be WARNING.
224 You can use the following variables: %{last_job_status}, %{display}
225
226 =item B<--critical-job-status>
227
228 Define the conditions to match for the status to be CRITICAL (default: '%{last_job_status} =~ /failed/').
229 You can use the following variables: %{last_job_status}, %{display}
230
231 =item B<--warning-*> B<--critical-*>
232
233 Thresholds.
234 Can be: 'total', 'successful', 'failed', 'running', 'canceled', 'pending', 'default', 'never'.
235
236 =back
237
238 =cut