This commit is contained in:
Quentin Garnier 2014-12-23 16:48:26 +01:00
commit 0330ff9644
3 changed files with 232 additions and 34 deletions

66
apps/checkmyws/plugin.pm Normal file
View File

@ -0,0 +1,66 @@
################################################################################
# Copyright 2005-2015 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation ; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses>.
#
# Linking this program statically or dynamically with other modules is making a
# combined work based on this program. Thus, the terms and conditions of the GNU
# General Public License cover the whole combination.
#
# As a special exception, the copyright holders of this program give MERETHIS
# permission to link this program with independent modules to produce an executable,
# regardless of the license terms of these independent modules, and to copy and
# distribute the resulting executable under terms of MERETHIS choice, provided that
# MERETHIS also meet, for each linked independent module, the terms and conditions
# of the license of that module. An independent module is a module which is not
# derived from this program. If you modify this program, you may extend this
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
#
# For more information : contact@centreon.com
# Authors : Mathieu Cinquin <mcinquin@merethis.com>
#
####################################################################################
package apps::checkmyws::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;
# $options->{options} = options object
$self->{version} = '1.0';
%{$self->{modes}} = (
'status' => 'apps::checkmyws::mode::status',
);
return $self;
}
1;
__END__
=head1 PLUGIN DESCRIPTION
Check Check my Website through its API
=cut

View File

@ -0,0 +1,99 @@
###############################################################################
# Copyright 2005-2015 MERETHIS
# Centreon is developped by : Julien Mathis and Romain Le Merlus under
# GPL Licence 2.0.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation ; either version 2 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, see <http://www.gnu.org/licenses>.
#
# Linking this program statically or dynamically with other modules is making a
# combined work based on this program. Thus, the terms and conditions of the GNU
# General Public License cover the whole combination.
#
# As a special exception, the copyright holders of this program give MERETHIS
# permission to link this program with independent modules to produce an timeelapsedutable,
# regardless of the license terms of these independent modules, and to copy and
# distribute the resulting timeelapsedutable under terms of MERETHIS choice, provided that
# MERETHIS also meet, for each linked independent module, the terms and conditions
# of the license of that module. An independent module is a module which is not
# derived from this program. If you modify this program, you may extend this
# exception to your version of the program, but you are not obliged to do so. If you
# do not wish to do so, delete this exception statement from your version.
#
# For more information : contact@centreon.com
# Author : Mathieu Cinquin <mcinquin@merethis.com>
#
####################################################################################
package centreon::plugins::checkmywsapi;
use strict;
use warnings;
use LWP::UserAgent;
use JSON;
use URI;
sub get_port {
my ($self, %options) = @_;
my $cache_port = '';
if (defined($self->{option_results}->{port}) && $self->{option_results}->{port} ne '') {
$cache_port = $self->{option_results}->{port};
} else {
$cache_port = 80 if ($self->{option_results}->{proto} eq 'http');
$cache_port = 443 if ($self->{option_results}->{proto} eq 'https');
}
return $cache_port;
}
sub connect {
my ($self, %options) = @_;
my $ua = LWP::UserAgent->new( keep_alive => 1, protocols_allowed => ['http','https'], timeout => $self->{option_results}->{timeout});
my $connection_exit = defined($options{connection_exit}) ? $options{connection_exit} : 'unknown';
my ($response, $content);
my $req;
my $url = $self->{option_results}->{proto}.'://api.checkmy.ws:'.get_port($self).'/api/status/'.$self->{option_results}->{uid};
my $uri = URI->new($url);
$req = HTTP::Request->new( GET => $uri);
if (defined($self->{option_results}->{proxyurl})) {
$ua->proxy(['http', 'https'], $self->{option_results}->{proxyurl});
}
$response = $ua->request($req);
if ($response->is_success) {
my $json = JSON->new;
eval {
$content = $json->decode($response->content);
};
if ($@) {
$self->{output}->add_option_msg(short_msg => "Cannot decode json response");
$self->{output}->option_exit();
}
return $content;
}
$self->{output}->output_add(severity => $connection_exit,
short_msg => $response->status_line);
$self->{output}->display();
$self->{output}->exit();
}
1;

