Debian Codegraph @

src/storage/hp/storeonce/ssh/custom/custom.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 storage::hp::storeonce::ssh::custom::custom;
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 });
50 }
51 $options{options}->add_help(package => __PACKAGE__, sections => 'SSH OPTIONS', once => 1);
52
53 $self->{output} = $options{output};
54 $self->{ssh} = centreon::plugins::ssh->new(%options);
55
56 return $self;
57 }
58
59 sub set_options {
60 my ($self, %options) = @_;
61
62 $self->{option_results} = $options{option_results};
63 }
64
65 sub set_defaults {}
66
67 sub check_options {
68 my ($self, %options) = @_;
69
70 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
71 $self->{ssh}->check_options(option_results => $self->{option_results});
72 }
73
74 centreon::plugins::misc::check_security_command(
75 output => $self->{output},
76 command => $self->{option_results}->{command},
77 command_options => $self->{option_results}->{command_options},
78 command_path => $self->{option_results}->{command_path}
79 );
80
81 return 0;
82 }
83
84 ##############
85 # Specific methods
86 ##############
87 sub execute_command {
88 my ($self, %options) = @_;
89
90 $self->{ssh_commands} = '';
91 foreach (@{$options{commands}}) {
92 $self->{ssh_commands} .= "$_\n";
93 }
94
95 my $content;
96 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
97 ($content) = $self->{ssh}->execute(
98 ssh_pipe => 1,
99 hostname => $self->{option_results}->{hostname},
100 command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $self->{ssh_commands},
101 command_path => $self->{option_results}->{command_path},
102 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef,
103 timeout => $self->{option_results}->{timeout}
104 );
105 } else {
106 if (!defined($self->{option_results}->{command}) || $self->{option_results}->{command} eq '') {
107 $self->{output}->add_option_msg(short_msg => 'please set --hostname option for ssh connection (or --command for local)');
108 $self->{output}->option_exit();
109 }
110 ($content) = centreon::plugins::misc::execute(
111 ssh_pipe => 1,
112 output => $self->{output},
113 options => { timeout => $self->{option_results}->{timeout} },
114 command => $self->{option_results}->{command},
115 command_path => $self->{option_results}->{command_path},
116 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef
117 );
118 }
119
120 return $content;
121 }
122
123 1;
124
125 __END__
126
127 =head1 NAME
128
129 ssh
130
131 =head1 SYNOPSIS
132
133 my ssh
134
135 =head1 SSH OPTIONS
136
137 =over 8
138
139 =item B<--hostname>
140
141 Hostname to query.
142
143 =item B<--timeout>
144
145 Timeout in seconds for the command (default: 45).
146
147 =item B<--command>
148
149 Command to get information. Used it you have output in a file.
150
151 =item B<--command-path>
152
153 Command path.
154
155 =item B<--command-options>
156
157 Command options.
158
159 =back
160
161 =head1 DESCRIPTION
162
163 B<custom>.
164
165 =cut