diff --git a/.project b/.project
deleted file mode 100644
index bc15201e8..000000000
--- a/.project
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
- Plugins
-
-
-
-
-
-
-
-
diff --git a/CHANGELOG b/CHANGELOG
deleted file mode 100644
index ab35ef867..000000000
--- a/CHANGELOG
+++ /dev/null
@@ -1,22 +0,0 @@
-##########################
-# Centreon Plugins Change Log
-##########################
-
-1.2.3 - 02/11/2005
-----------------
-* Enhanced Oreon Perl Package oreon.pm (wistof)
-* Integration of some Manubulon plugins (wistof)
-* New install script for plugins (wistof)
-* Minor bug fixes
-
-1.2.2 - xx/08/2005
-----------------
-* Add Oreon Perl Package oreon.pm (gollum123)
-* Globalization of paths in oreon.conf (gollum123)
-* Management of RRDTOOL errors (wistof)
-* Use Net::SNMP instead of snmpwalk and snmpget (wistof)
-* Disable regex for '-H' option (wistof)
-* Minor bug fixes
-
-
-
diff --git a/INSTALL b/INSTALL
deleted file mode 100644
index 8c50a6f25..000000000
--- a/INSTALL
+++ /dev/null
@@ -1,8 +0,0 @@
-#################################
-# Centreon Plugins Installation #
-#################################
-
-- just run install.sh in src/ directory
-- don't directly copy plugins in Nagios plugins directory
-
-
diff --git a/REQUIREMENT b/REQUIREMENT
deleted file mode 100644
index 8f96b4663..000000000
--- a/REQUIREMENT
+++ /dev/null
@@ -1,13 +0,0 @@
-################################
-# Centreon Plugins Requirement #
-################################
-
-- Perl in /usr/bin/perl - or just run 'perl script'
-- Net::SNMP
-- Getopt::Long;
-- FindBin::Bin;
-- Config::IniFiles
-- centreon perl module (centreon.pm)
-- Centreon configuration file for Centreon perl module (Centreon.conf)
-- file 'utils.pm' in plugins diretory (/usr/local/nagios/libexec)
-- sed program for installation script
\ No newline at end of file
diff --git a/centreon/plugins/dbi.pm b/centreon/plugins/dbi.pm
new file mode 100644
index 000000000..f512594d9
--- /dev/null
+++ b/centreon/plugins/dbi.pm
@@ -0,0 +1,266 @@
+################################################################################
+# Copyright 2005-2013 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 .
+#
+# 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 : Quentin Garnier
+#
+####################################################################################
+
+package centreon::plugins::dbi;
+
+use strict;
+use warnings;
+use DBI;
+use Digest::MD5 qw(md5_hex);
+
+sub new {
+ my ($class, %options) = @_;
+ my $self = {};
+ bless $self, $class;
+ # $options{options} = options object
+ # $options{output} = output object
+ # $options{exit_value} = integer
+ # $options{noptions} = integer
+
+ if (!defined($options{output})) {
+ print "Class DBI: Need to specify 'output' argument.\n";
+ exit 3;
+ }
+ if (!defined($options{options})) {
+ $options{output}->add_option_msg(short_msg => "Class DBI: Need to specify 'options' argument.");
+ $options{output}->option_exit();
+ }
+
+ if (!defined($options{noptions})) {
+ $options{options}->add_options(arguments =>
+ { "datasource:s@" => { name => 'data_source' },
+ "username:s@" => { name => 'username' },
+ "password:s@" => { name => 'password' },
+ "sql-errors-exit:s" => { name => 'sql_errors_exit', default => 'unknown' },
+ });
+ }
+ $options{options}->add_help(package => __PACKAGE__, sections => 'DBI OPTIONS', once => 1);
+
+ $self->{output} = $options{output};
+ $self->{mode} = $options{mode};
+ $self->{instance} = undef;
+ $self->{statement_handle} = undef;
+ $self->{version} = undef;
+
+ $self->{data_source} = undef;
+ $self->{username} = undef;
+ $self->{password} = undef;
+
+ return $self;
+}
+
+# Method to manage multiples
+sub set_options {
+ my ($self, %options) = @_;
+ # options{options_result}
+
+ $self->{option_results} = $options{option_results};
+}
+
+# Method to manage multiples
+sub set_defaults {
+ my ($self, %options) = @_;
+ # options{default}
+
+ # Manage default value
+ 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) = @_;
+ # return 1 = ok still data_source
+ # return 0 = no data_source left
+
+ $self->{data_source} = (defined($self->{option_results}->{data_source})) ? shift(@{$self->{option_results}->{data_source}}) : undef;
+ $self->{username} = (defined($self->{option_results}->{username})) ? shift(@{$self->{option_results}->{username}}) : undef;
+ $self->{password} = (defined($self->{option_results}->{password})) ? shift(@{$self->{option_results}->{password}}) : undef;
+ $self->{sql_errors_exit} = $self->{option_results}->{sql_errors_exit};
+
+ if (!defined($self->{data_source}) || $self->{data_source} eq '') {
+ $self->{output}->add_option_msg(short_msg => "Need to specify database arguments.");
+ $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
+ }
+
+ if (scalar(@{$self->{option_results}->{data_source}}) == 0) {
+ return 0;
+ }
+ return 1;
+}
+
+sub quote {
+ my $self = shift;
+
+ if (defined($self->{instance})) {
+ return $self->{instance}->quote($_[0]);
+ }
+ return undef;
+}
+
+sub is_version_minimum {
+ my ($self, %options) = @_;
+ # $options{version} = string version to check
+
+ my @version_src = split /\./, $self->{version};
+ my @versions = split /\./, $options{version};
+ for (my $i = 0; $i < scalar(@versions); $i++) {
+ return 1 if ($versions[$i] eq 'x');
+ return 1 if (!defined($version_src[$i]));
+ $version_src[$i] =~ /^([0-9]*)/;
+ next if ($versions[$i] == int($1));
+ return 0 if ($versions[$i] > int($1));
+ return 1 if ($versions[$i] < int($1));
+ }
+
+ return 1;
+}
+
+sub connect {
+ my ($self, %options) = @_;
+ my $dontquit = (defined($options{dontquit}) && $options{dontquit} == 1) ? 1 : 0;
+
+ $self->{instance} = DBI->connect(
+ "DBI:". $self->{data_source},
+ $self->{username},
+ $self->{password},
+ { "RaiseError" => 0, "PrintError" => 0, "AutoCommit" => 1 }
+ );
+
+ if (!defined($self->{instance})) {
+ if ($dontquit == 0) {
+ $self->{output}->add_option_msg(short_msg => "Cannot connect: " . $DBI::errstr);
+ $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
+ }
+ return (-1, "Cannot connect: " . $DBI::errstr);
+ }
+
+ $self->{version} = $self->{instance}->get_info(18); # SQL_DBMS_VER
+ return 0;
+}
+
+sub get_id {
+ my ($self, %options) = @_;
+
+ return $self->{data_source};
+}
+
+sub get_unique_id4save {
+ my ($self, %options) = @_;
+
+ return md5_hex($self->{data_source});
+}
+
+sub fetchall_arrayref {
+ my ($self, %options) = @_;
+
+ return $self->{statement_handle}->fetchall_arrayref();
+}
+
+sub fetchrow_array {
+ my ($self, %options) = @_;
+
+ return $self->{statement_handle}->fetchrow_array();
+}
+
+sub fetchrow_hashref {
+ my ($self, %options) = @_;
+
+ return $self->{statement_handle}->fetchrow_hashref();
+}
+
+sub query {
+ my ($self, %options) = @_;
+
+ $self->{statement_handle} = $self->{instance}->prepare($options{query});
+ if (!defined($self->{statement_handle})) {
+ $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{instance}->errstr);
+ $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
+ }
+
+ my $rv = $self->{statement_handle}->execute;
+ if (!$rv) {
+ $self->{output}->add_option_msg(short_msg => "Cannot execute query: " . $self->{statement_handle}->errstr);
+ $self->{output}->option_exit(exit_litteral => $self->{sql_errors_exit});
+ }
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+DBI global
+
+=head1 SYNOPSIS
+
+dbi class
+
+=head1 DBI OPTIONS
+
+=over 8
+
+=item B<--datasource>
+
+Hostname to query (required).
+
+=item B<--username>
+
+Database username.
+
+=item B<--password>
+
+Database password.
+
+=item B<--sql-errors-exit>
+
+Exit code for DB Errors (default: unknown)
+
+=back
+
+=head1 DESCRIPTION
+
+B.
+
+=cut
diff --git a/centreon/plugins/misc.pm b/centreon/plugins/misc.pm
new file mode 100644
index 000000000..598d4bbec
--- /dev/null
+++ b/centreon/plugins/misc.pm
@@ -0,0 +1,143 @@
+################################################################################
+# Copyright 2005-2013 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 .
+#
+# 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 : Quentin Garnier
+#
+####################################################################################
+
+package centreon::plugins::misc;
+
+use strict;
+use warnings;
+
+sub mymodule_load {
+ my (%options) = @_;
+ my $file;
+ ($file = $options{module} . ".pm") =~ s{::}{/}g;
+
+ eval {
+ local $SIG{__DIE__} = 'IGNORE';
+ require $file;
+ };
+ if ($@) {
+ $options{output}->add_option_msg(long_msg => $@);
+ $options{output}->add_option_msg(short_msg => $options{error_msg});
+ $options{output}->option_exit();
+ }
+}
+
+sub backtick {
+ my %arg = (
+ command => undef,
+ arguments => [],
+ timeout => 30,
+ wait_exit => 0,
+ redirect_stderr => 0,
+ @_,
+ );
+ my @output;
+ my $pid;
+ my $return_code;
+
+ my $sig_do;
+ if ($arg{wait_exit} == 0) {
+ $sig_do = 'IGNORE';
+ $return_code = undef;
+ } else {
+ $sig_do = 'DEFAULT';
+ }
+ local $SIG{CHLD} = $sig_do;
+
+ if (!defined($pid = open( KID, "-|" ))) {
+ return (-1001, "Cant fork: $!", -1);
+ }
+
+ if ($pid) {
+
+ eval {
+ local $SIG{ALRM} = sub { die "Timeout by signal ALARM\n"; };
+ alarm( $arg{timeout} );
+ while () {
+ chomp;
+ push @output, $_;
+ }
+
+ alarm(0);
+ };
+
+ if ($@) {
+ if ($pid != -1) {
+ kill -9, $pid;
+ }
+
+ alarm(0);
+ return (-1000, "Command too long to execute (timeout)...", -1);
+ } else {
+ if ($arg{wait_exit} == 1) {
+ # We're waiting the exit code
+ waitpid($pid, 0);
+ $return_code = ($? >> 8);
+ }
+ close KID;
+ }
+ } else {
+ # child
+ # set the child process to be a group leader, so that
+ # kill -9 will kill it and all its descendents
+ setpgrp( 0, 0 );
+
+ if ($arg{redirect_stderr} == 1) {
+ open STDERR, ">&STDOUT";
+ }
+ open STDERR, ">&STDOUT";
+ if (scalar(@{$arg{arguments}}) <= 0) {
+ exec($arg{command});
+ } else {
+ exec($arg{command}, @{$arg{arguments}});
+ }
+ exit(0);
+ }
+
+ return (0, join("\n", @output), $return_code);
+}
+
+sub trim {
+ # Sometimes there is a null character
+ $_[0] =~ s/\x00$//;
+ $_[0] =~ s/^[ \t]+//;
+ $_[0] =~ s/[ \t]+$//;
+ return $_[0];
+}
+
+1;
+
+__END__
+
diff --git a/centreon/plugins/mode.pm b/centreon/plugins/mode.pm
new file mode 100644
index 000000000..cc4a895cd
--- /dev/null
+++ b/centreon/plugins/mode.pm
@@ -0,0 +1,93 @@
+################################################################################
+# Copyright 2005-2013 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 .
+#
+# 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 : Quentin Garnier
+#
+####################################################################################
+
+package centreon::plugins::mode;
+
+use strict;
+use warnings;
+use centreon::plugins::perfdata;
+
+sub new {
+ my ($class, %options) = @_;
+ my $self = {};
+ bless $self, $class;
+
+ $self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output});
+ %{$self->{option_results}} = ();
+ $self->{output} = $options{output};
+ $self->{mode} = $options{mode};
+ $self->{version} = undef;
+
+ return $self;
+}
+
+sub init {
+ my ($self, %options) = @_;
+ # options{default} = [ {option_name => '', option_value => '' }, ]
+
+ %{$self->{option_results}} = %{$options{option_results}};
+ # Manage default value
+ return if (!defined($options{default}));
+ foreach (keys %{$options{default}}) {
+ if ($_ eq $self->{mode}) {
+ foreach my $value (keys %{$options{default}->{$_}}) {
+ if (!defined($self->{option_results}->{$value})) {
+ $self->{option_results}->{$value} = $options{default}->{$_}->{$value};
+ }
+ }
+ }
+ }
+}
+
+sub version {
+ my ($self, %options) = @_;
+
+ $self->{output}->add_option_msg(short_msg => "Mode Version: " . $self->{version});
+}
+
+sub disco_format {
+ my ($self, %options) = @_;
+
+}
+
+sub disco_show {
+ my ($self, %options) = @_;
+
+}
+
+1;
+
+__END__
+
diff --git a/centreon/plugins/options.pm b/centreon/plugins/options.pm
new file mode 100644
index 000000000..73acddc6c
--- /dev/null
+++ b/centreon/plugins/options.pm
@@ -0,0 +1,143 @@
+################################################################################
+# Copyright 2005-2013 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 .
+#
+# 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 : Quentin Garnier
+#
+####################################################################################
+
+package centreon::plugins::options;
+use Pod::Usage;
+use Pod::Find qw(pod_where);
+use Getopt::Long;
+Getopt::Long::Configure("pass_through");
+Getopt::Long::Configure('bundling');
+
+sub new {
+ my $class = shift;
+ my $self = {};
+ bless $self, $class;
+
+ $self->{options_stored} = {};
+ $self->{options} = {};
+ @{$self->{pod_package}} = ();
+ $self->{pod_packages_once} = {};
+ return $self;
+}
+
+sub set_output {
+ my ($self, %options) = @_;
+
+ $self->{output} = $options{output};
+}
+
+sub display_help {
+ my ($self, %options) = @_;
+
+ foreach (@{$self->{pod_package}}) {
+ my $stdout;
+
+ {
+ local *STDOUT;
+ open STDOUT, '>', \$stdout;
+ pod2usage(-exitval => 'NOEXIT', -input => pod_where({-inc => 1}, $_->{package}),
+ -verbose => 99,
+ -sections => $_->{sections});
+ }
+
+ $self->{output}->add_option_msg(long_msg => $stdout) if (defined($stdout));
+ }
+}
+
+sub add_help {
+ my ($self, %options) = @_;
+ # $options{package} = string package
+ # $options{sections} = string sections
+ # $options{help_first} = put at the beginning
+ # $options{once} = put help only one time for a package
+
+ if (defined($options{once}) && defined($self->{pod_packages_once}->{$options{package}})) {
+ return ;
+ }
+
+ if (defined($options{help_first})) {
+ shift @{$self->{pod_package}}, {package => $options{package}, sections => $options{sections}};
+ } else {
+ push @{$self->{pod_package}}, {package => $options{package}, sections => $options{sections}};
+ }
+
+ $self->{pod_packages_once}->{$options{package}} = 1;
+}
+
+sub add_options {
+ my ($self, %options) = @_;
+ # $options{arguments} = ref to hash table with string and name to store (example: { 'mode:s' => { name => 'mode', default => 'defaultvalue' )
+
+ foreach (keys %{$options{arguments}}) {
+ if (defined($options{arguments}->{$_}->{default})) {
+ $self->{options_stored}->{$options{arguments}->{$_}->{name}} = $options{arguments}->{$_}->{default};
+ } else {
+ $self->{options_stored}->{$options{arguments}->{$_}->{name}} = undef;
+ }
+ $self->{options}->{$_} = \$self->{options_stored}->{$options{arguments}->{$_}->{name}};
+ }
+}
+
+sub parse_options {
+ my $self = shift;
+ #%{$self->{options_stored}} = ();
+
+ GetOptions(
+ %{$self->{options}}
+ );
+ %{$self->{options}} = ();
+}
+
+sub get_option {
+ my ($self, %options) = @_;
+
+ return $self->{options_stored}->{$options{argument}};
+}
+
+sub get_options {
+ my $self = shift;
+
+ return $self->{options_stored};
+}
+
+sub clean {
+ my $self = shift;
+
+ $self->{options_stored} = {};
+}
+
+1;
+
+__END__
diff --git a/centreon/plugins/output.pm b/centreon/plugins/output.pm
new file mode 100644
index 000000000..9e73949f9
--- /dev/null
+++ b/centreon/plugins/output.pm
@@ -0,0 +1,656 @@
+################################################################################
+# Copyright 2005-2013 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 .
+#
+# 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 : Quentin Garnier
+#
+####################################################################################
+
+package centreon::plugins::output;
+use Encode;
+use centreon::plugins::misc;
+
+sub new {
+ my ($class, %options) = @_;
+ my $self = {};
+ bless $self, $class;
+ # $options->{options} = options object
+ if (!defined($options{options})) {
+ print "Class Output: Need to specify 'options' argument to load.\n";
+ exit 3;
+ }
+
+ $options{options}->add_options(arguments =>
+ {
+ "ignore-perfdata" => { name => 'ignore_perfdata' },
+ "verbose" => { name => 'verbose' },
+ "opt-exit:s" => { name => 'opt_exit', default => 'unknown' },
+ "output-xml" => { name => 'output_xml' },
+ "output-json" => { name => 'output_json' },
+ "disco-format" => { name => 'disco_format' },
+ "disco-show" => { name => 'disco_show' },
+ });
+ %{$self->{option_results}} = ();
+
+ $self->{option_msg} = [];
+
+ $self->{is_output_xml} = 0;
+ $self->{is_output_json} = 0;
+ $self->{errors} = {OK => 0, WARNING => 1, CRITICAL => 2, UNKNOWN => 3, PENDING => 4};
+ $self->{myerrors} = {0 => "OK", 1 => "WARNING", 3 => "CRITICAL", 7 => "UNKNOWN"};
+ $self->{myerrors_mask} = {CRITICAL => 3, WARNING => 1, UNKNOWN => 7, OK => 0};
+ $self->{global_short_concat_outputs} = {OK => undef, WARNING => undef, CRITICAL => undef, UNKNOWN => undef, UNQUALIFIED_YET => undef};
+ $self->{global_short_outputs} = {OK => [], WARNING => [], CRITICAL => [], UNKNOWN => [], UNQUALIFIED_YET => []};
+ $self->{global_long_output} = [];
+ $self->{perfdatas} = [];
+ $self->{global_status} = 0;
+
+ $self->{disco_elements} = [];
+ $self->{disco_entries} = [];
+
+ $self->{plugin} = '';
+ $self->{mode} = '';
+
+ return $self;
+}
+
+sub check_options {
+ my ($self, %options) = @_;
+ # $options{option_results} = ref to options result
+
+ %{$self->{option_results}} = %{$options{option_results}};
+ $self->{option_results}->{opt_exit} = lc($self->{option_results}->{opt_exit});
+ if (!$self->is_litteral_status(status => $self->{option_results}->{opt_exit})) {
+ $self->add_option_msg(short_msg => "Unknown value '" . $self->{option_results}->{opt_exit} . "' for --opt-exit.");
+ $self->option_exit(exit_litteral => 'unknown');
+ }
+ # Go in XML Mode
+ if ($self->is_disco_show() || $self->is_disco_format()) {
+ # By Default XML
+ if (!defined($self->{option_results}->{output_json})) {
+ $self->{option_results}->{output_xml} = 1;
+ }
+ }
+}
+
+sub add_option_msg {
+ my ($self, %options) = @_;
+ # $options{short_msg} = string msg
+ # $options{long_msg} = string msg
+ $options{severity} = 'UNQUALIFIED_YET';
+
+ $self->output_add(%options);
+}
+
+sub set_status {
+ my ($self, %options) = @_;
+ # $options{exit_litteral} = string litteral exit
+
+ # Nothing to do for 'UNQUALIFIED_YET'
+ if (!$self->{myerrors_mask}->{uc($options{exit_litteral})}) {
+ return ;
+ }
+ $self->{global_status} |= $self->{myerrors_mask}->{uc($options{exit_litteral})};
+}
+
+sub output_add {
+ my ($self, %params) = @_;
+ my %args = (
+ severity => 'OK',
+ separator => ' - ',
+ short_msg => undef,
+ long_msg => undef
+ );
+ my $options = {%args, %params};
+
+ if (defined($options->{short_msg})) {
+ chomp $options->{short_msg};
+ if (defined($self->{global_short_concat_outputs}->{uc($options->{severity})})) {
+ $self->{global_short_concat_outputs}->{uc($options->{severity})} .= $options->{separator} . $options->{short_msg};
+ } else {
+ $self->{global_short_concat_outputs}->{uc($options->{severity})} = $options->{short_msg};
+ }
+
+ push @{$self->{global_short_outputs}->{uc($options->{severity})}}, $options->{short_msg};
+ $self->set_status(exit_litteral => $options->{severity});
+ }
+ if (defined($options->{long_msg})) {
+ chomp $options->{long_msg};
+ push @{$self->{global_long_output}}, $options->{long_msg};
+ }
+}
+
+sub perfdata_add {
+ my ($self, %options) = @_;
+ my $perfdata = {'label' => '', 'value' => '', unit => '', warning => '', critical => '', min => '', max => ''};
+ $perfdata = {%$perfdata, %options};
+ push @{$self->{perfdatas}}, $perfdata;
+}
+
+sub output_json {
+ my ($self, %options) = @_;
+ my $force_ignore_perfdata = defined($options{force_ignore_perfdata}) ? 1 : 0;
+ my $json_content = {plugin => {
+ name => $self->{plugin},
+ mode => $self->{mode},
+ exit => $options{exit_litteral},
+ outputs => [],
+ perfdatas => []
+ }
+ };
+
+ foreach my $code_litteral (keys %{$self->{global_short_outputs}}) {
+ foreach (@{$self->{global_short_outputs}->{$code_litteral}}) {
+ my ($child_output, $child_type, $child_msg, $child_exit);
+ my $lcode_litteral = ($code_litteral eq 'UNQUALIFIED_YET' ? uc($options{exit_litteral}) : $code_litteral);
+
+ push @{$json_content->{plugin}->{outputs}}, {
+ type => 1,
+ msg => ($options{nolabel} == 0 ? ($lcode_litteral . ': ') : '') . $_,
+ exit => $lcode_litteral
+ };
+ }
+ }
+
+ if (defined($self->{option_results}->{verbose}) || defined($options{force_long_output})) {
+ foreach (@{$self->{global_long_output}}) {
+ push @{$json_content->{plugin}->{outputs}}, {
+ type => 2,
+ msg => $_,
+ };
+ }
+ }
+
+ if (!defined($self->{option_results}->{ignore_perfdata}) && $options{force_ignore_perfdata} == 1) {
+ foreach (@{$self->{perfdatas}}) {
+ my %values = ();
+ foreach my $key (keys %$_) {
+ $values{$key} = $_->{$key};
+ }
+
+ push @{$json_content->{plugin}->{perfdatas}}, {
+ %values
+ };
+ }
+ }
+
+ print $self->{json_output}->encode($json_content);
+}
+
+sub output_xml {
+ my ($self, %options) = @_;
+ my $force_ignore_perfdata = defined($options{force_ignore_perfdata}) ? 1 : 0;
+ my ($child_plugin_name, $child_plugin_mode, $child_plugin_exit, $child_plugin_output, $child_plugin_perfdata);
+
+ my $root = $self->{xml_output}->createElement('plugin');
+ $self->{xml_output}->setDocumentElement($root);
+
+ $child_plugin_name = $self->{xml_output}->createElement("name");
+ $child_plugin_name->appendText($self->{plugin});
+
+ $child_plugin_mode = $self->{xml_output}->createElement("mode");
+ $child_plugin_mode->appendText($self->{mode});
+
+ $child_plugin_exit = $self->{xml_output}->createElement("exit");
+ $child_plugin_exit->appendText($options{exit_litteral});
+
+ $child_plugin_output = $self->{xml_output}->createElement("outputs");
+ $child_plugin_perfdata = $self->{xml_output}->createElement("perfdatas");
+
+ $root->addChild($child_plugin_name);
+ $root->addChild($child_plugin_mode);
+ $root->addChild($child_plugin_exit);
+ $root->addChild($child_plugin_output);
+ $root->addChild($child_plugin_perfdata);
+
+ foreach my $code_litteral (keys %{$self->{global_short_outputs}}) {
+ foreach (@{$self->{global_short_outputs}->{$code_litteral}}) {
+ my ($child_output, $child_type, $child_msg, $child_exit);
+ my $lcode_litteral = ($code_litteral eq 'UNQUALIFIED_YET' ? uc($options{exit_litteral}) : $code_litteral);
+
+ $child_output = $self->{xml_output}->createElement("output");
+ $child_plugin_output->addChild($child_output);
+
+ $child_type = $self->{xml_output}->createElement("type");
+ $child_type->appendText(1); # short
+
+ $child_msg = $self->{xml_output}->createElement("msg");
+ $child_msg->appendText(($options{nolabel} == 0 ? ($lcode_litteral . ': ') : '') . $_);
+ $child_exit = $self->{xml_output}->createElement("exit");
+ $child_exit->appendText($lcode_litteral);
+
+ $child_output->addChild($child_type);
+ $child_output->addChild($child_exit);
+ $child_output->addChild($child_msg);
+ }
+ }
+
+ if (defined($self->{option_results}->{verbose}) || defined($options{force_long_output})) {
+ foreach (@{$self->{global_long_output}}) {
+ my ($child_output, $child_type, $child_msg);
+
+ $child_output = $self->{xml_output}->createElement("output");
+ $child_plugin_output->addChild($child_output);
+
+ $child_type = $self->{xml_output}->createElement("type");
+ $child_type->appendText(2); # long
+
+ $child_msg = $self->{xml_output}->createElement("msg");
+ $child_msg->appendText($_);
+
+ $child_output->addChild($child_type);
+ $child_output->addChild($child_msg);
+ }
+ }
+
+ if (!defined($self->{option_results}->{ignore_perfdata}) && $options{force_ignore_perfdata} == 1) {
+ foreach (@{$self->{perfdatas}}) {
+ my ($child_perfdata);
+ $child_perfdata = $self->{xml_output}->createElement("perfdata");
+ $child_plugin_perfdata->addChild($child_perfdata);
+ foreach my $key (keys %$_) {
+ my $child = $self->{xml_output}->createElement($key);
+ $child->appendText($_->{$key});
+ $child_perfdata->addChild($child);
+ }
+ }
+ }
+
+ print $self->{xml_output}->toString(1);
+}
+
+sub output_txt {
+ my ($self, %options) = @_;
+ my $force_ignore_perfdata = defined($options{force_ignore_perfdata}) ? 1 : 0;
+
+ if (defined($self->{global_short_concat_outputs}->{UNQUALIFIED_YET})) {
+ $self->output_add(severity => uc($options{exit_litteral}), short_msg => $self->{global_short_concat_outputs}->{UNQUALIFIED_YET});
+ }
+
+ if (defined($self->{global_short_concat_outputs}->{CRITICAL})) {
+ print (($options{nolabel} == 0 ? 'CRITICAL: ' : '') . $self->{global_short_concat_outputs}->{CRITICAL} . " ");
+ }
+ if (defined($self->{global_short_concat_outputs}->{WARNING})) {
+ print (($options{nolabel} == 0 ? 'WARNING: ' : '') . $self->{global_short_concat_outputs}->{WARNING} . " ");
+ }
+ if (defined($self->{global_short_concat_outputs}->{UNKNOWN})) {
+ print (($options{nolabel} == 0 ? 'UNKNOWN: ' : '') . $self->{global_short_concat_outputs}->{UNKNOWN} . " ");
+ }
+ if (uc($options{exit_litteral}) eq 'OK') {
+ print (($options{nolabel} == 0 ? 'OK: ' : '') . $self->{global_short_concat_outputs}->{OK});
+ }
+
+ if ($force_ignore_perfdata == 1 || defined($self->{option_results}->{ignore_perfdata})) {
+ print "\n";
+ } else {
+ print "|";
+ foreach (@{$self->{perfdatas}}) {
+ print " '" . $_->{label} . "'=" . $_->{value} . $_->{unit} . ";" . $_->{warning} . ";" . $_->{critical} . ";" . $_->{min} . ";" . $_->{max} . ";";
+ }
+ print "\n";
+ }
+
+ if (defined($self->{option_results}->{verbose}) || defined($options{force_long_output})) {
+ if (scalar(@{$self->{global_long_output}})) {
+ print join("\n", @{$self->{global_long_output}});
+ print "\n";
+ }
+ }
+}
+
+sub display {
+ my ($self, %options) = @_;
+ my $nolabel = defined($options{nolabel}) ? 1 : 0;
+ my $force_ignore_perfdata = defined($options{force_ignore_perfdata}) ? 1 : 0;
+
+ if (defined($self->{option_results}->{output_xml})) {
+ $self->create_xml_document();
+ if ($self->{is_output_xml}) {
+ $self->output_xml(exit_litteral => $self->get_litteral_status(),
+ nolabel => $nolabel, force_ignore_perfdata => $force_ignore_perfdata);
+ return ;
+ }
+ } elsif (defined($self->{option_results}->{output_json})) {
+ $self->create_json_document();
+ if ($self->{is_output_json}) {
+ $self->output_json(exit_litteral => $self->get_litteral_status(),
+ nolabel => $nolabel, force_ignore_perfdata => $force_ignore_perfdata);
+ return ;
+ }
+ }
+
+ $self->output_txt(exit_litteral => $self->get_litteral_status(),
+ nolabel => $nolabel, force_ignore_perfdata => $force_ignore_perfdata);
+}
+
+sub die_exit {
+ my ($self, %options) = @_;
+ # $options{exit_litteral} = string litteral exit
+ # $options{nolabel} = interger label display
+ my $exit_litteral = defined($options{exit_litteral}) ? $options{exit_litteral} : $self->{option_results}->{opt_exit};
+ my $nolabel = defined($options{nolabel}) ? 1 : 0;
+ # ignore long output in the following case
+ $self->{option_results}->{verbose} = undef;
+
+ if (defined($self->{option_results}->{output_xml})) {
+ $self->create_xml_document();
+ if ($self->{is_output_xml}) {
+ $self->output_xml(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+ }
+ } elsif (defined($self->{option_results}->{output_json})) {
+ $self->create_json_document();
+ if ($self->{is_output_json}) {
+ $self->output_json(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+ }
+ }
+
+ $self->output_txt(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+}
+
+sub option_exit {
+ my ($self, %options) = @_;
+ # $options{exit_litteral} = string litteral exit
+ # $options{nolabel} = interger label display
+ my $exit_litteral = defined($options{exit_litteral}) ? $options{exit_litteral} : $self->{option_results}->{opt_exit};
+ my $nolabel = defined($options{nolabel}) ? 1 : 0;
+
+ if (defined($self->{option_results}->{output_xml})) {
+ $self->create_xml_document();
+ if ($self->{is_output_xml}) {
+ $self->output_xml(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1, force_long_output => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+ }
+ } elsif (defined($self->{option_results}->{output_json})) {
+ $self->create_json_document();
+ if ($self->{is_output_json}) {
+ $self->output_json(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1, force_long_output => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+ }
+ }
+
+ $self->output_txt(exit_litteral => $exit_litteral, nolabel => $nolabel, force_ignore_perfdata => 1, force_long_output => 1);
+ $self->exit(exit_litteral => $exit_litteral);
+}
+
+sub exit {
+ my ($self, %options) = @_;
+ # $options{exit_litteral} = exit
+
+ if (defined($options{exit_litteral})) {
+ exit $self->{errors}->{uc($options{exit_litteral})};
+ }
+ exit $self->{errors}->{$self->{myerrors}->{$self->{global_status}}};
+}
+
+sub get_most_critical {
+ my ($self, %options) = @_;
+ my $current_status = 0; # For 'OK'
+
+ foreach (@{$options{status}}) {
+ if ($self->{myerrors_mask}->{uc($_)} > $current_status) {
+ $current_status = $self->{myerrors_mask}->{uc($_)};
+ }
+ }
+ return $self->{myerrors}->{$current_status};
+}
+
+sub get_litteral_status {
+ my ($self, %options) = @_;
+
+ if (defined($options{status})) {
+ if (defined($self->{myerrors}->{$options{status}})) {
+ return $self->{myerrors}->{$options{status}};
+ }
+ return $options{status};
+ } else {
+ return $self->{myerrors}->{$self->{global_status}};
+ }
+}
+
+sub is_status {
+ my ($self, %options) = @_;
+ # $options{value} = string status
+ # $options{litteral} = value is litteral
+ # $options{compare} = string status
+
+ if (defined($options{litteral})) {
+ my $value = defined($options{value}) ? $options{value} : $self->get_litteral_status();
+
+ if (uc($value) eq uc($options{compare})) {
+ return 1;
+ }
+ return 0;
+ }
+
+ my $value = defined($options{value}) ? $options{value} : $self->{global_status};
+ my $dec_val = $self->{myerrors_mask}->{$value};
+ my $lresult = $value & $dec_val;
+ # Need to manage 0
+ if ($lresult > 0 || ($dec_val == 0 && $value == 0)) {
+ return 1;
+ }
+ return 0;
+}
+
+sub is_litteral_status {
+ my ($self, %options) = @_;
+ # $options{status} = string status
+
+ if (defined($self->{errors}->{uc($options{status})})) {
+ return 1;
+ }
+
+ return 0;
+}
+
+sub create_json_document {
+ my ($self) = @_;
+
+ require JSON;
+ $self->{is_output_json} = 1;
+ $self->{json_output} = JSON->new->utf8();
+}
+
+sub create_xml_document {
+ my ($self) = @_;
+
+ require XML::LibXML;
+ $self->{is_output_xml} = 1;
+ $self->{xml_output} = XML::LibXML::Document->new('1.0', 'utf-8');
+}
+
+sub plugin {
+ my ($self, %options) = @_;
+ # $options{name} = string name
+
+ if (defined($options{name})) {
+ $self->{plugin} = $options{name};
+ }
+ return $self->{plugin};
+}
+
+sub mode {
+ my ($self, %options) = @_;
+ # $options{name} = string name
+
+ if (defined($options{name})) {
+ $self->{mode} = $options{name};
+ }
+ return $self->{mode};
+}
+
+sub add_disco_format {
+ my ($self, %options) = @_;
+
+ push @{$self->{disco_elements}}, @{$options{elements}};
+}
+
+sub display_disco_format {
+ my ($self, %options) = @_;
+
+ if (defined($self->{option_results}->{output_xml})) {
+ $self->create_xml_document();
+
+ my $root = $self->{xml_output}->createElement('data');
+ $self->{xml_output}->setDocumentElement($root);
+
+ foreach (@{$self->{disco_elements}}) {
+ my $child = $self->{xml_output}->createElement("element");
+ $child->appendText($_);
+ $root->addChild($child);
+ }
+
+ print $self->{xml_output}->toString(1);
+ } elsif (defined($self->{option_results}->{output_json})) {
+ $self->create_json_document();
+ my $json_content = {data => [] };
+ foreach (@{$self->{disco_elements}}) {
+ push @{$json_content->{data}}, $_;
+ }
+
+ print $self->{json_output}->encode($json_content);
+ }
+}
+
+sub display_disco_show {
+ my ($self, %options) = @_;
+
+ if (defined($self->{option_results}->{output_xml})) {
+ $self->create_xml_document();
+
+ my $root = $self->{xml_output}->createElement('data');
+ $self->{xml_output}->setDocumentElement($root);
+
+ foreach (@{$self->{disco_entries}}) {
+ my $child = $self->{xml_output}->createElement("label");
+ foreach my $key (keys %$_) {
+ $child->setAttribute($key, $_->{$key});
+ }
+ $root->addChild($child);
+ }
+
+ print $self->{xml_output}->toString(1);
+ } elsif (defined($self->{option_results}->{output_json})) {
+ $self->create_json_document();
+ my $json_content = {data => [] };
+ foreach (@{$self->{disco_entries}}) {
+ my %values = ();
+ foreach my $key (keys %$_) {
+ $values{$key} = $_->{$key};
+ }
+ push @{$json_content->{data}}, {%values};
+ }
+
+ print $self->{json_output}->encode($json_content);
+ }
+}
+
+sub to_utf8 {
+ my ($self, $value) = @_;
+
+ return centreon::plugins::misc::trim(Encode::decode('UTF-8', $value, Encode::PERLQQ));
+}
+
+sub add_disco_entry {
+ my ($self, %options) = @_;
+
+ push @{$self->{disco_entries}}, {%options};
+}
+
+sub is_disco_format {
+ my ($self) = @_;
+
+ if (defined($self->{option_results}->{disco_format}) ) {
+ return 1;
+ }
+ return 0;
+}
+
+sub is_disco_show {
+ my ($self) = @_;
+
+ if ( defined($self->{option_results}->{disco_show}) ) {
+ return 1;
+ }
+ return 0;
+}
+
+1;
+
+__END__
+
+=head1 NAME
+
+Output class
+
+=head1 SYNOPSIS
+
+-
+
+=head1 OUTPUT OPTIONS
+
+=over 8
+
+=item B<--verbose>
+
+Display long output.
+
+=item B<--ignore-perfdata>
+
+Don't display perfdata.
+
+=item B<--opt-exit>
+
+Exit code for an option error, usage (default: unknown).
+
+=item B<--output-xml>
+
+Display output in XML Format.
+
+=item B<--output-json>
+
+Display output in JSON Format.
+
+=item B<--disco-format>
+
+Display discovery arguments (if the mode manages it).
+
+=item B<--disco-show>
+
+Display discovery values (if the mode manages it).
+
+=head1 DESCRIPTION
+
+B