Monitoring-Icinga2-Client-Simple

 view release on metacpan or  search on metacpan

lib/Monitoring/Icinga2/Client/Simple.pm  view on Meta::CPAN

# ABSTRACT: Simpler REST client for Icinga2

package Monitoring::Icinga2::Client::Simple;
$Monitoring::Icinga2::Client::Simple::VERSION = '0.002001';
use strict;
use warnings;
use 5.010_001;
use Monitoring::Icinga2::Client::REST 2;
use parent -norequire, 'Monitoring::Icinga2::Client::REST';
use Carp;
use List::Util qw/ all any first /;
use constant DEBUG => $ENV{DEBUG};

sub new {
    my $class = shift;
    croak( "only hash-style args are supported" ) if @_ % 2;
    my %args = @_;
    # uncoverable condition false
    my $server = delete $args{server} // croak( "`server' arg is required" );
    my $ua = delete $args{useragent};
    my $self = $class->SUPER::new( $server, %args );
    if( defined $ua ) {
        # This is a hack as I don't maintain the superclass. However, I wrote its
        # constructor and we'll check whether it has changed so it should be fine.
        # uncoverable branch true
        defined $self->{ua} or croak( 'Monitoring::Icinga2::Client::REST seems to have changed internals; '. 'passing `useragent\' does not work. Please notify mbethke@cpan.org');
        $ua->default_header( 'Accept' => 'application/json' );
        $self->{ua} = $ua;
    }
    # uncoverable condition false
    # uncoverable branch right
    $self->{_mics_author} = getlogin || getpwuid($<);
    return $self;
}

sub schedule_downtime {
    my ($self, %args) = @_;
    _checkargs(\%args, qw/ start_time end_time comment host /);
    # uncoverable condition true
    $args{author} //= $self->{_mics_author};

    if( $args{service} and not $args{services} ) {
        return [ $self->_schedule_downtime_type( 'Service', \%args) ];
    }

    delete $args{service};  # make sure _schedule_downtime_type doesn't set a wrong filter
    my @results = $self->_schedule_downtime_type( 'Host', \%args );
    push @results, $self->_schedule_downtime_type( 'Service', \%args ) if $args{services};
    return \@results;
}

sub _schedule_downtime_type {
    my ($self, $type, $args) = @_;
    my $req_results = $self->_request('POST',
        '/actions/schedule-downtime',
        {
            type => $type,
            joins => [ "host.name" ],
            filter => _create_filter( $args ),
            map { $_ => $args->{$_} } qw/ author start_time end_time comment duration fixed /
        }
    );
    return @$req_results;
}

sub remove_downtime {
    my ($self, %args) = @_;

    defined $args{name}
        and return $self->_remove_downtime_type( 'Downtime', "downtime=$args{name}" );

    _checkargs(\%args, 'host');

    defined $args{service}
        and return $self->_remove_downtime_type( 'Service', \%args );

    return $self->_remove_downtime_type( 'Host', \%args );
}

sub _remove_downtime_type {
    my ($self, $type, $args) = @_;
    my @post_args;

    if(ref $args) {
        @post_args = (
            undef,
            {
                type => $type,
                joins => [ "host.name" ],
                filter => _create_filter( $args ),
            }
        );

lib/Monitoring/Icinga2/Client/Simple.pm  view on Meta::CPAN

The C<$hostname> parameter is not a positional one but passed hash-style, too, under the key C<server>.

=item *

An additional key C<useragent> allows to pass in your own L<LWP::UserAgent> object; this enables more complicated configurations like using TLS client certificates that would otherwise make the number of arguments explode.

=back

Note that the C<useragent> injection is a bit of a hack as it meddles with the
superclass' internals. I originally wrote quite some code (including the
constructor) in L<Monitoring::Icinga2::Client::REST> but I dont maintain it;
nevertheless I don't see any reason why it should change.

=head2 schedule_downtime

    $ia->schedule_downtime(
        host => 'web-1',
        start_time => scalar(time),
        end_time => time + 3600,
        comment => 'System maintenance',
    );

Set a downtime on a host, a host and all services, or a single service

Mandatory arguments:

=over 4

=item *

C<host>: the host name as it it known to Icinga

=item *

C<start_time>: start time as a Unix timestamp

=item *

C<end_time>: also a Unix timestamp

=item *

C<comment>: any string describing the reason for this downtime

=back

Optional arguments:

=over 4

=item *

C<service>: set a downtime for only this service on C<host>. Ignored when combined with C<services>.

=item *

C<services>: set to a true value to set downtimes on all of a host's services. Default is to set the downime on the host only.

=item *

C<author>: will use L<getlogin()|perlfunc/getlogin> (or L<getpwuid|perlfunc/getpwuid> where that's unavailable) if unset

=item *

C<fixed>: set to true for a fixed downtime, default is flexible

=back

The method returns a list of hashes with one element for each downtime
successfully set. The following keys are available:

=over 4

=item *

C<code>: HTTP result code. Should always be 200.

=item *

C<legacy_id>: Icinga2 internal ID to refer to this downtime

=item *

C<name>: a symbolic name to refer to this downtime in the API, e.g. to remove it later

=item *

C<status>: human-readable status message

=back

=head2 remove_downtime

    $ia->remove_downtime( name => "web-1!NTP!49747048-f8d9-4ecc-95a4-86aa4c1011a9" );
    $ia->remove_downtime( host => "web-1", service => 'NTP' );
    $ia->remove_downtime( host => "web-1", services => 1 );

Remove a downtime by name or host/service name

Arguments:

=over 4

=item *

host

=item *

service

=item *

name

=item *

services

=back



( run in 0.812 second using v1.01-cache-2.11-cpan-39bf76dae61 )