-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatusCheck
155 lines (118 loc) · 3.02 KB
/
statusCheck
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/perl
# status check -- ping a camera demon, do something if it does not
# respond.
# I have to be told what cameras to ping, that's input
$maxOffset = 1000000;
$| = 1;
use IPC::SysV qw(IPC_PRIVATE IPC_CREAT S_IRWXU S_IRWXG S_IRWXO);
use IPC::Msg;
open( LOCK, "<statusCheck" );
flock LOCK, 6 or die "statusCheck locked: $!\n";
$SIG{ALRM} = \&restart;
undef @mail;
# my response queue from demons
$me = $$;
$msgIn = new IPC::Msg( $me, IPC_CREAT|S_IRWXU|S_IRWXG|S_IRWXO);
die "cannot make queue for answers: $!\n" if !defined $msgIn;
$retryCount = 0;
# for each camera I am told to check ...
foreach $cam (@ARGV) {
next if $cam > 20;
# flag to say "I timed out."
$timeOut = 0; $skipAhead = 0;
$msgOut = new IPC::Msg( $cam, S_IRWXU|S_IRWXG|S_IRWXO);
do {
# no input queue, must start camera
print "connect to queue $cam failed, must restart\n";
&restart;
redo;
} if !defined $msgOut;
# queue found, lets poke it
alarm 10;
print "poking queue $cam...";
$msgOut->snd( 1, pack( "L a*", $me, "ping" ) );
# lets listen for an answer
$in = $msgIn->rcv( $buf, 256 );
print "poked\n";
alarm 0;
# if I did not timeout in getting answer, show it
do {
($code, $resp) = unpack( "L a*", $buf );
print "cam $cam: $resp ";
print "\n";
} if $timeOut == 0;
# too many, skip this one
next if $skipAhead;
# if I did time out, then redo the loop
redo if $timeOut;
# if I didn't time out, then clear retry count
$retryCount = 0;
}
$msgIn->remove;
do {
open( T, ">/tmp/restart.log" );
foreach $l (@mail) {
print T $l . "\n";
}
close T;
system( '/bin/mail -s restartTry [email protected] < /tmp/restart.log' );
} if @mail;
# restart a camera that is unresponsive
sub restart {
# record that this happened
$timeOut++; $retryCount++;
open( L, ">restartLogCam$cam" );
select L; $| = 1; select STDOUT;
print L scalar(localtime(time)) . "\n";
push( @mail, scalar(localtime(time)));
print L "restart cam $cam try $retryCount\n";
push( @mail, "restart cam $cam try $retryCount" );
do {
print L "retry count exceeded\n";
push( @mail, "retry count exceeded\n");
$retryCount = 0;
$timeOut = 0;
$skipAhead = 1;
close L;
return;
} if $retryCount > 5;
# look for an errant process to kill
open( P, "ps -ef |" );
undef $pid;
while(<P>) {
$pid = $1 if /root +(\d+).+ -c $cam /;
}
close P;
do {
print L "detected process running, won't kill!\n";
push( @mail, "detected process running, won't kill!" );
close L;
$retryCount = 0;
$timeOut = 0;
$skipAhead = 1;
return;
} if defined $pid;
# start up a new one
print "trying startup on cam $cam...\n";
open( R, "./startup.pl $cam |" );
while(<R>) {
print;
# cameras are confused
do {
#close R;
print L "power off, reload \n";
push( @mail, "power off, reload\n" );
print "power off\n";
system("./cameraPowerOff");
print "reload\n";
system("/usr/local/bin/reload");
last;
} if /found on any port at any node/;
# we got to the end
do {
#close R;
last;
} if /start done end/;
}
sleep 10;
}