Monitoring-Icinga2-Client-REST-2.0.0/000755 000765 000024 00000000000 13117770365 017352 5ustar00jocarstaff000000 000000 Monitoring-Icinga2-Client-REST-2.0.0/lib/000755 000765 000024 00000000000 13117770365 020120 5ustar00jocarstaff000000 000000 Monitoring-Icinga2-Client-REST-2.0.0/Makefile.PL000644 000765 000024 00000001454 13117770161 021322 0ustar00jocarstaff000000 000000 use warnings;
use strict;
use ExtUtils::MakeMaker;
WriteMakefile(
NAME => "Monitoring::Icinga2::Client::REST",
ABSTRACT_FROM => "lib/Monitoring/Icinga2/Client/REST.pod",
AUTHOR => ['Johan Carlquist ','Mikael Nordin "lib/Monitoring/Icinga2/Client/REST.pm",
PREREQ_PM => {
"Carp" => 0,
"Encode" => 0,
"ExtUtils::MakeMaker" => 0,
"HTTP::Request" => 0,
"JSON" => 0,
"LWP::UserAgent" => 0,
"Scalar::Util" => 0,
"URI::Escape" => 0,
"strict" => 0,
"warnings" => 0,
},
LICENSE => "bsd",
INSTALLDIRS => "$]" < 5.011 ? "perl" : "site",
);
1;
Monitoring-Icinga2-Client-REST-2.0.0/MANIFEST000644 000765 000024 00000000452 13117770365 020504 0ustar00jocarstaff000000 000000 lib/Monitoring/Icinga2/Client/REST.pm
lib/Monitoring/Icinga2/Client/REST.pod
Makefile.PL
MANIFEST This list of files
README.pod
META.yml Module YAML meta-data (added by MakeMaker)
META.json Module JSON meta-data (added by MakeMaker)
Monitoring-Icinga2-Client-REST-2.0.0/META.json000644 000765 000024 00000002302 13117770365 020770 0ustar00jocarstaff000000 000000 {
"abstract" : "REST integration with icinga2",
"author" : [
"Johan Carlquist ",
"Mikael Nordin '
- 'Mikael Nordin $port,
path => $path,
version => $version,
cacert => $cacert_file,
);
$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);
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $post_result = $icinga->do_request("POST", "/objects/hosts/localhost.example.com", "",$host_object);
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $get_result = $icinga->do_request("GET", "/objects/hosts/localhost.example.com");
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $delete_result = $icinga->do_request("DELETE", "/objects/hosts/localhost.example.com");
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
$icinga->logout;
=head1 DESCRIPTION
Module used to integrate with the REST API that Icinga2 provides.
=head1 CONSTRUCTOR
=over
=item new (FQDN, PORT, PATH, VERSION, INSECURE)
=item new (FQDN, PORT, PATH, VERSION, [ INSECURE ])
Monitoring::Icinga2::Client::REST object.
The former calling convention with four mandatory arguments is the old one. If a
port number is present after the Icinga host, the constructor assumes you want
this form. The arguments are:
=over
=item * FQDN is your Icinga monitoring host.
=item * PORT is the port the REST API runs on.
=item * PATH is the URI where to find the API, usually "/".
=item * VERSION should be set to the API version, usually 1.
=item * INSECURE is optional and makes this module skip certificate verification if set.
=back
The latter form treats only the FQDN as mandatory and allows all other arguments
to be passed hash style with the following keys:
=over
=item * C see above; defaults to C<5665>.
=item * C see above; defaults to C.
=item * C see above; defaults to 1 and is therefore completely
redundant as long as there is only one API version.
=item * C see above; defaults to false
=item * C The file name of a PEM-format file containing the certificate
of the CA that issued the server's HTTPS certificate. As internal monitoring
servers often use certs from in-house CAs, this still allows for secure peer
identification instead of using insecure mode.
=back
=back
=head1 METHODS
=over
=item login (USERNAME, PASSWORD)
Configures and validates credentials. Please note that user needs at least the permission
"status/query". This methos does nothing if it has been called on the sameobject
before unless L was called before.
=item logout
Destroys the login session.
=item login_status
Returns a human readable login status of the current session.
=item logged_in
Returns true when logged in, otherwise returns undef.
=item do_request (HTTP METHOD, URI, PARAMS, DATA)
Performs 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.
=item 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.
=item request_code
Returns the last HTTP status code.
=item request_status_line
Returns the last HTTP status line.
=back
=head1 DEPENDS
L,
L,
L,
L,
L
=head1 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.
=head1 AUTHOR
Johan Carlquist ,
Mikael Nordin
Bits and pieces by Matthias Bethke
Monitoring-Icinga2-Client-REST-2.0.0/lib/Monitoring/000755 000765 000024 00000000000 13117770365 022245 5ustar00jocarstaff000000 000000 Monitoring-Icinga2-Client-REST-2.0.0/lib/Monitoring/Icinga2/000755 000765 000024 00000000000 13117770365 023521 5ustar00jocarstaff000000 000000 Monitoring-Icinga2-Client-REST-2.0.0/lib/Monitoring/Icinga2/Client/000755 000765 000024 00000000000 13117770365 024737 5ustar00jocarstaff000000 000000 Monitoring-Icinga2-Client-REST-2.0.0/lib/Monitoring/Icinga2/Client/REST.pm000644 000765 000024 00000014544 13117767074 026065 0ustar00jocarstaff000000 000000 package Monitoring::Icinga2::Client::REST;
use strict;
use warnings;
use 5.010_001;
use Carp;
use LWP::UserAgent;
use HTTP::Request;
use URI::Escape;
use JSON;
use Encode qw( encode_utf8 );
use Scalar::Util 'looks_like_number';
use version 0.77; our $VERSION = version->declare('2.0.0');
sub new {
my ($class, $hostname, $port, $path, $version, $insecure) = @_;
my $cafile;
defined $hostname
or croak "hostname argument is required";
unless( looks_like_number($port) ) {
# enable hash-style arg passing
my %args = @_[2..$#_];
$port = $args{port};
$path = $args{path};
$version = $args{version};
$insecure = $args{insecure};
if(defined $args{cacert}) {
# Set ENV variable in case we're using Net::SSL for LWP's HTTPS
$cafile = $ENV{HTTPS_CA_FILE} = $args{cacert};
}
}
my $self = bless {
hostname => $hostname,
port => $port // 5665,
path => $path // '/',
version => $version // 1,
}, $class;
$self->{path} .= "/" unless $self->{path} =~ /\/$/;
$self->{url} = sprintf(
"https://%s:%d%sv%d",
@$self{qw/ hostname port path version /}
);
$self->{ua} = LWP::UserAgent->new(
$insecure ? (
ssl_opts => {
# Don't verify certs with either SSL module used by LWP
verify_hostname => 0,
SSL_verify_callback => sub { 1 },
# Set ca_file for IO::Socket::SSL
defined $cafile ? ( SSL_ca_file => $cafile) : (),
},
) : (),
);
$self->{ua}->default_header( 'Accept' => 'application/json' );
$self->{login_status} = "not logged in";
return $self;
}
sub do_request {
my ( $self, $method, $uri, $params, $data, $plaintext ) = @_;
my $request_url = "$self->{url}/$uri";
$request_url .= '?' . _encode_params($params) if $params;
my $req = HTTP::Request->new( $method => $request_url );
if ($data) {
$data = encode_json($data);
$req->content($data);
}
$self->{res} = $self->{ua}->request($req);
return unless $self->{res}->is_success;
# Try with first plaintext if plaintext is set
if ($plaintext) {
my $str = encode_utf8( $self->{res}->content );
if ($str) {
return $str;
}
}
# Handle non utf8 chars
my $json_result = decode_json( encode_utf8( $self->{res}->content ) );
if ($json_result) {
return $json_result;
}
return;
}
sub _encode_params {
return join '&', map { _encode_param($_) } split /&/, shift;
}
sub _encode_param {
return join '=', map { uri_escape( $_ ) } split /=/, shift;
}
{
my %type2keys = (
CheckCommand => [
[ qw/ arguments command env vars timeout / ],
[ qw/ templates zone / ],
],
Host => [
[ qw/ address6 address check_command display_name event_command
action_url notes notes_url vars icon_image icon_image_alt
check_interval max_check_attempts retry_interval /
],
[ qw/ check_period check_timeout enable_active_checks
enable_event_handler enable_flapping enable_notifications
enable_passive_checks enable_perfdata groups notes
retry_interval templates zone /
],
],
HostGroup => [
[ qw/ action_url display_name notes notes_url vars / ],
[ qw/ groups templates zone / ],
],
Service => [
[ qw/ vars action_url check_command check_interval display_name
notes notes_url event_command max_check_attempts
retry_interval /
],
[ qw/ check_period check_timeout enable_active_checks
enable_event_handler enable_flapping enable_notifications
enable_passive_checks enable_perfdata groups icon_image
icon_image_alt notes templates zone /
],
],
ServiceGroup => [
[ qw/ action_url display_name notes notes_url vars / ],
[ qw/ groups templates zone / ],
],
TimePeriod => [
[ qw/ display_name excludes includes prefer_includes
ranges vars /
],
[ qw/ templates zone / ],
],
User => [
[ qw/ display_name email enable_notifications pager states
period vars /
],
[ qw/ groups templates zone / ],
],
UserGroup => [
[ qw/ display_name vars / ],
[ qw/ groups templates zone / ],
],
);
sub export {
my ( $self, $full, $api_only ) = @_;
my $result = decode_json( encode_utf8( $self->{res}->content ) );
my $type = $result->{results}[0]{type};
# We only support certain object types
return unless exists $type2keys{$type};
my @keys = @{$type2keys{$type}[0]};
push @keys, @{$type2keys{$type}[1]} if $full;
my @results;
foreach my $object ( @{ $result->{results} } ) {
next if $api_only and $object->{attrs}{package} ne "_api";
# The object needs to know its own name stored in a read/write field
$object->{attrs}{vars}{__export_name} = $object->{attrs}{__name};
my %attrs;
@attrs{@keys} = @{$object->{attrs}}{@keys};
push @results, { attrs => \%attrs };
}
return \@results;
}
}
sub login {
my ( $self, $username, $password ) = @_;
return if $self->{logged_in};
$self->{ua}->credentials( "$self->{hostname}:$self->{port}",
"Icinga 2", $username, $password );
$self->do_request( "GET", "/status", "", "" );
if ( $self->request_code == 200 ) {
$self->{login_status} = "login successful";
$self->{logged_in} = 1;
}
elsif ( $self->request_code == 401 ) {
$self->{login_status} = "wrong username/password";
}
else {
$self->{login_status} =
"unknown status line: " . $self->{res}->status_line;
}
return $self->{logged_in};
}
sub logout { shift->{logged_in} = undef; }
sub request_code { return shift->{res}->code; }
sub request_status_line { return shift->{res}->status_line; }
sub logged_in { return shift->{logged_in}; }
sub login_status { return shift->{login_status}; }
1;
Monitoring-Icinga2-Client-REST-2.0.0/lib/Monitoring/Icinga2/Client/REST.pod000644 000765 000024 00000013756 13117767650 026237 0ustar00jocarstaff000000 000000 =head1 NAME
Monitoring::Icinga2::Client::REST - REST integration with icinga2
=head1 SYNOPSIS
use strict;
use warnings;
use Monitoring::Icinga2::Client::REST;
my $icinga = new Monitoring::Icinga2::Client::REST("fqdn",
port => $port,
path => $path,
version => $version,
cacert => $cacert_file,
);
$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);
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $post_result = $icinga->do_request("POST", "/objects/hosts/localhost.example.com", "",$host_object);
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $get_result = $icinga->do_request("GET", "/objects/hosts/localhost.example.com");
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
my $delete_result = $icinga->do_request("DELETE", "/objects/hosts/localhost.example.com");
or warn sprintf("%s: %s", $icinga->request_code, $icinga->request_status_line);
$icinga->logout;
=head1 DESCRIPTION
Module used to integrate with the REST API that Icinga2 provides.
=head1 CONSTRUCTOR
=over
=item new (FQDN, PORT, PATH, VERSION, INSECURE)
=item new (FQDN, PORT, PATH, VERSION, [ INSECURE ])
Monitoring::Icinga2::Client::REST object.
The former calling convention with four mandatory arguments is the old one. If a
port number is present after the Icinga host, the constructor assumes you want
this form. The arguments are:
=over
=item * FQDN is your Icinga monitoring host.
=item * PORT is the port the REST API runs on.
=item * PATH is the URI where to find the API, usually "/".
=item * VERSION should be set to the API version, usually 1.
=item * INSECURE is optional and makes this module skip certificate verification if set.
=back
The latter form treats only the FQDN as mandatory and allows all other arguments
to be passed hash style with the following keys:
=over
=item * C see above; defaults to C<5665>.
=item * C see above; defaults to C.
=item * C see above; defaults to 1 and is therefore completely
redundant as long as there is only one API version.
=item * C see above; defaults to false
=item * C The file name of a PEM-format file containing the certificate
of the CA that issued the server's HTTPS certificate. As internal monitoring
servers often use certs from in-house CAs, this still allows for secure peer
identification instead of using insecure mode.
=back
=back
=head1 METHODS
=over
=item login (USERNAME, PASSWORD)
Configures and validates credentials. Please note that user needs at least the permission
"status/query". This methos does nothing if it has been called on the sameobject
before unless L was called before.
=item logout
Destroys the login session.
=item login_status
Returns a human readable login status of the current session.
=item logged_in
Returns true when logged in, otherwise returns undef.
=item do_request (HTTP METHOD, URI, PARAMS, DATA)
Performs 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.
=item 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.
=item request_code
Returns the last HTTP status code.
=item request_status_line
Returns the last HTTP status line.
=back
=head1 DEPENDS
L,
L,
L,
L,
L
=head1 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.
=head1 AUTHOR
Johan Carlquist ,
Mikael Nordin
Bits and pieces by Matthias Bethke