-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkjob.pl
executable file
·136 lines (107 loc) · 2.59 KB
/
checkjob.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/perl
use Expect;
$Expect::Log_Stdout = 1;
use XML::DOM;
use Job;
use Dev;
$ENV{TERM} = "vt100";
#my $processid = $ARGV[1];
my $timeout = 10;
my $timeoutlong = 20;
#my ($host,$pass) = ("$ARGV[0]","Embe1mpls");
my $pass = "Embe1mpls";
my @hosts = qw(tunian cnrdvm31);
my %hash=("cnrdvm31"=>"10.208.130.44","tunian"=>"10.208.133.79");
while(1) {
foreach my $host (@hosts) {
my @pids = get_runningjob_pid("job.xml");
my $exp = Expect->new;
$exp = Expect->spawn("ssh -l root $host");
$exp->send("$pass\n") if ($exp->expect(undef,'password:'));
$exp->log_file("output1.log", "w");
# $exp->send("\n") if ($exp->expect($timeout,']#'));
#$exp->send("ps -aux | grep 11111 | grep -v grep\n") if ($exp->expect($timeout,']#'));
foreach my $pid (@pids)
{
if($pid =~ /\d{3,5}/) {
$exp->send("ps -aux | grep $pid | grep -v grep\n") if ($exp->expect($timeout,']#'));
$exp->send("\n") if ($exp->expect($timeout,']#'));
my $read = $exp->before();
print "read is $read\n";
my @lines = split ("\n",$read);
shift(@lines);
#use Data::Dumper;
#print Dumper(@lines);
#print "###########after lines#######\n";
foreach my $line (@lines)
{
if($line =~ /$pid/) {
print "process id found\n";
goto FINISH;
}
}
my $hostip = $hash{$host};
my $ip = find_serverip_by_pid("job.xml", "$pid");
print "hostip is $hostip, ip is $ip\n";
if( $hostip eq $ip) {
my $dev = find_dev_by_pid("job.xml", "$pid");
my $id = find_id_by_pid("job.xml", "$pid");
# exec("./getrsi.pl $dev $id");
my @childs;
my $ppid = fork();
if ($ppid) {
# parent
push(@childs, $ppid);
} elsif ($pid == 0) {
# child
# exec("$cmd &");
exec("./getrsi.pl $dev $id");
exit(0);
} else {
die "couldn't fork: $!\n";
}
foreach (@childs) {
waitpid($_, 0);
}
&set_status("$dev","free");
&move_job_to_finish("job.xml", "finishedjob.xml", "$pid");
print "hostip equals ip, move job to finish\n";
}
}
FINISH:
print "next pid\n";
}
$exp->send("exit\n") if ($exp->expect($timeout,']#'));
$exp->log_file(undef);
}
############### check queued job and move it to running #############
my @ids = get_queuedjob_id("job.xml");
foreach my $id (@ids)
{
my $dev = find_dev_by_id("job.xml", "$id");
my $status = get_status($dev);
if ($status eq "free") {
set_job_status("job.xml", $id, "running");
my $cmd = find_cmd_by_id("job.xml", $id);
set_status($dev,"inuse");
sleep 5;
#exec("$cmd &");
my @childs;
my $ppid = fork();
if ($ppid) {
# parent
push(@childs, $ppid);
} elsif ($pid == 0) {
# child
exec("$cmd &");
exit(0);
} else {
die "couldn't fork: $!\n";
}
foreach (@childs) {
waitpid($_, 0);
}
}
}
sleep 10;
}