View File

@ -55,25 +55,29 @@ Once the directory is created, create the plugin file inside it :
touch plugin.pm touch plugin.pm
Then, edit plugin.pm to add **license terms** by copying it from an other plugin. Don't forget to put your name at the end of it : Then, edit plugin.pm to add **license terms** by copying it from an other plugin. Don't forget to put your name at the end of it :
::
.. code-block:: perl
# ... # ...
# Authors : <your name> <<your email>> # Authors : <your name> <<your email>>
Next, describe your **package** name : it matches your plugin directory. Next, describe your **package** name : it matches your plugin directory.
::
.. code-block:: perl
package path::to::plugin; package path::to::plugin;
Declare used libraries (**strict** and **warnings** are mandatory). Centreon libraries are described later : Declare used libraries (**strict** and **warnings** are mandatory). Centreon libraries are described later :
::
.. code-block:: perl
use strict; use strict;
use warnings; use warnings;
use base qw(**centreon_library**); use base qw(**centreon_library**);
The plugin need a **new** function to instantiate the object : The plugin need a **new** function to instantiate the object :
::
.. code-block:: perl
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -86,12 +90,14 @@ The plugin need a **new** function to instantiate the object :
} }
Plugin version must be declared in the **new** function : Plugin version must be declared in the **new** function :
::
.. code-block:: perl
$self->{version} = '0.1'; $self->{version} = '0.1';
Several modes can be declared in the **new** function : Several modes can be declared in the **new** function :
::
.. code-block:: perl
%{$self->{modes}} = ( %{$self->{modes}} = (
'mode1' => '<plugin_path>::mode::mode1', 'mode1' => '<plugin_path>::mode::mode1',
@ -100,12 +106,14 @@ Several modes can be declared in the **new** function :
); );
Then, Declare the module : Then, Declare the module :
::
.. code-block:: perl
1; 1;
A description of the plugin is needed to generate the documentation : A description of the plugin is needed to generate the documentation :
::
.. code-block:: perl
__END__ __END__
@ -120,7 +128,7 @@ A description of the plugin is needed to generate the documentation :
you can copy-paste an other plugin.pm and adapt some lines (package, arguments...). you can copy-paste an other plugin.pm and adapt some lines (package, arguments...).
.. tip:: .. tip::
plugin has ".pm" extension because it's a perl module. So don't forget to add **1;** at then end of the file plugin has ".pm" extension because it's a perl module. So don't forget to add **1;** at the end of the file
------------- -------------
@ -134,25 +142,29 @@ Once **plugin.pm** is created and modes are declared in it, create modes in the
touch mode1.pm touch mode1.pm
Then, edit mode1.pm to add **license terms** by copying it from an other plugin. Don't forget to put your name at the end of it : Then, edit mode1.pm to add **license terms** by copying it from an other plugin. Don't forget to put your name at the end of it :
::
.. code-block:: perl
# ... # ...
# Authors : <your name> <<your email>> # Authors : <your name> <<your email>>
Next, describe your **package** name : it matches your mode directory. Next, describe your **package** name : it matches your mode directory.
::
.. code-block:: perl
package path::to::plugin::mode::mode1; package path::to::plugin::mode::mode1;
Declare used libraries (always the same) : Declare used libraries (always the same) :
::
.. code-block:: perl
use strict; use strict;
use warnings; use warnings;
use base qw(centreon::plugins::mode); use base qw(centreon::plugins::mode);
The mode need a **new** function to instantiate the object : The mode need a **new** function to instantiate the object :
::
.. code-block:: perl
sub new { sub new {
my ($class, %options) = @_; my ($class, %options) = @_;
@ -165,12 +177,14 @@ The mode need a **new** function to instantiate the object :
} }
Mode version must be declared in the **new** function : Mode version must be declared in the **new** function :
::
.. code-block:: perl
$self->{version} = '1.0'; $self->{version} = '1.0';
Several options can be declared in the **new** function : Several options can be declared in the **new** function :
::
.. code-block:: perl
$options{options}->add_options(arguments => $options{options}->add_options(arguments =>
{ {
@ -189,7 +203,8 @@ This the description of arguments of this example :
You can have more informations about options format here : http://perldoc.perl.org/Getopt/Long.html You can have more informations about options format here : http://perldoc.perl.org/Getopt/Long.html
The mode need a **check_options** function to validate options : The mode need a **check_options** function to validate options :
::
.. code-block:: perl
sub check_options { sub check_options {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -198,7 +213,8 @@ The mode need a **check_options** function to validate options :
} }
For example, Warning and Critical thresholds must be validate in **check_options** function : For example, Warning and Critical thresholds must be validate in **check_options** function :
::
.. code-block:: perl
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { 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}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
@ -213,7 +229,8 @@ In this example, help is printed if thresholds have not a correct format.
Then comes the **run** function, where you perform measurement, check thresholds, display output and format perfdatas. Then comes the **run** function, where you perform measurement, check thresholds, display output and format perfdatas.
This is an example to check a snmp value : This is an example to check a snmp value :
::
.. code-block:: perl
sub run { sub run {
my ($self, %options) = @_; my ($self, %options) = @_;
@ -307,7 +324,7 @@ Parameters
+=================+=================+=============+=========================================================+ +=================+=================+=============+=========================================================+
| severity | String | OK | Status of the output. | | severity | String | OK | Status of the output. |
+-----------------+-----------------+-------------+---------------------------------------------------------+ +-----------------+-----------------+-------------+---------------------------------------------------------+
| separator | String | '-' | Separator between status and output string. | | separator | String | \- | Separator between status and output string. |
+-----------------+-----------------+-------------+---------------------------------------------------------+ +-----------------+-----------------+-------------+---------------------------------------------------------+
| short_msg | String | | Short output (first line). | | short_msg | String | | Short output (first line). |
+-----------------+-----------------+-------------+---------------------------------------------------------+ +-----------------+-----------------+-------------+---------------------------------------------------------+
@ -318,7 +335,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to manage output : This is an example of how to manage output :
::
.. code-block:: perl
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(severity => 'OK',
short_msg => 'All is ok'); short_msg => 'All is ok');
@ -369,7 +387,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to add performance data : This is an example of how to add performance data :
::
.. code-block:: perl
$self->{output}->output_add(severity => 'OK', $self->{output}->output_add(severity => 'OK',
short_msg => 'Memory is ok'); short_msg => 'Memory is ok');
@ -425,7 +444,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to manage performance data for output : This is an example of how to manage performance data for output :
::
.. code-block:: perl
my $format_warning_perfdata = $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => 1000000000, cast_int => 1); my $format_warning_perfdata = $self->{perfdata}->get_perfdata_for_output(label => 'warning', total => 1000000000, cast_int => 1);
my $format_critical_perfdata = $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => 1000000000, cast_int => 1); my $format_critical_perfdata = $self->{perfdata}->get_perfdata_for_output(label => 'critical', total => 1000000000, cast_int => 1);
@ -464,7 +484,8 @@ Example
^^^^^^^ ^^^^^^^
This example checks if warning threshold is correct : This example checks if warning threshold is correct :
::
.. code-block:: perl
if (($self->{perfdata}->threshold_validate(label => 'warning', value => $self->{option_results}->{warning})) == 0) { 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}->add_option_msg(short_msg => "Wrong warning threshold '" . $self->{option_results}->{warning} . "'.");
@ -497,7 +518,8 @@ Example
^^^^^^^ ^^^^^^^
This example checks if performance data reached thresholds : This example checks if performance data reached thresholds :
::
.. code-block:: perl
$self->{perfdata}->threshold_validate(label => 'warning', value => 80); $self->{perfdata}->threshold_validate(label => 'warning', value => 80);
$self->{perfdata}->threshold_validate(label => 'critical', value => 90); $self->{perfdata}->threshold_validate(label => 'critical', value => 90);
@ -538,7 +560,8 @@ Example
^^^^^^^ ^^^^^^^
This example change bytes to human readable unit : This example change bytes to human readable unit :
::
.. code-block:: perl
my ($value, $unit) = $self->{perfdata}->change_bytes(value => 100000); my ($value, $unit) = $self->{perfdata}->change_bytes(value => 100000);
@ -555,7 +578,8 @@ Snmp
This library allows you to use snmp protocol in your plugin. This library allows you to use snmp protocol in your plugin.
To use it, Add the following line at the beginning of your **plugin.pm** : To use it, Add the following line at the beginning of your **plugin.pm** :
::
.. code-block:: perl
use base qw(centreon::plugins::script_snmp); use base qw(centreon::plugins::script_snmp);
@ -585,7 +609,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get 2 snmp values : This is an example of how to get 2 snmp values :
::
.. code-block:: perl
my $oid_hrSystemUptime = '.1.3.6.1.2.1.25.1.1.0'; my $oid_hrSystemUptime = '.1.3.6.1.2.1.25.1.1.0';
my $oid_sysUpTime = '.1.3.6.1.2.1.1.3.0'; my $oid_sysUpTime = '.1.3.6.1.2.1.1.3.0';
@ -625,7 +650,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get 4 instances of a snmp table by using **load** function : This is an example of how to get 4 instances of a snmp table by using **load** function :
::
.. code-block:: perl
my $oid_dskPath = '.1.3.6.1.4.1.2021.9.1.2'; my $oid_dskPath = '.1.3.6.1.4.1.2021.9.1.2';
@ -637,7 +663,8 @@ This is an example of how to get 4 instances of a snmp table by using **load** f
print Dumper($result); print Dumper($result);
This is an example of how to get multiple instances dynamically (memory modules of dell hardware) by using **load** function : This is an example of how to get multiple instances dynamically (memory modules of dell hardware) by using **load** function :
::
.. code-block:: perl
my $oid_memoryDeviceStatus = '.1.3.6.1.4.1.674.10892.1.1100.50.1.5'; my $oid_memoryDeviceStatus = '.1.3.6.1.4.1.674.10892.1.1100.50.1.5';
my $oid_memoryDeviceLocationName = '.1.3.6.1.4.1.674.10892.1.1100.50.1.8'; my $oid_memoryDeviceLocationName = '.1.3.6.1.4.1.674.10892.1.1100.50.1.8';
@ -686,7 +713,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get a snmp table : This is an example of how to get a snmp table :
::
.. code-block:: perl
my $oid_rcDeviceError = '.1.3.6.1.4.1.15004.4.2.1'; my $oid_rcDeviceError = '.1.3.6.1.4.1.15004.4.2.1';
my $oid_rcDeviceErrWatchdogReset = '.1.3.6.1.4.1.15004.4.2.1.2.0'; my $oid_rcDeviceErrWatchdogReset = '.1.3.6.1.4.1.15004.4.2.1.2.0';
@ -725,7 +753,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get 2 snmp tables : This is an example of how to get 2 snmp tables :
::
.. code-block:: perl
my $oid_sysDescr = ".1.3.6.1.2.1.1.1"; my $oid_sysDescr = ".1.3.6.1.2.1.1.1";
my $aix_swap_pool = ".1.3.6.1.4.1.2.6.191.2.4.2.1"; my $aix_swap_pool = ".1.3.6.1.4.1.2.6.191.2.4.2.1";
@ -756,7 +785,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get hostname parameter : This is an example of how to get hostname parameter :
::
.. code-block:: perl
my $hostname = $self->{snmp}->get_hostname(); my $hostname = $self->{snmp}->get_hostname();
@ -778,7 +808,8 @@ Example
^^^^^^^ ^^^^^^^
This is an example of how to get port parameter : This is an example of how to get port parameter :
::
.. code-block:: perl
my $port = $self->{snmp}->get_port(); my $port = $self->{snmp}->get_port();
@ -804,9 +835,11 @@ Example
^^^^^^^ ^^^^^^^
This example prints sorted OIDs : This example prints sorted OIDs :
::
.. code-block:: perl
foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$my_oid}})) { foreach my $oid ($self->{snmp}->oid_lex_sort(keys %{$self->{results}->{$my_oid}})) {
print $oid; print $oid;
} }