lib/Rex/Commands/Process.pm

filter: Statementstmt 32.8% 24 / 73 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 63.6% 7 / 11 Podpod 100% 4 / 4 totaltotal 28.9% 35 / 121 CC 18 SCAR 42.7
CC
18
Cov
24%
ps
156.0
killall
20.0
kill
12.0
CRAP 71.7
« lib/Rex/Template/NG.pm ↑ Summary lib/Rex/Hardware/Network/FreeBSD.pm »
1 #
2 # (c) Jan Gehring <jan.gehring@gmail.com>
3 #
4  
5 =head1 NAME
6  
7 Rex::Commands::Process - Process management commands
8  
9 =head1 DESCRIPTION
10  
11 With this module you can manage processes. List, Kill, and so on.
12  
13 Version <= 1.0: All these functions will not be reported.
14  
15 All these functions are not idempotent.
16  
17 =head1 SYNOPSIS
18  
19 kill $pid;
20 killall "apache2";
21 nice($pid, $level);
22  
23 =head1 EXPORTED FUNCTIONS
24  
25 =cut
26  
27 package Rex::Commands::Process;
28  
29 32 ▶use v5.14.4;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 32 our $VERSION = '1.16.1'; # VERSION
33  
34 32 require Rex::Exporter;
35 32 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 32 ▶use Rex::Helper::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 32 ▶use Rex::Commands::Gather;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 32 ▶use vars qw(@EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 32 ▶use base qw(Rex::Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
41  
42 32 @EXPORT = qw(kill killall
43 ps
44 nice);
45  
46 =head2 kill($pid, $sig)
47  
48 Will kill the given process id. If $sig is specified it will kill with the given signal.
49  
50 task "kill", "server01", sub {
51 kill 9931;
52 kill 9931, -9;
53 };
54  
55 =cut
56  
57 0 ▶sub kill {
Subroutine kill: not called CC=3 SCAR=24.8
Pod documented
58 0 my ( $process, $sig ) = @_;
59 0 ▶ $sig ||= "";
Condition0%0/2
$sig ||= ""
l!l
00
MC/DC0%0/1
$sig ||= ""
$sig
Truth table0%0/2
A
A= $sig
Aresult
11
00
60  
61 0 i_run( "kill $sig " . $process, fail_ok => 1 );
62 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
63 0 die("Error killing $process");
64 }
65 }
66  
67 =head2 killall($name, $sig)
68  
69 Will kill the given process. If $sig is specified it will kill with the given signal.
70  
71 task "kill-apaches", "server01", sub {
72 killall "apache2";
73 killall "apache2", -9;
74 };
75  
76 =cut
77  
78 0 ▶sub killall {
Subroutine killall: not called CC=4 SCAR=30.0
Pod documented
79 0 my ( $process, $sig ) = @_;
80 0 ▶ $sig ||= "";
Condition0%0/2
$sig ||= ""
l!l
00
MC/DC0%0/1
$sig ||= ""
$sig
Truth table0%0/2
A
A= $sig
Aresult
11
00
81  
82 0 ▶ if ( can_run("killall") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_run('killall')) { } 0 0 0
83 0 i_run( "killall $sig $process", fail_ok => 1 );
84 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
85 0 die("Error killing $process");
86 }
87 }
88 else {
89 0 die("Can't execute killall.");
90 }
91 }
92  
93 =head2 ps
94  
95 List all processes on a system. Will return all fields of a I<ps aux>.
96  
97 task "ps", "server01", sub {
98 for my $process (ps()) {
99 say "command > " . $process->{"command"};
100 say "pid > " . $process->{"pid"};
101 say "cpu-usage> " . $process->{"cpu"};
102 }
103 };
104  
105  
106 On most operating systems it is also possible to define custom parameters for ps() function.
107  
108 task "ps", "server01", sub {
109 my @list = grep { $_->{"ni"} == -5 } ps("command","ni");
110 };
111  
112 This example would contain all processes with a nice of -5.
113  
114  
115 =cut
116  
117 0 ▶sub ps {
Subroutine ps: not called CC=12 SCAR=50.5
Pod documented
118 0 my (@custom) = @_;
119 0 my @list;
120  
121 0 ▶ if (is_openwrt) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_openwrt()) { } 0 0 0
elsif (operating_system_is('SunOS') and operating_system_version() <= 510) { } 0 0 0
122  
123 # openwrt doesn't have ps aux
124 0 @list = i_run( "ps", fail_ok => 1 );
125  
126 0 my @ret = ();
127 0 for my $line (@list) {
128 0 $line =~ s/^\s*|\s*$//g;
129 0 my ( $pid, $user, $vsz, $stat, $command ) = split( /\s+/, $line, 5 );
130  
131 0 push(
132 @ret,
133 {
134 user => $user,
135 pid => $pid,
136 vsz => $vsz,
137 stat => $stat,
138 command => $command,
139 }
140 );
141 }
142  
143 0 return @ret;
144 }
145  
146 ▶ elsif ( operating_system_is("SunOS") && operating_system_version() <= 510 ) {
Condition0%0/3
operating_system_is("SunOS") and operating_system_version() <= 510
!ll&&!rl&&r
000
MC/DC0%0/2
operating_system_is("SunOS") and operating_system_version() <= 510
operating_system_is("SunOS") operating_system_version() <= 510
Truth table0%0/3
A and B
A= operating_system_is("SunOS")B= operating_system_version() <= 510
ABresult
0X0
100
111
147 0 ▶ if (@custom) {
Branch: 1 branch
BranchTrueFalseTotal
if (@custom) { } 0 0 0
148 0 @list =
149 i_run( "/usr/ucb/ps awwx -o" . join( ",", @custom ), fail_ok => 1 );
150 }
151 else {
152 0 @list = i_run( "/usr/ucb/ps auwwx", fail_ok => 1 );
153 }
154 }
155 else {
156 0 ▶ if (@custom) {
Branch: 1 branch
BranchTrueFalseTotal
if (@custom) { } 0 0 0
157 0 @list = i_run( "ps awwx -o" . join( ",", @custom ), fail_ok => 1 );
158 }
159 else {
160 0 @list = i_run( "ps auwwx", fail_ok => 1 );
161 }
162 }
163  
164 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
165 0 ▶ if (@custom) {
Branch: 1 branch
BranchTrueFalseTotal
if (@custom) { } 0 0 0
166 0 die( "Error running ps ax -o" . join( ",", @custom ) );
167 }
168 else {
169 0 die("Error running ps aux");
170 }
171 }
172 0 shift @list;
173  
174 0 my @ret = ();
175 0 ▶ if (@custom) {
Branch: 1 branch
BranchTrueFalseTotal
if (@custom) { } 0 0 0
176 0 for my $line (@list) {
177 0 $line =~ s/^\s+//;
178 0 my @col_vals = split( /\s+/, $line, scalar(@custom) );
179 0 my %vals;
180 0 @vals{@custom} = @col_vals;
181 0 push @ret, {%vals};
182 }
183 }
184 else {
185 0 for my $line (@list) {
186 my (
187 0 $user, $pid, $cpu, $mem, $vsz, $rss,
188 $tty, $stat, $start, $time, $command
189 ) = split( /\s+/, $line, 11 );
190  
191 0 push(
192 @ret,
193 {
194 user => $user,
195 pid => $pid,
196 cpu => $cpu,
197 mem => $mem,
198 vsz => $vsz,
199 rss => $rss,
200 tty => $tty,
201 stat => $stat,
202 start => $start,
203 time => $time,
204 command => $command,
205 }
206 );
207 }
208 }
209  
210 0 return @ret;
211 }
212  
213 #Will try to start a process with nohup in the background.
214 #
215 # task "start_in_bg", "server01", sub {
216 # nohup "/opt/srv/myserver";
217 # };
218  
219 #sub nohup {
220 # my ($cmd) = @_;
221 #
222 # run "nohup $cmd &";
223 #}
224  
225 =head2 nice($pid, $level)
226  
227 Renice a process identified by $pid with the priority $level.
228  
229 task "renice", "server01", sub {
230 nice (153, -5);
231 };
232  
233 =cut
234  
235 0 ▶sub nice {
Subroutine nice: not called CC=2 SCAR=17.9
Pod documented
236 0 my ( $pid, $level ) = @_;
237 0 i_run "renice $level $pid", fail_ok => 1;
238 0 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 0 0 0
239 0 die("Error renicing $pid");
240 }
241 }
242  
243 32 1;
« lib/Rex/Template/NG.pm ↑ Summary lib/Rex/Hardware/Network/FreeBSD.pm »