2014-07-24 17:37:33 +02:00
#
2020-01-06 15:19:23 +01:00
# Copyright 2020 Centreon (http://www.centreon.com/)
2015-07-21 11:51:02 +02:00
#
# 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.
#
2014-07-24 17:37:33 +02:00
package storage::hp::p2000::xmlapi::custom ;
use strict ;
use warnings ;
2015-07-28 18:17:04 +02:00
use centreon::plugins::http ;
2014-07-24 17:37:33 +02:00
use XML::XPath ;
use Digest::MD5 qw( md5_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 } ) ) {
2019-03-06 16:47:46 +01:00
$ options { options } - > add_options ( arguments = > {
2019-06-06 15:17:29 +02:00
'hostname:s@' = > { name = > 'hostname' } ,
'port:s@' = > { name = > 'port' } ,
'proto:s@' = > { name = > 'proto' } ,
'urlpath:s@' = > { name = > 'url_path' } ,
'username:s@' = > { name = > 'username' } ,
'password:s@' = > { name = > 'password' } ,
'timeout:s@' = > { name = > 'timeout' } ,
2019-09-24 14:50:37 +02:00
'unknown-http-status:s' = > { name = > 'unknown_http_status' } ,
'warning-http-status:s' = > { name = > 'warning_http_status' } ,
2020-07-01 18:15:11 +02:00
'critical-http-status:s' = > { name = > 'critical_http_status' }
2019-03-06 16:47:46 +01:00
} ) ;
2014-07-24 17:37:33 +02:00
}
$ options { options } - > add_help ( package = > __PACKAGE__ , sections = > 'P2000 OPTIONS' , once = > 1 ) ;
2019-03-06 16:47:46 +01:00
$ self - > { http } = centreon::plugins::http - > new ( % options ) ;
2014-07-24 17:37:33 +02:00
$ self - > { output } = $ options { output } ;
$ self - > { session_id } = '' ;
$ self - > { logon } = 0 ;
return $ self ;
}
sub set_options {
my ( $ self , % options ) = @ _ ;
$ self - > { option_results } = $ options { option_results } ;
}
2020-07-01 18:15:11 +02:00
sub set_defaults { }
2014-07-24 17:37:33 +02:00
sub check_options {
my ( $ self , % options ) = @ _ ;
$ self - > { hostname } = ( defined ( $ self - > { option_results } - > { hostname } ) ) ? shift ( @ { $ self - > { option_results } - > { hostname } } ) : 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 ;
2014-07-25 18:02:16 +02:00
$ self - > { timeout } = ( defined ( $ self - > { option_results } - > { timeout } ) ) ? shift ( @ { $ self - > { option_results } - > { timeout } } ) : 45 ;
2014-07-24 17:37:33 +02:00
$ self - > { port } = ( defined ( $ self - > { option_results } - > { port } ) ) ? shift ( @ { $ self - > { option_results } - > { port } } ) : undef ;
$ self - > { proto } = ( defined ( $ self - > { option_results } - > { proto } ) ) ? shift ( @ { $ self - > { option_results } - > { proto } } ) : 'http' ;
$ self - > { url_path } = ( defined ( $ self - > { option_results } - > { url_path } ) ) ? shift ( @ { $ self - > { option_results } - > { url_path } } ) : '/api/' ;
2019-09-24 14:50:37 +02:00
$ 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 } : '' ;
2015-07-28 18:17:04 +02:00
2014-07-24 17:37:33 +02:00
if ( ! defined ( $ self - > { hostname } ) ) {
2019-06-06 15:17:29 +02:00
$ self - > { output } - > add_option_msg ( short_msg = > 'Need to specify hostname option.' ) ;
2014-07-24 17:37:33 +02:00
$ self - > { output } - > option_exit ( ) ;
}
if ( ! defined ( $ self - > { username } ) || ! defined ( $ self - > { password } ) ) {
$ self - > { output } - > add_option_msg ( short_msg = > 'Need to specify username or/and password option.' ) ;
$ self - > { output } - > option_exit ( ) ;
}
if ( ! defined ( $ self - > { hostname } ) ||
scalar ( @ { $ self - > { option_results } - > { hostname } } ) == 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 } = $ self - > { port } ;
$ self - > { option_results } - > { proto } = $ self - > { proto } ;
$ self - > { option_results } - > { url_path } = $ self - > { url_path } ;
}
sub check_login {
my ( $ self , % options ) = @ _ ;
my ( $ xpath , $ nodeset ) ;
eval {
$ xpath = XML::XPath - > new ( xml = > $ options { content } ) ;
$ nodeset = $ xpath - > find ( "//OBJECT[\@basetype='status']//PROPERTY[\@name='return-code']" ) ;
} ;
if ( $@ ) {
$ self - > { output } - > add_option_msg ( short_msg = > "Cannot parse login response: $@" ) ;
$ self - > { output } - > option_exit ( ) ;
}
if ( scalar ( $ nodeset - > get_nodelist ) == 0 ) {
$ self - > { output } - > output_add ( severity = > 'UNKNOWN' ,
short_msg = > 'Cannot find login response.' ) ;
$ self - > { output } - > display ( ) ;
$ self - > { output } - > exit ( ) ;
}
foreach my $ node ( $ nodeset - > get_nodelist ( ) ) {
if ( $ node - > string_value != 1 ) {
$ self - > { output } - > output_add ( severity = > 'UNKNOWN' ,
short_msg = > 'Login authentification failed (return-code: ' . $ node - > string_value . ').' ) ;
$ self - > { output } - > display ( ) ;
$ self - > { output } - > exit ( ) ;
}
}
$ nodeset = $ xpath - > find ( "//OBJECT[\@basetype='status']//PROPERTY[\@name='response']" ) ;
my @ nodes = $ nodeset - > get_nodelist ( ) ;
my $ node = shift ( @ nodes ) ;
$ self - > { session_id } = $ node - > string_value ;
$ self - > { logon } = 1 ;
}
sub DESTROY {
my $ self = shift ;
if ( $ self - > { logon } == 1 ) {
2019-09-24 14:50:37 +02:00
$ self - > { http } - > request (
url_path = > $ self - > { url_path } . 'exit' ,
header = > [
'Cookie: wbisessionkey=' . $ self - > { session_id } . '; wbiusername=' . $ self - > { username } ,
'dataType: api' , 'sessionKey: ' . $ self - > { session_id }
] ,
unknown_status = > $ self - > { unknown_http_status } ,
warning_status = > $ self - > { warning_http_status } ,
critical_status = > $ self - > { critical_http_status } ,
) ;
2014-07-24 17:37:33 +02:00
}
}
2014-07-25 18:02:16 +02:00
sub get_infos {
my ( $ self , % options ) = @ _ ;
my ( $ xpath , $ nodeset ) ;
2019-04-18 15:32:38 +02:00
$ self - > login ( ) ;
2014-07-25 18:02:16 +02:00
my $ cmd = $ options { cmd } ;
$ cmd =~ s/ /\//g ;
2019-09-26 10:17:09 +02:00
my ( $ unknown_status , $ warning_status , $ critical_status ) = ( $ self - > { unknown_http_status } , $ self - > { warning_http_status } , $ self - > { critical_http_status } ) ;
if ( defined ( $ options { no_quit } ) && $ options { no_quit } == 1 ) {
( $ unknown_status , $ warning_status , $ critical_status ) = ( '' , '' , '' ) ;
}
2019-09-24 14:50:37 +02:00
my $ response = $ self - > { http } - > request (
url_path = > $ self - > { url_path } . $ cmd ,
header = > [
'Cookie: wbisessionkey=' . $ self - > { session_id } . '; wbiusername=' . $ self - > { username } ,
'dataType: api' , 'sessionKey: ' . $ self - > { session_id }
] ,
2019-09-26 10:17:09 +02:00
unknown_status = > $ unknown_status ,
warning_status = > $ warning_status ,
critical_status = > $ critical_status ,
2019-09-24 14:50:37 +02:00
) ;
2014-07-25 18:02:16 +02:00
eval {
$ xpath = XML::XPath - > new ( xml = > $ response ) ;
$ nodeset = $ xpath - > find ( "//OBJECT[\@basetype='" . $ options { base_type } . "']" ) ;
} ;
if ( $@ ) {
2019-09-26 10:17:09 +02:00
return ( { } , 0 ) if ( defined ( $ options { no_quit } ) && $ options { no_quit } == 1 ) ;
2014-07-25 18:02:16 +02:00
$ self - > { output } - > add_option_msg ( short_msg = > "Cannot parse 'cmd' response: $@" ) ;
$ self - > { output } - > option_exit ( ) ;
}
2017-05-16 14:54:46 +02:00
# Check if there is an error
#<OBJECT basetype="status" name="status" oid="1">
#<PROPERTY name="response-type" type="enumeration" size="12" draw="false" sort="nosort" display-name="Response Type">Error</PROPERTY>
#<PROPERTY name="response-type-numeric" type="enumeration" size="12" draw="false" sort="nosort" display-name="Response">1</PROPERTY>
#<PROPERTY name="response" type="string" size="180" draw="true" sort="nosort" display-name="Response">The command is ambiguous. Please check the help for this command.</PROPERTY>
#<PROPERTY name="return-code" type="int32" size="5" draw="false" sort="nosort" display-name="Return Code">-10028</PROPERTY>
#<PROPERTY name="component-id" type="string" size="80" draw="false" sort="nosort" display-name="Component ID"></PROPERTY>
#</OBJECT>
if ( my $ nodestatus = $ xpath - > find ( "//OBJECT[\@basetype='status']//PROPERTY[\@name='return-code']" ) ) {
my @ nodes = $ nodestatus - > get_nodelist ( ) ;
my $ node = shift @ nodes ;
my $ return_code = $ node - > string_value ;
if ( $ return_code != 0 ) {
$ nodestatus = $ xpath - > find ( "//OBJECT[\@basetype='status']//PROPERTY[\@name='response']" ) ;
@ nodes = $ nodestatus - > get_nodelist ( ) ;
2019-06-06 15:17:29 +02:00
$ node = shift @ nodes ;
return ( { } , 0 , $ node - > string_value ) if ( defined ( $ options { no_quit } ) && $ options { no_quit } == 1 ) ;
2017-05-16 14:54:46 +02:00
$ self - > { output } - > add_option_msg ( short_msg = > $ node - > string_value ) ;
$ self - > { output } - > option_exit ( ) ;
}
}
2014-07-25 18:02:16 +02:00
my $ results = { } ;
foreach my $ node ( $ nodeset - > get_nodelist ( ) ) {
my $ properties = { } ;
foreach my $ prop_node ( $ node - > getChildNodes ( ) ) {
my $ prop_name = $ prop_node - > getAttribute ( 'name' ) ;
if ( defined ( $ prop_name ) && ( $ prop_name eq $ options { key } ||
$ prop_name =~ /$options{properties_name}/ ) ) {
$ properties - > { $ prop_name } = $ prop_node - > string_value ;
}
}
if ( defined ( $ properties - > { $ options { key } } ) ) {
$ results - > { $ properties - > { $ options { key } } } = $ properties ;
}
}
2019-06-06 15:17:29 +02:00
return ( $ results , 1 ) ;
2014-07-25 18:02:16 +02:00
}
2014-07-24 17:37:33 +02:00
##############
# Specific methods
##############
sub login {
my ( $ self , % options ) = @ _ ;
2019-04-18 15:32:38 +02:00
return if ( $ self - > { logon } == 1 ) ;
2014-07-24 17:37:33 +02:00
$ self - > build_options_for_httplib ( ) ;
2015-07-28 18:17:04 +02:00
$ self - > { http } - > set_options ( % { $ self - > { option_results } } ) ;
2014-07-24 17:37:33 +02:00
# Login First
my $ md5_hash = md5_hex ( $ self - > { username } . '_' . $ self - > { password } ) ;
2019-09-24 14:50:37 +02:00
my $ response = $ self - > { http } - > request (
url_path = > $ self - > { url_path } . 'login/' . $ md5_hash ,
unknown_status = > $ self - > { unknown_http_status } ,
warning_status = > $ self - > { warning_http_status } ,
critical_status = > $ self - > { critical_http_status } ,
) ;
2014-07-24 17:37:33 +02:00
$ self - > check_login ( content = > $ response ) ;
}
1 ;
__END__
= head1 NAME
MSA p2000
= head1 SYNOPSIS
my p2000 xml api manage
= head1 P2000 OPTIONS
= over 8
= item B <--hostname>
HP p2000 Hostname .
= item B <--port>
Port used
= item B <--proto>
Specify https if needed
= item B <--urlpath>
Set path to xml api ( Default: '/api/' )
= item B <--username>
Username to connect .
= item B <--password>
Password to connect .
= item B <--timeout>
Set HTTP timeout
= back
= head1 DESCRIPTION
B <custom> .
2019-03-06 16:47:46 +01:00
= cut