Debian Codegraph @

src/apps/automation/ansible/tower/mode/schedules.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::schedules;
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 use DateTime;
29 use centreon::plugins::misc;
30
31 sub custom_start_output {
32 my ($self, %options) = @_;
33
34 return sprintf(
35 'last start: %s',
36 $self->{result_values}->{start_last_time} >= 0 ? centreon::plugins::misc::change_seconds(value => $self->{result_values}->{start_last_time}) : 'never'
37 );
38 }
39
40 sub prefix_output_global {
41 my ($self, %options) = @_;
42
43 return 'Schedules ';
44 }
45
46 sub prefix_output_schedule {
47 my ($self, %options) = @_;
48
49 return "Schedule '" . $options{instance_value}->{display} . "' ";
50 }
51
52 sub set_counters {
53 my ($self, %options) = @_;
54
55 $self->{maps_counters_type} = [
56 { name => 'global', type => 0, cb_prefix_output => 'prefix_output_global' },
57 { name => 'schedules', type => 1, cb_prefix_output => 'prefix_output_schedule', message_multiple => 'All schedules are ok', skipped_code => { -10 => 1 } }
58 ];
59
60 $self->{maps_counters}->{global} = [
61 { label => 'total', nlabel => 'schedules.total.count', set => {
62 key_values => [ { name => 'total' } ],
63 output_template => 'total: %d',
64 perfdatas => [
65 { value => 'total', template => '%d', min => 0 }
66 ]
67 }
68 }
69 ];
70
71 foreach ((['successful', 1], ['failed', 1], ['running', 1], ['canceled', 0], ['pending', 0], ['default', 0], ['never', 0])) {
72 push @{$self->{maps_counters}->{global}}, {
73 label => $_->[0], nlabel => 'schedules.' . $_->[0] . '.count', display_ok => $_->[1], set => {
74 key_values => [ { name => $_->[0] }, { name => 'total' } ],
75 output_template => $_->[0] . ': %d',
76 perfdatas => [
77 { template => '%d', min => 0, max => 'total' }
78 ]
79 }
80 };
81 }
82
83 $self->{maps_counters}->{schedules} = [
84 {
85 label => 'job-status', type => 2,
86 unknown_default => '%{last_job_status} =~ /default/',
87 critical_default => '%{last_job_status} =~ /failed/',
88 set => {
89 key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
90 output_template => "last job status is '%s'",
91 closure_custom_perfdata => sub { return 0; },
92 closure_custom_threshold_check => \&catalog_status_threshold_ng
93 }
94 },
95 { label => 'start-last-time', nlabel => 'schedule.start.last.time.seconds', set => {
96 key_values => [ { name => 'start_last_time' }, { name => 'display' } ],
97 closure_custom_output => $self->can('custom_start_output'),
98 perfdatas => [
99 { template => '%d', min => 0, unit => 's', label_extra_instance => 1, instance_use => 'display' }
100 ]
101 }
102 }
103 ];
104 }
105
106 sub new {
107 my ($class, %options) = @_;
108 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
109 bless $self, $class;
110
111 $options{options}->add_options(arguments => {
112 'filter-name:s' => { name => 'filter_name' }
113 });
114
115 return $self;
116 }
117
118 sub manage_selection {
119 my ($self, %options) = @_;
120
121 my $schedules = $options{custom}->tower_list_schedules(add_job_status => 1);
122
123 $self->{global} = { total => 0, failed => 0, successful => 0, canceled => 0, default => 0, pending => 0, running => 0, never => 0 };
124 $self->{schedules} = {};
125 foreach my $schedule (@$schedules) {
126 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
127 && $schedule->{name} !~ /$self->{option_results}->{filter_name}/);
128
129 my $start_last_time = -1;
130 if (defined($schedule->{last_job}->{started})) {
131 #2020-07-30T09:36:28.806283Z
132 if ($schedule->{last_job}->{started} =~ /^(\d+)-(\d+)-(\d+)T(\d+):(\d+):(\d+)/) {
133 my $dt = DateTime->new(year => $1, month => $2, day => $3, hour => $4, minute => $5, second => $6);
134 $start_last_time = time() - $dt->epoch();
135 }
136 }
137
138 $self->{schedules}->{ $schedule->{id} } = {
139 display => $schedule->{name},
140 last_job_status => defined($schedule->{last_job}->{status}) ? $schedule->{last_job}->{status} : 'never',
141 start_last_time => $start_last_time
142 };
143 $self->{global}->{total}++;
144
145 if (defined($schedule->{last_job}->{status})) {
146 $self->{global}->{ $schedule->{last_job}->{status} }++;
147 } else {
148 $self->{global}->{never}++;
149 }
150 }
151 }
152
153 1;
154
155 __END__
156
157 =head1 MODE
158
159 Check schedules.
160
161 =over 8
162
163 =item B<--filter-name>
164
165 Filter schedule name (can use regexp).
166
167 =item B<--unknown-job-status>
168
169 Define the conditions to match for the status to be UNKNOWN (default: '%{last_job_status} =~ /default/').
170 You can use the following variables: %{last_job_status}, %{display}
171
172 =item B<--warning-job-status>
173
174 Define the conditions to match for the status to be WARNING.
175 You can use the following variables: %{last_job_status}, %{display}
176
177 =item B<--critical-job-status>
178
179 Define the conditions to match for the status to be CRITICAL (default: '%{last_job_status} =~ /failed/').
180 You can use the following variables: %{last_job_status}, %{display}
181
182 =item B<--warning-*> B<--critical-*>
183
184 Thresholds.
185 Can be: 'total', 'successful', 'failed', 'running', 'canceled', 'pending', 'default', 'never', 'start-last-time' (s).
186
187 =back
188
189 =cut