diff --git a/centreon-plugins/network/fritzbox/mode/libgetdata.pm b/centreon-plugins/network/fritzbox/mode/libgetdata.pm deleted file mode 100644 index f0f56e98f..000000000 --- a/centreon-plugins/network/fritzbox/mode/libgetdata.pm +++ /dev/null @@ -1,77 +0,0 @@ -# -# Copyright 2021 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package network::fritzbox::mode::libgetdata; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use SOAP::Lite; - -my $soap; -my $response; - -sub init { - my ($self, %options) = @_; - - my $proxy = 'http://' . $self->{option_results}->{hostname} . ':' . $self->{option_results}->{port} . $options{pfad}; - - $soap = SOAP::Lite->new( - proxy => $proxy, - uri => $options{uri}, - timeout => $self->{option_results}->{timeout} - ); - $soap->on_fault( - sub { # SOAP fault handler - my $soap = shift; - my $res = shift; - - if(ref($res)) { - chomp( my $err = $res->faultstring ); - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => "SOAP Fault: $err"); - } else { - chomp( my $err = $soap->transport->status ); - $self->{output}->output_add(severity => 'UNKNOWN', - short_msg => "Transport error: $err"); - } - - $self->{output}->display(); - $self->{output}->exit(); - } - ); -} - -sub call { - my ($self, %options) = @_; - - my $method = $options{soap_method}; - $response = $soap->$method(); -} - -sub value { - my ($self, %options) = @_; - my $value = $response->valueof($options{path}); - - return $value; -} - -1; \ No newline at end of file diff --git a/centreon-plugins/network/fritzbox/mode/traffic.pm b/centreon-plugins/network/fritzbox/mode/traffic.pm deleted file mode 100644 index 66bd768e9..000000000 --- a/centreon-plugins/network/fritzbox/mode/traffic.pm +++ /dev/null @@ -1,230 +0,0 @@ -# -# Copyright 2021 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package network::fritzbox::mode::traffic; - -use base qw(centreon::plugins::mode); -use strict; -use warnings; -use centreon::plugins::statefile; -use network::fritzbox::mode::libgetdata; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => '49000' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "agent:s" => { name => 'agent', default => 'igdupnp' }, - "warning-in:s" => { name => 'warning_in', }, - "critical-in:s" => { name => 'critical_in', }, - "warning-out:s" => { name => 'warning_out', }, - "critical-out:s" => { name => 'critical_out', }, - "units:s" => { name => 'units', default => '%' }, - }); - $self->{statefile_value} = centreon::plugins::statefile->new(%options); - return $self; -} - -sub check_options { - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Need to specify an Hostname."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warning-in', value => $self->{option_results}->{warning_in})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning 'in' threshold '" . $self->{option_results}->{warning_in} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical-in', value => $self->{option_results}->{critical_in})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical 'in' threshold '" . $self->{option_results}->{critical_in} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warning-out', value => $self->{option_results}->{warning_out})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning 'out' threshold '" . $self->{option_results}->{warning_out} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical-out', value => $self->{option_results}->{critical_out})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical 'out' threshold '" . $self->{option_results}->{critical_out} . "'."); - $self->{output}->option_exit(); - } - - $self->{statefile_value}->check_options(%options); - $self->{hostname} = $self->{option_results}->{hostname}; -} - -sub run { - my ($self, %options) = @_; - - my $new_datas = {}; - $self->{statefile_value}->read(statefile => "cache_fritzbox_" . $self->{hostname} . '_' . $self->{mode}); - $new_datas->{last_timestamp} = time(); - my $old_timestamp = $self->{statefile_value}->get(name => 'last_timestamp'); - - ### GET DATA START - network::fritzbox::mode::libgetdata::init($self, pfad => '/' . $self->{option_results}->{agent} . '/control/WANCommonIFC1', - uri => 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1'); - network::fritzbox::mode::libgetdata::call($self, soap_method => 'GetAddonInfos'); - my $NewTotalBytesSent = network::fritzbox::mode::libgetdata::value($self, path => '//GetAddonInfosResponse/NewTotalBytesSent'); - my $NewTotalBytesReceived = network::fritzbox::mode::libgetdata::value($self, path => '//GetAddonInfosResponse/NewTotalBytesReceived'); - - network::fritzbox::mode::libgetdata::call($self, soap_method => 'GetCommonLinkProperties'); - my $NewLayer1UpstreamMaxBitRate = network::fritzbox::mode::libgetdata::value($self, path => '//GetCommonLinkPropertiesResponse/NewLayer1UpstreamMaxBitRate'); - my $NewLayer1DownstreamMaxBitRate = network::fritzbox::mode::libgetdata::value($self, path => '//GetCommonLinkPropertiesResponse/NewLayer1DownstreamMaxBitRate'); - ### GET DATA END - - # DID U KNOW? - # IN AND OUT IS BYTE - # TOTAL IS BIT... - # so if you want all in BYTE... - # (8 BIT = 1 BYTE) - # calc ($VAR / 8) - $NewLayer1UpstreamMaxBitRate = ($NewLayer1UpstreamMaxBitRate); - $NewLayer1DownstreamMaxBitRate = ($NewLayer1DownstreamMaxBitRate); - $new_datas->{in} = ($NewTotalBytesReceived) * 8; - $new_datas->{out} = ($NewTotalBytesSent) * 8; - $self->{statefile_value}->write(data => $new_datas); - - my $old_in = $self->{statefile_value}->get(name => 'in'); - my $old_out = $self->{statefile_value}->get(name => 'out'); - if (!defined($old_timestamp) || !defined($old_in) || !defined($old_out)) { - $self->{output}->output_add(severity => 'OK', - short_msg => "Buffer creation..."); - $self->{output}->display(); - $self->{output}->exit(); - } - - if ($new_datas->{in} < $old_in) { - # We set 0. Has reboot. - $old_in = 0; - } - if ($new_datas->{out} < $old_out) { - # We set 0. Has reboot. - $old_out = 0; - } - - my $time_delta = $new_datas->{last_timestamp} - $old_timestamp; - if ($time_delta <= 0) { - # At least one second. two fast calls ;) - $time_delta = 1; - } - my $in_per_sec = ($new_datas->{in} - $old_in) / $time_delta; - my $out_per_sec = ($new_datas->{out} - $old_out) / $time_delta; - - my ($exit, $in_prct, $out_prct); - - $in_prct = $in_per_sec * 100 / $NewLayer1DownstreamMaxBitRate; - $out_prct = $out_per_sec * 100 / $NewLayer1UpstreamMaxBitRate; - if ($self->{option_results}->{units} eq '%') { - my $exit1 = $self->{perfdata}->threshold_check(value => $in_prct, threshold => [ { label => 'critical-in', 'exit_litteral' => 'critical' }, { label => 'warning-in', exit_litteral => 'warning' } ]); - my $exit2 = $self->{perfdata}->threshold_check(value => $out_prct, threshold => [ { label => 'critical-out', 'exit_litteral' => 'critical' }, { label => 'warning-out', exit_litteral => 'warning' } ]); - $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2 ]); - } else { - my $exit1 = $self->{perfdata}->threshold_check(value => $in_per_sec, threshold => [ { label => 'critical-in', 'exit_litteral' => 'critical' }, { label => 'warning-in', exit_litteral => 'warning' } ]); - my $exit2 = $self->{perfdata}->threshold_check(value => $out_per_sec, threshold => [ { label => 'critical-out', 'exit_litteral' => 'critical' }, { label => 'warning-out', exit_litteral => 'warning' } ]); - $exit = $self->{output}->get_most_critical(status => [ $exit1, $exit2 ]); - } - $in_prct = sprintf("%.2f", $in_prct); - $out_prct = sprintf("%.2f", $out_prct); - - ### Manage Output - my ($in_value, $in_unit) = $self->{perfdata}->change_bytes(value => $in_per_sec, network => 1); - my ($out_value, $out_unit) = $self->{perfdata}->change_bytes(value => $out_per_sec, network => 1); - $self->{output}->output_add(short_msg => sprintf("Traffic In : %s/s (%s %%), Out : %s/s (%s %%)", - $in_value . $in_unit, $in_prct, - $out_value . $out_unit, $out_prct)); - if (!$self->{output}->is_status(value => $exit, compare => 'ok', litteral => 1)) { - $self->{output}->output_add(severity => $exit, - short_msg => sprintf("Traffic In : %s/s (%s %%), Out : %s/s (%s %%)", - $in_value . $in_unit, $in_prct, - $out_value . $out_unit, $out_prct)); - } - - $self->{output}->perfdata_add(label => 'traffic_in', - unit => 'b/s', - value => sprintf("%.2f", $in_per_sec), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-in', total => $NewLayer1DownstreamMaxBitRate), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-in', total => $NewLayer1DownstreamMaxBitRate), - min => 0, max => $NewLayer1DownstreamMaxBitRate); - $self->{output}->perfdata_add(label => 'traffic_out', - unit => 'b/s', - value => sprintf("%.2f", $out_per_sec), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-out', total => $NewLayer1UpstreamMaxBitRate), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-out', total => $NewLayer1UpstreamMaxBitRate), - min => 0, max => $NewLayer1UpstreamMaxBitRate); - - - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -This Mode Checks your FritzBox Traffic on WAN Interface. -This Mode needs UPNP. - -=over 8 - -=item B<--agent> - -Fritzbox has two different UPNP Agents. upnp or igdupnp. (Default: igdupnp) - -=item B<--warning-in> - -Threshold warning for 'in' traffic. - -=item B<--critical-in> - -Threshold critical for 'in' traffic. - -=item B<--warning-out> - -Threshold warning for 'out' traffic. - -=item B<--critical-out> - -Threshold critical for 'out' traffic. - -=item B<--units> - -Units of thresholds (Default: '%') ('%', 'b'). - -=item B<--hostname> - -Hostname to query. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=back - -=cut diff --git a/centreon-plugins/network/fritzbox/mode/upstatus.pm b/centreon-plugins/network/fritzbox/mode/upstatus.pm deleted file mode 100644 index 5b5ea91f3..000000000 --- a/centreon-plugins/network/fritzbox/mode/upstatus.pm +++ /dev/null @@ -1,142 +0,0 @@ -# -# Copyright 2021 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -package network::fritzbox::mode::upstatus; - -use base qw(centreon::plugins::mode); - -use strict; -use warnings; -use network::fritzbox::mode::libgetdata; -use POSIX; - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $options{options}->add_options(arguments => - { - "hostname:s" => { name => 'hostname' }, - "port:s" => { name => 'port', default => '49000' }, - "timeout:s" => { name => 'timeout', default => 30 }, - "agent:s" => { name => 'agent', default => 'igdupnp' }, - "warning:s" => { name => 'warning', }, - "critical:s" => { name => 'critical', }, - "seconds" => { name => 'seconds', }, - }); - return $self; -} - -sub check_options { - - my ($self, %options) = @_; - $self->SUPER::init(%options); - - if (!defined($self->{option_results}->{hostname})) { - $self->{output}->add_option_msg(short_msg => "Need to specify an Hostname."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'."); - $self->{output}->option_exit(); - } - if (($self->{perfdata}->threshold_validate(label => 'critical', value => $self->{option_results}->{critical})) == 0) { - $self->{output}->add_option_msg(short_msg => "Wrong critical threshold '" . $self->{option_results}->{critical} . "'."); - $self->{output}->option_exit(); - } -} - -sub run { - my ($self, %options) = @_; - my $exit_code; - - network::fritzbox::mode::libgetdata::init($self, pfad => '/' . $self->{option_results}->{agent} . '/control/WANCommonIFC1', - uri => 'urn:schemas-upnp-org:service:WANCommonInterfaceConfig:1'); - network::fritzbox::mode::libgetdata::call($self, soap_method => 'GetCommonLinkProperties'); - my $WANAccessType = network::fritzbox::mode::libgetdata::value($self, path => '//GetCommonLinkPropertiesResponse/NewWANAccessType'); - my $LinkStatus = network::fritzbox::mode::libgetdata::value($self, path => '//GetCommonLinkPropertiesResponse/NewPhysicalLinkStatus'); - - network::fritzbox::mode::libgetdata::init($self, pfad => '/' . $self->{option_results}->{agent} . '/control/WANIPConn1', - uri => 'urn:schemas-upnp-org:service:WANIPConnection:1'); - network::fritzbox::mode::libgetdata::call($self, soap_method => 'GetStatusInfo'); - my $uptime = network::fritzbox::mode::libgetdata::value($self, path => '//GetStatusInfoResponse/NewUptime'); - my $ConnectionStatus = network::fritzbox::mode::libgetdata::value($self, path => '//GetStatusInfoResponse/NewConnectionStatus'); - - $exit_code = $self->{perfdata}->threshold_check(value => floor($uptime), - threshold => [ { label => 'critical', exit_litteral => 'critical' }, - { label => 'warning', exit_litteral => 'warning' } ]); - - if ($LinkStatus !~ /^up$/i || $ConnectionStatus !~ /^connected$/i) { - $exit_code = 'critical'; - } - - $self->{output}->perfdata_add(label => 'uptime', - value => floor($uptime), - warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning'), - critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical'), - min => 0); - - $self->{output}->output_add(severity => $exit_code, - short_msg => sprintf("Physical Link is " . $LinkStatus . " and " . $WANAccessType . " is " . $ConnectionStatus . - " since: %s", defined($self->{option_results}->{seconds}) ? floor($uptime) . " seconds" : floor($uptime / 86400) . " days" )); - - $self->{output}->display(); - $self->{output}->exit(); -} - -1; - -__END__ - -=head1 MODE - -This Mode Checks Physical Link Status, Connection Status and Uptime of your Fritz!Box Internet Connection. -This Mode needs UPNP. - -=over 8 - -=item B<--agent> - -Fritzbox has two different UPNP Agents. upnp or igdupnp. (Default: igdupnp) - -=item B<--warning> - -Threshold warning in seconds. - -=item B<--critical> - -Threshold critical in seconds. - -=item B<--seconds> - -Display uptime in seconds. - -=item B<--hostname> - -Hostname to query. - -=item B<--timeout> - -Timeout in seconds for the command (Default: 30). - -=back - -=cut diff --git a/centreon-plugins/network/fritzbox/plugin.pm b/centreon-plugins/network/fritzbox/plugin.pm deleted file mode 100644 index 5745ba349..000000000 --- a/centreon-plugins/network/fritzbox/plugin.pm +++ /dev/null @@ -1,91 +0,0 @@ -# -# Copyright 2021 Centreon (http://www.centreon.com/) -# -# Centreon is a full-fledged industry-strength solution that meets -# the needs in IT infrastructure and application monitoring for -# service performance. -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# - -#################################################################################### -# -# Mode Tested with Fritz!Box 6360 -# -#################################################################################### - -#################################################################################### -# DOCUMENTATION -#################################################################################### - -###GetAddonInfos -#NewVoipDNSServer1 : 0.0.0.0 -#NewDNSServer2 : 10.10.133.78 -#NewDNSServer1 : 10.10.133.78 -#NewVoipDNSServer2 : 0.0.0.0 -#NewIdleDisconnectTime : 0 -#NewByteSendRate : 1560 -#NewAutoDisconnectTime : 0 -#NewTotalBytesSent : 411607957 -#NewByteReceiveRate : 5073 -#NewPacketReceiveRate : 31 -#NewRoutedBridgedModeBoth : 0 -#NewTotalBytesReceived : 4186731846 -#NewPacketSendRate : 17 -#NewUpnpControlEnabled : 0 - -###GetCommonLinkProperties -#NewPhysicalLinkStatus : Up -#NewLayer1DownstreamMaxBitRate : 112640000 -#NewWANAccessType : DSL -#NewLayer1UpstreamMaxBitRate : 5248000 - -###GetStatusInfo -#NewConnectionStatus : Connected -#NewLastConnectionError : ERROR_NONE -#NewUptime : 903867 - -###GetExternalIPAddress -#NewExternalIPAddress : 133.71.33.7 -#################################################################################### - -package network::fritzbox::plugin; - -use strict; -use warnings; -use base qw(centreon::plugins::script_simple); - -sub new { - my ($class, %options) = @_; - my $self = $class->SUPER::new(package => __PACKAGE__, %options); - bless $self, $class; - - $self->{version} = '0.1'; - %{$self->{modes}} = ( - 'upstatus' => 'network::fritzbox::mode::upstatus', - 'traffic' => 'network::fritzbox::mode::traffic', - ); - - return $self; -} - -1; - -__END__ - -=head1 PLUGIN DESCRIPTION - -This Plugin can check various things of your Fritz!Box. -Need perl-SOAP-Lite, you have to activate UPNP! - -=cut diff --git a/centreon-plugins/network/fritzbox/upnp/custom/soap.pm b/centreon-plugins/network/fritzbox/upnp/custom/soap.pm new file mode 100644 index 000000000..54f4dd3c2 --- /dev/null +++ b/centreon-plugins/network/fritzbox/upnp/custom/soap.pm @@ -0,0 +1,222 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::fritzbox::upnp::custom::soap; + +use strict; +use warnings; +use centreon::plugins::http; +use XML::LibXML::Simple; + +sub new { + my ($class, %options) = @_; + my $self = {}; + bless $self, $class; + + if (!defined($options{output})) { + print "Class Custom: Need to specify 'output' argument.\n"; + exit 3; + } + if (!defined($options{options})) { + $options{output}->add_option_msg(short_msg => "Class Custom: Need to specify 'options' argument."); + $options{output}->option_exit(); + } + + if (!defined($options{noptions})) { + $options{options}->add_options(arguments => { + 'hostname:s' => { name => 'hostname' }, + 'port:s' => { name => 'port' }, + 'proto:s' => { name => 'proto' }, + 'api-username:s' => { name => 'api_username' }, + 'api-password:s' => { name => 'api_password' }, + 'timeout:s' => { name => 'timeout' }, + 'agent:s' => { name => 'agent' }, + 'unknown-http-status:s' => { name => 'unknown_http_status' }, + 'warning-http-status:s' => { name => 'warning_http_status' }, + 'critical-http-status:s' => { name => 'critical_http_status' } + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'UNPNP API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{http} = centreon::plugins::http->new(%options); + + return $self; +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults {} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? $self->{option_results}->{hostname} : ''; + $self->{port} = (defined($self->{option_results}->{port})) ? $self->{option_results}->{port} : 49000; + $self->{proto} = (defined($self->{option_results}->{proto})) ? $self->{option_results}->{proto} : 'http'; + $self->{api_username} = (defined($self->{option_results}->{api_username})) ? $self->{option_results}->{api_username} : ''; + $self->{api_password} = (defined($self->{option_results}->{api_password})) ? $self->{option_results}->{api_password} : ''; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? $self->{option_results}->{timeout} : 30; + $self->{agent} = (defined($self->{option_results}->{agent}) && $self->{option_results}->{agent} ne '') ? + $self->{option_results}->{agent} : 'igdupnp'; + $self->{unknown_http_status} = (defined($self->{option_results}->{unknown_http_status})) ? $self->{option_results}->{unknown_http_status} : '%{http_code} < 200 or %{http_code} >= 300'; + $self->{warning_http_status} = (defined($self->{option_results}->{warning_http_status})) ? $self->{option_results}->{warning_http_status} : ''; + $self->{critical_http_status} = (defined($self->{option_results}->{critical_http_status})) ? $self->{option_results}->{critical_http_status} : ''; + + if ($self->{hostname} eq '') { + $self->{output}->add_option_msg(short_msg => "Need to specify --hostname option."); + $self->{output}->option_exit(); + } + + return 0; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = $self->{port}; + $self->{option_results}->{proto} = $self->{proto}; + if (defined($self->{api_username}) && $self->{api_username} ne '') { + $self->{option_results}->{username} = $self->{api_username}; + $self->{option_results}->{password} = $self->{api_password}; + $self->{option_results}->{credentials} = 1; + $self->{option_results}->{basic} = 1; + } +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub get_hostname { + my ($self, %options) = @_; + + return $self->{hostname}; +} + +sub get_port { + my ($self, %options) = @_; + + return $self->{port}; +} + +sub request { + my ($self, %options) = @_; + + $self->settings(); + my $data = < + + + + + +END_FILE + + my $content = $self->{http}->request( + method => 'POST', + url_path => '/' . $self->{agent} . '/control/' . $options{url}, + header => [ + 'SOAPAction: urn:schemas-upnp-org:service:' . $options{ns} . ':1#' . $options{verb}, + 'Content-type: text/xml' + ], + query_form_post => $data, + unknown_status => '(%{http_code} < 200 or %{http_code} >= 300) and %{http_code} != 500', + warning_status => '', + critical_status => '' + ); + + my $xml_result; + eval { + $SIG{__WARN__} = sub {}; + $xml_result = XMLin($content, ForceArray => [], KeyAttr => []); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode xml response: $@"); + $self->{output}->option_exit(); + } + if (defined($xml_result->{'soapenv:Body'}->{'soapenv:Fault'})) { + $self->{output}->add_option_msg(short_msg => 'soap response issue'); + $self->{output}->option_exit(); + } + + return $xml_result; +} + +1; + +__END__ + +=head1 NAME + +UPNP SOAP API + +=head1 SYNOPSIS + +UPNP SOAP API + +=head1 UNPNP API OPTIONS + +=over 8 + +=item B<--hostname> + +API hostname. + +=item B<--port> + +API port (Default: 49000) + +=item B<--proto> + +Specify https if needed (Default: 'http') + +=item B<--agent> + +Fritzbox has two different UPNP agents: upnp or igdupnp (Default: igdupnp). + +=item B<--api-username> + +Set API username + +=item B<--api-password> + +Set API password + +=item B<--timeout> + +Set HTTP timeout + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/centreon-plugins/network/fritzbox/upnp/mode/system.pm b/centreon-plugins/network/fritzbox/upnp/mode/system.pm new file mode 100644 index 000000000..87387ac0d --- /dev/null +++ b/centreon-plugins/network/fritzbox/upnp/mode/system.pm @@ -0,0 +1,175 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::fritzbox::upnp::mode::system; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use centreon::plugins::templates::catalog_functions qw(catalog_status_threshold_ng); +use POSIX; + +my $unitdiv = { s => 1, w => 604800, d => 86400, h => 3600, m => 60 }; +my $unitdiv_long = { s => 'seconds', w => 'weeks', d => 'days', h => 'hours', m => 'minutes' }; + +sub custom_uptime_perfdata { + my ($self, %options) = @_; + + $self->{output}->perfdata_add( + nlabel => $self->{nlabel} . '.' . $unitdiv_long->{ $self->{instance_mode}->{option_results}->{unit} }, + unit => $self->{instance_mode}->{option_results}->{unit}, + value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), + warning => $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}), + critical => $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}), + min => 0 + ); +} + +sub custom_uptime_threshold { + my ($self, %options) = @_; + + return $self->{perfdata}->threshold_check( + value => floor($self->{result_values}->{uptime} / $unitdiv->{ $self->{instance_mode}->{option_results}->{unit} }), + threshold => [ + { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, + { label => 'warning-'. $self->{thlabel}, exit_litteral => 'warning' }, + { label => 'unknown-'. $self->{thlabel}, exit_litteral => 'unknown' } + ] + ); +} + +sub custom_status_output { + my ($self, %options) = @_; + + return sprintf( + 'physical link status is %s and %s is %s', + $self->{result_values}->{link_status}, + $self->{result_values}->{wan_access_type}, + $self->{result_values}->{connection_status} + ); +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 } + ]; + + $self->{maps_counters}->{global} = [ + { + label => 'connection-status', + type => 2, + critical_default => '%{link_status} !~ /^up$/i and %{connection_status} !~ /^connected$/i', + set => { + key_values => [ + { name => 'connection_status' }, { name => 'link_status' }, { name => 'wan_access_type' } + ], + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => \&catalog_status_threshold_ng + } + }, + { label => 'uptime', nlabel => 'system.uptime', set => { + key_values => [ { name => 'uptime' }, { name => 'uptime_human' } ], + output_template => 'uptime: %s', + output_use => 'uptime_human', + closure_custom_perfdata => $self->can('custom_uptime_perfdata'), + closure_custom_threshold_check => $self->can('custom_uptime_threshold') + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'unit:s' => { name => 'unit', default => 's' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + if ($self->{option_results}->{unit} eq '' || !defined($unitdiv->{$self->{option_results}->{unit}})) { + $self->{option_results}->{unit} = 's'; + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{global} = {}; + + my $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetCommonLinkProperties'); + $self->{global}->{wan_access_type} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewWANAccessType}; + $self->{global}->{link_status} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewPhysicalLinkStatus}; + + $infos = $options{custom}->request(url => 'WANIPConn1', ns => 'WANIPConnection', verb => 'GetStatusInfo'); + $self->{global}->{connection_status} = $infos->{'s:Body'}->{'u:GetStatusInfoResponse'}->{NewConnectionStatus}; + $self->{global}->{uptime} = $infos->{'s:Body'}->{'u:GetStatusInfoResponse'}->{NewUptime}; # seconds + $self->{global}->{uptime_human} = centreon::plugins::misc::change_seconds(value => $self->{global}->{uptime}); +} + +1; + +__END__ + +=head1 MODE + +Check system. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='uptime' + +=item B<--warning-status> + +Set warning threshold for status. +Can use special variables like: %{connection_status}, %{link_status} + +=item B<--critical-status> + +Set critical threshold for status (Default: '%{link_status} !~ /^up$/i and %{connection_status} !~ /^connected$/i'). +Can use special variables like: %{connection_status}, %{link_status} + +=item B<--unit> + +Select the unit for uptime threshold. May be 's' for seconds, 'm' for minutes, +'h' for hours, 'd' for days, 'w' for weeks. Default is days. + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'uptime'. + +=back + +=cut diff --git a/centreon-plugins/network/fritzbox/upnp/mode/traffic.pm b/centreon-plugins/network/fritzbox/upnp/mode/traffic.pm new file mode 100644 index 000000000..3990f4029 --- /dev/null +++ b/centreon-plugins/network/fritzbox/upnp/mode/traffic.pm @@ -0,0 +1,225 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::fritzbox::upnp::mode::traffic; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +use Digest::MD5 qw(md5_hex); + +sub custom_traffic_perfdata { + my ($self, %options) = @_; + + my ($warning, $critical); + if ($self->{instance_mode}->{option_results}->{unit} eq 'percent_delta' && defined($self->{result_values}->{speed})) { + $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); + $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}, total => $self->{result_values}->{speed}, cast_int => 1); + } elsif ($self->{instance_mode}->{option_results}->{unit} =~ /bps|counter/) { + $warning = $self->{perfdata}->get_perfdata_for_output(label => 'warning-' . $self->{thlabel}); + $critical = $self->{perfdata}->get_perfdata_for_output(label => 'critical-' . $self->{thlabel}); + } + + if ($self->{instance_mode}->{option_results}->{unit} eq 'counter') { + my $nlabel = $self->{nlabel}; + $nlabel =~ s/bitspersecond/bits/; + $self->{output}->perfdata_add( + nlabel => $nlabel, + unit => 'b', + value => $self->{result_values}->{traffic_counter}, + warning => $warning, + critical => $critical, + min => 0 + ); + } else { + $self->{output}->perfdata_add( + nlabel => $self->{nlabel}, + unit => 'b/s', + value => sprintf('%.2f', $self->{result_values}->{traffic_per_seconds}), + warning => $warning, + critical => $critical, + min => 0, max => $self->{result_values}->{speed} + ); + } +} + +sub custom_traffic_threshold { + my ($self, %options) = @_; + + my $exit = 'ok'; + if ($self->{instance_mode}->{option_results}->{unit} eq 'percent_delta' && defined($self->{result_values}->{speed})) { + $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_prct}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); + } elsif ($self->{instance_mode}->{option_results}->{unit} eq 'bps') { + $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_per_seconds}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); + } elsif ($self->{instance_mode}->{option_results}->{unit} eq 'counter') { + $exit = $self->{perfdata}->threshold_check(value => $self->{result_values}->{traffic_counter}, threshold => [ { label => 'critical-' . $self->{thlabel}, exit_litteral => 'critical' }, { label => 'warning-' . $self->{thlabel}, exit_litteral => 'warning' } ]); + } + return $exit; +} + +sub custom_traffic_output { + my ($self, %options) = @_; + + my ($traffic_value, $traffic_unit) = $self->{perfdata}->change_bytes(value => $self->{result_values}->{traffic_per_seconds}, network => 1); + return sprintf( + 'Traffic %s: %s/s (%s)', + ucfirst($self->{result_values}->{label}), $traffic_value . $traffic_unit, + defined($self->{result_values}->{traffic_prct}) ? sprintf('%.2f%%', $self->{result_values}->{traffic_prct}) : '-' + ); +} + +sub custom_traffic_calc { + my ($self, %options) = @_; + + my $diff_traffic = ($options{new_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} } - $options{old_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} }); + $self->{result_values}->{traffic_per_seconds} = $diff_traffic / $options{delta_time}; + $self->{result_values}->{traffic_counter} = $options{new_datas}->{ $self->{instance} . '_total_' . $options{extra_options}->{label_ref} }; + if (defined($options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}}) && + $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}} > 0) { + $self->{result_values}->{traffic_prct} = $self->{result_values}->{traffic_per_seconds} * 100 / $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}}; + $self->{result_values}->{speed} = $options{new_datas}->{$self->{instance} . '_max_' . $options{extra_options}->{label_ref}}; + } + + $self->{result_values}->{label} = $options{extra_options}->{label_ref}; + return 0; +} + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 } + ]; + + $self->{maps_counters}->{global} = [ + { label => 'traffic-in', nlabel => 'system.interface.wan.traffic.in.bitspersecond', set => { + key_values => [ { name => 'total_in', diff => 1 }, { name => 'max_in'} ], + closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'in' }, + closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'Traffic In : %s', + closure_custom_perfdata => $self->can('custom_traffic_perfdata'), + closure_custom_threshold_check => $self->can('custom_traffic_threshold') + } + }, + { label => 'traffic-out', nlabel => 'system.interface.wan.traffic.out.bitspersecond', set => { + key_values => [ { name => 'total_out', diff => 1 }, { name => 'max_out'} ], + closure_custom_calc => $self->can('custom_traffic_calc'), closure_custom_calc_extra_options => { label_ref => 'out' }, + closure_custom_output => $self->can('custom_traffic_output'), output_error_template => 'Traffic Out : %s', + closure_custom_perfdata => $self->can('custom_traffic_perfdata'), + closure_custom_threshold_check => $self->can('custom_traffic_threshold') + } + } + ]; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options, statefile => 1, force_new_perfdata => 1); + bless $self, $class; + + $options{options}->add_options(arguments => { + 'unit:s' => { name => 'unit', default => 'percent_delta' } + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $self->{option_results}->{unit} = 'percent_delta' + if (!defined($self->{option_results}->{unit}) || + $self->{option_results}->{unit} eq '' || + $self->{option_results}->{unit} eq '%'); + if ($self->{option_results}->{unit} !~ /^(?:percent_delta|bps|counter)$/) { + $self->{output}->add_option_msg(short_msg => 'Wrong option --unit'); + $self->{output}->option_exit(); + } +} + +sub manage_selection { + my ($self, %options) = @_; + + $self->{cache_name} = 'fritzbox_' . $self->{mode} . '_' . $options{custom}->get_hostname() . '_' . $options{custom}->get_port() . '_' . + (defined($self->{option_results}->{filter_counters}) ? md5_hex($self->{option_results}->{filter_counters}) : md5_hex('all')); + $self->{global} = {}; + +=pod + + + + +6686 <--- upload +1414078 <--- download +0 +0 +819115474 +2080148768 +0 +0 +83.169.185.161 +83.169.185.225 +83.169.185.132 +83.169.185.127 +1 +1 +819115474 +2080148768 +Cable + + +=cut + my $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetAddonInfos'); + $self->{global}->{total_out} = $infos->{'s:Body'}->{'u:GetAddonInfosResponse'}->{NewTotalBytesSent} * 8; + $self->{global}->{total_in} = $infos->{'s:Body'}->{'u:GetAddonInfosResponse'}->{NewTotalBytesReceived} * 8; + + $infos = $options{custom}->request(url => 'WANCommonIFC1', ns => 'WANCommonInterfaceConfig', verb => 'GetCommonLinkProperties'); + $self->{global}->{max_out} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewLayer1UpstreamMaxBitRate}; + $self->{global}->{max_in} = $infos->{'s:Body'}->{'u:GetCommonLinkPropertiesResponse'}->{NewLayer1DownstreamMaxBitRate}; +} + +1; + +__END__ + +=head1 MODE + +Checks your FritzBox traffic on WAN interface. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^connections$' + +=item B<--unit> + +Unit of thresholds for the traffic (Default: 'percent_delta') ('percent_delta', 'bps', 'counter'). + +=item B<--warning-*> B<--critical-*> + +Thresholds. +Can be: 'traffic-in', 'traffic-out'. + +=back + +=cut diff --git a/centreon-plugins/network/fritzbox/upnp/plugin.pm b/centreon-plugins/network/fritzbox/upnp/plugin.pm new file mode 100644 index 000000000..451515782 --- /dev/null +++ b/centreon-plugins/network/fritzbox/upnp/plugin.pm @@ -0,0 +1,51 @@ +# +# Copyright 2021 Centreon (http://www.centreon.com/) +# +# Centreon is a full-fledged industry-strength solution that meets +# the needs in IT infrastructure and application monitoring for +# service performance. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +package network::fritzbox::upnp::plugin; + +use strict; +use warnings; +use base qw(centreon::plugins::script_custom); + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '0.1'; + $self->{modes} = { + 'system' => 'network::fritzbox::upnp::mode::system', + 'traffic' => 'network::fritzbox::upnp::mode::traffic' + }; + + $self->{custom_modes}->{soap} = 'network::fritzbox::upnp::custom::soap'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Fritz!Box equipment. +You have to activate UPNP! + +=cut