| 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::openvpn::omi::custom::api; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use IO::Socket::INET; |
| 26 |
use IO::Select; |
| 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 |
'omi-hostname:s@' => { name => 'omi_hostname' }, |
| 45 |
'omi-port:s@' => { name => 'omi_port' }, |
| 46 |
'omi-password:s@' => { name => 'omi_password' }, |
| 47 |
'timeout:s@' => { name => 'timeout' } |
| 48 |
}); |
| 49 |
} |
| 50 |
$options{options}->add_help(package => __PACKAGE__, sections => 'MANAGEMENT API OPTIONS', once => 1); |
| 51 |
|
| 52 |
$self->{output} = $options{output}; |
| 53 |
$self->{cnx_omi} = undef; |
| 54 |
|
| 55 |
return $self; |
| 56 |
|
| 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 |
$self->{omi_hostname} = (defined($self->{option_results}->{omi_hostname})) ? shift(@{$self->{option_results}->{omi_hostname}}) : undef; |
| 71 |
$self->{omi_password} = (defined($self->{option_results}->{omi_password})) ? shift(@{$self->{option_results}->{omi_password}}) : undef; |
| 72 |
$self->{omi_port} = (defined($self->{option_results}->{omi_port})) ? shift(@{$self->{option_results}->{omi_port}}) : 7505; |
| 73 |
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10; |
| 74 |
|
| 75 |
if (!defined($self->{omi_hostname})) { |
| 76 |
$self->{output}->add_option_msg(short_msg => "Need to specify --omi-hostname option."); |
| 77 |
$self->{output}->option_exit(); |
| 78 |
} |
| 79 |
|
| 80 |
if (!defined($self->{omi_hostname}) || |
| 81 |
scalar(@{$self->{option_results}->{omi_hostname}}) == 0) { |
| 82 |
return 0; |
| 83 |
} |
| 84 |
|
| 85 |
return 1; |
| 86 |
} |
| 87 |
|
| 88 |
sub get_connect_info { |
| 89 |
my ($self, %options) = @_; |
| 90 |
|
| 91 |
return $self->{omi_hostname} . '_' . $self->{omi_port}; |
| 92 |
} |
| 93 |
|
| 94 |
sub write_omi_protocol { |
| 95 |
my ($self, %options) = @_; |
| 96 |
|
| 97 |
$self->{cnx_omi}->send($options{cmd}); |
| 98 |
} |
| 99 |
|
| 100 |
sub read_omi_protocol { |
| 101 |
my ($self, %options) = @_; |
| 102 |
|
| 103 |
my $select = IO::Select->new($self->{cnx_omi}); |
| 104 |
my $read_msg; |
| 105 |
my $message = ''; |
| 106 |
while (1) { |
| 107 |
if (!$select->can_read(10)) { |
| 108 |
$self->{output}->output_add(long_msg => $message, debug => 1); |
| 109 |
$self->{output}->add_option_msg(short_msg => "Communication issue [Timeout or unexpected protocol]"); |
| 110 |
$self->{output}->option_exit(); |
| 111 |
} |
| 112 |
|
| 113 |
my $status = $self->{cnx_omi}->recv($read_msg, 4096); |
| 114 |
$message .= $read_msg; |
| 115 |
last if ($message =~ /$options{expected}/ms); |
| 116 |
} |
| 117 |
|
| 118 |
$self->{output}->output_add(long_msg => $message, debug => 1); |
| 119 |
$message =~ s/\r//msg; |
| 120 |
#if ($response !~ /Success|Follows/) { |
| 121 |
# $message =~ s/\n+$//msg; |
| 122 |
# $message =~ s/\n/ -- /msg; |
| 123 |
# $self->{output}->add_option_msg(short_msg => "Communication issue [" . $message . "]"); |
| 124 |
# $self->{output}->option_exit(); |
| 125 |
#} |
| 126 |
return $message; |
| 127 |
} |
| 128 |
|
| 129 |
sub command_omi_protocol { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
# Three types of message: |
| 133 |
# CMD1 |
| 134 |
# SUCCESS: nclients=5,bytesin=7761549522,bytesout=18417469629 |
| 135 |
# |
| 136 |
# CMD1 |
| 137 |
# ERROR: unknown command, enter 'help' for more options |
| 138 |
# |
| 139 |
# CMD1 |
| 140 |
# ... |
| 141 |
# END |
| 142 |
$self->write_omi_protocol(cmd => "$options{cmd} |
| 143 |
"); |
| 144 |
my $message = $self->read_omi_protocol(expected => '^(END[^\n]*?|SUCCESS:[^\n]*?|ERROR:[^\n]*?)\n'); |
| 145 |
if ($message =~ /^ERROR:(.*)/m) { |
| 146 |
$self->{output}->add_option_msg(short_msg => "Protocol error [" . $message . "]"); |
| 147 |
$self->{output}->option_exit(); |
| 148 |
} |
| 149 |
|
| 150 |
return $message; |
| 151 |
} |
| 152 |
|
| 153 |
sub login { |
| 154 |
my ($self, %options) = @_; |
| 155 |
|
| 156 |
my $message = $self->read_omi_protocol(expected => '^(>INFO:OpenVPN.*|ENTER\s+PASSWORD:)'); |
| 157 |
if ($message =~ /PASSWORD/) { |
| 158 |
if (!defined($self->{omi_password}) || $self->{omi_password} eq '') { |
| 159 |
$self->{output}->add_option_msg(short_msg => "Openvpn management interface require a password. please set --omi-password option"); |
| 160 |
$self->{output}->option_exit(); |
| 161 |
} |
| 162 |
$self->write_omi_protocol(cmd => $self->{omi_password} . "\n"); |
| 163 |
$message = $self->read_omi_protocol(expected => '^(SUCCESS|ERROR):[^\n]*?\n'); |
| 164 |
if ($message =~ /^ERROR:(.*)/m) { |
| 165 |
$self->{output}->add_option_msg(short_msg => "Password error [" . $1 . "]"); |
| 166 |
$self->{output}->option_exit(); |
| 167 |
} |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
sub connect { |
| 172 |
my ($self, %options) = @_; |
| 173 |
|
| 174 |
$self->{cnx_omi} = IO::Socket::INET->new( |
| 175 |
PeerAddr => $self->{omi_hostname}, |
| 176 |
PeerPort => $self->{omi_port}, |
| 177 |
Proto => 'tcp', |
| 178 |
Timeout => $self->{timeout}, |
| 179 |
); |
| 180 |
if (!defined($self->{cnx_omi})) { |
| 181 |
$self->{output}->add_option_msg(short_msg => "Can't bind : $@"); |
| 182 |
$self->{output}->option_exit(); |
| 183 |
} |
| 184 |
|
| 185 |
$self->{cnx_omi}->autoflush(1); |
| 186 |
$self->login(); |
| 187 |
} |
| 188 |
|
| 189 |
sub command { |
| 190 |
my ($self, %options) = @_; |
| 191 |
|
| 192 |
if (!defined($self->{cnx_omi})) { |
| 193 |
$self->connect(); |
| 194 |
} |
| 195 |
|
| 196 |
return $self->command_omi_protocol(%options); |
| 197 |
} |
| 198 |
|
| 199 |
sub DESTROY { |
| 200 |
my $self = shift; |
| 201 |
|
| 202 |
if (defined($self->{cnx_omi})) { |
| 203 |
$self->{cnx_omi}->close(); |
| 204 |
} |
| 205 |
} |
| 206 |
|
| 207 |
1; |
| 208 |
|
| 209 |
__END__ |
| 210 |
|
| 211 |
=head1 NAME |
| 212 |
|
| 213 |
Openvpn Management interface |
| 214 |
|
| 215 |
=head1 SYNOPSIS |
| 216 |
|
| 217 |
Openvpn Management interface custom mode |
| 218 |
|
| 219 |
=head1 MANAGEMENT API OPTIONS |
| 220 |
|
| 221 |
=over 8 |
| 222 |
|
| 223 |
=item B<--omi-hostname> |
| 224 |
|
| 225 |
OMI hostname. |
| 226 |
|
| 227 |
=item B<--omi-port> |
| 228 |
|
| 229 |
OMI port (default: 7505). |
| 230 |
|
| 231 |
=item B<--omi-password> |
| 232 |
|
| 233 |
OMI password. |
| 234 |
|
| 235 |
=item B<--timeout> |
| 236 |
|
| 237 |
Set TCP timeout |
| 238 |
|
| 239 |
=back |
| 240 |
|
| 241 |
=head1 DESCRIPTION |
| 242 |
|
| 243 |
B<custom>. |
| 244 |
|
| 245 |
=cut |