line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Hardware::Memory; |
6
|
|
|
|
|
|
|
|
7
|
36
|
|
|
36
|
|
501
|
use v5.12.5; |
|
36
|
|
|
|
|
133
|
|
8
|
36
|
|
|
36
|
|
210
|
use warnings; |
|
36
|
|
|
|
|
70
|
|
|
36
|
|
|
|
|
1696
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
11
|
|
|
|
|
|
|
|
12
|
36
|
|
|
36
|
|
241
|
use Rex::Hardware::Host; |
|
36
|
|
|
|
|
118
|
|
|
36
|
|
|
|
|
253
|
|
13
|
36
|
|
|
36
|
|
1047
|
use Rex::Commands::Run; |
|
36
|
|
|
|
|
85
|
|
|
36
|
|
|
|
|
227
|
|
14
|
36
|
|
|
36
|
|
245
|
use Rex::Helper::Run; |
|
36
|
|
|
|
|
93
|
|
|
36
|
|
|
|
|
2511
|
|
15
|
36
|
|
|
36
|
|
444
|
use Rex::Commands::Sysctl; |
|
36
|
|
|
|
|
87
|
|
|
36
|
|
|
|
|
292
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
require Rex::Hardware; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub get { |
20
|
|
|
|
|
|
|
|
21
|
9
|
|
|
9
|
0
|
219
|
my $cache = Rex::get_cache(); |
22
|
9
|
|
|
|
|
177
|
my $cache_key_name = $cache->gen_key_name("hardware.memory"); |
23
|
|
|
|
|
|
|
|
24
|
9
|
50
|
|
|
|
209
|
if ( $cache->valid($cache_key_name) ) { |
25
|
0
|
|
|
|
|
0
|
return $cache->get($cache_key_name); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
9
|
|
|
|
|
250
|
my $os = Rex::Hardware::Host::get_operating_system(); |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
my $convert = sub { |
31
|
|
|
|
|
|
|
|
32
|
0
|
0
|
|
0
|
|
0
|
if ( $_[1] eq "G" ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
33
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024 * 1024 * 1024; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
elsif ( $_[1] eq "M" ) { |
36
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024 * 1024; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif ( $_[1] eq "K" ) { |
39
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
9
|
|
|
|
|
338
|
}; |
43
|
|
|
|
|
|
|
|
44
|
9
|
|
|
|
|
95
|
my $data = {}; |
45
|
|
|
|
|
|
|
|
46
|
9
|
50
|
|
|
|
291
|
if ( $os eq "Windows" ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
47
|
0
|
|
|
|
|
0
|
my $conn = Rex::get_current_connection()->{conn}; |
48
|
|
|
|
|
|
|
$data = { |
49
|
|
|
|
|
|
|
used => $conn->post("/os/memory/used")->{used}, |
50
|
|
|
|
|
|
|
total => $conn->post("/os/memory/max")->{max}, |
51
|
|
|
|
|
|
|
free => $conn->post("/os/memory/free")->{free}, |
52
|
0
|
|
|
|
|
0
|
}; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
elsif ( $os eq "SunOS" ) { |
55
|
0
|
|
|
|
|
0
|
my @data = i_run "echo ::memstat | mdb -k", fail_ok => 1; |
56
|
|
|
|
|
|
|
|
57
|
0
|
|
|
|
|
0
|
my ($free_cache) = map { /\D+\d+\s+(\d+)/ } grep { /^Free \(cache/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
58
|
0
|
|
|
|
|
0
|
my ($free_list) = map { /\D+\d+\s+(\d+)/ } grep { /^Free \(freel/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
59
|
0
|
|
|
|
|
0
|
my ($page_cache) = map { /\s+\d+\s+(\d+)/ } grep { /^Page cache/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
my $free = $free_cache + $free_list; |
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
#my ($total, $total_e) = grep { $_=$1 if /^Memory Size: (\d+) ([a-z])/i } i_run "prtconf"; |
64
|
0
|
|
|
|
|
0
|
my ($total) = map { /\s+\d+\s+(\d+)/ } grep { /^Total/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
&$convert( $free, "M" ); |
67
|
0
|
|
|
|
|
0
|
&$convert( $total, "M" ); |
68
|
0
|
|
|
|
|
0
|
my $used = $total - $free; |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
0
|
$data = { |
71
|
|
|
|
|
|
|
used => $used, |
72
|
|
|
|
|
|
|
total => $total, |
73
|
|
|
|
|
|
|
free => $free, |
74
|
|
|
|
|
|
|
}; |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
elsif ( $os eq "OpenBSD" ) { |
78
|
0
|
|
|
|
|
0
|
my $mem_str = i_run "top -d1 | grep Memory:", fail_ok => 1; |
79
|
0
|
|
|
|
|
0
|
my $total_mem = sysctl("hw.physmem"); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
my ( $phys_mem, $p_m_ent, $virt_mem, $v_m_ent, $free, $f_ent ) = |
82
|
|
|
|
|
|
|
( $mem_str =~ m/(\d+)([a-z])\/(\d+)([a-z])[^\d]+(\d+)([a-z])/i ); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
0
|
&$convert( $phys_mem, $p_m_ent ); |
85
|
0
|
|
|
|
|
0
|
&$convert( $virt_mem, $v_m_ent ); |
86
|
0
|
|
|
|
|
0
|
&$convert( $free, $f_ent ); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
|
|
|
0
|
$data = { |
89
|
|
|
|
|
|
|
used => $phys_mem + $virt_mem, |
90
|
|
|
|
|
|
|
total => $total_mem, |
91
|
|
|
|
|
|
|
free => $free, |
92
|
|
|
|
|
|
|
}; |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
elsif ( $os eq "NetBSD" ) { |
96
|
0
|
|
|
|
|
0
|
my $mem_str = i_run "top -d1 | grep Memory:", fail_ok => 1; |
97
|
0
|
|
|
|
|
0
|
my $total_mem = sysctl("hw.physmem"); |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my ( |
100
|
0
|
|
|
|
|
0
|
$active, $a_ent, $wired, $w_ent, $exec, |
101
|
|
|
|
|
|
|
$e_ent, $file, $f_ent, $free, $fr_ent |
102
|
|
|
|
|
|
|
) |
103
|
|
|
|
|
|
|
= ( $mem_str =~ |
104
|
|
|
|
|
|
|
m/(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])/i |
105
|
|
|
|
|
|
|
); |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
0
|
&$convert( $active, $a_ent ); |
108
|
0
|
|
|
|
|
0
|
&$convert( $wired, $w_ent ); |
109
|
0
|
|
|
|
|
0
|
&$convert( $exec, $e_ent ); |
110
|
0
|
|
|
|
|
0
|
&$convert( $file, $f_ent ); |
111
|
0
|
|
|
|
|
0
|
&$convert( $free, $fr_ent ); |
112
|
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
0
|
$data = { |
114
|
|
|
|
|
|
|
total => $total_mem, |
115
|
|
|
|
|
|
|
used => $active + $exec + $file + $wired, |
116
|
|
|
|
|
|
|
free => $free, |
117
|
|
|
|
|
|
|
file => $file, |
118
|
|
|
|
|
|
|
exec => $exec, |
119
|
|
|
|
|
|
|
wired => $wired, |
120
|
|
|
|
|
|
|
}; |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
elsif ( $os =~ /FreeBSD/ ) { |
124
|
0
|
|
|
|
|
0
|
my $mem_str = i_run "top -d1 | grep Mem:", fail_ok => 1; |
125
|
0
|
|
|
|
|
0
|
my $total_mem = sysctl("hw.physmem"); |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my ( |
128
|
0
|
|
|
|
|
0
|
$active, $a_ent, $inactive, $i_ent, $wired, $w_ent, |
129
|
|
|
|
|
|
|
$cache, $c_ent, $buf, $b_ent, $free, $f_ent |
130
|
|
|
|
|
|
|
) |
131
|
|
|
|
|
|
|
= ( $mem_str =~ |
132
|
|
|
|
|
|
|
m/(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])/i |
133
|
|
|
|
|
|
|
); |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
|
|
|
0
|
if ( !$active ) { |
136
|
|
|
|
|
|
|
( |
137
|
0
|
|
|
|
|
0
|
$active, $a_ent, $inactive, $i_ent, $wired, |
138
|
|
|
|
|
|
|
$w_ent, $buf, $b_ent, $free, $f_ent |
139
|
|
|
|
|
|
|
) |
140
|
|
|
|
|
|
|
= ( $mem_str =~ |
141
|
|
|
|
|
|
|
m/(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])[^\d]+(\d+)([a-z])/i |
142
|
|
|
|
|
|
|
); |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
|
145
|
0
|
|
|
|
|
0
|
&$convert( $active, $a_ent ); |
146
|
0
|
|
|
|
|
0
|
&$convert( $inactive, $i_ent ); |
147
|
0
|
0
|
|
|
|
0
|
&$convert( $wired, $w_ent ) if ($wired); |
148
|
0
|
0
|
|
|
|
0
|
&$convert( $cache, $c_ent ) if ($cache); |
149
|
0
|
0
|
|
|
|
0
|
&$convert( $buf, $b_ent ) if ($buf); |
150
|
0
|
|
|
|
|
0
|
&$convert( $free, $f_ent ); |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
0
|
$data = { |
153
|
|
|
|
|
|
|
total => $total_mem, |
154
|
|
|
|
|
|
|
used => $active + $inactive + $wired, |
155
|
|
|
|
|
|
|
free => $free, |
156
|
|
|
|
|
|
|
cached => $cache, |
157
|
|
|
|
|
|
|
buffers => $buf, |
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
elsif ( $os eq "OpenWrt" ) { |
161
|
0
|
|
|
|
|
0
|
my @data = i_run "cat /proc/meminfo", fail_ok => 1; |
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
my ($total) = map { /(\d+)/ } grep { /^MemTotal:/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
164
|
0
|
|
|
|
|
0
|
my ($free) = map { /(\d+)/ } grep { /^MemFree:/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
165
|
0
|
|
|
|
|
0
|
my ($shared) = map { /(\d+)/ } grep { /^Shmem:/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
166
|
0
|
|
|
|
|
0
|
my ($buffers) = map { /(\d+)/ } grep { /^Buffers:/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
167
|
0
|
|
|
|
|
0
|
my ($cached) = map { /(\d+)/ } grep { /^Cached:/ } @data; |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
168
|
|
|
|
|
|
|
|
169
|
0
|
|
|
|
|
0
|
$data = { |
170
|
|
|
|
|
|
|
total => $total, |
171
|
|
|
|
|
|
|
used => $total - $free, |
172
|
|
|
|
|
|
|
free => $free, |
173
|
|
|
|
|
|
|
shared => $shared, |
174
|
|
|
|
|
|
|
buffers => $buffers, |
175
|
|
|
|
|
|
|
cached => $cached |
176
|
|
|
|
|
|
|
}; |
177
|
|
|
|
|
|
|
} |
178
|
|
|
|
|
|
|
else { |
179
|
|
|
|
|
|
|
# default for linux |
180
|
9
|
50
|
|
|
|
147
|
if ( !can_run("free") ) { |
181
|
0
|
|
|
|
|
0
|
$data = { |
182
|
|
|
|
|
|
|
total => 0, |
183
|
|
|
|
|
|
|
used => 0, |
184
|
|
|
|
|
|
|
free => 0, |
185
|
|
|
|
|
|
|
shared => 0, |
186
|
|
|
|
|
|
|
buffers => 0, |
187
|
|
|
|
|
|
|
cached => 0, |
188
|
|
|
|
|
|
|
}; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
9
|
|
|
|
|
208
|
my $free_str = [ grep { /^Mem:/ } i_run( "free -m", fail_ok => 1 ) ]->[0]; |
|
27
|
|
|
|
|
516
|
|
192
|
|
|
|
|
|
|
|
193
|
9
|
50
|
|
|
|
182
|
if ( !$free_str ) { |
194
|
0
|
|
|
|
|
0
|
$data = { |
195
|
|
|
|
|
|
|
total => 0, |
196
|
|
|
|
|
|
|
used => 0, |
197
|
|
|
|
|
|
|
free => 0, |
198
|
|
|
|
|
|
|
shared => 0, |
199
|
|
|
|
|
|
|
buffers => 0, |
200
|
|
|
|
|
|
|
cached => 0, |
201
|
|
|
|
|
|
|
}; |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
else { |
205
|
|
|
|
|
|
|
|
206
|
9
|
|
|
|
|
399
|
my ( $total, $used, $free, $shared, $buffers, $cached ) = ( $free_str =~ |
207
|
|
|
|
|
|
|
m/^Mem:\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/ ); |
208
|
|
|
|
|
|
|
|
209
|
9
|
|
|
|
|
414
|
$data = { |
210
|
|
|
|
|
|
|
total => $total, |
211
|
|
|
|
|
|
|
used => $used, |
212
|
|
|
|
|
|
|
free => $free, |
213
|
|
|
|
|
|
|
shared => $shared, |
214
|
|
|
|
|
|
|
buffers => $buffers, |
215
|
|
|
|
|
|
|
cached => $cached |
216
|
|
|
|
|
|
|
}; |
217
|
|
|
|
|
|
|
} |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
} |
220
|
|
|
|
|
|
|
|
221
|
9
|
|
|
|
|
294
|
$cache->set( $cache_key_name, $data ); |
222
|
|
|
|
|
|
|
|
223
|
9
|
|
|
|
|
626
|
return $data; |
224
|
|
|
|
|
|
|
} |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
1; |