script/mpd-kill.pl untested

filter: Statementstmt 0.0% 0 / 80 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 18 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 132 CC 3 SCAR 18.4
CC
3
Cov
0%
checkProc
12.0
BEGIN
2.0
BEGIN
2.0
CRAP 6.3
1 #!/usr/bin/env perl
2  
3 # read pidlist avery N seconds and (kill -9|STOP) a random pid
4 # usage mpd-kill.pl --time=5 --hostname=node-0.phenyxtest --username=evaleto --loop
5  
6 use Data::Dumper;
7 use Parallel::Mpich::MPD;
8 use Proc::ProcessTable;
9  
10  
11 # mpd-killer --time 20 --hostname=node-0.phenyxtest
12 use Getopt::Long;
13 my($time, $hostname, $username, $verbose, $loop);
14 $time=1;
15 $usename="evaleto";
16 if (!GetOptions(
17 "time=s"=>\$time,
18 "hostname=s" =>\$hostname,
19 "username=s" =>\$username,
20 "verbose" =>\$verbose,
21 "loop" =>\$loop,
22 )|!defined($hostname)|!defined($username)){
23 printf STDERR "Usage:\n";
24 printf STDERR "mpd-killer --time=5 --hostname=node-0.phenyxtest --username=evaleto \n\n";
25 exit 1;
26 }
27  
28  
29  
30 #$Parallel::Mpich::MPD::Common::DEBUG=1;
31 #$Parallel::Mpich::MPD::Common::WARN=1;
32  
33 Parallel::Mpich::MPD::Common::env_Check();
34  
35 print "#\n";
36 print "#\n";
37 my %cached_psids;
38 my @mpd_pids;
39 my $pid;
40 my @sleepingpids;
41 my $maxsleep=10;
42 my $index;
43 my $delay=0;
44  
45 sub checkProc{
46 my @tmp=@_;
47 %cached_psids={};
48 my %psids={};
49 my $id=0;
50 foreach $p (@tmp){
51 print "$p, ";
52 $psids{$p}=1;
53 }
54
55 Parallel::Mpich::MPD::Common::__exec(cmd => "id -u $username", stdout =>\$id);
56 my $FORMAT = "%-6s %-8s %-24s %-3s %-30s\n";
57 printf($FORMAT, "PID", "STAT", "START", "MPD", "COMMAND");
58 $t = new Proc::ProcessTable(cache_ttys=>1);
59  
60 foreach $p ( @{$t->table} ){
61 if ($id == $p->uid){
62 $cached_psids{$p->pid}=1;
63 printf($FORMAT,
64 $p->pid,
65 $p->state,
66 scalar(localtime($p->start)),
67 ($psids{$p->pid}==1)?"Yes":"No",
68 $p->cmndline);
69 }
70 }
71 print "\n\n";
72 }
73  
74 my $lastcmd;
75 my %tmppids;
76 my $ret;
77 do{
78 system "clear";
79
80 %tmppids=Parallel::Mpich::MPD::findJob( username=>$username, return=>'host2pidlist', reloadlist=>(($delay % $time) ==1)?1:0);
81 @mpd_pids= keys %{$tmppids{$hostname}};
82 $pidcount=scalar @mpd_pids;
83
84 my %jobids=Parallel::Mpich::MPD::findJob(username=>$username);
85 #mpd infos:
86 print "\n\nmpd says : jobids [".join(', ',keys %jobids)."] are running on [". join(',',keys %tmppids)."]\n";
87 print " local pids [".join(', ',@mpd_pids)."]\n\n";
88
89 $index =int(rand($pidcount));
90 if ($pidcount>0 && (($delay % $time) ==1)){
91 $lastcmd="";
92 if ($cached_psids{$mpd_pids[$index]}!=1){
93 $lastcmd="WARNING(1) pid ".$mpd_pids[$index]." is already dead !!! MPD doesn't update his psids\n";
94 }elsif (int(rand(2)+0.5)==1){
95 #
96 # kill
97 #
98 $lastcmd= "kill -9 the pid ".$mpd_pids[$index]."\n";
99 ($ret=Parallel::Mpich::MPD::Common::__exec(cmd => "/bin/kill -9 ". $mpd_pids[$index]))==0 or printf STDERR "ERROR: kill SIGKILL ".$mpd_pids[$index]. " return $ret\n";
100 }else{
101 if (int(rand($pidcount*2))==$index && defined($mpd_pids[$index])){
102 #
103 #wakeup
104 #
105 $lastcmd= "wakeup the pid ".$mpd_pids[$index]."\n";
106 ($ret=Parallel::Mpich::MPD::Common::__exec(cmd => "/bin/kill -18 ".$mpd_pids[$index]))==0 or printf STDERR "ERROR: continue SIGCONT ".$mpd_pids[$index]. " return $ret\n";
107 $sleepingpids[$index]=undef;
108 }
109 $lastcmd.= "sleep the pid ".$mpd_pids[$index]."\n";
110 #
111 #sleep
112 #
113 $sleepingpids[$index]=$mpd_pids[$index];
114 ($ret=Parallel::Mpich::MPD::Common::__exec(cmd => "/bin/kill -19 ".$mpd_pids[$index]))==0 or printf STDERR "ERROR: stop SIGSTOP ".$mpd_pids[$index]. " return $ret\n";
115 }
116 $delay=1;
117 }
118 printf $lastcmd;
119 $lastcmd="";
120 checkProc(@mpd_pids);
121 $delay++;
122 sleep 2;
123 }while (1 && defined($loop));
124