lib/Rex/Hardware/Memory.pm

filter: Statementstmt 40.0% 48 / 120 Branchbran 33.3% 10 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.0% 9 / 10 Podpod 0.0% 0 / 1 totaltotal 41.6% 67 / 161 CC 17 SCAR 39.5
CC
17
Cov
39%
get
96.7
__ANON__
20.0
__parse_top_output
2.0
CRAP 51.9
« lib/Rex/Cloud/Jiffybox.pm ↑ Summary lib/Rex/Pkg/Arch.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 package Rex::Hardware::Memory;
6  
7 37 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 37 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 37 our $VERSION = '1.16.1'; # VERSION
11  
12 37 ▶use English qw(-no_match_vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 37 ▶use Rex::Hardware::Host;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 37 ▶use Rex::Commands::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 37 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 37 ▶use Rex::Commands::Sysctl;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 37 require Rex::Hardware;
19  
20 9 ▶sub get {
Subroutine get: called CC=13 SCAR=45.7
Pod undocumented
21  
22 9 my $cache = Rex::get_cache();
23 9 my $cache_key_name = $cache->gen_key_name("hardware.memory");
24  
25 9 ▶ if ( $cache->valid($cache_key_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cache->valid($cache_key_name)) 0 9 9
26 0 return $cache->get($cache_key_name);
27 }
28  
29 9 my $os = Rex::Hardware::Host::get_operating_system();
30  
31 0 ▶ my $convert = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
32  
33 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
34 0 $_[0] = $_[0] * 1024 * 1024 * 1024;
35 }
36 elsif ( $_[1] eq "M" ) {
37 0 $_[0] = $_[0] * 1024 * 1024;
38 }
39 elsif ( $_[1] eq "K" ) {
40 0 $_[0] = $_[0] * 1024;
41 }
42  
43 9 };
44  
45 9 my $data = {};
46  
47 9 ▶ if ( $os eq "Windows" ) {
Branch: 6 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
elsif ($os eq 'OpenWrt') { } 0 9 9
48 0 my $conn = Rex::get_current_connection()->{conn};
49 $data = {
50 used => $conn->post("/os/memory/used")->{used},
51 total => $conn->post("/os/memory/max")->{max},
52 free => $conn->post("/os/memory/free")->{free},
53 0 };
54 }
55 elsif ( $os eq "SunOS" ) {
56 0 my @data = i_run "echo ::memstat | mdb -k", fail_ok => 1;
57  
58 0 ▶ if ( $CHILD_ERROR == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Rex::Hardware::Memory::CHILD_ERROR == 0) { } 0 0 0
59 my ($free_cache) =
60 0 map { /\D+\d+\s+(\d+)/ } grep { /^Free \(cache/ } @data;
61 my ($free_list) =
62 0 map { /\D+\d+\s+(\d+)/ } grep { /^Free (\s+|\(freel)/ } @data;
63 0 my ($page_cache) = map { /\s+\d+\s+(\d+)/ } grep { /^Page cache/ } @data;
64  
65 0 my $free = $free_cache + $free_list;
66  
67 #my ($total, $total_e) = grep { $_=$1 if /^Memory Size: (\d+) ([a-z])/i } i_run "prtconf";
68 0 my ($total) = map { /\s+\d+\s+(\d+)/ } grep { /^Total/ } @data;
69  
70 0 &$convert( $free, "M" );
71 0 &$convert( $total, "M" );
72 0 my $used = $total - $free;
73  
74 0 $data = {
75 used => $used,
76 total => $total,
77 free => $free,
78 };
79 }
80 else {
81 0 $data = {
82 used => 0,
83 total => 0,
84 free => 0,
85 };
86 }
87 }
88 elsif ( $os eq "OpenBSD" ) {
89 0 my $mem_str = i_run "top -d1 | grep Memory:", fail_ok => 1;
90 0 my $total_mem = sysctl("hw.physmem");
91  
92 0 my ( $phys_mem, $p_m_ent, $virt_mem, $v_m_ent, $free, $f_ent ) =
93 ( $mem_str =~ m/(\d+)([a-z])\/(\d+)([a-z])[^\d]+(\d+)([a-z])/i );
94  
95 0 &$convert( $phys_mem, $p_m_ent );
96 0 &$convert( $virt_mem, $v_m_ent );
97 0 &$convert( $free, $f_ent );
98  
99 0 $data = {
100 used => $phys_mem + $virt_mem,
101 total => $total_mem,
102 free => $free,
103 };
104  
105 }
106 elsif ( $os eq "NetBSD" ) {
107 0 my $mem_str = i_run "top -d1 | grep Memory:", fail_ok => 1;
108 0 my $total_mem = sysctl("hw.physmem");
109  
110 my (
111 0 $active, $a_ent, $wired, $w_ent, $exec,
112 $e_ent, $file, $f_ent, $free, $fr_ent
113 )
114 = ( $mem_str =~
115 m/(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])/i
116 );
117  
118 0 &$convert( $active, $a_ent );
119 0 &$convert( $wired, $w_ent );
120 0 &$convert( $exec, $e_ent );
121 0 &$convert( $file, $f_ent );
122 0 &$convert( $free, $fr_ent );
123  
124 0 $data = {
125 total => $total_mem,
126 used => $active + $exec + $file + $wired,
127 free => $free,
128 file => $file,
129 exec => $exec,
130 wired => $wired,
131 };
132  
133 }
134 elsif ( $os =~ /FreeBSD/ ) {
135 0 my $mem_str = i_run "top -d1 | grep Mem:", fail_ok => 1;
136 0 my $total_mem = sysctl("hw.physmem");
137  
138 0 my $memory_details = __parse_top_output($mem_str);
139  
140 0 for my $stat (qw(active inactive wired laundry cache buf free)) {
141  
142 0 ▶ if ( exists $memory_details->{$stat} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $memory_details->{$stat}) { } 0 0 0
143  
144 0 my ( $value, $unit ) = $memory_details->{$stat} =~ qr{(\d+)([KMG])}msx;
145  
146 0 $memory_details->{$stat} = $value;
147  
148 0 &$convert( $memory_details->{$stat}, $unit );
149 }
150 else {
151 0 $memory_details->{$stat} = 0;
152 }
153 }
154  
155 $data = {
156 total => $total_mem,
157 used => $memory_details->{active} +
158 $memory_details->{inactive} +
159 $memory_details->{wired} +
160 $memory_details->{laundry},
161 free => $memory_details->{free},
162 cached => $memory_details->{cache},
163 buffers => $memory_details->{buf},
164 0 };
165 }
166 elsif ( $os eq "OpenWrt" ) {
167 0 my @data = i_run "cat /proc/meminfo", fail_ok => 1;
168  
169 0 my ($total) = map { /(\d+)/ } grep { /^MemTotal:/ } @data;
170 0 my ($free) = map { /(\d+)/ } grep { /^MemFree:/ } @data;
171 0 my ($shared) = map { /(\d+)/ } grep { /^Shmem:/ } @data;
172 0 my ($buffers) = map { /(\d+)/ } grep { /^Buffers:/ } @data;
173 0 my ($cached) = map { /(\d+)/ } grep { /^Cached:/ } @data;
174  
175 0 $data = {
176 total => $total,
177 used => $total - $free,
178 free => $free,
179 shared => $shared,
180 buffers => $buffers,
181 cached => $cached
182 };
183 }
184 else {
185 # default for linux
186 9 ▶ if ( !can_run("free") ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_run("free")) 0 9 9
187 0 $data = {
188 total => 0,
189 used => 0,
190 free => 0,
191 shared => 0,
192 buffers => 0,
193 cached => 0,
194 };
195 }
196  
197 9 my $free_str = [ grep { /^Mem:/ } i_run( "free -m", fail_ok => 1 ) ]->[0];
198  
199 9 ▶ if ( !$free_str ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $free_str) { } 0 9 9
200 0 $data = {
201 total => 0,
202 used => 0,
203 free => 0,
204 shared => 0,
205 buffers => 0,
206 cached => 0,
207 };
208 }
209  
210 else {
211  
212 9 my ( $total, $used, $free, $shared, $buffers, $cached ) = ( $free_str =~
213 m/^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/ );
214  
215 9 $data = {
216 total => $total,
217 used => $used,
218 free => $free,
219 shared => $shared,
220 buffers => $buffers,
221 cached => $cached
222 };
223 }
224  
225 }
226  
227 9 $cache->set( $cache_key_name, $data );
228  
229 9 return $data;
230 }
231  
232 3 ▶sub __parse_top_output {
Subroutine __parse_top_output: called CC=2 SCAR=6.9
233 3 my $top_output = shift;
234  
235 3 my @matches = $top_output =~ m{
236 \d+ # one or more digits
237 [KMG] # unit
238 [ ] # space
239 \w+ # memory use type
240 }gmsx;
241  
242 3 @matches = map { split qr{[ ]}msx } @matches;
243  
244 3 ▶ if ( $matches[0] =~ qr{\d}msx ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($matches[0] =~ qr/\d/msux) 3 0 3
245 3 @matches = reverse @matches;
246 }
247  
248 3 my %top_memory_data = @matches;
249  
250 %top_memory_data =
251 3 map { lc $_ => $top_memory_data{$_} } keys %top_memory_data;
252  
253 3 return \%top_memory_data;
254 }
255  
256 37 1;
« lib/Rex/Cloud/Jiffybox.pm ↑ Summary lib/Rex/Pkg/Arch.pm »