| 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::fritzbox::upnp::custom::soap; |
| 22 |
|
| 23 |
use strict; |
| 24 |
use warnings; |
| 25 |
use centreon::plugins::http; |
| 26 |
use XML::LibXML::Simple; |
| 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 |
'port:s' => { name => 'port' }, |
| 46 |
'proto:s' => { name => 'proto' }, |
| 47 |
'api-username:s' => { name => 'api_username' }, |
| 48 |
'api-password:s' => { name => 'api_password' }, |
| 49 |
'timeout:s' => { name => 'timeout' }, |
| 50 |
'agent:s' => { name => 'agent' }, |
| 51 |
'unknown-http-status:s' => { name => 'unknown_http_status' }, |
| 52 |
'warning-http-status:s' => { name => 'warning_http_status' }, |
| 53 |
'critical-http-status:s' => { name => 'critical_http_status' } |
| 54 |
}); |
| 55 |
} |
| 56 |
$options{options}->add_help(package => __PACKAGE__, sections => 'UNPNP API OPTIONS', once => 1); |
| 57 |
|
| 58 |
$self->{output} = $options{output}; |
| 59 |
$self->{http} = centreon::plugins::http->new(%options); |
| 60 |
|
| 61 |
return $self; |
| 62 |
} |
| 63 |
|
| 64 |
sub set_options { |
| 65 |
my ($self, %options) = @_; |
| 66 |
|
| 67 |
$self->{option_results} = $options{option_results}; |
| 68 |
} |
| 69 |
|
| 70 |
sub set_defaults {} |
| 71 |
|
| 72 |
sub check_options { |
| 73 |
my ($self, %options) = @_; |
| 74 |
|
| 75 |
$self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; |
| 76 |
$self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 49000; |
| 77 |
$self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http'; |
| 78 |
$self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; |
| 79 |
$self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; |
| 80 |
$self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; |
| 81 |
$self->{agent} = (defined($self->{option_results}->{agent}) && $self->{option_results}->{agent} ne '') ? |
| 82 |
$self->{option_results}->{agent} : 'igdupnp'; |
| 83 |
$self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; |
| 84 |
$self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; |
| 85 |
$self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; |
| 86 |
|
| 87 |
if ($self->{hostname} eq '') { |
| 88 |
$self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); |
| 89 |
$self->{output}->option_exit(); |
| 90 |
} |
| 91 |
|
| 92 |
return 0; |
| 93 |
} |
| 94 |
|
| 95 |
sub build_options_for_httplib { |
| 96 |
my ($self, %options) = @_; |
| 97 |
|
| 98 |
$self->{option_results}->{hostname} = $self->{hostname}; |
| 99 |
$self->{option_results}->{timeout} = $self->{timeout}; |
| 100 |
$self->{option_results}->{port} = $self->{port}; |
| 101 |
$self->{option_results}->{proto} = $self->{proto}; |
| 102 |
if (defined($self->{api_username}) && $self->{api_username} ne '') { |
| 103 |
$self->{option_results}->{username} = $self->{api_username}; |
| 104 |
$self->{option_results}->{password} = $self->{api_password}; |
| 105 |
$self->{option_results}->{credentials} = 1; |
| 106 |
$self->{option_results}->{basic} = 1; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
sub settings { |
| 111 |
my ($self, %options) = @_; |
| 112 |
|
| 113 |
$self->build_options_for_httplib(); |
| 114 |
$self->{http}->set_options(%{$self->{option_results}}); |
| 115 |
} |
| 116 |
|
| 117 |
sub get_hostname { |
| 118 |
my ($self, %options) = @_; |
| 119 |
|
| 120 |
return $self->{hostname}; |
| 121 |
} |
| 122 |
|
| 123 |
sub get_port { |
| 124 |
my ($self, %options) = @_; |
| 125 |
|
| 126 |
return $self->{port}; |
| 127 |
} |
| 128 |
|
| 129 |
sub request { |
| 130 |
my ($self, %options) = @_; |
| 131 |
|
| 132 |
$self->settings(); |
| 133 |
my $data = <<END_FILE; |
| 134 |
<?xml version="1.0" encoding="utf-8"?> |
| 135 |
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"> |
| 136 |
<s:Body> |
| 137 |
<u:$options{verb} xmlns:u="urn:schemas-upnp-org:service:$options{ns}:1" /> |
| 138 |
</s:Body> |
| 139 |
</s:Envelope> |
| 140 |
END_FILE |
| 141 |
|
| 142 |
my $content = $self->{http}->request( |
| 143 |
method => 'POST', |
| 144 |
url_path => '/' . $self->{agent} . '/control/' . $options{url}, |
| 145 |
header => [ |
| 146 |
'SOAPAction: urn:schemas-upnp-org:service:' . $options{ns} . ':1#' . $options{verb}, |
| 147 |
'Content-type: text/xml' |
| 148 |
], |
| 149 |
query_form_post => $data, |
| 150 |
unknown_status => '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 500', |
| 151 |
warning_status => '', |
| 152 |
critical_status => '' |
| 153 |
); |
| 154 |
|
| 155 |
my $xml_result; |
| 156 |
eval { |
| 157 |
$SIG{__WARN__} = sub {}; |
| 158 |
$xml_result = XMLin($content, ForceArray => [], KeyAttr => []); |
| 159 |
}; |
| 160 |
if ($@) { |
| 161 |
$self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@"); |
| 162 |
$self->{output}->option_exit(); |
| 163 |
} |
| 164 |
if (defined($xml_result->{'soapenv:Body'}->{'soapenv:Fault'})) { |
| 165 |
$self->{output}->add_option_msg(short_msg => 'soap response issue'); |
| 166 |
$self->{output}->option_exit(); |
| 167 |
} |
| 168 |
|
| 169 |
return $xml_result; |
| 170 |
} |
| 171 |
|
| 172 |
1; |
| 173 |
|
| 174 |
__END__ |
| 175 |
|
| 176 |
=head1 NAME |
| 177 |
|
| 178 |
UPNP SOAP API |
| 179 |
|
| 180 |
=head1 SYNOPSIS |
| 181 |
|
| 182 |
UPNP SOAP API |
| 183 |
|
| 184 |
=head1 UNPNP API OPTIONS |
| 185 |
|
| 186 |
=over 8 |
| 187 |
|
| 188 |
=item B<--hostname> |
| 189 |
|
| 190 |
API hostname. |
| 191 |
|
| 192 |
=item B<--port> |
| 193 |
|
| 194 |
API port (default: 49000) |
| 195 |
|
| 196 |
=item B<--proto> |
| 197 |
|
| 198 |
Specify https if needed (default: 'http') |
| 199 |
|
| 200 |
=item B<--agent> |
| 201 |
|
| 202 |
Fritzbox has two different UPNP agents: upnp or igdupnp (default: igdupnp). |
| 203 |
|
| 204 |
=item B<--api-username> |
| 205 |
|
| 206 |
Set API username |
| 207 |
|
| 208 |
=item B<--api-password> |
| 209 |
|
| 210 |
Set API password |
| 211 |
|
| 212 |
=item B<--timeout> |
| 213 |
|
| 214 |
Set HTTP timeout |
| 215 |
|
| 216 |
=back |
| 217 |
|
| 218 |
=head1 DESCRIPTION |
| 219 |
|
| 220 |
B<custom>. |
| 221 |
|
| 222 |
=cut |