Debian Codegraph @

src/storage/ibm/storwize/ssh/custom/api.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::ibm::storwize::ssh::custom::api;
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 => 30 },
46 'sudo' => { name => 'sudo' },
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 => 'STORWIZE 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_hasharray {
86 my ($self, %options) = @_;
87
88 my $result = [];
89 return $result if ($options{content} eq '');
90 my ($header, @lines) = split /\n/, $options{content};
91 my @header_names = split /$options{delim}/, $header;
92
93 for (my $i = 0; $i <= $#lines; $i++) {
94 my @content = split /$options{delim}/, $lines[$i];
95 my $data = {};
96 for (my $j = 0; $j <= $#header_names; $j++) {
97 $data->{$header_names[$j]} = $content[$j];
98 }
99 push @$result, $data;
100 }
101
102 return $result;
103 }
104
105 sub execute_command {
106 my ($self, %options) = @_;
107
108 my $content;
109 if (defined($self->{option_results}->{hostname}) && $self->{option_results}->{hostname} ne '') {
110 ($content) = $self->{ssh}->execute(
111 hostname => $self->{option_results}->{hostname},
112 command => defined($self->{option_results}->{command}) && $self->{option_results}->{command} ne '' ? $self->{option_results}->{command} : $options{command},
113 command_path => $self->{option_results}->{command_path},
114 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef,
115 timeout => $self->{option_results}->{timeout},
116 sudo => $self->{option_results}->{sudo}
117 );
118 } else {
119 if (!defined($self->{option_results}->{command}) || $self->{option_results}->{command} eq '') {
120 $self->{output}->add_option_msg(short_msg => 'please set --hostname option for ssh connection (or --command for local)');
121 $self->{output}->option_exit();
122 }
123 ($content) = centreon::plugins::misc::execute(
124 output => $self->{output},
125 options => { timeout => $self->{option_results}->{timeout} },
126 sudo => $self->{option_results}->{sudo},
127 command => $self->{option_results}->{command},
128 command_path => $self->{option_results}->{command_path},
129 command_options => defined($self->{option_results}->{command_options}) && $self->{option_results}->{command_options} ne '' ? $self->{option_results}->{command_options} : undef
130 );
131 }
132
133 return $content;
134 }
135
136 1;
137
138 __END__
139
140 =head1 NAME
141
142 storwize
143
144 =head1 SYNOPSIS
145
146 storwize
147
148 =head1 STORWIZE OPTIONS
149
150 =over 8
151
152 =item B<--hostname>
153
154 Hostname to query.
155
156 =item B<--timeout>
157
158 Timeout in seconds for the command (default: 30).
159
160 =item B<--sudo>
161
162 Use 'sudo' to execute the command.
163
164 =item B<--command>
165
166 Command to get information. Used it you have output in a file.
167
168 =item B<--command-path>
169
170 Command path.
171
172 =item B<--command-options>
173
174 Command options.
175
176 =back
177
178 =head1 DESCRIPTION
179
180 B<custom>.
181
182 =cut