Monitoring::Livestatus::INET - connector with tcp sockets


Monitoring-Livestatus documentation Contained in the Monitoring-Livestatus distribution.

Index


Code Index:

NAME

Top

Monitoring::Livestatus::INET - connector with tcp sockets

SYNOPSIS

Top

    use Monitoring::Livestatus;
    my $nl = Monitoring::Livestatus::INET->new( 'localhost:9999' );
    my $hosts = $nl->selectall_arrayref("GET hosts");

CONSTRUCTOR

Top

new ( [ARGS] )

Creates an Monitoring::Livestatus::INET object. new takes at least the server. Arguments are the same as in Monitoring::Livestatus. If the constructor is only passed a single argument, it is assumed to be a the server specification. Use either socker OR server.

METHODS

Top

AUTHOR

Top

Sven Nierlein, <nierlein@cpan.org>

COPYRIGHT AND LICENSE

Top


Monitoring-Livestatus documentation Contained in the Monitoring-Livestatus distribution.
package Monitoring::Livestatus::INET;

use 5.000000;
use strict;
use warnings;
use IO::Socket::INET;
use Socket qw(IPPROTO_TCP TCP_NODELAY);
use Carp;
use base "Monitoring::Livestatus";

sub new {
    my $class = shift;
    unshift(@_, "peer") if scalar @_ == 1;
    my(%options) = @_;
    $options{'name'} = $options{'peer'} unless defined $options{'name'};

    $options{'backend'} = $class;
    my $self = Monitoring::Livestatus->new(%options);
    bless $self, $class;
    confess('not a scalar') if ref $self->{'peer'} ne '';

    return $self;
}


########################################

sub _open {
    my $self = shift;
    my $sock;

    eval {
        local $SIG{'ALRM'} = sub { die("connection timeout"); };
        alarm($self->{'connect_timeout'});
        $sock = IO::Socket::INET->new(
                                         PeerAddr => $self->{'peer'},
                                         Type     => SOCK_STREAM,
                                         Timeout  => $self->{'connect_timeout'},
                                         );
        if(!defined $sock or !$sock->connected()) {
            my $msg = "failed to connect to $self->{'peer'} :$!";
            if($self->{'errors_are_fatal'}) {
                croak($msg);
            }
            $Monitoring::Livestatus::ErrorCode    = 500;
            $Monitoring::Livestatus::ErrorMessage = $msg;
            alarm(0);
            return;
        }

        if(defined $self->{'query_timeout'}) {
            # set timeout
            $sock->timeout($self->{'query_timeout'});
        }

        setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, 1);

    };
    alarm(0);

    if($@) {
        $Monitoring::Livestatus::ErrorCode    = 500;
        $Monitoring::Livestatus::ErrorMessage = $@;
        return;
    }

    return($sock);
}


########################################

sub _close {
    my $self = shift;
    my $sock = shift;
    return unless defined $sock;
    return close($sock);
}


1;

__END__