line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# (c) Jan Gehring |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Rex::Hardware::Swap; |
6
|
|
|
|
|
|
|
|
7
|
3
|
|
|
3
|
|
96
|
use v5.12.5; |
|
3
|
|
|
|
|
1562
|
|
8
|
3
|
|
|
3
|
|
34
|
use warnings; |
|
3
|
|
|
|
|
7
|
|
|
3
|
|
|
|
|
320
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
our $VERSION = '1.14.2.2'; # TRIAL VERSION |
11
|
|
|
|
|
|
|
|
12
|
3
|
|
|
3
|
|
556
|
use Rex::Commands::Run; |
|
3
|
|
|
|
|
24
|
|
|
3
|
|
|
|
|
113
|
|
13
|
3
|
|
|
3
|
|
46
|
use Rex::Helper::Run; |
|
3
|
|
|
|
|
10
|
|
|
3
|
|
|
|
|
464
|
|
14
|
3
|
|
|
3
|
|
24
|
use Rex::Hardware::Host; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
69
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
require Rex::Hardware; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub get { |
19
|
|
|
|
|
|
|
|
20
|
9
|
|
|
9
|
0
|
119
|
my $cache = Rex::get_cache(); |
21
|
9
|
|
|
|
|
147
|
my $cache_key_name = $cache->gen_key_name("hardware.swap"); |
22
|
|
|
|
|
|
|
|
23
|
9
|
50
|
|
|
|
1454
|
if ( $cache->valid($cache_key_name) ) { |
24
|
0
|
|
|
|
|
0
|
return $cache->get($cache_key_name); |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
|
27
|
9
|
|
|
|
|
170
|
my $os = Rex::Hardware::Host::get_operating_system(); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my $convert = sub { |
30
|
|
|
|
|
|
|
|
31
|
0
|
0
|
|
0
|
|
0
|
if ( !defined $_[0] ) { |
32
|
0
|
|
|
|
|
0
|
return 0; |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
0
|
0
|
|
|
|
0
|
if ( $_[1] eq "G" ) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
36
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024 * 1024 * 1024; |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
elsif ( $_[1] eq "M" ) { |
39
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024 * 1024; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
elsif ( $_[1] eq "K" ) { |
42
|
0
|
|
|
|
|
0
|
$_[0] = $_[0] * 1024; |
43
|
|
|
|
|
|
|
} |
44
|
|
|
|
|
|
|
|
45
|
9
|
|
|
|
|
298
|
}; |
46
|
|
|
|
|
|
|
|
47
|
9
|
|
|
|
|
98
|
my $data = {}; |
48
|
|
|
|
|
|
|
|
49
|
9
|
50
|
|
|
|
154
|
if ( $os eq "Windows" ) { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
50
|
0
|
|
|
|
|
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
|
|
|
|
|
0
|
}; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
elsif ( $os eq "SunOS" ) { |
58
|
0
|
|
|
|
|
0
|
my ($swap_str) = i_run( "swap -s", fail_ok => 1 ); |
59
|
|
|
|
|
|
|
|
60
|
0
|
|
|
|
|
0
|
my ( $used, $u_ent, $avail, $a_ent ) = |
61
|
|
|
|
|
|
|
( $swap_str =~ m/(\d+)([a-z]) used, (\d+)([a-z]) avail/ ); |
62
|
|
|
|
|
|
|
|
63
|
0
|
|
|
|
|
0
|
&$convert( $used, uc($u_ent) ); |
64
|
0
|
|
|
|
|
0
|
&$convert( $avail, uc($a_ent) ); |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
0
|
$data = { |
67
|
|
|
|
|
|
|
total => $used + $avail, |
68
|
|
|
|
|
|
|
used => $used, |
69
|
|
|
|
|
|
|
free => $avail, |
70
|
|
|
|
|
|
|
}; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
elsif ( $os eq "OpenBSD" ) { |
73
|
0
|
|
|
|
|
0
|
my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1; |
74
|
|
|
|
|
|
|
|
75
|
0
|
|
|
|
|
0
|
my ( $used, $u_ent, $total, $t_ent ) = |
76
|
|
|
|
|
|
|
( $swap_str =~ m/Swap: (\d+)([a-z])\/(\d+)([a-z])/i ); |
77
|
|
|
|
|
|
|
|
78
|
0
|
|
|
|
|
0
|
&$convert( $used, $u_ent ); |
79
|
0
|
|
|
|
|
0
|
&$convert( $total, $t_ent ); |
80
|
|
|
|
|
|
|
|
81
|
0
|
|
|
|
|
0
|
$data = { |
82
|
|
|
|
|
|
|
total => $total, |
83
|
|
|
|
|
|
|
used => $used, |
84
|
|
|
|
|
|
|
free => $total - $used, |
85
|
|
|
|
|
|
|
}; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
elsif ( $os eq "NetBSD" ) { |
88
|
0
|
|
|
|
|
0
|
my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1; |
89
|
|
|
|
|
|
|
|
90
|
0
|
|
|
|
|
0
|
my ( $total, $t_ent, $free, $f_ent ) = |
91
|
|
|
|
|
|
|
( $swap_str =~ m/(\d+)([a-z])[^\d]+(\d+)([a-z])/i ); |
92
|
|
|
|
|
|
|
|
93
|
0
|
|
|
|
|
0
|
&$convert( $total, $t_ent ); |
94
|
0
|
|
|
|
|
0
|
&$convert( $free, $f_ent ); |
95
|
|
|
|
|
|
|
|
96
|
0
|
|
|
|
|
0
|
$data = { |
97
|
|
|
|
|
|
|
total => $total, |
98
|
|
|
|
|
|
|
used => $total - $free, |
99
|
|
|
|
|
|
|
free => $free, |
100
|
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
elsif ( $os =~ /FreeBSD/ ) { |
104
|
0
|
|
|
|
|
0
|
my $swap_str = i_run "top -d1 | grep Swap:", fail_ok => 1; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
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
|
0
|
|
|
|
0
|
if ( !$total ) { |
110
|
0
|
|
|
|
|
0
|
( $total, $t_ent, $free, $f_ent ) = |
111
|
|
|
|
|
|
|
( $swap_str =~ m/(\d+)([a-z])[^\d]+(\d+)([a-z])/i ); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
0
|
0
|
|
|
|
0
|
&$convert( $total, $t_ent ) if ($total); |
115
|
0
|
0
|
|
|
|
0
|
&$convert( $used, $u_ent ) if ($used); |
116
|
0
|
0
|
|
|
|
0
|
&$convert( $free, $f_ent ) if ($free); |
117
|
|
|
|
|
|
|
|
118
|
0
|
0
|
0
|
|
|
0
|
if ( !$used && $total && $free ) { |
|
|
|
0
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
$used = $total - $free; |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
$data = { |
123
|
0
|
|
0
|
|
|
0
|
total => $total || 0, |
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
124
|
|
|
|
|
|
|
used => $used || 0, |
125
|
|
|
|
|
|
|
free => $free || 0, |
126
|
|
|
|
|
|
|
}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
else { |
129
|
|
|
|
|
|
|
# linux as default |
130
|
9
|
50
|
|
|
|
98
|
if ( !can_run("free") ) { |
131
|
0
|
|
|
|
|
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
|
|
|
|
|
209
|
my $free_str = [ grep { /^Swap:/ } i_run( "free -m", fail_ok => 1 ) ]->[0]; |
|
27
|
|
|
|
|
453
|
|
142
|
9
|
50
|
|
|
|
124
|
if ( !$free_str ) { |
143
|
0
|
|
|
|
|
0
|
$data = { |
144
|
|
|
|
|
|
|
total => 0, |
145
|
|
|
|
|
|
|
used => 0, |
146
|
|
|
|
|
|
|
free => 0, |
147
|
|
|
|
|
|
|
}; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
else { |
151
|
|
|
|
|
|
|
|
152
|
9
|
|
|
|
|
113
|
$free_str =~ s/\r//g; |
153
|
9
|
|
|
|
|
172
|
$free_str =~ s/^\s+|\s+$//g; |
154
|
|
|
|
|
|
|
|
155
|
9
|
|
|
|
|
241
|
my ( $total, $used, $free ) = |
156
|
|
|
|
|
|
|
( $free_str =~ m/^Swap:\s+(\d+)\s+(\d+)\s+(\d+)$/ ); |
157
|
|
|
|
|
|
|
|
158
|
9
|
|
|
|
|
301
|
$data = { |
159
|
|
|
|
|
|
|
total => $total, |
160
|
|
|
|
|
|
|
used => $used, |
161
|
|
|
|
|
|
|
free => $free, |
162
|
|
|
|
|
|
|
}; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} |
167
|
|
|
|
|
|
|
|
168
|
9
|
|
|
|
|
243
|
$cache->set( $cache_key_name, $data ); |
169
|
|
|
|
|
|
|
|
170
|
9
|
|
|
|
|
597
|
return $data; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
1; |