From d9b4e82be248654cfdf9b2c5b1b3ac712e02210b Mon Sep 17 00:00:00 2001 From: qgarnier Date: Tue, 21 Nov 2017 13:16:22 +0100 Subject: [PATCH] freebox plugin: wip --- network/freebox/restapi/custom/api.pm | 272 +++++++++++++++++++++++++ network/freebox/restapi/mode/system.pm | 225 ++++++++++++++++++++ network/freebox/restapi/plugin.pm | 49 +++++ 3 files changed, 546 insertions(+) create mode 100644 network/freebox/restapi/custom/api.pm create mode 100644 network/freebox/restapi/mode/system.pm create mode 100644 network/freebox/restapi/plugin.pm diff --git a/network/freebox/restapi/custom/api.pm b/network/freebox/restapi/custom/api.pm new file mode 100644 index 000000000..13e31220c --- /dev/null +++ b/network/freebox/restapi/custom/api.pm @@ -0,0 +1,272 @@ +# +# Copyright 2017 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::freebox::restapi::custom::api; + +use strict; +use warnings; +use centreon::plugins::http; +use JSON; +use Digest::SHA qw(hmac_sha1_hex); + +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' }, + "freebox-app-id:s@" => { name => 'freebox_app_id' }, + "freebox-app-token:s@" => { name => 'freebox_app_token' }, + "freebox-api-version:s@" => { name => 'freebox_api_version', }, + "proxyurl:s@" => { name => 'proxyurl' }, + "timeout:s@" => { name => 'timeout' }, + "precision:s@" => { name => 'precision' }, + }); + } + $options{options}->add_help(package => __PACKAGE__, sections => 'REST API OPTIONS', once => 1); + + $self->{output} = $options{output}; + $self->{mode} = $options{mode}; + $self->{http} = centreon::plugins::http->new(output => $self->{output}); + + $self->{session_token} = undef; + + return $self; + +} + +sub set_options { + my ($self, %options) = @_; + + $self->{option_results} = $options{option_results}; +} + +sub set_defaults { + my ($self, %options) = @_; + + foreach (keys %{$options{default}}) { + if ($_ eq $self->{mode}) { + for (my $i = 0; $i < scalar(@{$options{default}->{$_}}); $i++) { + foreach my $opt (keys %{$options{default}->{$_}[$i]}) { + if (!defined($self->{option_results}->{$opt}[$i])) { + $self->{option_results}->{$opt}[$i] = $options{default}->{$_}[$i]->{$opt}; + } + } + } + } + } +} + +sub check_options { + my ($self, %options) = @_; + + $self->{hostname} = (defined($self->{option_results}->{hostname})) ? shift(@{$self->{option_results}->{hostname}}) : 'mafreebox.free.fr'; + $self->{freebox_app_id} = (defined($self->{option_results}->{freebox_app_id})) ? shift(@{$self->{option_results}->{freebox_app_id}}) : undef; + $self->{freebox_app_token} = (defined($self->{option_results}->{freebox_app_token})) ? shift(@{$self->{option_results}->{freebox_app_token}}) : undef; + $self->{freebox_api_version} = (defined($self->{option_results}->{freebox_api_version})) ? shift(@{$self->{option_results}->{freebox_api_version}}) : 'v4'; + $self->{timeout} = (defined($self->{option_results}->{timeout})) ? shift(@{$self->{option_results}->{timeout}}) : 10; + $self->{proxyurl} = (defined($self->{option_results}->{proxyurl})) ? shift(@{$self->{option_results}->{proxyurl}}) : undef; + $self->{resolution} = (defined($self->{option_results}->{resolution})) ? shift(@{$self->{option_results}->{resolution}}) : 300; + + if (!defined($self->{hostname})) { + $self->{output}->add_option_msg(short_msg => "Need to specify hostname option."); + $self->{output}->option_exit(); + } + if (!defined($self->{freebox_app_id})) { + $self->{output}->add_option_msg(short_msg => "Need to specify freebox-app-id option."); + $self->{output}->option_exit(); + } + if (!defined($self->{freebox_app_token})) { + $self->{output}->add_option_msg(short_msg => "Need to specify freebox-app-token option."); + $self->{output}->option_exit(); + } + + if (!defined($self->{option_results}->{freebox_app_id}) || + scalar(@{$self->{option_results}->{freebox_app_id}}) == 0) { + return 0; + } + return 1; +} + +sub build_options_for_httplib { + my ($self, %options) = @_; + + $self->{option_results}->{hostname} = $self->{hostname}; + $self->{option_results}->{timeout} = $self->{timeout}; + $self->{option_results}->{port} = 80; + $self->{option_results}->{proto} = 'http'; + $self->{option_results}->{proxyurl} = $self->{proxyurl}; +} + +sub settings { + my ($self, %options) = @_; + + $self->build_options_for_httplib(); + if (defined($self->{session_token})) { + $self->{http}->add_header(key => 'X-Fbx-App-Auth', value => $self->{session_token}); + } + $self->{http}->add_header(key => 'Accept', value => 'application/json'); + $self->{http}->set_options(%{$self->{option_results}}); +} + +sub manage_response { + my ($self, %options) = @_; + + my $response = $self->{http}->get_response(); + if ($response->code() != 200) { + $self->{output}->add_option_msg(short_msg => "Connection issue: " . $options{content}); + $self->{output}->option_exit(); + } + + my $decoded; + eval { + $decoded = decode_json($options{content}); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot decode json response"); + $self->{output}->option_exit(); + } + + if (!$decoded->{success}) { + $self->{output}->add_option_msg(short_msg => "Unsuccessful $options{type} response"); + $self->{output}->option_exit(); + } + + return $decoded; +} + +sub get_session { + my ($self, %options) = @_; + + $self->settings(); + my $content = $self->{http}->request(url_path => '/api/' . $self->{freebox_api_version} . '/login/', + critical_status => '', warning_status => ''); + my $decoded = $self->manage_response(content => $content); + my $challenge = $decoded->{result}->{challenge}; + my $password = hmac_sha1_hex($challenge, $self->{freebox_app_token}); + + my $json_request = { app_id => $self->{freebox_app_id}, password => $password }; + my $encoded; + eval { + $encoded = encode_json($json_request); + }; + if ($@) { + $self->{output}->add_option_msg(short_msg => "Cannot encode json request"); + $self->{output}->option_exit(); + } + + $content = $self->{http}->request(url_path => '/api/' . $self->{freebox_api_version} . '/login/session/', method => 'POST', + query_form_post => $encoded, critical_status => '', warning_status => ''); + $decoded = $self->manage_response(content => $content); + + $self->{session_token} = $decoded->{result}->{session_token}; +} + +sub get_data { + my ($self, %options) = @_; + + if (!defined($self->{session_token})) { + $self->get_session(); + } + + $self->settings(); + my $content = $self->{http}->request(url_path => '/api/' . $self->{freebox_api_version} . '/' . $options{path}, + critical_status => '', warning_status => ''); + my $decoded = $self->manage_response(content => $content); + return $decoded->{result}; +} + +sub get_performance { + my ($self, %options) = @_; + + +} + +sub DESTROY { + my $self = shift; + + if (defined($self->{session_token})) { + $self->{http}->request(url_path => '/api/' . $self->{freebox_api_version} . '/login/logout/', method => 'POST'); + } +} + +1; + +__END__ + +=head1 NAME + +FREEBOX REST API + +=head1 SYNOPSIS + +Freebox Rest API custom mode + +=head1 REST API OPTIONS + +=over 8 + +=item B<--hostname> + +Freebox hostname (Default: 'mafreebox.free.fr'). + +=item B<--freebox-app-id> + +Freebox App ID. + +=item B<--freebox-app-token> + +Freebox App Token. + +=item B<--freebox-api-version> + +Freebox API version (Default: 'v4'). + +=item B<--proxyurl> + +Proxy URL if any. + +=item B<--timeout> + +Set HTTP timeout in seconds (Default: '10'). + +=item B<--precision> + +Selected data performance resolution in seconds (Default: '300'). + +=back + +=head1 DESCRIPTION + +B. + +=cut diff --git a/network/freebox/restapi/mode/system.pm b/network/freebox/restapi/mode/system.pm new file mode 100644 index 000000000..81f8db97e --- /dev/null +++ b/network/freebox/restapi/mode/system.pm @@ -0,0 +1,225 @@ +# +# Copyright 2017 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::freebox::restapi::mode::system; + +use base qw(centreon::plugins::templates::counter); + +use strict; +use warnings; +my $instance_mode; + +sub set_counters { + my ($self, %options) = @_; + + $self->{maps_counters_type} = [ + { name => 'global', type => 0 }, + ]; + + $self->{maps_counters}->{global} = [ + { label => 'temperature-cpum', set => { + key_values => [ { name => 'temp_cpum' } ], + output_template => 'Temperature cpum : %s C', + perfdatas => [ + { label => 'temp_cpum', value => 'temp_cpum_absolute', template => '%s', + unit => 'C' }, + ], + } + }, + { label => 'temperature-cpub', set => { + key_values => [ { name => 'temp_cpub' } ], + output_template => 'Temperature cpub : %s C', + perfdatas => [ + { label => 'temp_cpub', value => 'temp_cpub_absolute', template => '%s', + unit => 'C' }, + ], + } + }, + { label => 'temperature-switch', set => { + key_values => [ { name => 'temp_sw' } ], + output_template => 'Temperature switch : %s C', + perfdatas => [ + { label => 'temp_sw', value => 'temp_sw_absolute', template => '%s', + unit => 'C' }, + ], + } + }, + { label => 'fan-speed', set => { + key_values => [ { name => 'fan_rpm' } ], + output_template => 'fan speed : %s rpm', + perfdatas => [ + { label => 'fan_rpm', value => 'fan_rpm_absolute', template => '%s', + min => 0, unit => 'rpm' }, + ], + } + }, + { label => 'disk-status', threshold => 0, set => { + key_values => [ { name => 'disk_status' } ], + closure_custom_calc => $self->can('custom_status_calc'), closure_custom_calc_extra_options => { label_ref => 'disk' }, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + { label => 'wifi-status', threshold => 0, set => { + key_values => [ { name => 'wifi_status' } ], + closure_custom_calc => $self->can('custom_status_calc'), closure_custom_calc_extra_options => { label_ref => 'wifi' }, + closure_custom_output => $self->can('custom_status_output'), + closure_custom_perfdata => sub { return 0; }, + closure_custom_threshold_check => $self->can('custom_status_threshold'), + } + }, + ]; +} + +sub custom_status_threshold { + my ($self, %options) = @_; + my $status = 'ok'; + my $message; + + eval { + local $SIG{__WARN__} = sub { $message = $_[0]; }; + local $SIG{__DIE__} = sub { $message = $_[0]; }; + + my $label = $self->{label}; + $label =~ s/-/_/g; + if (defined($instance_mode->{option_results}->{'critical_' . $label}) && $instance_mode->{option_results}->{'critical_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'critical_' . $label}") { + $status = 'critical'; + } elsif (defined($instance_mode->{option_results}->{'warning_' . $label}) && $instance_mode->{option_results}->{'warning_' . $label} ne '' && + eval "$instance_mode->{option_results}->{'warning_' . $label}") { + $status = 'warning'; + } + }; + if (defined($message)) { + $self->{output}->output_add(long_msg => 'filter status issue: ' . $message); + } + + return $status; +} + +sub custom_status_output { + my ($self, %options) = @_; + my $msg = ucfirst($self->{result_values}->{label}) . ' status : ' . $self->{result_values}->{status}; + + return $msg; +} + +sub custom_status_calc { + my ($self, %options) = @_; + + $self->{result_values}->{label} = $options{extra_options}->{label_ref}; + $self->{result_values}->{status} = $options{new_datas}->{$self->{instance} . '_' . $self->{result_values}->{label} . '_status'}; + return 0; +} + +sub new { + my ($class, %options) = @_; + my $self = $class->SUPER::new(package => __PACKAGE__, %options); + bless $self, $class; + + $self->{version} = '1.0'; + $options{options}->add_options(arguments => + { + "warning-wifi-status:s" => { name => 'warning_wifi_status', default => '%{status} =~ /bad_param/i' }, + "critical-wifi-status:s" => { name => 'critical_wifi_status', default => '%{status} =~ /failed/i' }, + "warning-disk-status:s" => { name => 'warning_disk_status', default => '' }, + "critical-disk-status:s" => { name => 'critical_disk_status', default => '%{status} =~ /error/i' }, + }); + + return $self; +} + +sub check_options { + my ($self, %options) = @_; + $self->SUPER::check_options(%options); + + $instance_mode = $self; + $self->change_macros(); +} + +sub change_macros { + my ($self, %options) = @_; + + foreach (('warning_wifi_status', 'critical_wifi_status', 'warning_disk_status', 'critical_disk_status')) { + if (defined($self->{option_results}->{$_})) { + $self->{option_results}->{$_} =~ s/%\{(.*?)\}/\$self->{result_values}->{$1}/g; + } + } +} + +sub manage_selection { + my ($self, %options) = @_; + + my $result = $options{custom}->get_data(path => 'system/'); + $self->{global} = { %{$result} }; + + $result = $options{custom}->get_data(path => 'wifi/ap/'); + $self->{global}->{wifi_status} = $result->{status}->{state}; +} + +1; + +__END__ + +=head1 MODE + +Check system. + +=over 8 + +=item B<--filter-counters> + +Only display some counters (regexp can be used). +Example: --filter-counters='^temperature-cpum$' + +=item B<--warning-wifi-status> + +Set warning threshold for wifi status (Default: '%{status} =~ /bad_param/i'). +Can used special variables like: %{status} + +=item B<--critical-wifi-status> + +Set critical threshold for wifi status (Default: '%{status} =~ /failed/i'). +Can used special variables like: %{status} + +=item B<--warning-disk-status> + +Set warning threshold for disk status. +Can used special variables like: %{status} + +=item B<--critical-disk-status> + +Set critical threshold for disk status (Default: '%{status} =~ /error/i'). +Can used special variables like: %{status} + +=item B<--warning-*> + +Threshold warning. +Can be: 'temperature-cpum', 'temperature-cpub', 'temperature-switch', 'fan-speed'. + +=item B<--critical-*> + +Threshold critical. +Can be: 'temperature-cpum', 'temperature-cpub', 'temperature-switch', 'fan-speed'. + +=back + +=cut diff --git a/network/freebox/restapi/plugin.pm b/network/freebox/restapi/plugin.pm new file mode 100644 index 000000000..4f0451268 --- /dev/null +++ b/network/freebox/restapi/plugin.pm @@ -0,0 +1,49 @@ +# +# Copyright 2017 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::freebox::restapi::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} = '1.0'; + %{$self->{modes}} = ( + 'system' => 'network::freebox::restapi::mode::system', + ); + + $self->{custom_modes}{api} = 'network::freebox::restapi::custom::api'; + return $self; +} + +1; + +__END__ + +=head1 PLUGIN DESCRIPTION + +Check Freebox through HTTP/REST API. + +=cut