| 1 | package Zabbix7::API; | ||
| 2 | |||
| 3 | 14 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 14 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 14 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 14 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 14 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 14 | ▶ | use Scalar::Util qw/blessed/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 14 | ▶ | use Module::Loaded; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 14 | ▶ | use Module::Load; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 14 | ▶ | use Params::Validate qw/validate :types/; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | use JSON; | ||
| 15 | use LWP::UserAgent; | ||
| 16 | use Log::Any; | ||
| 17 | |||
| 18 | our $VERSION = '1.3'; | ||
| 19 | |||
| 20 | has 'server' => (is => 'ro', required => 1); | ||
| 21 | has 'ua' => (is => 'ro', lazy => 1, builder => '_build_ua'); | ||
| 22 | has 'token' => (is => 'ro', predicate => 1, clearer => '_clear_token', writer => '_set_token'); | ||
| 23 | has 'use_bearer' => (is => 'rw', default => 0); # Flag to enable Bearer token auth | ||
| 24 | has 'user' => (is => 'ro', init_arg => undef, clearer => '_clear_user', writer => '_set_user'); | ||
| 25 | has 'pull_after_push_mode' => (is => 'rw', default => 1); | ||
| 26 | |||
| 27 | state $global_id = int(rand(10000)); | ||
| 28 | |||
| 29 | sub _build_ua { | ||
| 30 | my $self = shift; | ||
| 31 | my $ua = LWP::UserAgent->new( | ||
| 32 | agent => 'Zabbix API client (libwww-perl)', | ||
| 33 | ssl_opts => { verify_hostname => 1 }, | ||
| 34 | ); | ||
| 35 | return $ua; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub useragent { | ||
| 39 | return shift->ua; | ||
| 40 | } | ||
| 41 | |||
| 42 | sub api_version { | ||
| 43 | my $self = shift; | ||
| 44 | # can't use _raw_query here because the server refuses the request | ||
| 45 | # if an auth parameter is present -- wtf zabbix | ||
| 46 | my $response = eval { $self->ua->post($self->server, | ||
| 47 | 'Content-Type' => 'application/json-rpc', | ||
| 48 | Content => encode_json({ | ||
| 49 | jsonrpc => '2.0', | ||
| 50 | id => $global_id++, | ||
| 51 | method => 'apiinfo.version', | ||
| 52 | }) | ||
| 53 | ) | ||
| 54 | }; | ||
| 55 | |||
| 56 | if (my $error = $@) { | ||
| 57 | # no good | ||
| 58 | croak sprintf('Could not request API version info: %s (%s, %s)', | ||
| 59 | $error, $response->code, $response->message); | ||
| 60 | } | ||
| 61 | |||
| 62 | my $decoded = eval { decode_json($response->decoded_content) }; | ||
| 63 | |||
| 64 | if (my $error = $@) { | ||
| 65 | # no good either | ||
| 66 | croak sprintf('Could not request API version info: %s (%s, %s)', | ||
| 67 | $error, $response->code, $response->message); | ||
| 68 | } | ||
| 69 | |||
| 70 | if ($decoded->{error}) { | ||
| 71 | croak 'Could not request API version info: '.$decoded->{error}->{data}; | ||
| 72 | } | ||
| 73 | |||
| 74 | return $decoded->{result}; | ||
| 75 | } | ||
| 76 | |||
| 77 | sub login { | ||
| 78 | my $self = shift; | ||
| 79 | my %args = validate(@_, { user => 1, password => 1 }); | ||
| 80 | $self->_clear_token; | ||
| 81 | $self->_clear_user; | ||
| 82 | |||
| 83 | my $response = $self->_raw_query( | ||
| 84 | method => 'user.login', | ||
| 85 | params => { | ||
| 86 | username => $args{user}, | ||
| 87 | password => $args{password}, | ||
| 88 | } | ||
| 89 | ); | ||
| 90 | |||
| 91 | my $decoded = eval { decode_json($response->decoded_content) }; | ||
| 92 | if (my $error = $@) { | ||
| 93 | croak sprintf('Could not connect: %s (%s, %s)', | ||
| 94 | $error, $response->code, $response->message); | ||
| 95 | } | ||
| 96 | if ($decoded->{error}) { | ||
| 97 | croak 'Could not log in: ' . $decoded->{error}->{data}; | ||
| 98 | } | ||
| 99 | |||
| 100 | Log::Any->get_logger->debug("Login successful, token: " . $decoded->{result}); | ||
| 101 | $self->_set_token($decoded->{result}); | ||
| 102 | $self->_set_user($args{user}); | ||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | |||
| 107 | sub set_bearer_token { | ||
| 108 | my ($self, $token) = @_; | ||
| 109 | croak "No token provided" unless $token; | ||
| 110 | $self->_clear_token; | ||
| 111 | $self->_set_token($token); | ||
| 112 | $self->use_bearer(1); # Enable Bearer token mode | ||
| 113 | Log::Any->get_logger->debug("Set Bearer token: $token"); | ||
| 114 | return $self; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub logout { | ||
| 118 | my $self = shift; | ||
| 119 | $self->_clear_token; | ||
| 120 | $self->_clear_user; | ||
| 121 | $self->use_bearer(0); # Reset Bearer mode | ||
| 122 | return $self; | ||
| 123 | } | ||
| 124 | |||
| 125 | |||
| 126 | sub _raw_query { | ||
| 127 | my ($self, %args) = @_; | ||
| 128 | $args{'jsonrpc'} = '2.0'; | ||
| 129 | $args{'id'} = $global_id++; | ||
| 130 | $args{'params'} //= {}; | ||
| 131 | |||
| 132 | my %headers = ('Content-Type' => 'application/json-rpc'); | ||
| 133 | if ($self->token && $args{'method'} ne 'apiinfo.version' && $args{'method'} ne 'user.login') { | ||
| 134 | if ($self->use_bearer) { | ||
| 135 | $headers{'Authorization'} = 'Bearer ' . $self->token; | ||
| 136 | } else { | ||
| 137 | $args{'auth'} = $self->token; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | my $json_payload = encode_json(\%args); | ||
| 142 | |||
| 143 | Log::Any->get_logger->debug("Sending JSON payload: $json_payload"); | ||
| 144 | Log::Any->get_logger->debug("Headers: " . join(", ", map { "$_ => $headers{$_}" } keys %headers)); | ||
| 145 | my $response = eval { | ||
| 146 | $self->ua->post( | ||
| 147 | $self->server, | ||
| 148 | Content => $json_payload, | ||
| 149 | %headers | ||
| 150 | ) | ||
| 151 | }; | ||
| 152 | if (my $error = $@) { | ||
| 153 | Log::Any->get_logger->error("HTTP request failed: $error"); | ||
| 154 | confess $error; | ||
| 155 | } | ||
| 156 | Log::Any->get_logger->trace("Request: " . $response->request->as_string); | ||
| 157 | Log::Any->get_logger->debug("Response: " . $response->as_string); | ||
| 158 | return $response; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub query { | ||
| 162 | my $self = shift; | ||
| 163 | my %args = validate(@_, { | ||
| 164 | method => { type => SCALAR }, | ||
| 165 | params => { type => HASHREF, optional => 1, default => {} } | ||
| 166 | }); | ||
| 167 | |||
| 168 | my $response = $self->_raw_query(%args); | ||
| 169 | if ($response->is_success) { | ||
| 170 | my $decoded = decode_json($response->decoded_content); | ||
| 171 | if ($decoded->{error}) { | ||
| 172 | croak(sprintf('While calling method %s, Zabbix server replied: %s', | ||
| 173 | $args{method}, | ||
| 174 | $decoded->{error}->{data})); | ||
| 175 | } | ||
| 176 | return $decoded->{result}; | ||
| 177 | } | ||
| 178 | croak 'Received HTTP error: ' . $response->decoded_content; | ||
| 179 | } | ||
| 180 | |||
| 181 | sub fetch { | ||
| 182 | my $self = shift; | ||
| 183 | my $class = shift; | ||
| 184 | my %args = validate(@_, { | ||
| 185 | params => { type => HASHREF, default => {} } | ||
| 186 | }); | ||
| 187 | |||
| 188 | $class =~ s/^(?:Zabbix7::API::)?/Zabbix7::API::/; | ||
| 189 | unless (Module::Loaded::is_loaded($class)) { | ||
| 190 | eval { Module::Load::load($class) }; | ||
| 191 | if (my $error = $@) { | ||
| 192 | croak qq{Could not load class '$class': $error}; | ||
| 193 | } | ||
| 194 | $class->can('new') | ||
| 195 | or croak "Class '$class' does not implement required 'new' method"; | ||
| 196 | $class->can('_prefix') | ||
| 197 | or croak "Class '$class' does not implement required '_prefix' method"; | ||
| 198 | $class->can('_extension') | ||
| 199 | or croak "Class '$class' does not implement required '_extension' method"; | ||
| 200 | } | ||
| 201 | |||
| 202 | my $response = $self->query( | ||
| 203 | method => $class->_prefix('.get'), | ||
| 204 | params => { | ||
| 205 | %{$args{params}}, | ||
| 206 | $class->_extension | ||
| 207 | } | ||
| 208 | ); | ||
| 209 | |||
| 210 | return [ map { $class->new(root => $self, data => $_) } @{$response} ]; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub fetch_single { | ||
| 214 | my ($self, $class, %args) = @_; | ||
| 215 | my $results = $self->fetch($class, %args); | ||
| 216 | my $result_count = scalar @{$results}; | ||
| 217 | if ($result_count > 1) { | ||
| 218 | croak qq{Too many results for 'fetch_single': expected 0 or 1, got $result_count}; | ||
| 219 | } | ||
| 220 | return $results->[0]; | ||
| 221 | } | ||
| 222 | |||
| 223 | 1; | ||
| 224 | __END__ | ||