NAME

SU::API::Icinga2

SYNOPSIS

use strict;
use warnings;

use SU::API::Icinga2;

my $icinga = new SU::API::Icinga2("fqdn","port","path","version");
$icinga->login("username","password") or die $icinga->login_status;

my $host_object = {
                    'attrs' => {
                                 'address'       => "127.0.0.1",
                                 'check_command' => "hostalive"
                               }
                   };


my $put_result = $icinga->do_request("PUT", "/objects/hosts/localhost.example.com", "", $host_object);
if (!$put_result) {
    warn $icinga->request_code;
    warn $icinga->request_status_line;
};

my $post_result = $icinga->do_request("POST", "/objects/hosts/localhost.example.com", "",$host_object);
if (!$post_result) {
    warn $icinga->request_code;
    warn $icinga->request_status_line;
};

my $get_result = $icinga->do_request("GET", "/objects/hosts/localhost.example.com");
if (!$get_result) {
    warn $icinga->request_code;
    warn $icinga->request_status_line;
};

my $delete_result = $icinga->do_request("DELETE", "/objects/hosts/localhost.example.com");
if (!$delete_result) {
    warn $icinga->request_code;
    warn $icinga->request_status_line;
};

$icinga->logout();

DESCRIPTION

Module used to integrate with the REST API that Icinga2 provides.

CONSTRUCTOR

new (FQDN, PORT, PATH, VERSION, INSECURE)

Returns a new SU::API::Icinga object.

"PATH" is the URI where to find the API. Usually "/".

"VERSION" should be set to the API version. Usually "1".

"INSECURE" is optional and makes this module skip certificate verification if set.

METHODS

login (USERNAME, PASSWORD)

Configure and validate credantials. Please note that user needs at least the permission "status/query"

logout

Destroy the login session.

login_status

Returns a human readable login status of the current session.

logged_in

Returns true when logged in, otherwise returns undef.

do_request (HTTP METHOD, URI, PARAMS, DATA)

Perform the actual request.

"PARAMS" is optional and can be any parameters that the Icinga API supports.

"DATA" is optional and is a Perl data structure that will be converted to JSON.

Returns the result in a perl reference.

export (FULL, API_ONLY)

Returns a Perl Data structure without unneccessary meta data. $icinga->export can only be called after do_request. The name of the object is stored in $object->{attrs}{vars}{__export_name} so that you can access if you need it, please note that you can't use this variable name for other things if you whis to use this library as it will be overwritten in the exported object.

"FULL" is optional and if it is true, write once attributes will be included in the export. Objects exported with FULL set to true can be converted to json and be used for PUT requests, but not neccessarily for POST requests.

"API_ONLY" is optional and if it is true only objects created via the API will be exported.

request_code

Returns the last HTTP status code.

request_status_line

Returns the last HTTP status line.

DEPENDS

LWP::UserAgent, HTTP::Request, URI::Escape, JSON, Encode

LICENSE

Copyright (c) 2016-, IT Services, Stockholm University

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

Neither the name of Stockholm University nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

AUTHOR

Johan Carlquist <jocar@su.se>, Mikael Nordin <mikael.nordin@su.se>