line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package WebService::Mackerel; |
2
|
2
|
|
|
2
|
|
115620
|
use 5.008001; |
|
2
|
|
|
|
|
8
|
|
|
2
|
|
|
|
|
85
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
69
|
|
4
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
9
|
|
|
2
|
|
|
|
|
73
|
|
5
|
2
|
|
|
2
|
|
13
|
use Carp qw/croak/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
135
|
|
6
|
2
|
|
|
2
|
|
12
|
use JSON; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
14
|
|
7
|
2
|
|
|
2
|
|
2063
|
use HTTP::Tiny; |
|
2
|
|
|
|
|
149969
|
|
|
2
|
|
|
|
|
2282
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our $VERSION = "0.02"; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
12
|
|
|
12
|
0
|
46098
|
my ($class, %args) = @_; |
13
|
12
|
100
|
|
|
|
360
|
$args{api_key} or croak "api key is required"; |
14
|
11
|
100
|
|
|
|
302
|
$args{service_name} or croak "service name is required"; |
15
|
10
|
|
50
|
|
|
200
|
my $self = { |
16
|
|
|
|
|
|
|
api_key => $args{api_key}, |
17
|
|
|
|
|
|
|
service_name => $args{service_name}, |
18
|
|
|
|
|
|
|
mackerel_origin => $args{mackerel_origin} || 'https://mackerel.io', |
19
|
|
|
|
|
|
|
agent => HTTP::Tiny->new( agent => "WebService::Mackerel agent $VERSION" ), |
20
|
|
|
|
|
|
|
}; |
21
|
10
|
|
|
|
|
1256
|
bless $self, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub post_service_metrics { |
25
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
26
|
0
|
|
|
|
|
|
my $path = '/api/v0/services/' . $self->{service_name} . '/tsdb'; |
27
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, { |
28
|
|
|
|
|
|
|
content => encode_json $args, |
29
|
|
|
|
|
|
|
headers => { |
30
|
|
|
|
|
|
|
'content-type' => 'application/json', |
31
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
32
|
|
|
|
|
|
|
}, |
33
|
|
|
|
|
|
|
}); |
34
|
0
|
|
|
|
|
|
return $res->{content}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub create_host { |
38
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
39
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts'; |
40
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, { |
41
|
|
|
|
|
|
|
content => encode_json $args, |
42
|
|
|
|
|
|
|
headers => { |
43
|
|
|
|
|
|
|
'content-type' => 'application/json', |
44
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
45
|
|
|
|
|
|
|
}, |
46
|
|
|
|
|
|
|
}); |
47
|
0
|
|
|
|
|
|
return $res->{content}; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
sub get_host { |
51
|
0
|
|
|
0
|
0
|
|
my ($self, $hostId) = @_; |
52
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts/' . $hostId; |
53
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, { |
54
|
|
|
|
|
|
|
headers => { |
55
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
56
|
|
|
|
|
|
|
}, |
57
|
|
|
|
|
|
|
}); |
58
|
0
|
|
|
|
|
|
return $res->{content}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub update_host { |
62
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
63
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts/' . $args->{hostId}; |
64
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('PUT', $self->{mackerel_origin} . $path, { |
65
|
|
|
|
|
|
|
content => encode_json $args->{data}, |
66
|
|
|
|
|
|
|
headers => { |
67
|
|
|
|
|
|
|
'content-type' => 'application/json', |
68
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
69
|
|
|
|
|
|
|
}, |
70
|
|
|
|
|
|
|
}); |
71
|
0
|
|
|
|
|
|
return $res->{content}; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub update_host_status { |
75
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
76
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts/' . $args->{hostId} . '/status'; |
77
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, { |
78
|
|
|
|
|
|
|
content => encode_json $args->{data}, |
79
|
|
|
|
|
|
|
headers => { |
80
|
|
|
|
|
|
|
'content-type' => 'application/json', |
81
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
82
|
|
|
|
|
|
|
}, |
83
|
|
|
|
|
|
|
}); |
84
|
0
|
|
|
|
|
|
return $res->{content}; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub host_retire { |
88
|
0
|
|
|
0
|
0
|
|
my ($self, $hostId) = @_; |
89
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts/' . $hostId . '/retire'; |
90
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, { |
91
|
|
|
|
|
|
|
content => encode_json +{}, |
92
|
|
|
|
|
|
|
headers => { |
93
|
|
|
|
|
|
|
'content-type' => 'application/json', |
94
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
95
|
|
|
|
|
|
|
}, |
96
|
|
|
|
|
|
|
}); |
97
|
0
|
|
|
|
|
|
return $res->{content}; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub post_host_metrics { |
101
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
102
|
0
|
|
|
|
|
|
my $path = '/api/v0/tsdb'; |
103
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('POST', $self->{mackerel_origin} . $path, { |
104
|
|
|
|
|
|
|
content => encode_json $args, |
105
|
|
|
|
|
|
|
headers => { |
106
|
|
|
|
|
|
|
'content-type' => 'application/json', |
107
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
108
|
|
|
|
|
|
|
}, |
109
|
|
|
|
|
|
|
}); |
110
|
0
|
|
|
|
|
|
return $res->{content}; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
sub get_latest_host_metrics { |
114
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
115
|
0
|
|
|
|
|
|
my $path = '/api/v0/tsdb/latest?hostId=' . $args->{hostId} . '&name=' . $args->{name}; |
116
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, { |
117
|
|
|
|
|
|
|
headers => { |
118
|
|
|
|
|
|
|
'content-type' => 'application/json', |
119
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
120
|
|
|
|
|
|
|
}, |
121
|
|
|
|
|
|
|
}); |
122
|
0
|
|
|
|
|
|
return $res->{content}; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
sub get_hosts { |
126
|
0
|
|
|
0
|
0
|
|
my ($self, $args) = @_; |
127
|
0
|
|
|
|
|
|
my $path = '/api/v0/hosts.json'; |
128
|
0
|
|
|
|
|
|
my $res = $self->{agent}->request('GET', $self->{mackerel_origin} . $path, { |
129
|
|
|
|
|
|
|
headers => { |
130
|
|
|
|
|
|
|
'content-type' => 'application/json', |
131
|
|
|
|
|
|
|
'X-Api-Key' => $self->{api_key}, |
132
|
|
|
|
|
|
|
}, |
133
|
|
|
|
|
|
|
}); |
134
|
0
|
|
|
|
|
|
return $res->{content}; |
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
1; |
138
|
|
|
|
|
|
|
__END__ |