line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Linux::GetPidstat::Writer::Mackerel; |
2
|
15
|
|
|
15
|
|
125152
|
use 5.008001; |
|
15
|
|
|
|
|
44
|
|
3
|
15
|
|
|
15
|
|
75
|
use strict; |
|
15
|
|
|
|
|
18
|
|
|
15
|
|
|
|
|
256
|
|
4
|
15
|
|
|
15
|
|
53
|
use warnings; |
|
15
|
|
|
|
|
30
|
|
|
15
|
|
|
|
|
336
|
|
5
|
|
|
|
|
|
|
|
6
|
15
|
|
|
15
|
|
62
|
use Carp; |
|
15
|
|
|
|
|
33
|
|
|
15
|
|
|
|
|
672
|
|
7
|
15
|
|
|
15
|
|
76
|
use Time::Piece; |
|
15
|
|
|
|
|
31
|
|
|
15
|
|
|
|
|
72
|
|
8
|
15
|
|
|
15
|
|
4198
|
use WebService::Mackerel; |
|
15
|
|
|
|
|
682029
|
|
|
15
|
|
|
|
|
618
|
|
9
|
15
|
|
|
15
|
|
125
|
use JSON::XS qw/decode_json encode_json/; |
|
15
|
|
|
|
|
50
|
|
|
15
|
|
|
|
|
6463
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
12
|
14
|
|
|
14
|
0
|
20665
|
my ( $class, %opt ) = @_; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
my $mackerel = WebService::Mackerel->new( |
15
|
|
|
|
|
|
|
api_key => $opt{mackerel_api_key}, |
16
|
|
|
|
|
|
|
service_name => $opt{mackerel_service_name}, |
17
|
14
|
|
|
|
|
120
|
); |
18
|
14
|
|
|
|
|
2429
|
$opt{mackerel} = $mackerel; |
19
|
14
|
50
|
|
|
|
53
|
$opt{mackerel_metric_key_prefix} = "" unless defined $opt{mackerel_metric_key_prefix}; |
20
|
|
|
|
|
|
|
|
21
|
14
|
|
|
|
|
44
|
bless \%opt, $class; |
22
|
|
|
|
|
|
|
} |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub output { |
25
|
7
|
|
|
7
|
0
|
2553
|
my ($self, $program_name, $metric_name, $metric) = @_; |
26
|
7
|
|
|
|
|
28
|
my $graph_name = sprintf("custom.%s%s.%s", $self->{mackerel_metric_key_prefix}, $metric_name, $program_name); |
27
|
|
|
|
|
|
|
|
28
|
7
|
|
|
|
|
13
|
my $metric_type = $self->{mackerel_metric_type}; |
29
|
7
|
100
|
|
|
|
17
|
if ($self->{dry_run}) { |
30
|
|
|
|
|
|
|
printf "(dry_run) mackerel post: type=%s, name=%s, time=%s, metric=%s\n", |
31
|
1
|
|
|
|
|
4
|
$metric_type, $graph_name, $self->{now}->epoch, $metric; |
32
|
1
|
|
|
|
|
81
|
return; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
6
|
|
|
|
|
7
|
my $res = do { |
36
|
6
|
100
|
|
|
|
15
|
if ($metric_type eq "service") { |
|
|
100
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$self->{mackerel}->post_service_metrics([{ |
38
|
|
|
|
|
|
|
"name" => $graph_name, |
39
|
|
|
|
|
|
|
"time" => $self->{now}->epoch, |
40
|
4
|
|
|
|
|
14
|
"value" => $metric, |
41
|
|
|
|
|
|
|
}]); |
42
|
|
|
|
|
|
|
} elsif ($metric_type eq "host") { |
43
|
|
|
|
|
|
|
$self->{mackerel}->post_host_metrics([{ |
44
|
|
|
|
|
|
|
"hostId" => $self->{mackerel_host_id}, |
45
|
|
|
|
|
|
|
"name" => $graph_name, |
46
|
|
|
|
|
|
|
"time" => $self->{now}->epoch, |
47
|
1
|
|
|
|
|
5
|
"value" => $metric, |
48
|
|
|
|
|
|
|
}]); |
49
|
|
|
|
|
|
|
} else { |
50
|
1
|
|
|
|
|
112
|
croak "Invalid metric type of mackerel: type=$metric_type"; |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
}; |
53
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
4809
|
my $content = eval { decode_json $res; }; |
|
5
|
|
|
|
|
37
|
|
55
|
5
|
100
|
|
|
|
15
|
if (chomp $@) { |
56
|
1
|
|
|
|
|
84
|
carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; |
57
|
1
|
|
|
|
|
57
|
return; |
58
|
|
|
|
|
|
|
} |
59
|
|
|
|
|
|
|
|
60
|
4
|
|
100
|
|
|
39
|
my $is_success = $content->{success} || 0; |
61
|
4
|
100
|
66
|
|
|
29
|
if ($is_success != JSON::true or $content->{error}) { |
62
|
15
|
|
|
15
|
|
7058
|
use Data::Dumper; |
|
15
|
|
|
|
|
100059
|
|
|
15
|
|
|
|
|
7000
|
|
63
|
1
|
|
|
|
|
11
|
local $Data::Dumper::Terse = 1; |
64
|
1
|
|
|
|
|
2
|
local $Data::Dumper::Indent = 0; |
65
|
1
|
|
|
|
|
6
|
carp "Failed mackerel post $metric_type metrics: res=" . Data::Dumper::Dumper($content); |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub bulk_output { |
70
|
6
|
|
|
6
|
0
|
25
|
my ($self, $ret_pidstats) = @_; |
71
|
|
|
|
|
|
|
|
72
|
6
|
|
|
|
|
31
|
my $metric_type = $self->{mackerel_metric_type}; |
73
|
6
|
|
|
|
|
24
|
my @request_stuffs; |
74
|
|
|
|
|
|
|
# ex. backup_mysql => { cpu => 21.0 } |
75
|
6
|
|
|
|
|
43
|
while (my ($program_name, $s) = each %$ret_pidstats) { |
76
|
12
|
|
|
|
|
64
|
while (my ($metric_name, $metric) = each %$s) { |
77
|
108
|
|
|
|
|
390
|
my $graph_name = sprintf("custom.%s%s.%s", $self->{mackerel_metric_key_prefix}, $metric_name, $program_name); |
78
|
|
|
|
|
|
|
my %stuff = ( |
79
|
|
|
|
|
|
|
"name" => $graph_name, |
80
|
|
|
|
|
|
|
"time" => $self->{now}->epoch, |
81
|
|
|
|
|
|
|
"value" => $metric, |
82
|
108
|
50
|
|
|
|
329
|
($metric_type eq "host" ? (hostId => $self->{mackerel_host_id}) : ()), |
83
|
|
|
|
|
|
|
); |
84
|
108
|
|
|
|
|
1163
|
push @request_stuffs, \%stuff; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
6
|
100
|
|
|
|
45
|
if ($self->{dry_run}) { |
89
|
|
|
|
|
|
|
printf "(dry_run) mackerel post: type=%s, time=%s, metric=%s\n", |
90
|
2
|
|
|
|
|
11
|
$metric_type, $self->{now}->epoch, encode_json(\@request_stuffs); |
91
|
2
|
|
|
|
|
193
|
return; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
20
|
my $res = do { |
95
|
4
|
50
|
|
|
|
31
|
if ($metric_type eq "service") { |
|
|
0
|
|
|
|
|
|
96
|
4
|
|
|
|
|
45
|
$self->{mackerel}->post_service_metrics(\@request_stuffs) |
97
|
|
|
|
|
|
|
} elsif ($metric_type eq "host") { |
98
|
0
|
|
|
|
|
0
|
$self->{mackerel}->post_host_metrics(\@request_stuffs) |
99
|
|
|
|
|
|
|
} else { |
100
|
0
|
|
|
|
|
0
|
croak "Invalid metric type of mackerel: type=$metric_type"; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
}; |
103
|
|
|
|
|
|
|
|
104
|
4
|
|
|
|
|
502
|
my $content = eval { decode_json $res; }; |
|
4
|
|
|
|
|
148
|
|
105
|
4
|
50
|
|
|
|
25
|
if (chomp $@) { |
106
|
0
|
|
|
|
|
0
|
carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; |
107
|
0
|
|
|
|
|
0
|
return; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
4
|
|
50
|
|
|
226
|
my $is_success = $content->{success} || 0; |
111
|
4
|
50
|
33
|
|
|
113
|
if ($is_success != JSON::true or $content->{error}) { |
112
|
15
|
|
|
15
|
|
155
|
use Data::Dumper; |
|
15
|
|
|
|
|
60
|
|
|
15
|
|
|
|
|
2165
|
|
113
|
0
|
|
|
|
|
|
local $Data::Dumper::Terse = 1; |
114
|
0
|
|
|
|
|
|
local $Data::Dumper::Indent = 0; |
115
|
0
|
|
|
|
|
|
carp "Failed mackerel post $metric_type metrics: res=" . Data::Dumper::Dumper($content); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
1; |
120
|
|
|
|
|
|
|
__END__ |