Debian Codegraph @

src/apps/automation/ansible/tower/mode/hosts.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::hosts;
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 'Hosts ';
33 }
34
35 sub prefix_output_host {
36 my ($self, %options) = @_;
37
38 return "Host '" . $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 => 'hosts', type => 1, cb_prefix_output => 'prefix_output_host', message_multiple => 'All hosts are ok', skipped_code => { -10 => 1 } }
47 ];
48
49 $self->{maps_counters}->{global} = [
50 { label => 'total', nlabel => 'hosts.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 { label => 'failed', nlabel => 'hosts.failed.count', set => {
59 key_values => [ { name => 'failed' }, { name => 'total' } ],
60 output_template => 'failed: %d',
61 perfdatas => [
62 { template => '%d', min => 0, max => 'total' }
63 ]
64 }
65 }
66 ];
67
68 $self->{maps_counters}->{hosts} = [
69 {
70 label => 'job-status', type => 2,
71 unknown_default => '%{last_job_status} =~ /default/',
72 critical_default => '%{last_job_status} =~ /failed/',
73 set => {
74 key_values => [ { name => 'last_job_status' }, { name => 'display' } ],
75 output_template => "last job status is '%s'",
76 closure_custom_perfdata => sub { return 0; },
77 closure_custom_threshold_check => \&catalog_status_threshold_ng
78 }
79 }
80 ];
81 }
82
83 sub new {
84 my ($class, %options) = @_;
85 my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1);
86 bless $self, $class;
87
88 $options{options}->add_options(arguments => {
89 'filter-name:s' => { name => 'filter_name' },
90 'display-failed-hosts' => { name => 'display_failed_hosts' }
91 });
92
93 return $self;
94 }
95
96 sub manage_selection {
97 my ($self, %options) = @_;
98
99 my $hosts = $options{custom}->tower_list_hosts();
100
101 $self->{global} = {
102 total => scalar(@$hosts),
103 failed => 0
104 };
105 $self->{hosts} = {};
106
107 my $failed_hosts = [];
108 foreach my $host (@$hosts) {
109 next if (defined($self->{option_results}->{filter_name}) && $self->{option_results}->{filter_name} ne ''
110 && $host->{name} !~ /$self->{option_results}->{filter_name}/);
111
112 $self->{hosts}->{ $host->{id} } = {
113 display => $host->{name},
114 last_job_status => $host->{summary_fields}->{last_job}->{status}
115 };
116
117 if ($host->{has_active_failures}) {
118 $self->{global}->{failed}++;
119 push @$failed_hosts, $host->{name};
120 }
121 }
122
123 if (defined($self->{option_results}->{display_failed_hosts})) {
124 $self->{output}->output_add(long_msg => 'Failed hosts list: ' . join(', ', @$failed_hosts));
125 }
126 }
127
128 1;
129
130 __END__
131
132 =head1 MODE
133
134 Check hosts.
135
136 =over 8
137
138 =item B<--filter-name>
139
140 Filter host name (can use regexp).
141
142 =item B<--display-failed-hosts>
143
144 Display failed hosts list in verbose output.
145
146 =item B<--unknown-job-status>
147
148 Define the conditions to match for the status to be UNKNOWN (default: '%{last_job_status} =~ /default/').
149 You can use the following variables: %{last_job_status}, %{display}
150
151 =item B<--warning-job-status>
152
153 Define the conditions to match for the status to be WARNING.
154 You can use the following variables: %{last_job_status}, %{display}
155
156 =item B<--critical-job-status>
157
158 Define the conditions to match for the status to be CRITICAL (default: '%{last_job_status} =~ /failed/').
159 You can use the following variables: %{last_job_status}, %{display}
160
161 =item B<--warning-*> B<--critical-*>
162
163 Thresholds.
164 Can be: 'total', 'failed'.
165
166 =back
167
168 =cut