| 1 | package Zabbix::Tiny; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use LWP; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use JSON; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 8 | use String::Random; | ||
| 9 | |||
| 10 | our $VERSION = "1.00"; | ||
| 11 | |||
| 12 | has 'server' => ( | ||
| 13 | is => 'rw', | ||
| 14 | required => 1, | ||
| 15 | ); | ||
| 16 | has 'user' => ( | ||
| 17 | is => 'rw', | ||
| 18 | required => 1, | ||
| 19 | ); | ||
| 20 | has 'password' => ( | ||
| 21 | is => 'rw', | ||
| 22 | required => 1, | ||
| 23 | ); | ||
| 24 | has 'auth' => ( is => 'ro', ); | ||
| 25 | has 'ua' => ( is => 'ro', ); | ||
| 26 | has 'last_response' => ( is => 'ro', ); | ||
| 27 | has 'response_content' => ( is => 'ro', ); | ||
| 28 | |||
| 29 | my @content_type = ( 'content-type', 'application/json', ); | ||
| 30 | |||
| 31 | sub BUILD { | ||
| 32 | my $self = shift; | ||
| 33 | $self->{ua} = LWP::UserAgent->new; | ||
| 34 | my $ua = $self->ua; | ||
| 35 | my $url = $self->server; | ||
| 36 | my $id = new String::Random; | ||
| 37 | my $json_data = { | ||
| 38 | jsonrpc => '2.0', | ||
| 39 | id => $id->randpattern("nnnnnnnnnn"), | ||
| 40 | method => 'user.login', | ||
| 41 | params => { | ||
| 42 | user => $self->user, | ||
| 43 | password => $self->password, | ||
| 44 | }, | ||
| 45 | }; | ||
| 46 | my $json = encode_json($json_data); | ||
| 47 | my $post_response = $ua->post( $url, @content_type, Content => $json ); | ||
| 48 | $self->{last_response} = $ua->post( $url, @content_type, Content => $json ); | ||
| 49 | $self->{response_content} = | ||
| 50 | decode_json( $self->{last_response}->{_content} ); | ||
| 51 | |||
| 52 | if ( $self->{response_content}->{error} ) { | ||
| 53 | my $error = $self->{response_content}->{error}->{data}; | ||
| 54 | croak("Error: $error"); | ||
| 55 | } | ||
| 56 | $self->{auth} = $self->{response_content}->{'result'}; | ||
| 57 | } | ||
| 58 | |||
| 59 | sub do { | ||
| 60 | my $self = shift; | ||
| 61 | my $method = shift; | ||
| 62 | my %args = @_; | ||
| 63 | my $id = new String::Random; | ||
| 64 | my $ua = $self->ua; | ||
| 65 | my $auth = $self->auth; | ||
| 66 | my $url = $self->server; | ||
| 67 | my $json_data = { | ||
| 68 | jsonrpc => '2.0', | ||
| 69 | id => $id->randpattern("nnnnnnnnnn"), | ||
| 70 | method => $method, | ||
| 71 | auth => $auth, | ||
| 72 | params => \%args, | ||
| 73 | }; | ||
| 74 | my $json = encode_json($json_data); | ||
| 75 | my $post_response = $ua->post( $url, @content_type, Content => $json ); | ||
| 76 | $self->{last_response} = $ua->post( $url, @content_type, Content => $json ); | ||
| 77 | $self->{response_content} = | ||
| 78 | decode_json( $self->{last_response}->{_content} ); | ||
| 79 | |||
| 80 | if ( $self->{response_content}->{error} ) { | ||
| 81 | my $error = $self->{response_content}->{error}->{data}; | ||
| 82 | croak("Error: $error"); | ||
| 83 | } | ||
| 84 | return $self->{response_content}->{'result'}; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub DEMOLISH { | ||
| 88 | my $self = shift; | ||
| 89 | my $method = shift; | ||
| 90 | my $id = new String::Random; | ||
| 91 | my $ua = $self->ua; | ||
| 92 | my $auth = $self->auth; | ||
| 93 | my $url = $self->server; | ||
| 94 | my $json_data = { | ||
| 95 | jsonrpc => '2.0', | ||
| 96 | id => $id->randpattern("nnnnnnnnnn"), | ||
| 97 | method => 'user.logout', | ||
| 98 | auth => $auth, | ||
| 99 | }; | ||
| 100 | my $json = encode_json($json_data); | ||
| 101 | $self->{last_response} = $ua->post( $url, @content_type, Content => $json ); | ||
| 102 | } | ||
| 103 | |||
| 104 | 1; | ||
| 105 | |||
| 106 | __END__ | ||