File Coverage

blib/lib/WebService/Mackerel.pm
Criterion Covered Total %
statement 43 109 39.4
branch 4 4 100.0
condition 2 2 100.0
subroutine 11 27 40.7
pod 0 20 0.0
total 60 162 37.0


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