-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbserverd.php
executable file
·129 lines (111 loc) · 3.37 KB
/
bserverd.php
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
<?php
$exec_location = "/home/bserver/";
$exec_name = "bserver";
$options = getopt("a:p::l::v::m::");
if(!isset($options["a"])) {
usage();
exit;
}
if(isset($options["m"]))
$emails = $options["m"];
else
$emails = false;
$executable = $exec_location.$exec_name." -a{$options["a"]}"
.(isset($options["p"]) ? " -p{$options["p"]}" : "")
.(isset($options["v"]) ? " -v{$options["v"]}" : "")
.(isset($options["l"]) ? " > {$options["l"]}" : " > /dev/null");
// check if the daemon is already running... counting this one there will be 2
$daemon_running = (int) `ps aux | grep bserverd.php | grep -v "grep" | wc -l`;
if($daemon_running >= 2) {
exit("Daemon already running, exiting...");
}
$bad_response = 0;
$bin_position = 0;
$last_bin_position = 0;
$server_starting = false;
$starting_count = 0;
while(1) {
$server_running = (int) `ps aux | grep bserver | grep -v "grep" | grep -v "bserverd" | wc -l`;
if($server_starting) {
if($starting_count++ > 5) {
$subject = "!! ALERT !! Socket Server {$_SERVER["HOSTNAME"]} failed to restart, trying again...";
$message = "Server hasn't restarted in 5 seconds, trying again...\n";
echo $message;
startServer();
mailPeeps($subject, $message);
}
} else if($server_running === 0) {
echo "Server not running, starting executable: $executable\n";
startServer();
} else {
$server_starting = false;
echo "Server already running, checking if responding...\n";
if(checkServer() === false) {
echo "SERVER FAILED TO RESPOND! #bad responses [$bad_response]\n";
$bad_response++;
} else {
echo "Server responded with bin position $bin_position, last position $last_bin_position\n";
$last_bin_position = $bin_position;
if($bad_response > 0)
$bad_response--;
}
if($bad_response > 3) {
$subject = "!! ALERT !! Socket Server {$_SERVER["HOSTNAME"]} is being restarted!";
$message = "MORE THAN THREE BAD RESPONSES IN A ROW, RESTARTING SERVER!\n";
echo $message;
startServer();
mailPeeps($subject, $message);
}
}
sleep(1);
}
function startServer() {
global $executable, $server_starting;
$server_starting = true;
exec("killall bserver");
exec("killall curl");
exec($executable);
}
function checkServer() {
global $last_bin_position, $bin_position;
$errno = false;
$errstr = false;
$fp = fsockopen("localhost", 80, $errno, $errstr, 1);
$last_bin_position = $bin_position;
$bin_position = false;
if (!$fp) {
echo "fsockopen error: $errstr ($errno)\n";
return false;
} else {
$out = "GET /QStats1979 HTTP/1.1\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$str = fgets($fp, 128);
if(($pos = strpos($str, "BIN_POSITION")) !== false) {
$bin_position = (int)substr($str, $pos+strlen("BIN_POSITION")+1);
break;
}
}
fclose($fp);
}
return true;
}
function mailPeeps($subject, $message) {
global $emails, $options;
if($emails !== false) {
$message .= "<br>\nServer: {$_SERVER["HOSTNAME"]}<br>\n".print_r($options,true);
mail($emails, $subject, $message);
}
}
function usage() {
echo "Usage:
-a <API IP/base url>
-l <file to log to, full path>
-p <http port> [default 80]
-v <verbose output/debugging>
-vv <really verbose>
-vvv <annoyingly verbose>
-m <email addresses separated by comma>\n";
}
?>