blib/lib/Linux/GetPidstat.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 88.8% 16 / 18 Conditioncond 73.3% 11 / 15 MC/DCmcdc 55.5% 5 / 9 Subroutinesub 100% 11 / 11 Podpod 50.0% 1 / 2 totaltotal 89.5% 103 / 115 CC 15 SCAR 19.2
CC
15
Cov
92%
_validate_args
12.2
run
4.0
BEGIN
1.0
CRAP 6.9
« blib/lib/Linux/GetPidstat/Writer/Mackerel.pm ↑ Summary blib/lib/Linux/GetPidstat/Reader.pm »
1 package Linux::GetPidstat;
2 13 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 our $VERSION = "0.09";
7  
8 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use Time::Piece::MySQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶use Linux::GetPidstat::Reader;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 13 ▶use Linux::GetPidstat::Collector;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 13 ▶use Linux::GetPidstat::Writer;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 sub new {
16 12 ▶ my $class = shift;
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
17 12 bless {}, $class;
18 }
19  
20 sub run {
21 46 ▶ my ($self, %args) = @_;
Subroutine run: called CC=4 SCAR=13.9
Pod documented
22  
23 46 $self->_validate_args(%args);
24  
25 46 my $pid_dir_path = $args{pid_dir};
26 46 my $datetime;
27 46 ▶ unless (length $args{datetime}) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $args{'datetime'}) { } 46 0 46
28 0 $datetime = localtime;
29 } else {
30 46 $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 )->get_program_pid_mapping;
38  
39 46 ▶ unless (@$program_pid_mapping) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$program_pid_mapping) 11 35 46
40 11 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 )->get_pidstats_results($program_pid_mapping);
47  
48 25 ▶ unless (%$ret_pidstats) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%$ret_pidstats) 9 16 25
49 9 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 )->output($ret_pidstats);
62 }
63  
64 sub _validate_args {
65 134 ▶ my ($self, %args) = @_;
Subroutine _validate_args: called CC=12 SCAR=25.0
66  
67 134 ▶ unless (length $args{pid_dir}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $args{"pid_dir"}) 11 123 134
68 11 croak("pid_dir required");
69 }
70  
71 123 my $mackerel_metric_type = $args{mackerel_metric_type};
72 123 my $mackerel_api_key = $args{mackerel_api_key};
73 123 my $mackerel_service_name = $args{mackerel_service_name};
74 123 ▶ if (length $mackerel_metric_type && $mackerel_metric_type eq "service") {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mackerel_metric_type and $mackerel_metric_type eq "service") 41 82 123
Condition100%3/3
length $mackerel_metric_type and $mackerel_metric_type eq "service"
!ll&&!rl&&r
602241
MC/DC100%2/2
length $mackerel_metric_type and $mackerel_metric_type eq "service"
length $mackerel_metric_type $mackerel_metric_type eq "service"
Truth table100%3/3
A and B
A= length $mackerel_metric_typeB= $mackerel_metric_type eq "service"
ABresult
0X0
100
111
75 41 ▶ unless (length $mackerel_api_key &&
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mackerel_api_key and length $mackerel_service_name) 11 30 41
Condition66%2/3
length $mackerel_api_key and length $mackerel_service_name
!ll&&!rl&&r
11030
MC/DC50%1/2
length $mackerel_api_key and length $mackerel_service_name
length $mackerel_api_key length $mackerel_service_name
Truth table66%2/3
A and B
A= length $mackerel_api_keyB= length $mackerel_service_name
ABresult
0X0
100
111
76 length $mackerel_service_name) {
77 11 croak("when mackerel_metric_type is 'service', mackerel_[api_key|service_name] are required");
78 }
79 30 return;
80 }
81  
82 82 my $mackerel_host_id = $args{mackerel_host_id};
83 82 ▶ if (length $mackerel_metric_type && $mackerel_metric_type eq "host") {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mackerel_metric_type and $mackerel_metric_type eq "host") 22 60 82
Condition66%2/3
length $mackerel_metric_type and $mackerel_metric_type eq "host"
!ll&&!rl&&r
60022
MC/DC50%1/2
length $mackerel_metric_type and $mackerel_metric_type eq "host"
length $mackerel_metric_type $mackerel_metric_type eq "host"
Truth table66%2/3
A and B
A= length $mackerel_metric_typeB= $mackerel_metric_type eq "host"
ABresult
0X0
100
111
84 22 ▶ unless (length $mackerel_api_key &&
Branch: 1 branch
BranchTrueFalseTotal
unless (length $mackerel_api_key and length $mackerel_service_name and length $mackerel_host_id) 0 11 11
Condition66%4/6
length $mackerel_api_key and length $mackerel_service_name and length $mackerel_host_id
!ll&&!rl&&r
11011
length $mackerel_api_key and length $mackerel_service_name
!ll&&!rl&&r
11011
MC/DC33%1/3
length $mackerel_api_key and length $mackerel_service_name and length $mackerel_host_id
length $mackerel_api_key length $mackerel_service_name length $mackerel_host_id
Truth table50%2/4
A and B and C
A= length $mackerel_api_keyB= length $mackerel_service_nameC= length $mackerel_host_id
ABCresult
0XX0
10X0
1100
1111
85 length $mackerel_service_name &&
86 length $mackerel_host_id) {
87 11 croak("when mackerel_metric_type is 'host', mackerel_[api_key|service_name|host_id] are required");
88 }
89 11 return;
90 }
91  
92 60 my $res_file = $args{res_file};
93 60 ▶ if (length $res_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $res_file) 49 11 60
94 49 return;
95 }
96  
97 11 croak("res_file or mackerel_metric_type required");
98 }
99  
100 13 1;
101 __END__
« blib/lib/Linux/GetPidstat/Writer/Mackerel.pm ↑ Summary blib/lib/Linux/GetPidstat/Reader.pm »