diff --git a/bin/main.pl b/bin/main.pl index 8d6ecd9..aaaf354 100755 --- a/bin/main.pl +++ b/bin/main.pl @@ -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 ) { diff --git a/lib/Pabbix.pm b/lib/Pabbix.pm index 8088b1e..de0ca41 100644 --- a/lib/Pabbix.pm +++ b/lib/Pabbix.pm @@ -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); @@ -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__ diff --git a/lib/Pabbix/Auth.pm b/lib/Pabbix/Auth.pm new file mode 100644 index 0000000..a02c8f0 --- /dev/null +++ b/lib/Pabbix/Auth.pm @@ -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; diff --git a/lib/Pabbix/Request.pm b/lib/Pabbix/Request.pm new file mode 100644 index 0000000..d7c6891 --- /dev/null +++ b/lib/Pabbix/Request.pm @@ -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; diff --git a/lib/Pabbix/Trigger.pm b/lib/Pabbix/Trigger.pm new file mode 100644 index 0000000..9f90350 --- /dev/null +++ b/lib/Pabbix/Trigger.pm @@ -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;