Debian Codegraph @

src/apps/oracle/gg/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 apps::oracle::gg::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' },
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 'ggs-home:s' => { name => 'ggs_home' },
51 'oracle-home:s' => { name => 'oracle_home' }
52 });
53 }
54
55 $options{options}->add_help(package => __PACKAGE__, sections => 'CLI OPTIONS', once => 1);
56
57 $self->{output} = $options{output};
58 $self->{ssh} = centreon::plugins::ssh->new(%options);
59
60 return $self;
61 }
62
63 sub set_options {
64 my ($self, %options) = @_;
65
66 $self->{option_results} = $options{option_results};
67 }
68
69 sub set_defaults {}
70
71 sub check_options {
72 my ($self, %options) = @_;
73
74 if (defined($self->{option_results}->{timeout}) && $self->{option_results}->{timeout} =~ /(\d+)/) {
75 $self->{timeout} = $1;
76 }
77 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
78 $self->{ssh}->check_options(option_results => $self->{option_results});
79 }
80
81 centreon::plugins::misc::check_security_command(
82 output => $self->{output},
83 command => $self->{option_results}->{command},
84 command_options => $self->{option_results}->{command_options},
85 command_path => $self->{option_results}->{command_path}
86 );
87
88 return 0;
89 }
90
91 sub get_identifier {
92 my ($self, %options) = @_;
93
94 my $id = defined($self->{option_results}->{hostname}) ? $self->{option_results}->{hostname} : 'me';
95 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
96 $id .= ':' . $self->{ssh}->get_port();
97 }
98 return $id;
99 }
100
101 sub set_local_environment {
102 my ($self, %options) = @_;
103
104 if (!defined($ENV{GGS_HOME}) || $ENV{GGS_HOME} eq '') {
105 if (!defined($self->{option_results}->{ggs_home}) || $self->{option_results}->{ggs_home} eq '') {
106 $self->{output}->add_option_msg(short_msg => "Please set option --ggs-home.");
107 $self->{output}->option_exit();
108 }
109
110 $ENV{GGS_HOME} = $self->{option_results}->{ggs_home};
111 }
112
113 if (!defined($ENV{ORACLE_HOME}) || $ENV{ORACLE_HOME} eq '') {
114 if (!defined($self->{option_results}->{oracle_home_home}) || $self->{option_results}->{oracle_home} eq '') {
115 $self->{output}->add_option_msg(short_msg => "Please set option --oracle-home.");
116 $self->{output}->option_exit();
117 }
118
119 $ENV{ORACLE_HOME} = $self->{option_results}->{oracle_home};
120 $ENV{LD_LIBRARY_PATH} = $ENV{GGS_HOME} . ':' . $ENV{ORACLE_HOME} . '/lib';
121 }
122 }
123
124 sub set_ssh_command {
125 my ($self, %options) = @_;
126
127 my $env_vars = '';
128 my $command = 'ggsci';
129 if (!defined($self->{option_results}->{ggs_home})) {
130 $self->{output}->add_option_msg(short_msg => "Please set option --ggs-home.");
131 $self->{output}->option_exit();
132 }
133 if ($self->{option_results}->{ggs_home} ne '') {
134 $command = '"' . $self->{option_results}->{ggs_home} . '/ggsci"';
135 }
136
137 if (!defined($self->{option_results}->{oracle_home})) {
138 $self->{output}->add_option_msg(short_msg => "Please set option --oracle-home.");
139 $self->{output}->option_exit();
140 }
141 if ($self->{option_results}->{oracle_home} ne '') {
142 $env_vars = 'export ORACLE_HOME="' . $self->{option_results}->{oracle_home} . '"; ';
143 $env_vars .= 'export LD_LIBRARY_PATH="' . $self->{option_results}->{oracle_home} . '/lib"; ';
144 }
145
146 return $env_vars . 'echo "' . $options{commands} . '" | ' . $command;
147 }
148
149 sub execute_command {
150 my ($self, %options) = @_;
151
152 my $timeout = $self->{timeout};
153 if (!defined($timeout)) {
154 $timeout = defined($options{timeout}) ? $options{timeout} : 45;
155 }
156
157 my $command_override = 0;
158 if (defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '') {
159 $command_override = 1;
160 }
161 my $command_options = defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : $options{command_options};
162
163 my $command = $self->{option_results}->{command};
164 my ($stdout, $exit_code);
165 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
166 if ($command_override == 0) {
167 $command = $self->set_ssh_command(commands => $options{commands});
168 }
169
170 ($stdout, $exit_code) = $self->{ssh}->execute(
171 hostname => $self->{option_results}->{hostname},
172 sudo => $self->{option_results}->{sudo},
173 command => $command,
174 command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path},
175 command_options => $command_options,
176 timeout => $timeout,
177 no_quit => $options{no_quit}
178 );
179 } else {
180 if ($command_override == 0) {
181 $self->set_local_environment();
182 $command = 'echo "' . $options{commands} . '" | "' . $ENV{GGS_HOME} . '/ggsci"';
183 }
184 ($stdout, $exit_code) = centreon::plugins::misc::execute(
185 output => $self->{output},
186 sudo => $self->{option_results}->{sudo},
187 options => { timeout => $timeout },
188 command => $command,
189 command_path => defined($self->{option_results}->{command_path}) && $self->{option_results}->{command_path} ne '' ? $self->{option_results}->{command_path} : $options{command_path},
190 command_options => $command_options,
191 no_quit => $options{no_quit}
192 );
193 }
194
195 $self->{output}->output_add(long_msg => "command response: $stdout", debug => 1);
196
197 return ($stdout, $exit_code);
198 }
199
200 1;
201
202 __END__
203
204 =head1 NAME
205
206 ggsci cli
207
208 =head1 SYNOPSIS
209
210 ggsci cli
211
212 =head1 CLI OPTIONS
213
214 =over 8
215
216 =item B<--hostname>
217
218 Hostname to query.
219
220 =item B<--timeout>
221
222 Timeout in seconds for the command (default: 45). Default value can be override by the mode.
223
224 =item B<--command>
225
226 Command to get information. Used it you have output in a file.
227
228 =item B<--command-path>
229
230 Command path.
231
232 =item B<--command-options>
233
234 Command options.
235
236 =item B<--sudo>
237
238 sudo command.
239
240 =item B<--ggs-home>
241
242 Directory with ggsci command line.
243
244 Mandatory in ssh (you can set it empty if you have an user with GGS_HOME set in PATH environment)
245
246 Mandatory in local if GSS_HOME environment variable is not.
247
248 =item B<--oracle-home>
249
250 Oracle home directory.
251
252 Mandatory in ssh (you can set it empty if you have an user with ORACLE_HOME set in environment)
253
254 Mandatory in local if ORACLE_HOME environment variable is not set.
255
256 =back
257
258 =head1 DESCRIPTION
259
260 B<custom>.
261
262 =cut