| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Linux::GetPidstat::Writer::Mackerel; |
|
2
|
15
|
|
|
15
|
|
154093
|
use 5.008001; |
|
|
15
|
|
|
|
|
46
|
|
|
3
|
15
|
|
|
15
|
|
61
|
use strict; |
|
|
15
|
|
|
|
|
29
|
|
|
|
15
|
|
|
|
|
232
|
|
|
4
|
15
|
|
|
15
|
|
49
|
use warnings; |
|
|
15
|
|
|
|
|
27
|
|
|
|
15
|
|
|
|
|
318
|
|
|
5
|
|
|
|
|
|
|
|
|
6
|
15
|
|
|
15
|
|
71
|
use Carp; |
|
|
15
|
|
|
|
|
29
|
|
|
|
15
|
|
|
|
|
689
|
|
|
7
|
15
|
|
|
15
|
|
74
|
use Time::Piece; |
|
|
15
|
|
|
|
|
17
|
|
|
|
15
|
|
|
|
|
94
|
|
|
8
|
15
|
|
|
15
|
|
4238
|
use WebService::Mackerel; |
|
|
15
|
|
|
|
|
539876
|
|
|
|
15
|
|
|
|
|
466
|
|
|
9
|
15
|
|
|
15
|
|
136
|
use JSON::XS qw/decode_json encode_json/; |
|
|
15
|
|
|
|
|
30
|
|
|
|
15
|
|
|
|
|
4323
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
sub new { |
|
12
|
14
|
|
|
14
|
0
|
21840
|
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
|
|
|
|
|
114
|
); |
|
18
|
14
|
|
|
|
|
2251
|
$opt{mackerel} = $mackerel; |
|
19
|
14
|
50
|
|
|
|
42
|
$opt{mackerel_metric_key_prefix} = "" unless defined $opt{mackerel_metric_key_prefix}; |
|
20
|
|
|
|
|
|
|
|
|
21
|
14
|
|
|
|
|
53
|
bless \%opt, $class; |
|
22
|
|
|
|
|
|
|
} |
|
23
|
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
sub output { |
|
25
|
7
|
|
|
7
|
0
|
2892
|
my ($self, $program_name, $metric_name, $metric) = @_; |
|
26
|
7
|
|
|
|
|
31
|
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
|
|
|
|
16
|
if ($self->{dry_run}) { |
|
30
|
|
|
|
|
|
|
printf "(dry_run) mackerel post: type=%s, name=%s, time=%s, metric=%s\n", |
|
31
|
1
|
|
|
|
|
6
|
$metric_type, $graph_name, $self->{now}->epoch, $metric; |
|
32
|
1
|
|
|
|
|
90
|
return; |
|
33
|
|
|
|
|
|
|
} |
|
34
|
|
|
|
|
|
|
|
|
35
|
6
|
|
|
|
|
8
|
my $res = do { |
|
36
|
6
|
100
|
|
|
|
18
|
if ($metric_type eq "service") { |
|
|
|
100
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
$self->{mackerel}->post_service_metrics([{ |
|
38
|
|
|
|
|
|
|
"name" => $graph_name, |
|
39
|
|
|
|
|
|
|
"time" => $self->{now}->epoch, |
|
40
|
4
|
|
|
|
|
16
|
"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
|
|
|
|
|
138
|
croak "Invalid metric type of mackerel: type=$metric_type"; |
|
51
|
|
|
|
|
|
|
} |
|
52
|
|
|
|
|
|
|
}; |
|
53
|
|
|
|
|
|
|
|
|
54
|
5
|
|
|
|
|
4854
|
my $content = eval { decode_json $res; }; |
|
|
5
|
|
|
|
|
43
|
|
|
55
|
5
|
100
|
|
|
|
14
|
if (chomp $@) { |
|
56
|
1
|
|
|
|
|
76
|
carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; |
|
57
|
1
|
|
|
|
|
55
|
return; |
|
58
|
|
|
|
|
|
|
} |
|
59
|
|
|
|
|
|
|
|
|
60
|
4
|
|
100
|
|
|
45
|
my $is_success = $content->{success} || 0; |
|
61
|
4
|
100
|
66
|
|
|
33
|
if ($is_success != JSON::true or $content->{error}) { |
|
62
|
15
|
|
|
15
|
|
10499
|
use Data::Dumper; |
|
|
15
|
|
|
|
|
72815
|
|
|
|
15
|
|
|
|
|
5198
|
|
|
63
|
1
|
|
|
|
|
12
|
local $Data::Dumper::Terse = 1; |
|
64
|
1
|
|
|
|
|
4
|
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
|
20
|
my ($self, $ret_pidstats) = @_; |
|
71
|
|
|
|
|
|
|
|
|
72
|
6
|
|
|
|
|
11
|
my $metric_type = $self->{mackerel_metric_type}; |
|
73
|
6
|
|
|
|
|
16
|
my @request_stuffs; |
|
74
|
|
|
|
|
|
|
# ex. backup_mysql => { cpu => 21.0 } |
|
75
|
6
|
|
|
|
|
31
|
while (my ($program_name, $s) = each %$ret_pidstats) { |
|
76
|
12
|
|
|
|
|
41
|
while (my ($metric_name, $metric) = each %$s) { |
|
77
|
108
|
|
|
|
|
286
|
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
|
|
|
|
223
|
($metric_type eq "host" ? (hostId => $self->{mackerel_host_id}) : ()), |
|
83
|
|
|
|
|
|
|
); |
|
84
|
108
|
|
|
|
|
852
|
push @request_stuffs, \%stuff; |
|
85
|
|
|
|
|
|
|
} |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
|
|
88
|
6
|
100
|
|
|
|
18
|
if ($self->{dry_run}) { |
|
89
|
|
|
|
|
|
|
printf "(dry_run) mackerel post: type=%s, time=%s, metric=%s\n", |
|
90
|
2
|
|
|
|
|
9
|
$metric_type, $self->{now}->epoch, encode_json(\@request_stuffs); |
|
91
|
2
|
|
|
|
|
185
|
return; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
4
|
|
|
|
|
19
|
my $res = do { |
|
95
|
4
|
50
|
|
|
|
12
|
if ($metric_type eq "service") { |
|
|
|
0
|
|
|
|
|
|
|
96
|
4
|
|
|
|
|
22
|
$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
|
|
|
|
|
294
|
my $content = eval { decode_json $res; }; |
|
|
4
|
|
|
|
|
86
|
|
|
105
|
4
|
50
|
|
|
|
19
|
if (chomp $@) { |
|
106
|
0
|
|
|
|
|
0
|
carp "Failed mackerel post $metric_type metrics: err=$@, res=$res"; |
|
107
|
0
|
|
|
|
|
0
|
return; |
|
108
|
|
|
|
|
|
|
} |
|
109
|
|
|
|
|
|
|
|
|
110
|
4
|
|
50
|
|
|
147
|
my $is_success = $content->{success} || 0; |
|
111
|
4
|
50
|
33
|
|
|
66
|
if ($is_success != JSON::true or $content->{error}) { |
|
112
|
15
|
|
|
15
|
|
149
|
use Data::Dumper; |
|
|
15
|
|
|
|
|
46
|
|
|
|
15
|
|
|
|
|
1480
|
|
|
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__ |