line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Linux::GetPidstat; |
2
|
13
|
|
|
13
|
|
1371407
|
use 5.008001; |
|
13
|
|
|
|
|
51
|
|
3
|
13
|
|
|
13
|
|
52
|
use strict; |
|
13
|
|
|
|
|
26
|
|
|
13
|
|
|
|
|
218
|
|
4
|
13
|
|
|
13
|
|
51
|
use warnings; |
|
13
|
|
|
|
|
25
|
|
|
13
|
|
|
|
|
416
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = "0.08"; |
7
|
|
|
|
|
|
|
|
8
|
13
|
|
|
13
|
|
52
|
use Carp; |
|
13
|
|
|
|
|
21
|
|
|
13
|
|
|
|
|
585
|
|
9
|
13
|
|
|
13
|
|
3228
|
use Time::Piece::MySQL; |
|
13
|
|
|
|
|
102864
|
|
|
13
|
|
|
|
|
97
|
|
10
|
|
|
|
|
|
|
|
11
|
13
|
|
|
13
|
|
4979
|
use Linux::GetPidstat::Reader; |
|
13
|
|
|
|
|
28
|
|
|
13
|
|
|
|
|
334
|
|
12
|
13
|
|
|
13
|
|
3021
|
use Linux::GetPidstat::Collector; |
|
13
|
|
|
|
|
37
|
|
|
13
|
|
|
|
|
371
|
|
13
|
13
|
|
|
13
|
|
3549
|
use Linux::GetPidstat::Writer; |
|
13
|
|
|
|
|
38
|
|
|
13
|
|
|
|
|
4005
|
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub new { |
16
|
12
|
|
|
12
|
0
|
6297
|
my $class = shift; |
17
|
12
|
|
|
|
|
37
|
bless {}, $class; |
18
|
|
|
|
|
|
|
} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub run { |
21
|
46
|
|
|
46
|
1
|
189754
|
my ($self, %args) = @_; |
22
|
|
|
|
|
|
|
|
23
|
46
|
|
|
|
|
273
|
$self->_validate_args(%args); |
24
|
|
|
|
|
|
|
|
25
|
46
|
|
|
|
|
124
|
my $pid_dir_path = $args{pid_dir}; |
26
|
46
|
|
|
|
|
82
|
my $datetime; |
27
|
46
|
50
|
|
|
|
140
|
unless (length $args{datetime}) { |
28
|
0
|
|
|
|
|
0
|
$datetime = localtime; |
29
|
|
|
|
|
|
|
} else { |
30
|
46
|
|
|
|
|
289
|
$datetime = localtime->from_mysql_datetime($args{datetime}); |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
my $program_pid_mapping = Linux::GetPidstat::Reader->new( |
34
|
|
|
|
|
|
|
pid_dir => $pid_dir_path, |
35
|
|
|
|
|
|
|
include_child => $args{include_child}, |
36
|
|
|
|
|
|
|
max_child_limit => $args{max_child_limit}, |
37
|
46
|
|
|
|
|
8539
|
)->get_program_pid_mapping; |
38
|
|
|
|
|
|
|
|
39
|
46
|
100
|
|
|
|
588
|
unless (@$program_pid_mapping) { |
40
|
11
|
|
|
|
|
693
|
croak "Not found pids in pid_dir: $pid_dir_path"; |
41
|
|
|
|
|
|
|
} |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
my $ret_pidstats = Linux::GetPidstat::Collector->new( |
44
|
|
|
|
|
|
|
interval => $args{interval}, |
45
|
|
|
|
|
|
|
count => $args{count}, |
46
|
35
|
|
|
|
|
634
|
)->get_pidstats_results($program_pid_mapping); |
47
|
|
|
|
|
|
|
|
48
|
25
|
100
|
|
|
|
9942
|
unless (%$ret_pidstats) { |
49
|
9
|
|
|
|
|
1827
|
croak "Failed to collect metrics"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
Linux::GetPidstat::Writer->new( |
53
|
|
|
|
|
|
|
res_file => $args{res_file}, |
54
|
|
|
|
|
|
|
mackerel_metric_type => $args{mackerel_metric_type}, |
55
|
|
|
|
|
|
|
mackerel_api_key => $args{mackerel_api_key}, |
56
|
|
|
|
|
|
|
mackerel_service_name => $args{mackerel_service_name}, |
57
|
|
|
|
|
|
|
mackerel_metric_key_prefix => $args{mackerel_metric_key_prefix}, |
58
|
|
|
|
|
|
|
mackerel_host_id => $args{mackerel_host_id}, |
59
|
|
|
|
|
|
|
now => $datetime, |
60
|
|
|
|
|
|
|
dry_run => $args{dry_run}, |
61
|
16
|
|
|
|
|
438
|
)->output($ret_pidstats); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
sub _validate_args { |
65
|
134
|
|
|
134
|
|
95347
|
my ($self, %args) = @_; |
66
|
|
|
|
|
|
|
|
67
|
134
|
100
|
|
|
|
490
|
unless (length $args{pid_dir}) { |
68
|
11
|
|
|
|
|
2640
|
croak("pid_dir required"); |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
|
71
|
123
|
|
|
|
|
211
|
my $mackerel_metric_type = $args{mackerel_metric_type}; |
72
|
123
|
|
|
|
|
157
|
my $mackerel_api_key = $args{mackerel_api_key}; |
73
|
123
|
|
|
|
|
161
|
my $mackerel_service_name = $args{mackerel_service_name}; |
74
|
123
|
100
|
100
|
|
|
441
|
if (length $mackerel_metric_type && $mackerel_metric_type eq "service") { |
75
|
41
|
100
|
66
|
|
|
153
|
unless (length $mackerel_api_key && |
76
|
|
|
|
|
|
|
length $mackerel_service_name) { |
77
|
11
|
|
|
|
|
693
|
croak("when mackerel_metric_type is 'service', mackerel_[api_key|service_name] are required"); |
78
|
|
|
|
|
|
|
} |
79
|
30
|
|
|
|
|
93
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
82
|
|
|
|
|
173
|
my $mackerel_host_id = $args{mackerel_host_id}; |
83
|
82
|
100
|
66
|
|
|
244
|
if (length $mackerel_metric_type && $mackerel_metric_type eq "host") { |
84
|
22
|
50
|
66
|
|
|
88
|
unless (length $mackerel_api_key && |
|
|
|
66
|
|
|
|
|
85
|
|
|
|
|
|
|
length $mackerel_service_name && |
86
|
|
|
|
|
|
|
length $mackerel_host_id) { |
87
|
11
|
|
|
|
|
693
|
croak("when mackerel_metric_type is 'host', mackerel_[api_key|service_name|host_id] are required"); |
88
|
|
|
|
|
|
|
} |
89
|
11
|
|
|
|
|
44
|
return; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
60
|
|
|
|
|
96
|
my $res_file = $args{res_file}; |
93
|
60
|
100
|
|
|
|
256
|
if (length $res_file) { |
94
|
49
|
|
|
|
|
625
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
|
97
|
11
|
|
|
|
|
704
|
croak("res_file or mackerel_metric_type required"); |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
1; |
101
|
|
|
|
|
|
|
__END__ |