File Coverage

blib/lib/WebService/Mackerel.pm
Criterion Covered Total %
statement 37 103 35.9
branch 4 4 100.0
condition 2 2 100.0
subroutine 10 26 38.4
pod 0 19 0.0
total 53 154 34.4


line stmt bran cond sub pod time code
1             package WebService::Mackerel;
2 4     4   219048 use 5.008001;
  4         12  
3 4     4   14 use strict;
  4         4  
  4         67  
4 4     4   12 use warnings;
  4         8  
  4         89  
5 4     4   13 use Carp qw/croak/;
  4         2  
  4         154  
6 4     4   12 use JSON;
  4         6  
  4         16  
7 4     4   2787 use HTTP::Tiny;
  4         87379  
  4         141  
8 4     4   2142 use URI;
  4         13275  
  4         4193  
9              
10             our $VERSION = "0.04";
11              
12             sub new {
13 18     18 0 135780 my ($class, %args) = @_;
14 18 100       200 $args{api_key} or croak "api key is required";
15 17 100       120 $args{service_name} or croak "service name is required";
16             my $self = {
17             api_key => $args{api_key},
18             service_name => $args{service_name},
19 16   100     165 mackerel_origin => $args{mackerel_origin} || 'https://mackerel.io',
20             agent => HTTP::Tiny->new( agent => "WebService::Mackerel agent $VERSION" ),
21             };
22 16         797 bless $self, $class;
23             }
24              
25             sub post_service_metrics {
26 0     0 0 0 my ($self, $args) = @_;
27 0         0 my $path = '/api/v0/services/' . $self->{service_name} . '/tsdb';
28             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
29             content => encode_json $args,
30             headers => {
31             'content-type' => 'application/json',
32             'X-Api-Key' => $self->{api_key},
33             },
34 0         0 });
35 0         0 return $res->{content};
36             }
37              
38             sub create_host {
39 0     0 0 0 my ($self, $args) = @_;
40 0         0 my $path = '/api/v0/hosts';
41             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
42             content => encode_json $args,
43             headers => {
44             'content-type' => 'application/json',
45             'X-Api-Key' => $self->{api_key},
46             },
47 0         0 });
48 0         0 return $res->{content};
49             }
50              
51             sub get_host {
52 0     0 0 0 my ($self, $hostId) = @_;
53 0         0 my $path = '/api/v0/hosts/' . $hostId;
54             my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, {
55             headers => {
56             'X-Api-Key' => $self->{api_key},
57             },
58 0         0 });
59 0         0 return $res->{content};
60             }
61              
62             sub update_host {
63 0     0 0 0 my ($self, $args) = @_;
64 0         0 my $path = '/api/v0/hosts/' . $args->{hostId};
65             my $res = $self->{agent}->request('PUT', $self->{mackerel_origin} . $path, {
66             content => encode_json $args->{data},
67             headers => {
68             'content-type' => 'application/json',
69             'X-Api-Key' => $self->{api_key},
70             },
71 0         0 });
72 0         0 return $res->{content};
73             }
74              
75             sub update_host_status {
76 0     0 0 0 my ($self, $args) = @_;
77 0         0 my $path = '/api/v0/hosts/' . $args->{hostId} . '/status';
78             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
79             content => encode_json $args->{data},
80             headers => {
81             'content-type' => 'application/json',
82             'X-Api-Key' => $self->{api_key},
83             },
84 0         0 });
85 0         0 return $res->{content};
86             }
87              
88             sub host_retire {
89 0     0 0 0 my ($self, $hostId) = @_;
90 0         0 my $path = '/api/v0/hosts/' . $hostId . '/retire';
91             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
92             content => encode_json +{},
93             headers => {
94             'content-type' => 'application/json',
95             'X-Api-Key' => $self->{api_key},
96             },
97 0         0 });
98 0         0 return $res->{content};
99             }
100              
101             sub post_host_metrics {
102 0     0 0 0 my ($self, $args) = @_;
103 0         0 my $path = '/api/v0/tsdb';
104             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
105             content => encode_json $args,
106             headers => {
107             'content-type' => 'application/json',
108             'X-Api-Key' => $self->{api_key},
109             },
110 0         0 });
111 0         0 return $res->{content};
112             }
113              
114             sub get_host_metrics {
115 1     1 0 11 my ($self, $host_id, $query_parameters) = @_;
116 1         16 my $uri = URI->new($self->{mackerel_origin});
117 1         5829 $uri->path("/api/v0/hosts/$host_id/metrics");
118 1         82 $uri->query_form($query_parameters);
119             my $res = $self->{agent}->request('GET', $uri->as_string, {
120             headers => {
121             'content-type' => 'application/json',
122             'X-Api-Key' => $self->{api_key},
123             },
124 1         112 });
125 1         6390 return $res->{content};
126             }
127              
128             sub get_latest_host_metrics {
129 0     0 0 0 my ($self, $args) = @_;
130 0         0 my $path = '/api/v0/tsdb/latest?hostId=' . $args->{hostId} . '&name=' . $args->{name};
131             my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, {
132             headers => {
133             'content-type' => 'application/json',
134             'X-Api-Key' => $self->{api_key},
135             },
136 0         0 });
137 0         0 return $res->{content};
138             }
139              
140             sub get_hosts {
141 2     2 0 35 my ($self, $query_parameters) = @_;
142 2         28 my $uri = URI->new($self->{mackerel_origin});
143 2         5426 $uri->path('/api/v0/hosts.json');
144 2         140 $uri->query_form($query_parameters);
145             my $res = $self->{agent}->request('GET', $uri->as_string, {
146             headers => {
147             'content-type' => 'application/json',
148             'X-Api-Key' => $self->{api_key},
149             },
150 2         218 });
151 2         10721 return $res->{content};
152             }
153              
154             sub create_monitor {
155 0     0 0   my ($self, $args) = @_;
156 0           my $path = '/api/v0/monitors';
157             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
158             content => encode_json $args,
159             headers => {
160             'content-type' => 'application/json',
161             'X-Api-Key' => $self->{api_key},
162             },
163 0           });
164 0           return $res->{content};
165             }
166              
167             sub get_monitor {
168 0     0 0   my ($self, $args) = @_;
169 0           my $path = '/api/v0/monitors';
170             my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, {
171             headers => {
172             'content-type' => 'application/json',
173             'X-Api-Key' => $self->{api_key},
174             },
175 0           });
176 0           return $res->{content};
177             }
178              
179             sub update_monitor {
180 0     0 0   my ($self, $monitorId, $args) = @_;
181 0           my $path = '/api/v0/monitors/' . $monitorId;
182             my $res = $self->{agent}->request('PUT', $self->{mackerel_origin} . $path, {
183             content => encode_json $args,
184             headers => {
185             'content-type' => 'application/json',
186             'X-Api-Key' => $self->{api_key},
187             },
188 0           });
189 0           return $res->{content};
190             }
191              
192             sub delete_monitor {
193 0     0 0   my ($self, $monitorId) = @_;
194 0           my $path = '/api/v0/monitors/' . $monitorId;
195             my $res = $self->{agent}->request('DELETE', $self->{mackerel_origin} . $path, {
196             headers => {
197             'content-type' => 'application/json',
198             'X-Api-Key' => $self->{api_key},
199             },
200 0           });
201 0           return $res->{content};
202             }
203              
204             sub create_graph_annotation {
205 0     0 0   my ($self, $args) = @_;
206 0           my $path = '/api/v0/graph-annotations';
207             my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, {
208             content => encode_json $args,
209             headers => {
210             'content-type' => 'application/json',
211             'X-Api-Key' => $self->{api_key},
212             },
213 0           });
214 0           return $res->{content};
215             }
216              
217             sub get_graph_annotations {
218 0     0 0   my ($self, $query_parameters) = @_;
219 0           my $uri = URI->new($self->{mackerel_origin});
220 0           $uri->path('/api/v0/graph-annotations');
221 0           $uri->query_form($query_parameters);
222             my $res = $self->{agent}->request('GET', $uri->as_string, {
223             headers => {
224             'content-type' => 'application/json',
225             'X-Api-Key' => $self->{api_key},
226             },
227 0           });
228 0           return $res->{content};
229             }
230              
231             sub update_graph_annotation {
232 0     0 0   my ($self, $graphAnnotationId, $args) = @_;
233 0           my $path = '/api/v0/graph-annotations/' . $graphAnnotationId;
234             my $res = $self->{agent}->request('PUT', $self->{mackerel_origin} . $path, {
235             content => encode_json $args,
236             headers => {
237             'content-type' => 'application/json',
238             'X-Api-Key' => $self->{api_key},
239             },
240 0           });
241 0           return $res->{content};
242             }
243              
244             sub delete_graph_annotation {
245 0     0 0   my ($self, $graphAnnotationId) = @_;
246 0           my $path = '/api/v0/graph-annotations/' . $graphAnnotationId;
247             my $res = $self->{agent}->request('DELETE', $self->{mackerel_origin} . $path, {
248             headers => {
249             'content-type' => 'application/json',
250             'X-Api-Key' => $self->{api_key},
251             },
252 0           });
253 0           return $res->{content};
254             }
255              
256             1;
257             __END__