Debian Codegraph @

src/os/aix/local/custom/cli.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 os::aix::local::custom::cli;
22
23 use strict;
24 use warnings;
25 use centreon::plugins::ssh;
26 use centreon::plugins::misc;
27
28 sub new {
29 my ($class, %options) = @_;
30 my $self = {};
31 bless $self, $class;
32
33 if (!defined($options{output})) {
34 print "Class Custom: Need to specify 'output' argument.\n";
35 exit 3;
36 }
37 if (!defined($options{options})) {
38 $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument.");
39 $options{output}->option_exit();
40 }
41
42 if (!defined($options{noptions})) {
43 $options{options}->add_options(arguments => {
44 'hostname:s' => { name => 'hostname' },
45 'timeout:s' => { name => 'timeout', default => 45 },
46 'command:s' => { name => 'command' },
47 'command-path:s' => { name => 'command_path' },
48 'command-options:s' => { name => 'command_options' },
49 'sudo:s' => { name => 'sudo' },
50 'ignore-exit-code:s' => { name => 'ignore_exit_code' }
51 });
52 }
53 $options{options}->add_help(package => __PACKAGE__, sections => 'CLI OPTIONS', once => 1);
54
55 $self->{output} = $options{output};
56 $self->{ssh} = centreon::plugins::ssh->new(%options);
57
58 return $self;
59 }
60
61 sub set_options {
62 my ($self, %options) = @_;
63
64 $self->{option_results} = $options{option_results};
65 }
66
67 sub set_defaults {}
68
69 sub check_options {
70 my ($self, %options) = @_;
71
72 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
73 $self->{ssh}->check_options(option_results => $self->{option_results});
74 }
75
76 centreon::plugins::misc::check_security_command(
77 output => $self->{output},
78 command => $self->{option_results}->{command},
79 command_options => $self->{option_results}->{command_options},
80 command_path => $self->{option_results}->{command_path}
81 );
82
83 return 0;
84 }
85
86 sub execute_command {
87 my ($self, %options) = @_;
88
89 my $no_quit = defined($self->{option_results}->{ignore_exit_code}) && $self->{option_results}->{ignore_exit_code} ne '' ? 1 : 0;
90 if (defined($options{no_quit})) {
91 $no_quit = $options{no_quit};
92 }
93
94 my ($stdout, $exit_code);
95 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
96 ($stdout, $exit_code) = $self->{ssh}->execute(
97 hostname => $self->{option_results}->{hostname},
98 sudo => $self->{option_results}->{sudo},
99 command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command},
100 command_path => $self->{option_results}->{command_path},
101 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $options{command_options},
102 timeout => $self->{option_results}->{timeout},
103 no_quit => $no_quit
104 );
105 } else {
106 ($stdout, $exit_code) = centreon::plugins::misc::execute(
107 output => $self->{output},
108 sudo => $self->{option_results}->{sudo},
109 options => { timeout => $self->{option_results}->{timeout} },
110 command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command},
111 command_path => $self->{option_results}->{command_path},
112 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $options{command_options},
113 no_quit => $no_quit
114 );
115 }
116
117 $self->{output}->output_add(long_msg => "command response: $stdout", debug => 1);
118
119 if (defined($self->{option_results}->{ignore_exit_code}) && $self->{option_results}->{ignore_exit_code} ne '' &&
120 $exit_code != 0 && $exit_code !~ /$self->{option_results}->{ignore_exit_code}/) {
121 $self->{output}->add_option_msg(short_msg => sprintf('command execution error [exit code: %s]', $exit_code));
122 $self->{output}->option_exit();
123 }
124
125 return ($stdout, $exit_code);
126 }
127
128 1;
129
130 __END__
131
132 =head1 NAME
133
134 ssh
135
136 =head1 SYNOPSIS
137
138 my ssh
139
140 =head1 CLI OPTIONS
141
142 =over 8
143
144 =item B<--hostname>
145
146 Hostname to query.
147
148 =item B<--timeout>
149
150 Timeout in seconds for the command (default: 45).
151
152 =item B<--command>
153
154 Command to get information. Used it you have output in a file.
155
156 =item B<--command-path>
157
158 Command path.
159
160 =item B<--command-options>
161
162 Command options.
163
164 =item B<--sudo>
165
166 sudo command.
167
168 =item B<--ignore-exit-code>
169
170 Don't quit if the exit code matches that option.
171
172 =back
173
174 =head1 DESCRIPTION
175
176 B<custom>.
177
178 =cut