Debian Codegraph @

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