lib/Rex/Hardware/Swap.pm

filter: Statementstmt 48.5% 34 / 70 Branchbran 23.5% 8 / 34 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 85.7% 6 / 7 Podpod 0.0% 0 / 1 totaltotal 36.9% 48 / 130 CC 23 SCAR 47.6
CC
23
Cov
36%
get
169.8
__ANON__
30.0
BEGIN
1.0
CRAP 116.6
« lib/Rex/Interface/Shell/Csh.pm ↑ Summary lib/Rex/Commands/Run.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Hardware::Swap;
6  
7 3 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 our $VERSION = '1.16.1'; # VERSION
11  
12 3 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 3 ▶use Rex::Hardware::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 require Rex::Hardware;
17  
18 9 ▶sub get {
Subroutine get: called CC=19 SCAR=51.3
Pod undocumented
19  
20 9 my $cache = Rex::get_cache();
21 9 my $cache_key_name = $cache->gen_key_name("hardware.swap");
22  
23 9 ▶ if ( $cache->valid($cache_key_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid($cache_key_name)) 0 9 9
24 0 return $cache->get($cache_key_name);
25 }
26  
27 9 my $os = Rex::Hardware::Host::get_operating_system();
28  
29 0 ▶ my $convert = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
30  
31 0 ▶ if ( !defined $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_[0]) 0 0 0
32 0 return 0;
33 }
34  
35 0 ▶ if ( $_[1] eq "G" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[1] eq 'G') { } 0 0 0
elsif ($_[1] eq 'M') { } 0 0 0
elsif ($_[1] eq 'K') { } 0 0 0
36 0 $_[0] = $_[0] * 1024 * 1024 * 1024;
37 }
38 elsif ( $_[1] eq "M" ) {
39 0 $_[0] = $_[0] * 1024 * 1024;
40 }
41 elsif ( $_[1] eq "K" ) {
42 0 $_[0] = $_[0] * 1024;
43 }
44  
45 9 };
46  
47 9 my $data = {};
48  
49 9 ▶ if ( $os eq "Windows" ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($os eq 'Windows') { } 0 9 9
elsif ($os eq 'SunOS') { } 0 9 9
elsif ($os eq 'OpenBSD') { } 0 9 9
elsif ($os eq 'NetBSD') { } 0 9 9
elsif ($os =~ /FreeBSD/u) { } 0 9 9
50 0 my $conn = Rex::get_current_connection()->{conn};
51 $data = {
52 used => $conn->post("/os/swap/used")->{used},
53 total => $conn->post("/os/swap/max")->{max},
54 free => $conn->post("/os/swap/free")->{free},
55 0 };
56 }
57 elsif ( $os eq "SunOS" ) {
58 0 my ($swap_str) = i_run( "swap -s", fail_ok => 1 );
59  
60 0 my ( $used, $u_ent, $avail, $a_ent ) =
61 ( $swap_str =~ m/(\d+)([a-z]) used, (\d+)([a-z]) avail/ );
62  
63 0 &$convert( $used, uc($u_ent) );
64 0 &$convert( $avail, uc($a_ent) );
65  
66 0 $data = {
67 total => $used + $avail,
68 used => $used,
69 free => $avail,
70 };
71 }
72 elsif ( $os eq "OpenBSD" ) {
73 0 my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1;
74  
75 0 my ( $used, $u_ent, $total, $t_ent ) =
76 ( $swap_str =~ m/Swap: (\d+)([a-z])\/(\d+)([a-z])/i );
77  
78 0 &$convert( $used, $u_ent );
79 0 &$convert( $total, $t_ent );
80  
81 0 $data = {
82 total => $total,
83 used => $used,
84 free => $total - $used,
85 };
86 }
87 elsif ( $os eq "NetBSD" ) {
88 0 my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1;
89  
90 0 my ( $total, $t_ent, $free, $f_ent ) =
91 ( $swap_str =~ m/(\d+)([a-z])[^\d]+(\d+)([a-z])/i );
92  
93 0 &$convert( $total, $t_ent );
94 0 &$convert( $free, $f_ent );
95  
96 0 $data = {
97 total => $total,
98 used => $total - $free,
99 free => $free,
100 };
101  
102 }
103 elsif ( $os =~ /FreeBSD/ ) {
104 0 my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1;
105  
106 0 my ( $total, $t_ent, $used, $u_ent, $free, $f_ent ) =
107 ( $swap_str =~ m/(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])/i );
108  
109 0 ▶ if ( !$total ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($total) 0 0 0
110 0 ( $total, $t_ent, $free, $f_ent ) =
111 ( $swap_str =~ m/(\d+)([a-z])[^\d]+(\d+)([a-z])/i );
112 }
113  
114 0 ▶ &$convert( $total, $t_ent ) if ($total);
Branch: 1 branch
BranchTrueFalseTotal
if $total 0 0 0
115 0 ▶ &$convert( $used, $u_ent ) if ($used);
Branch: 1 branch
BranchTrueFalseTotal
if $used 0 0 0
116 0 ▶ &$convert( $free, $f_ent ) if ($free);
Branch: 1 branch
BranchTrueFalseTotal
if $free 0 0 0
117  
118 0 ▶ if ( !$used && $total && $free ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $used and $total and $free) 0 0 0
Condition0%0/6
not $used and $total and $free
!ll&&!rl&&r
000
not $used and $total
!ll&&!rl&&r
000
MC/DC0%0/3
not $used and $total and $free
not $used $total $free
Truth table0%0/4
not(A) and B and C
A= not $usedB= $totalC= $free
ABCresult
0XX0
10X0
1100
1111
119 0 $used = $total - $free;
120 }
121  
122 $data = {
123 0 ▶ total => $total || 0,
Condition0%0/6
$total || 0
l!l
00
$used || 0
l!l
00
$free || 0
l!l
00
MC/DC0%0/1
$total || 0
$total
MC/DC0%0/1
$used || 0
$used
MC/DC0%0/1
$free || 0
$free
Truth table0%0/2
A
A= $total
Aresult
11
00
Truth table0%0/2
A
A= $used
Aresult
11
00
Truth table0%0/2
A
A= $free
Aresult
11
00
124 used => $used || 0,
125 free => $free || 0,
126 };
127 }
128 else {
129 # linux as default
130 9 ▶ if ( !can_run("free") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_run("free")) 0 9 9
131 0 $data = {
132 total => 0,
133 used => 0,
134 free => 0,
135 shared => 0,
136 buffers => 0,
137 cached => 0,
138 };
139 }
140  
141 9 my $free_str = [ grep { /^Swap:/ } i_run( "free -m", fail_ok => 1 ) ]->[0];
142 9 ▶ if ( !$free_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $free_str) { } 0 9 9
143 0 $data = {
144 total => 0,
145 used => 0,
146 free => 0,
147 };
148 }
149  
150 else {
151  
152 9 $free_str =~ s/\r//g;
153 9 $free_str =~ s/^\s+|\s+$//g;
154  
155 9 my ( $total, $used, $free ) =
156 ( $free_str =~ m/^Swap:\s+(\d+)\s+(\d+)\s+(\d+)$/ );
157  
158 9 $data = {
159 total => $total,
160 used => $used,
161 free => $free,
162 };
163  
164 }
165  
166 }
167  
168 9 $cache->set( $cache_key_name, $data );
169  
170 9 return $data;
171 }
172  
173 3 1;
« lib/Rex/Interface/Shell/Csh.pm ↑ Summary lib/Rex/Commands/Run.pm »