script/sys-monitor-lite

filter: Statementstmt 76.0% 89 / 117 Branchbran 53.9% 41 / 76 Conditioncond 47.6% 20 / 42 MC/DCmcdc 4.7% 1 / 21 Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 60.0% 158 / 263 CC 1 SCAR 0.0
CC
1
Cov
64%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« lib/Sys/Monitor/Lite.pm ↑ Summary
1 #!/usr/bin/env perl
2 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use FindBin;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use lib "$FindBin::Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Sys::Monitor::Lite;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 4 my $interval = 0;
11 4 my $once = 0;
12 4 my $collect = undef;
13 4 my $output = 'json';
14 4 my $pretty = 0;
15 4 my $prefix = '';
16 4 my $labels = '';
17 4 my $with_timestamp = 0;
18 4 my $help = 0;
19 4 my @warn;
20 my @crit;
21 4 my $check = 0;
22 4 my @top;
23 my @watch;
24  
25 4 ▶GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("interval=f", \$interval, "once", \$once, "collect=s", \$collect, "output=s", \$output, "pretty!", \$pretty, "prefix=s", \$prefix, "labels=s", \$labels, "timestamp!", \$with_timestamp, "warn=s\@", \@warn, "crit=s\@", \@crit, "check!", \$check, "top=s\@", \@top, "watch=s\@", \@watch, "help", \$help) or pod2usage(2) 4 0 4
26 'interval=f' => \$interval,
27 'once' => \$once,
28 'collect=s' => \$collect,
29 'output=s' => \$output,
30 'pretty!' => \$pretty,
31 'prefix=s' => \$prefix,
32 'labels=s' => \$labels,
33 'timestamp!' => \$with_timestamp,
34 'warn=s@' => \@warn,
35 'crit=s@' => \@crit,
36 'check!' => \$check,
37 'top=s@' => \@top,
38 'watch=s@' => \@watch,
39 'help' => \$help,
40 ) or pod2usage(2);
41  
42 4 ▶pod2usage(0) if $help;
Branch: 1 branch
BranchTrueFalseTotal
if $help 0 4 4
43  
44 4 ▶my @metrics = defined $collect ? split(/,/, $collect) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $collect ? : 3 1 4
45 4 my %valid = map { $_ => 1 } Sys::Monitor::Lite::available_metrics();
46 4 ▶if (@metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (@metrics) 3 1 4
47 3 my @unknown = grep { !$valid{$_} } @metrics;
48 3 ▶ if (@unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) 0 3 3
49 0 die "Unknown metrics: " . join(', ', @unknown) . "\n";
50 }
51 }
52  
53 4 ▶$check ||= @warn || @crit;
Condition33%2/6
$check ||= @warn || @crit
l!l&&r!l&&!r
004
@warn || @crit
l!l&&r!l&&!r
004
MC/DC0%0/3
$check ||= @warn || @crit
$check @warn @crit
Truth table0%0/4
A ||= B || C
A= $checkB= @warnC= @crit
ABCresult
1XX1
01X1
0011
0000
54  
55 4 ▶if ($check && !@warn && !@crit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check and not @warn and not @crit) 0 4 4
Condition33%2/6
$check and not @warn and not @crit
!ll&&!rl&&r
400
$check and not @warn
!ll&&!rl&&r
400
MC/DC0%0/3
$check and not @warn and not @crit
$check not @warn not @crit
Truth table25%1/4
A and not(B) and not(C)
A= $checkB= not @warnC= not @crit
ABCresult
0XX0
10X0
1100
1111
56 0 die "--check requires at least one --warn or --crit threshold\n";
57 }
58  
59 4 $output = lc $output;
60 4 ▶die "Unsupported output format: $output\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $output eq "json" or $output eq "jsonl" or $output eq "yaml" or $output eq "yml" or $output eq "prometheus" 0 4 4
Condition75%9/12
$output eq "json" or $output eq "jsonl" or $output eq "yaml" or $output eq "yml" or $output eq "prometheus"
l!l&&r!l&&!r
310
$output eq "json" or $output eq "jsonl" or $output eq "yaml" or $output eq "yml"
l!l&&r!l&&!r
301
$output eq "json" or $output eq "jsonl" or $output eq "yaml"
l!l&&r!l&&!r
211
$output eq "json" or $output eq "jsonl"
l!l&&r!l&&!r
202
MC/DC0%0/5
$output eq "json" or $output eq "jsonl" or $output eq "yaml" or $output eq "yml" or $output eq "prometheus"
$output eq "json" $output eq "jsonl" $output eq "yaml" $output eq "yml" $output eq "prometheus"
Truth table50%3/6
A or B or C or D or E
A= $output eq "json"B= $output eq "jsonl"C= $output eq "yaml"D= $output eq "yml"E= $output eq "prometheus"
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
61 unless $output eq 'json'
62 || $output eq 'jsonl'
63 || $output eq 'yaml'
64 || $output eq 'yml'
65 || $output eq 'prometheus';
66  
67 4 ▶my $encoder_pretty = $output eq 'jsonl' ? 0 : $pretty;
Branch: 1 branch
BranchTrueFalseTotal
$output eq 'jsonl' ? : 0 4 4
68  
69 4 ▶my $run_once = $once || $interval <= 0 || $check;
Condition33%2/6
$once || $interval <= 0 || $check
l!l&&r!l&&!r
400
$once or $interval <= 0
l!l&&r!l&&!r
400
MC/DC0%0/3
$once || $interval <= 0 || $check
$once $interval <= 0 $check
Truth table25%1/4
A or B || C
A= $onceB= $interval <= 0C= $check
ABCresult
1XX1
01X1
0011
0000
70 4 ▶my $sleep_interval = $interval > 0 ? $interval : 5;
Branch: 1 branch
BranchTrueFalseTotal
$interval > 0 ? : 0 4 4
71  
72 4 my @threshold_metrics = Sys::Monitor::Lite::metrics_from_thresholds(\@warn, \@crit);
73 4 ▶if ($check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($check) 0 4 4
74 0 my @unknown_thresholds = grep { !$valid{$_} } @threshold_metrics;
75 0 ▶ if (@unknown_thresholds) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown_thresholds) 0 0 0
76 0 die "Unknown metrics in thresholds: " . join(', ', @unknown_thresholds) . "\n";
77 }
78  
79 0 ▶ if (@metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (@metrics) { } 0 0 0
80 0 my %seen = map { $_ => 1 } @metrics;
81 0 push @metrics, grep { !$seen{$_} } @threshold_metrics;
82 } else {
83 0 ▶ @metrics = @threshold_metrics if @threshold_metrics;
Branch: 1 branch
BranchTrueFalseTotal
if @threshold_metrics 0 0 0
84 }
85 }
86  
87 4 my %proc_opts;
88 4 ▶if (@top) {
Branch: 1 branch
BranchTrueFalseTotal
if (@top) 1 3 4
89 1 for my $entry (@top) {
90 1 my ($field, $count) = $entry =~ /^(\w+)=([0-9]+)$/;
91 1 ▶ die "--top expects format cpu=5 or rss=5\n" unless defined $field && defined $count;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field and defined $count 0 1 1
Condition33%1/3
defined $field and defined $count
!ll&&!rl&&r
001
MC/DC0%0/2
defined $field and defined $count
defined $field defined $count
Truth table33%1/3
A and B
A= defined $fieldB= defined $count
ABresult
0X0
100
111
92 1 ▶ if ($field eq 'cpu') {
Branch: 2 branches
BranchTrueFalseTotal
if ($field eq 'cpu') { } 1 0 1
elsif ($field eq 'rss') { } 0 0 0
93 1 $proc_opts{top_cpu} = $count;
94 } elsif ($field eq 'rss') {
95 0 $proc_opts{top_rss} = $count;
96 } else {
97 0 die "Unsupported --top field: $field (use cpu or rss)\n";
98 }
99 }
100 }
101  
102 4 ▶if (@watch) {
Branch: 1 branch
BranchTrueFalseTotal
if (@watch) 0 4 4
103 0 my @names;
104 0 for my $w (@watch) {
105 0 push @names, split(/,/, $w);
106 }
107 0 $proc_opts{watch} = [ grep { length } @names ];
108 }
109  
110 4 my %fixed_labels;
111 4 ▶if (length $labels) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $labels) 1 3 4
112 1 for my $pair (split(/,/, $labels)) {
113 1 ▶ next unless length $pair;
Branch: 1 branch
BranchTrueFalseTotal
unless length $pair 0 1 1
114 1 my ($key, $value) = split(/=/, $pair, 2);
115 1 ▶ die "--labels expects key=value pairs separated by commas\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key and defined $value and length $key 0 1 1
Condition33%2/6
defined $key and defined $value and length $key
!ll&&!rl&&r
001
defined $key and defined $value
!ll&&!rl&&r
001
MC/DC0%0/3
defined $key and defined $value and length $key
defined $key defined $value length $key
Truth table25%1/4
A and B and C
A= defined $keyB= defined $valueC= length $key
ABCresult
0XX0
10X0
1100
1111
116 unless defined $key && defined $value && length $key;
117 1 $fixed_labels{$key} = $value;
118 }
119 }
120  
121 4 ▶my $needs_process = %proc_opts ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
%proc_opts ? : 1 3 4
122 4 ▶if ($needs_process) {
Branch: 1 branch
BranchTrueFalseTotal
if ($needs_process) 1 3 4
123 1 ▶ if (@metrics) {
Branch: 1 branch
BranchTrueFalseTotal
if (@metrics) { } 0 1 1
124 0 my %seen = map { $_ => 1 } @metrics;
125 0 ▶ push @metrics, 'process' unless $seen{process};
Branch: 1 branch
BranchTrueFalseTotal
unless $seen{"process"} 0 0 0
126 }
127 else {
128 1 @metrics = ('process');
129 }
130 }
131  
132 4 while (1) {
133 4 ▶ my $data = @metrics
Branch: 1 branch
BranchTrueFalseTotal
@metrics ? : 4 0 4
134 ? Sys::Monitor::Lite::collect(\@metrics, { process => \%proc_opts })
135 : Sys::Monitor::Lite::collect_all({ process => \%proc_opts });
136  
137 4 ▶ if ($check) {
Branch: 3 branches
BranchTrueFalseTotal
if ($check) { } 0 4 4
elsif ($output eq 'prometheus') { } 1 3 4
elsif ($output eq 'yaml' or $output eq 'yml') { } 1 2 3
138 0 my $result = Sys::Monitor::Lite::evaluate_thresholds(
139 $data,
140 warn => \@warn,
141 crit => \@crit,
142 );
143 0 print $result->{message}, "\n";
144 0 exit $result->{status};
145 } elsif ($output eq 'prometheus') {
146 1 my %opts;
147 1 ▶ $opts{prefix} = $prefix if length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if length $prefix 1 0 1
148 1 ▶ $opts{labels} = \%fixed_labels if %fixed_labels;
Branch: 1 branch
BranchTrueFalseTotal
if %fixed_labels 1 0 1
149 1 ▶ $opts{timestamp} = 1 if $with_timestamp;
Branch: 1 branch
BranchTrueFalseTotal
if $with_timestamp 0 1 1
150 1 my $text = Sys::Monitor::Lite::to_prometheus($data, %opts);
151 1 print $text;
152 1 ▶ print "\n" unless $text =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n\z/ 0 1 1
153 ▶ } elsif ($output eq 'yaml' || $output eq 'yml') {
Condition66%2/3
$output eq "yaml" or $output eq "yml"
l!l&&r!l&&!r
102
MC/DC50%1/2
$output eq "yaml" or $output eq "yml"
$output eq "yaml" $output eq "yml"
Truth table66%2/3
A or B
A= $output eq "yaml"B= $output eq "yml"
ABresult
1X1
011
000
154 1 my $yaml = Sys::Monitor::Lite::to_yaml($data);
155 1 print $yaml;
156 1 ▶ print "\n" unless $yaml =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml =~ /\n\z/ 0 1 1
157 } else {
158 2 ▶ my %opts = $encoder_pretty ? (pretty => 1) : ();
Branch: 1 branch
BranchTrueFalseTotal
$encoder_pretty ? : 0 2 2
159 2 my $json = Sys::Monitor::Lite::to_json($data, %opts);
160 2 ▶ if ($output eq 'jsonl') {
Branch: 1 branch
BranchTrueFalseTotal
if ($output eq 'jsonl') { } 0 2 2
161 0 $json =~ s/\s+\z//;
162 0 print $json, "\n";
163 } else {
164 2 print $json;
165 2 ▶ print "\n" unless $json =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $json =~ /\n\z/ 2 0 2
166 }
167 }
168  
169 4 ▶ last if $run_once;
Branch: 1 branch
BranchTrueFalseTotal
if $run_once 4 0 4
170 0 sleep $sleep_interval;
171 }
172  
173 __END__
« lib/Sys/Monitor/Lite.pm ↑ Summary