lib/Sys/Monitor/Lite.pm

filter: Statementstmt 87.6% 470 / 536 Branchbran 58.5% 178 / 304 Conditioncond 43.9% 83 / 189 MC/DCmcdc 15.6% 16 / 102 Subroutinesub 96.1% 50 / 52 Podpod 100% 8 / 8 totaltotal 67.5% 805 / 1191 CC 246 SCAR 31.4
CC
246
Cov
71%
_process_matches
90.0
_disk_usage
89.5
_process_list
56.7
CRAP 23.1
↑ Summary script/sys-monitor-lite »
1 package Sys::Monitor::Lite;
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use POSIX qw(uname);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Time::HiRes qw(sleep);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use JSON::PP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util qw(looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 8 our $VERSION = '0.07';
10  
11 8 my %COLLECTORS = (
12 system => \&_system_info,
13 cpu => \&_cpu_usage,
14 load => \&_load_average,
15 mem => \&_memory_usage,
16 disk => \&_disk_usage,
17 disk_io => \&_disk_io,
18 mounts => \&_mount_info,
19 net => \&_network_io,
20 process => \&_process_list,
21 );
22  
23 1 ▶sub collect_all {
Subroutine collect_all: called CC=1 SCAR=0.0
Pod documented
24 1 my ($options) = @_;
25 1 return collect(undef, $options);
26 }
27  
28 7 ▶sub collect {
Subroutine collect: called CC=10 SCAR=25.1
Pod documented
29 7 my ($which, $options) = @_;
30 7 my @names;
31 7 ▶ if (!defined $which) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $which) { } 1 6 7
elsif (ref $which eq 'ARRAY') { } 6 0 6
32 1 @names = qw(system cpu load mem disk disk_io mounts net);
33 1 ▶ if ($options && ref $options eq 'HASH' && exists $options->{process}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options and ref $options eq "HASH" and exists $options->{"process"}) 0 1 1
Condition33%2/6
$options and ref $options eq "HASH" and exists $options->{"process"}
!ll&&!rl&&r
100
$options and ref $options eq "HASH"
!ll&&!rl&&r
100
MC/DC0%0/3
$options and ref $options eq "HASH" and exists $options->{"process"}
$options ref $options eq "HASH" exists $options->{"process"}
Truth table25%1/4
A and B and C
A= $optionsB= ref $options eq "HASH"C= exists $options->{"process"}
ABCresult
0XX0
10X0
1100
1111
34 0 push @names, 'process';
35 }
36 } elsif (ref $which eq 'ARRAY') {
37 6 @names = @$which;
38 } else {
39 0 @names = @_;
40 }
41  
42 7 my %data = (timestamp => _timestamp());
43 7 ▶ my %collector_opts = ref $options eq 'HASH' ? %$options : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $options eq 'HASH' ? : 4 3 7
44 7 for my $name (@names) {
45 15 ▶ my $collector = $COLLECTORS{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $collector = $COLLECTORS{$name} or next 15 0 15
46 15 my $value = eval { $collector->($collector_opts{$name}) };
47 15 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 15 15
48 15 $data{$name} = $value;
49 }
50 7 return \%data;
51 }
52  
53 5 ▶sub _system_info {
Subroutine _system_info: called CC=1 SCAR=0.0
54 5 my @u = uname();
55 5 my $uptime = _uptime_seconds();
56 return {
57 5 os => $u[0],
58 kernel => $u[2],
59 hostname => $u[1],
60 architecture => $u[4],
61 uptime_sec => $uptime,
62 };
63 }
64  
65 2 ▶sub _cpu_usage {
Subroutine _cpu_usage: called CC=3 SCAR=11.0
66 2 ▶ open my $fh, '<', '/proc/stat' or return {};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/stat" or return {} 2 0 2
67 2 my ($user, $nice, $system, $idle, $iowait) = (split /\s+/, (grep { /^cpu\s/ } <$fh>)[0])[1..5];
68 2 close $fh;
69 2 sleep 0.1;
70 2 ▶ open $fh, '<', '/proc/stat' or return {};
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", "/proc/stat" or return {} 2 0 2
71 2 my ($u2, $n2, $s2, $i2, $w2) = (split /\s+/, (grep { /^cpu\s/ } <$fh>)[0])[1..5];
72 2 close $fh;
73 2 my $diff_total = ($u2+$n2+$s2+$i2+$w2) - ($user+$nice+$system+$idle+$iowait);
74 2 my $diff_idle = ($i2+$w2) - ($idle+$iowait);
75 2 my $used_pct = _percent($diff_total - $diff_idle, $diff_total);
76 return {
77 2 cores => _cpu_cores(),
78 usage_pct => { total => $used_pct },
79 };
80 }
81  
82 1 ▶sub _load_average {
Subroutine _load_average: called CC=3 SCAR=11.3
83 1 ▶ open my $fh, '<', '/proc/loadavg' or return {};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/loadavg" or return {} 1 0 1
84 1 ▶ my $line = <$fh> // '';
Condition50%1/2
readline $fh // ""
l!l
10
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
85 1 close $fh;
86 1 my ($l1, $l5, $l15) = (split /\s+/, $line)[0..2];
87 return {
88 1 '1min' => _maybe_number($l1),
89 '5min' => _maybe_number($l5),
90 '15min' => _maybe_number($l15),
91 };
92 }
93  
94 1 ▶sub _memory_usage {
Subroutine _memory_usage: called CC=13 SCAR=28.0
95 1 ▶ open my $fh, '<', '/proc/meminfo' or return {};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/meminfo" or return {} 1 0 1
96 1 my %info;
97 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 54 1 55
98 54 ▶ next unless $line =~ /^(\w+):\s+(\d+)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $line =~ /^(\w+):\s+(\d+)/ 4 50 54
99 50 $info{$1} = $2 * 1024;
100 }
101 1 close $fh;
102  
103 1 ▶ my $total = $info{MemTotal} // 0;
Condition50%1/2
$info{"MemTotal"} // 0
l!l
10
MC/DC0%0/1
$info{"MemTotal"} // 0
$info{"MemTotal"}
Truth table50%1/2
A
A= $info{"MemTotal"}
Aresult
11
00
104 1 ▶ my $available = $info{MemAvailable} // ($info{MemFree} // 0);
Condition25%1/4
$info{"MemAvailable"} // ($info{"MemFree"} // 0)
l!l
10
$info{"MemFree"} // 0
l!l
00
MC/DC0%0/1
$info{"MemAvailable"} // ($info{"MemFree"} // 0)
$info{"MemAvailable"}
Truth table50%1/2
A
A= $info{"MemAvailable"}
Aresult
11
00
105 1 ▶ my $free = $info{MemFree} // 0;
Condition50%1/2
$info{"MemFree"} // 0
l!l
10
MC/DC0%0/1
$info{"MemFree"} // 0
$info{"MemFree"}
Truth table50%1/2
A
A= $info{"MemFree"}
Aresult
11
00
106 1 ▶ my $buffers = $info{Buffers} // 0;
Condition50%1/2
$info{"Buffers"} // 0
l!l
10
MC/DC0%0/1
$info{"Buffers"} // 0
$info{"Buffers"}
Truth table50%1/2
A
A= $info{"Buffers"}
Aresult
11
00
107 1 ▶ my $cached = ($info{Cached} // 0) + ($info{SReclaimable} // 0);
Condition50%2/4
$info{"Cached"} // 0
l!l
10
$info{"SReclaimable"} // 0
l!l
10
MC/DC0%0/1
$info{"Cached"} // 0
$info{"Cached"}
MC/DC0%0/1
$info{"SReclaimable"} // 0
$info{"SReclaimable"}
Truth table50%1/2
A
A= $info{"Cached"}
Aresult
11
00
Truth table50%1/2
A
A= $info{"SReclaimable"}
Aresult
11
00
108 1 my $used = $total - $available;
109 1 ▶ my $swap_total = $info{SwapTotal} // 0;
Condition50%1/2
$info{"SwapTotal"} // 0
l!l
10
MC/DC0%0/1
$info{"SwapTotal"} // 0
$info{"SwapTotal"}
Truth table50%1/2
A
A= $info{"SwapTotal"}
Aresult
11
00
110 1 ▶ my $swap_free = $info{SwapFree} // 0;
Condition50%1/2
$info{"SwapFree"} // 0
l!l
10
MC/DC0%0/1
$info{"SwapFree"} // 0
$info{"SwapFree"}
Truth table50%1/2
A
A= $info{"SwapFree"}
Aresult
11
00
111 1 my $swap_used = $swap_total - $swap_free;
112  
113 return {
114 1 total_bytes => $total,
115 available_bytes => $available,
116 used_bytes => $used,
117 free_bytes => $free,
118 buffers_bytes => $buffers,
119 cached_bytes => $cached,
120 used_pct => _percent($used, $total),
121 swap => {
122 total_bytes => $swap_total,
123 used_bytes => $swap_used,
124 free_bytes => $swap_free,
125 used_pct => _percent($swap_used, $swap_total),
126 },
127 };
128 }
129  
130 1 ▶sub _disk_usage {
Subroutine _disk_usage: called CC=17 SCAR=44.9
131 1 my $mounts = _mounted_filesystems();
132 1 my %seen;
133 my @disks;
134 1 my $has_statvfs = POSIX->can('statvfs');
135 1 my $df_stats = _df_stats();
136  
137 1 for my $mount_info (@$mounts) {
138 3 my $mount = $mount_info->{mount};
139 3 ▶ next if $seen{$mount}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$mount}++ 0 3 3
140 3 ▶ next unless -d $mount;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $mount 3 0 3
141  
142 0 my ($total, $used, $free, $inode_total, $inode_used, $inode_free);
143 0 ▶ if ($has_statvfs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_statvfs) 0 0 0
144 0 my @stat = eval { POSIX::statvfs($mount) };
145 0 ▶ if (@stat) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stat) 0 0 0
146 0 my ($bsize, $frsize, $blocks, $bfree, $bavail, $files, $ffree) = @stat;
147 0 $total = $blocks * $frsize;
148 0 $free = $bavail * $frsize;
149 0 $used = $total - ($bfree * $frsize);
150 0 $inode_total = _maybe_number($files);
151 0 $inode_free = _maybe_number($ffree);
152 0 ▶ $inode_used = defined $inode_total && defined $inode_free
Branch: 1 branch
BranchTrueFalseTotal
defined $inode_total && defined $inode_free ? : 0 0 0
Condition0%0/3
defined $inode_total and defined $inode_free
!ll&&!rl&&r
000
MC/DC0%0/2
defined $inode_total and defined $inode_free
defined $inode_total defined $inode_free
Truth table0%0/3
A and B
A= defined $inode_totalB= defined $inode_free
ABresult
0X0
100
111
153 ? $inode_total - $inode_free
154 : undef;
155 }
156 }
157  
158 0 ▶ if (!defined $total) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $total) 0 0 0
159 0 my $info = $df_stats->{$mount};
160 0 ▶ if ($info) {
Branch: 1 branch
BranchTrueFalseTotal
if ($info) 0 0 0
161 0 $total = $info->{total};
162 0 $used = $info->{used};
163 0 $free = $info->{avail};
164 }
165 }
166  
167 push @disks, {
168 mount => $mount,
169 filesystem => $mount_info->{device},
170 type => $mount_info->{type},
171 options => $mount_info->{options},
172 read_only => $mount_info->{read_only},
173 0 total_bytes => $total,
174 used_bytes => $used,
175 free_bytes => $free,
176 used_pct => _percent($used, $total),
177 inodes_total => $inode_total,
178 inodes_used => $inode_used,
179 inodes_free => $inode_free,
180 inodes_used_pct => _percent($inode_used, $inode_total),
181 };
182 }
183  
184 1 ▶ if (!@disks && $df_stats && %$df_stats) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @disks and $df_stats and %$df_stats) 1 0 1
Condition33%2/6
not @disks and $df_stats and %$df_stats
!ll&&!rl&&r
001
not @disks and $df_stats
!ll&&!rl&&r
001
MC/DC0%0/3
not @disks and $df_stats and %$df_stats
not @disks $df_stats %$df_stats
Truth table25%1/4
not(A) and B and C
A= not @disksB= $df_statsC= %$df_stats
ABCresult
0XX0
10X0
1100
1111
185 1 for my $mount (sort keys %$df_stats) {
186 7 ▶ next if $seen{$mount}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$mount}++ 1 6 7
187 6 my $info = $df_stats->{$mount};
188 6 ▶ next unless $info->{total};
Branch: 1 branch
BranchTrueFalseTotal
unless $info->{"total"} 0 6 6
189 push @disks, {
190 mount => $mount,
191 filesystem => $info->{filesystem},
192 type => $info->{type} // 'unknown',
193 options => [],
194 read_only => undef,
195 total_bytes => $info->{total},
196 used_bytes => $info->{used},
197 free_bytes => $info->{avail},
198 6 ▶ used_pct => _percent($info->{used}, $info->{total}),
Condition50%1/2
$info->{"type"} // "unknown"
l!l
60
MC/DC0%0/1
$info->{"type"} // "unknown"
$info->{"type"}
Truth table50%1/2
A
A= $info->{"type"}
Aresult
11
00
199 inodes_total => undef,
200 inodes_used => undef,
201 inodes_free => undef,
202 inodes_used_pct => undef,
203 };
204 }
205 }
206  
207 1 return \@disks;
208 }
209  
210 1 ▶sub _mount_info {
Subroutine _mount_info: called CC=3 SCAR=11.0
211 1 my $mounts = _mounted_filesystems();
212 1 my %by_mount;
213  
214 1 for my $info (@$mounts) {
215 3 my $key = _mount_key($info->{mount});
216 3 ▶ next unless length $key;
Branch: 1 branch
BranchTrueFalseTotal
unless length $key 0 3 3
217 $by_mount{$key} = {
218 present => 1,
219 mount => $info->{mount},
220 filesystem => $info->{device},
221 type => $info->{type},
222 options => $info->{options},
223 read_only => $info->{read_only},
224 3 };
225 }
226  
227 1 return \%by_mount;
228 }
229  
230 1 ▶sub _disk_io {
Subroutine _disk_io: called CC=11 SCAR=33.5
231 1 ▶ open my $fh, '<', '/proc/diskstats' or return [];
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/diskstats" or return [] 1 0 1
232 1 my @devices;
233 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 22 1 23
234 22 chomp $line;
235 22 my @fields = split /\s+/, $line;
236 22 ▶ next unless @fields >= 14;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields >= 14 0 22 22
237 my (
238 22 $major, $minor, $name,
239 $reads_completed, $reads_merged, $sectors_read, $read_ms,
240 $writes_completed,$writes_merged, $sectors_written, $write_ms,
241 $io_in_progress, $io_ms, $weighted_io_ms
242 ) = @fields[0..13];
243  
244 22 ▶ next unless defined $name && length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 22 22
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
245 22 ▶ next unless -r "/sys/block/$name/stat";
Branch: 1 branch
BranchTrueFalseTotal
unless -r "/sys/block/$name/stat" 22 0 22
246  
247 0 my $sector_size = _sector_size($name);
248 0 my $reads_sectors = _maybe_number($sectors_read);
249 0 my $writes_sectors = _maybe_number($sectors_written);
250 0 ▶ my $reads_bytes = defined $reads_sectors ? $reads_sectors * $sector_size : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $reads_sectors ? : 0 0 0
251 0 ▶ my $writes_bytes = defined $writes_sectors ? $writes_sectors * $sector_size : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $writes_sectors ? : 0 0 0
252  
253 0 ▶ push @devices, {
Branch: 2 branches
BranchTrueFalseTotal
defined $reads_bytes ? : 0 0 0
defined $writes_bytes ? : 0 0 0
254 device => $name,
255 major => _maybe_number($major),
256 minor => _maybe_number($minor),
257 sector_size => $sector_size,
258 reads => {
259 ios => _maybe_number($reads_completed),
260 merged => _maybe_number($reads_merged),
261 sectors => $reads_sectors,
262 bytes => defined $reads_bytes ? 0 + $reads_bytes : undef,
263 ms => _maybe_number($read_ms),
264 },
265 writes => {
266 ios => _maybe_number($writes_completed),
267 merged => _maybe_number($writes_merged),
268 sectors => $writes_sectors,
269 bytes => defined $writes_bytes ? 0 + $writes_bytes : undef,
270 ms => _maybe_number($write_ms),
271 },
272 in_progress => _maybe_number($io_in_progress),
273 io_ms => _maybe_number($io_ms),
274 weighted_io_ms => _maybe_number($weighted_io_ms),
275 };
276 }
277 1 close $fh;
278 1 return \@devices;
279 }
280  
281 1 ▶sub _network_io {
Subroutine _network_io: called CC=5 SCAR=16.2
282 1 ▶ open my $fh, '<', '/proc/net/dev' or return [];
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/net/dev" or return [] 1 0 1
283 1 my @ifaces;
284 1 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 4 1 5
285 4 ▶ next if $line =~ /^(?:Inter| face)/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^(?:Inter| face)/ 2 2 4
286 2 $line =~ s/^\s+//;
287 2 my ($iface, @fields) = split /[:\s]+/, $line;
288 2 ▶ next unless defined $iface;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $iface 0 2 2
289 2 my ($rx_bytes, $rx_packets, undef, undef, undef, undef, undef, undef,
290 $tx_bytes, $tx_packets) = @fields;
291 2 push @ifaces, {
292 iface => $iface,
293 rx_bytes => _maybe_number($rx_bytes),
294 rx_packets => _maybe_number($rx_packets),
295 tx_bytes => _maybe_number($tx_bytes),
296 tx_packets => _maybe_number($tx_packets),
297 };
298 }
299 1 close $fh;
300 1 return \@ifaces;
301 }
302  
303 2 ▶sub _process_list {
Subroutine _process_list: called CC=35 SCAR=40.4
304 2 my ($opts) = @_;
305 2 ▶ $opts ||= {};
Condition100%2/2
$opts ||= {}
l!l
11
MC/DC100%1/1
$opts ||= {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
306  
307 2 my $uptime = _uptime_seconds();
308 2 ▶ my $clk_tck = POSIX::sysconf(&POSIX::_SC_CLK_TCK) || 100;
Condition50%1/2
POSIX::sysconf(&POSIX::_SC_CLK_TCK) || 100
l!l
20
MC/DC0%0/1
POSIX::sysconf(&POSIX::_SC_CLK_TCK) || 100
POSIX::sysconf(&POSIX::_SC_CLK_TCK)
Truth table50%1/2
A
A= POSIX::sysconf(&POSIX::_SC_CLK_TCK)
Aresult
11
00
309 2 ▶ my $page_size = POSIX::sysconf(&POSIX::_SC_PAGESIZE) || 4096;
Condition50%1/2
POSIX::sysconf(&POSIX::_SC_PAGESIZE) || 4096
l!l
20
MC/DC0%0/1
POSIX::sysconf(&POSIX::_SC_PAGESIZE) || 4096
POSIX::sysconf(&POSIX::_SC_PAGESIZE)
Truth table50%1/2
A
A= POSIX::sysconf(&POSIX::_SC_PAGESIZE)
Aresult
11
00
310 2 ▶ my $cores = _cpu_cores() || 1;
Condition50%1/2
_cpu_cores() || 1
l!l
20
MC/DC0%0/1
_cpu_cores() || 1
_cpu_cores()
Truth table50%1/2
A
A= _cpu_cores()
Aresult
11
00
311  
312 2 ▶ opendir my $dh, '/proc' or return [];
Branch: 1 branch
BranchTrueFalseTotal
opendir my $dh, "/proc" or return [] 2 0 2
313 2 my @pids = grep { /^\d+$/ } readdir $dh;
314 2 closedir $dh;
315  
316 2 my @processes;
317 2 for my $pid (@pids) {
318 19 my $stat_path = "/proc/$pid/stat";
319 19 my $cmd_path = "/proc/$pid/cmdline";
320 19 my $status_path = "/proc/$pid/status";
321 19 ▶ next unless -r $stat_path;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $stat_path 0 19 19
322  
323 19 ▶ open my $sfh, '<', $stat_path or next;
Branch: 1 branch
BranchTrueFalseTotal
open my $sfh, "<", $stat_path or next 19 0 19
324 19 ▶ my $stat_line = <$sfh> // '';
Condition50%1/2
readline $sfh // ""
l!l
190
MC/DC0%0/1
readline $sfh // ""
readline $sfh
Truth table50%1/2
A
A= readline $sfh
Aresult
11
00
325 19 close $sfh;
326 19 ▶ next unless $stat_line =~ /^(\d+)\s+\((.*)\)\s+(.+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $stat_line =~ /^(\d+)\s+\((.*)\)\s+(.+)$/ 0 19 19
327  
328 19 my ($parsed_pid, $comm, $rest) = ($1, $2, $3);
329 19 my @fields = split /\s+/, $rest;
330 19 ▶ next unless @fields >= 22;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields >= 22 0 19 19
331  
332 19 my $state = $fields[0];
333 19 my $ppid = _maybe_number($fields[1]);
334 19 my $utime = _maybe_number($fields[11]);
335 19 my $stime = _maybe_number($fields[12]);
336 19 my $starttime = _maybe_number($fields[19]);
337 19 my $rss_pages = _maybe_number($fields[21]);
338 19 my $threads = _maybe_number($fields[16]);
339  
340 19 ▶ my $elapsed = defined $uptime && defined $starttime
Branch: 1 branch
BranchTrueFalseTotal
defined $uptime && defined $starttime ? : 19 0 19
Condition33%1/3
defined $uptime and defined $starttime
!ll&&!rl&&r
0019
MC/DC0%0/2
defined $uptime and defined $starttime
defined $uptime defined $starttime
Truth table33%1/3
A and B
A= defined $uptimeB= defined $starttime
ABresult
0X0
100
111
341 ? $uptime - ($starttime / $clk_tck)
342 : undef;
343 19 ▶ my $cpu_pct =
Branch: 1 branch
BranchTrueFalseTotal
defined $elapsed && $elapsed > 0 && defined $utime && defined $stime ? : 19 0 19
Condition33%3/9
defined $elapsed and $elapsed > 0 and defined $utime and defined $stime
!ll&&!rl&&r
0019
defined $elapsed and $elapsed > 0 and defined $utime
!ll&&!rl&&r
0019
defined $elapsed and $elapsed > 0
!ll&&!rl&&r
0019
MC/DC0%0/4
defined $elapsed and $elapsed > 0 and defined $utime and defined $stime
defined $elapsed $elapsed > 0 defined $utime defined $stime
Truth table20%1/5
A and B and C and D
A= defined $elapsedB= $elapsed > 0C= defined $utimeD= defined $stime
ABCDresult
0XXX0
10XX0
110X0
11100
11111
344 defined $elapsed && $elapsed > 0 && defined $utime && defined $stime
345 ? sprintf('%.1f', (($utime + $stime) / $clk_tck) / $elapsed * 100 / $cores)
346 : 0;
347  
348 19 ▶ my $rss_bytes = defined $rss_pages ? $rss_pages * $page_size : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $rss_pages ? : 19 0 19
349  
350 19 my $cmdline = '';
351 19 ▶ if (open my $cfh, '<', $cmd_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $cfh, "<", $cmd_path) 19 0 19
352 19 local $/ = undef;
353 19 ▶ my $raw = <$cfh> // '';
Condition50%1/2
readline $cfh // ""
l!l
190
MC/DC0%0/1
readline $cfh // ""
readline $cfh
Truth table50%1/2
A
A= readline $cfh
Aresult
11
00
354 19 close $cfh;
355 19 $raw =~ s/\0/ /g;
356 19 $cmdline = $raw;
357 19 $cmdline =~ s/\s+$//;
358 }
359  
360 19 my $uid;
361 19 ▶ if (open my $stfh, '<', $status_path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $stfh, "<", $status_path) 19 0 19
362 19 ▶ while (my $line = <$stfh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $stfh)) 171 0 171
363 171 ▶ if ($line =~ /^Uid:\s+(\d+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^Uid:\s+(\d+)/) 19 152 171
364 19 $uid = _maybe_number($1);
365 19 last;
366 }
367 }
368 19 close $stfh;
369 }
370  
371 19 ▶ push @processes, {
Branch: 1 branch
BranchTrueFalseTotal
length $cmdline ? : 19 0 19
372 pid => _maybe_number($parsed_pid),
373 ppid => $ppid,
374 name => $comm,
375 command => length($cmdline) ? $cmdline : $comm,
376 state => $state,
377 threads => $threads,
378 uid => $uid,
379 cpu_pct => _maybe_number($cpu_pct),
380 rss_bytes => $rss_bytes,
381 };
382 }
383  
384 0 ▶ my @watch = map { lc $_ } @{ $opts->{watch} // [] };
Condition50%1/2
$opts->{"watch"} // []
l!l
02
MC/DC0%0/1
$opts->{"watch"} // []
$opts->{"watch"}
Truth table50%1/2
A
A= $opts->{"watch"}
Aresult
11
00
385 2 ▶ my $has_watch = @watch ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@watch ? : 0 2 2
386  
387 2 my @selected;
388 2 ▶ if ($has_watch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($has_watch) 0 2 2
389 0 push @selected, grep { _process_matches($_, \@watch) } @processes;
390 }
391  
392 2 ▶ if (my $n = $opts->{top_cpu}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $opts->{"top_cpu"}) 1 1 2
393 1 push @selected, _top_processes(\@processes, 'cpu_pct', $n);
394 }
395  
396 2 ▶ if (my $n = $opts->{top_rss}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = $opts->{"top_rss"}) 0 2 2
397 0 push @selected, _top_processes(\@processes, 'rss_bytes', $n);
398 }
399  
400 2 ▶ if (!$has_watch && !$opts->{top_cpu} && !$opts->{top_rss}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($has_watch or $opts->{"top_cpu"} or $opts->{"top_rss"}) 1 1 2
Condition66%4/6
$has_watch or $opts->{"top_cpu"} or $opts->{"top_rss"}
l!l&&r!l&&!r
101
$has_watch or $opts->{"top_cpu"}
l!l&&r!l&&!r
011
MC/DC33%1/3
$has_watch or $opts->{"top_cpu"} or $opts->{"top_rss"}
$has_watch $opts->{"top_cpu"} $opts->{"top_rss"}
Truth table50%2/4
A or B or C
A= $has_watchB= $opts->{"top_cpu"}C= $opts->{"top_rss"}
ABCresult
1XX1
01X1
0011
0000
401 1 return \@processes;
402 }
403  
404 1 my %seen;
405 my @deduped;
406 1 for my $proc (@selected) {
407 3 ▶ next if $seen{ $proc->{pid} }++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$proc->{"pid"}}++ 0 3 3
408 3 push @deduped, $proc;
409 }
410  
411 1 return \@deduped;
412 }
413  
414 1 ▶sub _top_processes {
Subroutine _top_processes: called CC=8 SCAR=25.8
415 1 my ($processes, $field, $n) = @_;
416 1 ▶ return () unless $processes && @$processes && $n && $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $processes and @$processes and $n and $field 0 1 1
Condition33%3/9
$processes and @$processes and $n and $field
!ll&&!rl&&r
001
$processes and @$processes and $n
!ll&&!rl&&r
001
$processes and @$processes
!ll&&!rl&&r
001
MC/DC0%0/4
$processes and @$processes and $n and $field
$processes @$processes $n $field
Truth table20%1/5
A and B and C and D
A= $processesB= @$processesC= $nD= $field
ABCDresult
0XXX0
10XX0
110X0
11100
11111
417 1 ▶ my @sorted = sort { ($b->{$field} // 0) <=> ($a->{$field} // 0) } @$processes;
Condition50%2/4
$Sys::Monitor::Lite::b->{$field} // 0
l!l
230
$Sys::Monitor::Lite::a->{$field} // 0
l!l
230
MC/DC0%0/1
$Sys::Monitor::Lite::b->{$field} // 0
$Sys::Monitor::Lite::b->{$field}
MC/DC0%0/1
$Sys::Monitor::Lite::a->{$field} // 0
$Sys::Monitor::Lite::a->{$field}
Truth table50%1/2
A
A= $Sys::Monitor::Lite::b->{$field}
Aresult
11
00
Truth table50%1/2
A
A= $Sys::Monitor::Lite::a->{$field}
Aresult
11
00
418 1 ▶ my $limit = $n < @sorted ? $n : scalar @sorted;
Branch: 1 branch
BranchTrueFalseTotal
$n < @sorted ? : 1 0 1
419 1 return @sorted[0 .. $limit - 1];
420 }
421  
422 0 ▶sub _process_matches {
Subroutine _process_matches: not called CC=9 SCAR=45.0
423 0 my ($proc, $watch) = @_;
424 0 ▶ return 0 unless $proc && $watch && @$watch;
Branch: 1 branch
BranchTrueFalseTotal
unless $proc and $watch and @$watch 0 0 0
Condition0%0/6
$proc and $watch and @$watch
!ll&&!rl&&r
000
$proc and $watch
!ll&&!rl&&r
000
MC/DC0%0/3
$proc and $watch and @$watch
$proc $watch @$watch
Truth table0%0/4
A and B and C
A= $procB= $watchC= @$watch
ABCresult
0XX0
10X0
1100
1111
425 0 ▶ my $name = lc($proc->{name} // '');
Condition0%0/2
$proc->{"name"} // ""
l!l
00
MC/DC0%0/1
$proc->{"name"} // ""
$proc->{"name"}
Truth table0%0/2
A
A= $proc->{"name"}
Aresult
11
00
426 0 ▶ my $cmd = lc($proc->{command} // '');
Condition0%0/2
$proc->{"command"} // ""
l!l
00
MC/DC0%0/1
$proc->{"command"} // ""
$proc->{"command"}
Truth table0%0/2
A
A= $proc->{"command"}
Aresult
11
00
427 0 for my $needle (@$watch) {
428 0 ▶ return 1 if index($name, $needle) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($name, $needle) != -1 0 0 0
429 0 ▶ return 1 if index($cmd, $needle) >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($cmd, $needle) != -1 0 0 0
430 }
431 0 return 0;
432 }
433  
434 1 ▶sub _df_stats {
Subroutine _df_stats: called CC=8 SCAR=22.3
435 1 ▶ open my $df, '-|', 'df', '-P', '-k' or return {};
Branch: 1 branch
BranchTrueFalseTotal
open my $df, "-|", "df", "-P", "-k" or return {} 1 0 1
436 1 my %stats;
437 1 my $header = <$df>;
438 1 ▶ while (my $line = <$df>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $df)) 7 1 8
439 7 chomp $line;
440 7 $line =~ s/^\s+//;
441 7 my @fields = split /\s+/, $line;
442 7 ▶ next unless @fields >= 6;
Branch: 1 branch
BranchTrueFalseTotal
unless @fields >= 6 0 7 7
443 7 my ($fs, $blocks, $used, $avail, undef, $mount) = @fields[0,1,2,3,4,5];
444 7 my $total = _maybe_number($blocks);
445 7 my $used_bytes = _maybe_number($used);
446 7 my $avail_bytes = _maybe_number($avail);
447 7 ▶ next unless defined $mount && defined $total && defined $used_bytes && defined $avail_bytes;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mount and defined $total and defined $used_bytes and defined $avail_bytes 0 7 7
Condition33%3/9
defined $mount and defined $total and defined $used_bytes and defined $avail_bytes
!ll&&!rl&&r
007
defined $mount and defined $total and defined $used_bytes
!ll&&!rl&&r
007
defined $mount and defined $total
!ll&&!rl&&r
007
MC/DC0%0/4
defined $mount and defined $total and defined $used_bytes and defined $avail_bytes
defined $mount defined $total defined $used_bytes defined $avail_bytes
Truth table20%1/5
A and B and C and D
A= defined $mountB= defined $totalC= defined $used_bytesD= defined $avail_bytes
ABCDresult
0XXX0
10XX0
110X0
11100
11111
448 7 $stats{$mount} = {
449 filesystem => $fs,
450 type => 'unknown',
451 total => $total * 1024,
452 used => $used_bytes * 1024,
453 avail => $avail_bytes * 1024,
454 };
455 }
456 1 close $df;
457 1 return \%stats;
458 }
459  
460 2 ▶sub _mounted_filesystems {
Subroutine _mounted_filesystems: called CC=11 SCAR=25.6
461 2 my %seen;
462 my @mounts;
463  
464 2 ▶ if (open my $fh, '<', '/proc/mounts') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/proc/mounts") 2 0 2
465 2 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 48 2 50
466 48 my ($device, $mount, $type, $opts) = (split /\s+/, $line)[0..3];
467 48 ▶ next if $seen{$mount}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$mount}++ 0 48 48
468 48 ▶ next if $mount =~ m{^/(?:proc|sys|dev|run|snap)};
Branch: 1 branch
BranchTrueFalseTotal
if $mount =~ m[^/(?:proc|sys|dev|run|snap)] 40 8 48
469 8 ▶ next if $type =~ /^(?:proc|sysfs|tmpfs|devtmpfs|cgroup.+|rpc_pipefs|overlay)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /^(?:proc|sysfs|tmpfs|devtmpfs|cgroup.+|rpc_pipefs|overlay)$/ 2 6 8
470 6 ▶ next unless defined $mount && length $mount;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mount and length $mount 0 6 6
Condition33%1/3
defined $mount and length $mount
!ll&&!rl&&r
006
MC/DC0%0/2
defined $mount and length $mount
defined $mount length $mount
Truth table33%1/3
A and B
A= defined $mountB= length $mount
ABresult
0X0
100
111
471  
472 6 ▶ my @options = defined $opts && length $opts ? split(/,/, $opts) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts && length $opts ? : 6 0 6
Condition33%1/3
defined $opts and length $opts
!ll&&!rl&&r
006
MC/DC0%0/2
defined $opts and length $opts
defined $opts length $opts
Truth table33%1/3
A and B
A= defined $optsB= length $opts
ABresult
0X0
100
111
473 6 ▶ my $read_only = grep { $_ eq 'ro' } @options ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@options ? : 6 0 6
474  
475 6 push @mounts, {
476 device => $device,
477 mount => $mount,
478 type => $type,
479 options => \@options,
480 read_only => $read_only,
481 };
482 }
483 2 close $fh;
484 }
485  
486 2 return \@mounts;
487 }
488  
489 3 ▶sub _mount_key {
Subroutine _mount_key: called CC=3 SCAR=11.3
490 3 my ($path) = @_;
491 3 ▶ return '' unless defined $path && length $path;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and length $path 0 3 3
Condition33%1/3
defined $path and length $path
!ll&&!rl&&r
003
MC/DC0%0/2
defined $path and length $path
defined $path length $path
Truth table33%1/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
492 3 my $key = $path;
493 3 $key =~ s{^/}{root_};
494 3 $key =~ s{[^A-Za-z0-9_.]}{_}g;
495 3 $key =~ s{_+}{_}g;
496 3 $key =~ s{_\z}{};
497 3 return $key;
498 }
499  
500 4 ▶sub to_json {
Subroutine to_json: called CC=2 SCAR=6.9
Pod documented
501 4 my ($data, %opts) = @_;
502 4 my $encoder = JSON::PP->new->canonical->ascii(0);
503 4 ▶ if ($opts{pretty}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"pretty"}) 1 3 4
504 1 $encoder = $encoder->pretty;
505 }
506 4 return $encoder->encode($data);
507 }
508  
509 2 ▶sub to_yaml {
Subroutine to_yaml: called CC=2 SCAR=7.0
Pod documented
510 2 my ($data) = @_;
511 2 my $yaml = _yaml_dump($data, 0);
512 2 ▶ $yaml .= "\n" unless $yaml =~ /\n\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $yaml =~ /\n\z/ 0 2 2
513 2 return $yaml;
514 }
515  
516 4 ▶sub to_prometheus {
Subroutine to_prometheus: called CC=8 SCAR=21.2
Pod documented
517 4 my ($data, %opts) = @_;
518 4 ▶ return '' unless defined $data;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 4 4
519  
520 4 my $prefix = _sanitize_prefix($opts{prefix});
521 4 my %base_labels;
522 4 ▶ if ($opts{labels} && ref $opts{labels} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"labels"} and ref $opts{"labels"} eq "HASH") 3 1 4
Condition66%2/3
$opts{"labels"} and ref $opts{"labels"} eq "HASH"
!ll&&!rl&&r
103
MC/DC50%1/2
$opts{"labels"} and ref $opts{"labels"} eq "HASH"
$opts{"labels"} ref $opts{"labels"} eq "HASH"
Truth table66%2/3
A and B
A= $opts{"labels"}B= ref $opts{"labels"} eq "HASH"
ABresult
0X0
100
111
523 3 for my $key (sort keys %{ $opts{labels} }) {
524 3 my $label = _label_name($key);
525 3 ▶ next unless length $label;
Branch: 1 branch
BranchTrueFalseTotal
unless length $label 0 3 3
526 3 $base_labels{$label} = $opts{labels}{$key};
527 }
528 }
529  
530 4 ▶ my $timestamp = $opts{timestamp} ? ($data->{timestamp} // _timestamp()) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'timestamp'} ? : 1 3 4
Condition33%1/3
$data->{"timestamp"} // _timestamp()
l!l&&r!l&&!r
100
MC/DC0%0/2
$data->{"timestamp"} // _timestamp()
$data->{"timestamp"} _timestamp()
Truth table33%1/3
A // B
A= $data->{"timestamp"}B= _timestamp()
ABresult
1X1
011
000
531  
532 4 my @lines;
533 my %type_declared;
534 4 _prometheus_walk($data, [], \%base_labels, \@lines, {
535 prefix => $prefix,
536 timestamp => $timestamp,
537 type_declared => \%type_declared,
538 });
539  
540 4 return join('', @lines);
541 }
542  
543 5 ▶sub _yaml_dump {
Subroutine _yaml_dump: called CC=11 SCAR=25.7
544 5 my ($data, $indent) = @_;
545 5 my $prefix = ' ' x $indent;
546  
547 5 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 0 5 5
548 0 return $prefix . "null\n";
549 }
550  
551 5 ▶ if (!ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 0 5 5
552 0 return $prefix . _yaml_scalar($data) . "\n";
553 }
554  
555 5 ▶ if (ref $data eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 4 1 5
556 4 my $out = '';
557 4 for my $key (sort keys %$data) {
558 11 my $value = $data->{$key};
559 11 ▶ if (_yaml_is_simple($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_yaml_is_simple($value)) { } 8 3 11
560 8 $out .= $prefix . $key . ': ' . _yaml_scalar($value) . "\n";
561 } else {
562 3 $out .= $prefix . "$key:\n" . _yaml_dump($value, $indent + 1);
563 }
564 }
565 4 ▶ return $out eq '' ? $prefix . "{}\n" : $out;
Branch: 1 branch
BranchTrueFalseTotal
$out eq '' ? : 0 4 4
566 }
567  
568 1 ▶ if (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 1 0 1
569 1 ▶ return $prefix . "[]\n" unless @$data;
Branch: 1 branch
BranchTrueFalseTotal
unless @$data 0 1 1
570  
571 1 my $out = '';
572 1 for my $value (@$data) {
573 2 ▶ if (_yaml_is_simple($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_yaml_is_simple($value)) { } 2 0 2
574 2 $out .= $prefix . '- ' . _yaml_scalar($value) . "\n";
575 } else {
576 0 $out .= $prefix . "-\n" . _yaml_dump($value, $indent + 1);
577 }
578 }
579 1 return $out;
580 }
581  
582 0 return $prefix . _yaml_scalar($data) . "\n";
583 }
584  
585 22 ▶sub _prometheus_walk {
Subroutine _prometheus_walk: called CC=15 SCAR=27.4
586 22 my ($value, $path, $labels, $output, $ctx) = @_;
587  
588 22 ▶ if (!ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $value) 11 11 22
589 11 _emit_prom_metric($path, $labels, $value, $output, $ctx);
590 11 return;
591 }
592  
593 11 ▶ if (ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH") 9 2 11
594 9 my %label_set = %$labels;
595 9 ▶ if (@$path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$path) 5 4 9
596 5 for my $key (sort keys %$value) {
597 13 my $v = $value->{$key};
598 13 ▶ if (!ref($v) && !looks_like_number($v)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $v or &looks_like_number($v)) 6 7 13
Condition100%3/3
ref $v or &looks_like_number($v)
l!l&&r!l&&!r
166
MC/DC100%2/2
ref $v or &looks_like_number($v)
ref $v &looks_like_number($v)
Truth table100%3/3
A or B
A= ref $vB= &looks_like_number($v)
ABresult
1X1
011
000
599 6 my $label_name = _label_name($key);
600 6 ▶ $label_set{$label_name} = $v if length $label_name && defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
if length $label_name and defined $v and $v ne "" 6 0 6
Condition33%2/6
length $label_name and defined $v and $v ne ""
!ll&&!rl&&r
006
length $label_name and defined $v
!ll&&!rl&&r
006
MC/DC0%0/3
length $label_name and defined $v and $v ne ""
length $label_name defined $v $v ne ""
Truth table25%1/4
A and B and C
A= length $label_nameB= defined $vC= $v ne ""
ABCresult
0XX0
10X0
1100
1111
601 }
602 }
603 }
604  
605 9 for my $key (sort keys %$value) {
606 23 my $v = $value->{$key};
607 23 ▶ next if !ref($v) && !looks_like_number($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v or &looks_like_number($v) 8 15 23
Condition100%3/3
ref $v or &looks_like_number($v)
l!l&&r!l&&!r
698
MC/DC100%2/2
ref $v or &looks_like_number($v)
ref $v &looks_like_number($v)
Truth table100%3/3
A or B
A= ref $vB= &looks_like_number($v)
ABresult
1X1
011
000
608 15 _prometheus_walk($v, [@$path, $key], \%label_set, $output, $ctx);
609 }
610 9 return;
611 }
612  
613 2 ▶ if (ref $value eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "ARRAY") 2 0 2
614 2 for my $i (0 .. $#$value) {
615 3 my %with_index = (%$labels, index => $i);
616 3 _prometheus_walk($value->[$i], $path, \%with_index, $output, $ctx);
617 }
618 2 return;
619 }
620 }
621  
622 11 ▶sub _emit_prom_metric {
Subroutine _emit_prom_metric: called CC=7 SCAR=20.0
623 11 my ($path, $labels, $value, $output, $ctx) = @_;
624 11 ▶ return unless defined $value && looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and &looks_like_number($value) 0 11 11
Condition33%1/3
defined $value and &looks_like_number($value)
!ll&&!rl&&r
0011
MC/DC0%0/2
defined $value and &looks_like_number($value)
defined $value &looks_like_number($value)
Truth table33%1/3
A and B
A= defined $valueB= &looks_like_number($value)
ABresult
0X0
100
111
625 11 ▶ return unless @$path;
Branch: 1 branch
BranchTrueFalseTotal
unless @$path 0 11 11
626  
627 11 my $name = _metric_name($path, $ctx->{prefix});
628 11 ▶ return unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 11 11
629  
630 11 ▶ if (!$ctx->{type_declared}{$name}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ctx->{"type_declared"}{$name}++) 10 1 11
631 10 push @$output, "# TYPE $name gauge\n";
632 }
633  
634 11 my $label_str = _format_prom_labels($labels);
635 11 my $line = $name . $label_str . ' ' . $value;
636 11 ▶ if (defined $ctx->{timestamp}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ctx->{"timestamp"}) 8 3 11
637 8 $line .= ' ' . $ctx->{timestamp};
638 }
639 11 push @$output, $line . "\n";
640 }
641  
642 11 ▶sub _format_prom_labels {
Subroutine _format_prom_labels: called CC=7 SCAR=19.7
643 11 my ($labels) = @_;
644 11 ▶ return '' unless $labels && %$labels;
Branch: 1 branch
BranchTrueFalseTotal
unless $labels and %$labels 1 10 11
Condition66%2/3
$labels and %$labels
!ll&&!rl&&r
0110
MC/DC50%1/2
$labels and %$labels
$labels %$labels
Truth table66%2/3
A and B
A= $labelsB= %$labels
ABresult
0X0
100
111
645  
646 10 my @pairs;
647 10 for my $key (sort keys %$labels) {
648 25 my $name = _label_name($key);
649 25 ▶ next unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 25 25
650 25 ▶ my $value = defined $labels->{$key} ? $labels->{$key} : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $labels->{$key} ? : 25 0 25
651 25 my $escaped = $value;
652 25 $escaped =~ s/\\/\\\\/g;
653 25 $escaped =~ s/"/\\"/g;
654 25 $escaped =~ s/\n/\\n/g;
655 25 push @pairs, $name . '="' . $escaped . '"';
656 }
657  
658 10 ▶ return '' unless @pairs;
Branch: 1 branch
BranchTrueFalseTotal
unless @pairs 0 10 10
659 10 return '{' . join(',', @pairs) . '}';
660 }
661  
662 4 ▶sub _sanitize_prefix {
Subroutine _sanitize_prefix: called CC=5 SCAR=16.5
663 4 my ($prefix) = @_;
664 4 ▶ return '' unless defined $prefix && length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prefix and length $prefix 1 3 4
Condition66%2/3
defined $prefix and length $prefix
!ll&&!rl&&r
103
MC/DC50%1/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table66%2/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
665 3 my $clean = _snake_case($prefix);
666 3 ▶ return '' unless length $clean;
Branch: 1 branch
BranchTrueFalseTotal
unless length $clean 0 3 3
667 3 ▶ $clean .= '_' unless $clean =~ /_\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $clean =~ /_\z/ 3 0 3
668 3 return $clean;
669 }
670  
671 11 ▶sub _metric_name {
Subroutine _metric_name: called CC=5 SCAR=16.3
672 11 my ($path, $prefix) = @_;
673 11 my @parts = map { _snake_case($_) } @$path;
674 11 @parts = grep { length } @parts;
675 11 ▶ return '' unless @parts;
Branch: 1 branch
BranchTrueFalseTotal
unless @parts 0 11 11
676  
677 11 my $name = join('_', @parts);
678 11 ▶ $name = $prefix . $name if defined $prefix && length $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if defined $prefix and length $prefix 10 1 11
Condition66%2/3
defined $prefix and length $prefix
!ll&&!rl&&r
0110
MC/DC50%1/2
defined $prefix and length $prefix
defined $prefix length $prefix
Truth table66%2/3
A and B
A= defined $prefixB= length $prefix
ABresult
0X0
100
111
679 11 ▶ $name = '_' . $name if $name !~ /^[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[A-Za-z_]/ 0 11 11
680 11 return $name;
681 }
682  
683 34 ▶sub _label_name {
Subroutine _label_name: called CC=3 SCAR=11.3
684 34 my ($name) = @_;
685 34 $name = _snake_case($name);
686 34 ▶ return '' unless length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless length $name 0 34 34
687 34 ▶ $name = '_' . $name if $name !~ /^[A-Za-z_]/;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[A-Za-z_]/ 0 34 34
688 34 return $name;
689 }
690  
691 55 ▶sub _snake_case {
Subroutine _snake_case: called CC=3 SCAR=11.3
692 55 my ($text) = @_;
693 55 ▶ return '' unless defined $text && length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 0 55 55
Condition33%1/3
defined $text and length $text
!ll&&!rl&&r
0055
MC/DC0%0/2
defined $text and length $text
defined $text length $text
Truth table33%1/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
694 55 $text =~ s/([a-z0-9])([A-Z])/$1_$2/g;
695 55 $text =~ s/[^A-Za-z0-9]+/_/g;
696 55 $text = lc $text;
697 55 $text =~ s/^_+//;
698 55 $text =~ s/_+$//;
699 55 $text =~ s/_+/_/g;
700 55 return $text;
701 }
702  
703 13 ▶sub _yaml_is_simple {
Subroutine _yaml_is_simple: called CC=1 SCAR=0.0
704 13 my ($value) = @_;
705 13 return !ref($value);
706 }
707  
708 10 ▶sub _yaml_scalar {
Subroutine _yaml_scalar: called CC=6 SCAR=18.2
709 10 my ($value) = @_;
710  
711 10 ▶ return 'null' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 10 10
712  
713 10 ▶ if (looks_like_number($value) && $value !~ /^0[0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (&looks_like_number($value) and not $value =~ /^0[0-9]/) 3 7 10
Condition66%2/3
&looks_like_number($value) and not $value =~ /^0[0-9]/
!ll&&!rl&&r
703
MC/DC50%1/2
&looks_like_number($value) and not $value =~ /^0[0-9]/
&looks_like_number($value) not $value =~ /^0[0-9]/
Truth table66%2/3
A and not(B)
A= &looks_like_number($value)B= not $value =~ /^0[0-9]/
ABresult
0X0
100
111
714 3 return $value;
715 }
716  
717 7 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq "") 0 7 7
718 0 return "''";
719 }
720  
721 7 ▶ if ($value =~ /[\s#:>{}\[\],]|\A[-?]|\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /[\s#:>{}\[\],]|\A[-?]|\n/) 1 6 7
722 1 my $escaped = $value;
723 1 $escaped =~ s/'/''/g;
724 1 $escaped =~ s/\n/\\n/g;
725 1 return "'$escaped'";
726 }
727  
728 6 return $value;
729 }
730  
731 7 ▶sub _timestamp {
Subroutine _timestamp: called CC=1 SCAR=0.0
732 7 my @t = gmtime();
733 7 return sprintf("%04d-%02d-%02dT%02d:%02d:%02dZ", $t[5]+1900,$t[4]+1,@t[3,2,1,0]);
734 }
735  
736 5 ▶sub available_metrics {
Subroutine available_metrics: called CC=1 SCAR=0.0
Pod documented
737 5 return sort keys %COLLECTORS;
738 }
739  
740 5 ▶sub metrics_from_thresholds {
Subroutine metrics_from_thresholds: called CC=6 SCAR=19.0
Pod documented
741 5 my ($warn, $crit) = @_;
742 5 my %roots;
743 5 ▶ for my $expr (@{ $warn // [] }, @{ $crit // [] }) {
Condition50%2/4
$warn // []
l!l
50
$crit // []
l!l
50
MC/DC0%0/1
$warn // []
$warn
MC/DC0%0/1
$crit // []
$crit
Truth table50%1/2
A
A= $warn
Aresult
11
00
Truth table50%1/2
A
A= $crit
Aresult
11
00
744 2 my $rule = _parse_threshold_expression($expr);
745 2 ▶ next unless $rule->{parts} && @{ $rule->{parts} };
Branch: 1 branch
BranchTrueFalseTotal
unless $rule->{"parts"} and @{$rule->{"parts"};} 0 2 2
Condition33%1/3
$rule->{"parts"} and @{$rule->{"parts"};}
!ll&&!rl&&r
002
MC/DC0%0/2
$rule->{"parts"} and @{$rule->{"parts"};}
$rule->{"parts"} @{$rule->{"parts"};}
Truth table33%1/3
A and B
A= $rule->{"parts"}B= @{$rule->{"parts"};}
ABresult
0X0
100
111
746 2 $roots{ $rule->{parts}[0] } = 1;
747 }
748 5 return sort keys %roots;
749 }
750  
751 4 ▶sub evaluate_thresholds {
Subroutine evaluate_thresholds: called CC=8 SCAR=20.8
Pod documented
752 4 my ($data, %opts) = @_;
753 4 ▶ die "evaluate_thresholds requires a hash reference of data" unless ref $data eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $data eq "HASH" 0 4 4
754  
755 4 ▶ my @warn_expr = @{ $opts{warn} // [] };
Condition100%2/2
$opts{"warn"} // []
l!l
22
MC/DC100%1/1
$opts{"warn"} // []
$opts{"warn"}
Truth table100%2/2
A
A= $opts{"warn"}
Aresult
11
00
756 4 ▶ my @crit_expr = @{ $opts{crit} // [] };
Condition100%2/2
$opts{"crit"} // []
l!l
31
MC/DC100%1/1
$opts{"crit"} // []
$opts{"crit"}
Truth table100%2/2
A
A= $opts{"crit"}
Aresult
11
00
757  
758 4 my @rules;
759 4 push @rules, map { _build_threshold_rule($_, 'warn') } @warn_expr;
760 4 push @rules, map { _build_threshold_rule($_, 'crit') } @crit_expr;
761  
762 4 my %paths;
763 4 for my $rule (@rules) {
764 6 push @{ $paths{ $rule->{path} }{ $rule->{severity} } }, $rule;
765 6 $paths{ $rule->{path} }{parts} = $rule->{parts};
766 }
767  
768 4 my $status = 0;
769 4 my @messages;
770 4 for my $path (sort keys %paths) {
771 5 my $info = $paths{$path};
772 5 my $value = _dig_value($data, $info->{parts});
773 5 my $path_status = _evaluate_path_thresholds($value, $info);
774 5 ▶ $status = $path_status if $path_status > $status;
Branch: 1 branch
BranchTrueFalseTotal
if $path_status > $status 3 2 5
775 5 push @messages, _format_threshold_message($path, $value, $info);
776 }
777  
778 4 my $label = _status_label($status);
779 4 ▶ my $message = @messages ? join(' ', $label . ' -', join('; ', @messages)) : $label;
Branch: 1 branch
BranchTrueFalseTotal
@messages ? : 4 0 4
780  
781 return {
782 4 status => $status,
783 label => $label,
784 message => $message,
785 details => \%paths,
786 };
787 }
788  
789 6 ▶sub _build_threshold_rule {
Subroutine _build_threshold_rule: called CC=1 SCAR=0.0
790 6 my ($expr, $severity) = @_;
791 6 my $parsed = _parse_threshold_expression($expr);
792 6 $parsed->{severity} = $severity;
793 6 return $parsed;
794 }
795  
796 8 ▶sub _parse_threshold_expression {
Subroutine _parse_threshold_expression: called CC=4 SCAR=14.8
797 8 my ($expr) = @_;
798 8 ▶ die "Invalid threshold expression" unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 8 8
799  
800 8 my ($path, $op, $value) = $expr =~ /^([A-Za-z0-9_\.]+)\s*(>=|<=|==|!=|>|<)\s*(.+)$/;
801 8 ▶ die "Invalid threshold expression: $expr" unless defined $path && defined $op;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path and defined $op 0 8 8
Condition33%1/3
defined $path and defined $op
!ll&&!rl&&r
008
MC/DC0%0/2
defined $path and defined $op
defined $path defined $op
Truth table33%1/3
A and B
A= defined $pathB= defined $op
ABresult
0X0
100
111
802  
803 8 $value = _maybe_number($value);
804 8 my @parts = split /\./, $path;
805 8 return { path => $path, op => $op, expect => $value, parts => \@parts };
806 }
807  
808 5 ▶sub _dig_value {
Subroutine _dig_value: called CC=7 SCAR=21.8
809 5 my ($data, $parts) = @_;
810 5 my $current = $data;
811 5 for my $part (@$parts) {
812 9 ▶ if (ref $current eq 'HASH' && exists $current->{$part}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current eq "HASH" and exists $current->{$part}) 8 1 9
Condition66%2/3
ref $current eq "HASH" and exists $current->{$part}
!ll&&!rl&&r
018
MC/DC50%1/2
ref $current eq "HASH" and exists $current->{$part}
ref $current eq "HASH" exists $current->{$part}
Truth table66%2/3
A and B
A= ref $current eq "HASH"B= exists $current->{$part}
ABresult
0X0
100
111
813 8 $current = $current->{$part};
814 8 next;
815 }
816 1 ▶ if (ref $current eq 'ARRAY' && $part =~ /^\d+$/ && $part < @$current) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $current eq "ARRAY" and $part =~ /^\d+$/ and $part < @$current) 0 1 1
Condition33%2/6
ref $current eq "ARRAY" and $part =~ /^\d+$/ and $part < @$current
!ll&&!rl&&r
100
ref $current eq "ARRAY" and $part =~ /^\d+$/
!ll&&!rl&&r
100
MC/DC0%0/3
ref $current eq "ARRAY" and $part =~ /^\d+$/ and $part < @$current
ref $current eq "ARRAY" $part =~ /^\d+$/ $part < @$current
Truth table25%1/4
A and B and C
A= ref $current eq "ARRAY"B= $part =~ /^\d+$/C= $part < @$current
ABCresult
0XX0
10X0
1100
1111
817 0 $current = $current->[$part];
818 0 next;
819 }
820 1 return undef;
821 }
822 4 return $current;
823 }
824  
825 5 ▶sub _evaluate_path_thresholds {
Subroutine _evaluate_path_thresholds: called CC=8 SCAR=20.8
826 5 my ($value, $info) = @_;
827  
828 5 ▶ return 2 unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 4 5
829  
830 4 ▶ for my $rule (@{ $info->{crit} // [] }) {
Condition100%2/2
$info->{"crit"} // []
l!l
22
MC/DC100%1/1
$info->{"crit"} // []
$info->{"crit"}
Truth table100%2/2
A
A= $info->{"crit"}
Aresult
11
00
831 2 ▶ return 2 if _threshold_matched($value, $rule);
Branch: 1 branch
BranchTrueFalseTotal
if _threshold_matched($value, $rule) 1 1 2
832 }
833 3 ▶ for my $rule (@{ $info->{warn} // [] }) {
Condition50%1/2
$info->{"warn"} // []
l!l
30
MC/DC0%0/1
$info->{"warn"} // []
$info->{"warn"}
Truth table50%1/2
A
A= $info->{"warn"}
Aresult
11
00
834 3 ▶ return 1 if _threshold_matched($value, $rule);
Branch: 1 branch
BranchTrueFalseTotal
if _threshold_matched($value, $rule) 1 2 3
835 }
836 2 return 0;
837 }
838  
839 5 ▶sub _threshold_matched {
Subroutine _threshold_matched: called CC=9 SCAR=32.8
840 5 my ($value, $rule) = @_;
841  
842 5 ▶ return 0 unless defined $rule;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rule 0 5 5
843 5 my $expected = $rule->{expect};
844 5 my $op = $rule->{op};
845  
846 5 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 5 5
847 0 return 0;
848 }
849  
850 5 ▶ if ($op eq '>') { return $value > $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq ">") 3 2 5
851 2 ▶ if ($op eq '>=') { return $value >= $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq ">=") 2 0 2
852 0 ▶ if ($op eq '<') { return $value < $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<") 0 0 0
853 0 ▶ if ($op eq '<=') { return $value <= $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "<=") 0 0 0
854 0 ▶ if ($op eq '==') { return $value == $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "==") 0 0 0
855 0 ▶ if ($op eq '!=') { return $value != $expected }
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "!=") 0 0 0
856 0 return 0;
857 }
858  
859 5 ▶sub _format_threshold_message {
Subroutine _format_threshold_message: called CC=5 SCAR=16.1
860 5 my ($path, $value, $info) = @_;
861 5 ▶ my $value_str = defined $value ? $value : 'N/A';
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 4 1 5
862  
863 5 my @parts;
864 5 ▶ if (my $warn = $info->{warn}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $warn = $info->{"warn"}) 3 2 5
865 3 push @parts, map { _format_rule($_) } @$warn;
866 }
867 5 ▶ if (my $crit = $info->{crit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $crit = $info->{"crit"}) 3 2 5
868 3 push @parts, map { _format_rule($_) } @$crit;
869 }
870  
871 5 ▶ my $suffix = @parts ? '(' . join(' ', @parts) . ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 5 0 5
872 5 return join('', $path, '=', $value_str, ' ', $suffix) =~ s/\s+\z//r;
873 }
874  
875 6 ▶sub _format_rule {
Subroutine _format_rule: called CC=1 SCAR=0.0
876 6 my ($rule) = @_;
877 6 return sprintf('%s%s', $rule->{op}, $rule->{expect});
878 }
879  
880 4 ▶sub _status_label {
Subroutine _status_label: called CC=3 SCAR=11.0
881 4 my ($status) = @_;
882 4 ▶ return $status == 2 ? 'CRIT' : $status == 1 ? 'WARN' : 'OK';
Branch: 2 branches
BranchTrueFalseTotal
$status == 2 ? : 2 2 4
$status == 1 ? : 1 1 2
883 }
884  
885 10 ▶sub _percent {
Subroutine _percent: called CC=4 SCAR=14.6
886 10 my ($num, $den) = @_;
887 10 ▶ return 0 unless defined $num && defined $den && $den;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $num and defined $den and $den 1 9 10
Condition50%3/6
defined $num and defined $den and $den
!ll&&!rl&&r
019
defined $num and defined $den
!ll&&!rl&&r
0010
MC/DC33%1/3
defined $num and defined $den and $den
defined $num defined $den $den
Truth table50%2/4
A and B and C
A= defined $numB= defined $denC= $den
ABCresult
0XX0
10X0
1100
1111
888 9 return sprintf('%.1f', ($num / $den) * 100);
889 }
890  
891 7 ▶sub _uptime_seconds {
Subroutine _uptime_seconds: called CC=3 SCAR=11.3
892 7 ▶ open my $fh, '<', '/proc/uptime' or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", "/proc/uptime" or return undef 7 0 7
893 7 ▶ my $line = <$fh> // '';
Condition50%1/2
readline $fh // ""
l!l
70
MC/DC0%0/1
readline $fh // ""
readline $fh
Truth table50%1/2
A
A= readline $fh
Aresult
11
00
894 7 close $fh;
895 7 my ($uptime) = split /\s+/, $line;
896 7 return _maybe_number($uptime);
897 }
898  
899 4 ▶sub _cpu_cores {
Subroutine _cpu_cores: called CC=5 SCAR=16.2
900 4 my $count = 0;
901 4 ▶ if (open my $fh, '<', '/proc/cpuinfo') {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", "/proc/cpuinfo") 4 0 4
902 4 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 1404 4 1408
903 1404 ▶ $count++ if $line =~ /^processor\s*:\s*\d+/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^processor\s*:\s*\d+/ 52 1352 1404
904 }
905 4 close $fh;
906 }
907 4 ▶ return $count || undef;
Condition50%1/2
$count || undef
l!l
40
MC/DC0%0/1
$count || undef
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
908 }
909  
910 218 ▶sub _maybe_number {
Subroutine _maybe_number: called CC=3 SCAR=11.7
911 218 my ($value) = @_;
912 218 ▶ return undef unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 218 218
913 218 ▶ return looks_like_number($value) ? 0 + $value : $value;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($value) ? : 218 0 218
914 }
915  
916 0 ▶sub _sector_size {
Subroutine _sector_size: not called CC=6 SCAR=37.4
917 0 my ($device) = @_;
918 0 ▶ return 512 unless defined $device && length $device;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $device and length $device 0 0 0
Condition0%0/3
defined $device and length $device
!ll&&!rl&&r
000
MC/DC0%0/2
defined $device and length $device
defined $device length $device
Truth table0%0/3
A and B
A= defined $deviceB= length $device
ABresult
0X0
100
111
919 0 my $path = "/sys/block/$device/queue/hw_sector_size";
920 0 ▶ if (open my $fh, '<', $path) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, "<", $path) 0 0 0
921 0 my $size = <$fh>;
922 0 close $fh;
923 0 ▶ if (defined $size) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $size) 0 0 0
924 0 $size =~ s/\s+//g;
925 0 ▶ return looks_like_number($size) ? 0 + $size : 512;
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($size) ? : 0 0 0
926 }
927 }
928 0 return 512;
929 }
930  
931 8 1;
932 __END__
↑ Summary script/sys-monitor-lite »