Skip to content

Commit

Permalink
mooified it for future changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert Abraham committed Sep 3, 2012
1 parent ae7a024 commit 725defe
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 63 deletions.
13 changes: 11 additions & 2 deletions bin/main.pl
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,17 @@ sub uniqArray
die "blah\n";
}

my $authToken = Pabbix::getAuthToken( $url, $user, $passwd );
my $response = Pabbix::getTrigger( $url, $authToken );
my $authToken = Pabbix::Auth->new(
url => $url,
user => 'Admin',
passwd => 'CrKyrJegLL'
)->getAuthToken();

my $response = Pabbix::Trigger->new(
authToken => $authToken,
url => $url,
)->getTrigger;


if( -f $file )
{
Expand Down
64 changes: 3 additions & 61 deletions lib/Pabbix.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package Pabbix;
use 5.012004;
use strict;
use warnings;
use LWP::UserAgent;
use Data::Dumper;
use JSON::XS;

use Pabbix::Auth;
use Pabbix::Trigger;
require Exporter;

our @ISA = qw(Exporter);
Expand All @@ -30,63 +28,7 @@ our @EXPORT = qw(

our $VERSION = '0.01';

sub makeReq
{
my ( $url, $json ) = @_;
my $req = HTTP::Request->new( POST => $url );
$req->content( encode_json( $json ) );
my $ua = LWP::UserAgent->new;
$ua->timeout(10);

$req->content_type( 'application/json-rpc' );
my $response = $ua->request($req);
if( $response->is_success )
{
return decode_json( $response->decoded_content );
}
else
{
die $response->status_line;
}
}

sub getAuthToken
{
my ( $url, $user, $passwd ) = @_;
my $json = {
jsonrpc => "2.0",
method => "user.authenticate",
params => {
user => $user,
password => $passwd
},
id => "0"
};

my $response = makeReq( $url, $json );
return $response->{"result"};
}

sub getTrigger
{
my ( $url, $authToken ) = @_;
my $json = {
jsonrpc => "2.0",
method => "trigger.get",
params => {
output => "extend",
expandData => "host",
filter => {
value => 1
}
},
auth => $authToken,
id => 0
};

my $response = makeReq( $url, $json );
return $response;
}


1;
__END__
Expand Down
40 changes: 40 additions & 0 deletions lib/Pabbix/Auth.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Pabbix::Auth;

use strict;
use warnings;
use Moo;
use Pabbix::Request;

sub getAuthToken
{
my $self = shift;
my $json = {
jsonrpc => "2.0",
method => "user.authenticate",
params => {
user => $self->user,
password => $self->passwd
},
id => "0"
};

my $response = Pabbix::Request->new(
url => $self->url,
json => $json,
);
return $response->makeReq()->{'result'};
}

has url => (
is => 'ro',
);

has user => (
is => 'ro',
);

has passwd => (
is => 'ro',
);

1;
37 changes: 37 additions & 0 deletions lib/Pabbix/Request.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package Pabbix::Request;

use strict;
use warnings;
use Moo;
use LWP::UserAgent;
use JSON::XS;

sub makeReq
{
my $self = shift;
my $req = HTTP::Request->new( POST => ($self->url) );
$req->content( encode_json( $self->json ) );
my $ua = LWP::UserAgent->new;
$ua->timeout(10);

$req->content_type( 'application/json-rpc' );
my $response = $ua->request($req);
if( $response->is_success )
{
return decode_json( $response->decoded_content );
}
else
{
die $response->status_line;
}
}

has url => (
is => 'ro',
);

has json => (
is => 'ro'
);

1;
40 changes: 40 additions & 0 deletions lib/Pabbix/Trigger.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Pabbix::Trigger;

use strict;
use warnings;
use Moo;
use Pabbix::Request;

sub getTrigger
{
my $self = shift;
my $json = {
jsonrpc => "2.0",
method => "trigger.get",
params => {
output => "extend",
expandData => "host",
filter => {
value => 1
}
},
auth => $self->authToken,
id => 0
};

my $response = Pabbix::Request->new(
url => $self->url,
json => $json,
);
return $response->makeReq();
}

has url => (
is => 'ro',
);

has authToken => (
is => 'ro',
);

1;

0 comments on commit 725defe

Please sign in to comment.