-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_solr_slave.php
executable file
·278 lines (235 loc) · 7.83 KB
/
check_solr_slave.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/php -q
<?php
/* Use like on solr master:
./check_solr_slave.php -i /var/log/jetty/2013_01_04.request.log -s 192.168.128.31:8080 -t
*/
error_reporting(E_ALL);
ini_set("max_execution_time", "0");
ini_set("max_input_time", "0");
ini_set("memory_limit","500M");
set_time_limit(0);
/* Check if PHP version is sufficient for the things we use here */
$phpVersion = phpversion();
if (function_exists("version_compare") && version_compare($phpVersion, "5.2.0",'<')) {
die("Sorry! Your PHP version is too old. PEAR and this script requires at least PHP 5.3.0 for stable operation.");
}
// working dir
$base= realpath( dirname(__FILE__));
/* Command line quick stuff */
$cliargs = array(
'input' => array(
'short' => 'i',
'type' => 'required',
'description' => "The input file to get the requests from",
'default' => ''
),
'slave' => array(
'short' => 's',
'type' => 'required',
'description' => "host:port ( host can be an ip or a hostname)",
'default' => ''
),
'tail' => array(
'short' => 't',
'type' => 'switch',
'description' => "Use tail on the file so you check in realtime"
),
'debug' => array(
'short' => 'd',
'type' => 'optional',
'description' => "Debug flag level. ( value range 1..6 )",
'default' => 4
)
);
/* command line errors are thrown hereafter */
$options = cliargs_get_options($cliargs);
if (empty($options['input']) && empty($options['slave'])) {
cliargs_print_usage_and_exit($cliargs);
}
/* Real work starts here */
$pos = strrpos($options['slave'], ':');
if ($pos === false) {
// : is not found... so using standard port
$options['port']=80;
} else {
list($slave, $port) = preg_split('/:/', $options['slave'], -1, PREG_SPLIT_NO_EMPTY);
if (preg_last_error() == PREG_NO_ERROR) {
$options['slave']=$slave;
$options['port']=$port;
}
}
if (!empty($options['tail'])) {
$file_handle = popen(sprintf("tail -f %s 2>&1",$options['input']), 'r');
} else {
$file_handle = fopen($options['input'], "r");
}
while (!feof($file_handle)) {
$buffer = fgets($file_handle);
// echo "$buffer\n";
check_slave($buffer, $options);
flush();
}
if (!empty($options['tail'])) {
pclose($file_handle);
} else {
fclose($file_handle);
}
function check_slave($buf, $options) {
preg_match("/(^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s-\s(\s+)-(\s+)\[(.*)\]\s\"(.*)\"\s(\d+)\s(\d+)/", $buf, $results);
if (preg_last_error() == PREG_NO_ERROR) {
$call['source_ip'] = $results[1];
$path_r = preg_split('/ /', $results[5], -1, PREG_SPLIT_NO_EMPTY);
$call['source_method'] = $path_r[0];
$call['source_path'] = $path_r[1];
$call['source_result'] = $results[6];
} else {
if (preg_last_error() == PREG_INTERNAL_ERROR) {
print 'There is an internal error!';
}
else if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
print 'Backtrack limit was exhausted!';
}
else if (preg_last_error() == PREG_RECURSION_LIMIT_ERROR) {
print 'Recursion limit was exhausted!';
}
else if (preg_last_error() == PREG_BAD_UTF8_ERROR) {
print 'Bad UTF8 error!';
} else if (preg_last_error() == PREG_BAD_UTF8_OFFSET_ERROR) {
// From php 5.3.0 onwards
print 'Bad UTF8 offset error!';
}
exit();
}
/* Now build up a request for the slave */
$url = sprintf('http://%s:%d%s',$options['slave'], $options['port'], $call['source_path']);
// echo "Checking slave : $url\n";
/* Only do GET request */
if ($call['source_method'] !== 'GET') {
echo "Skipping non GET request : $url\n";
return;
}
/* Don't send calls the slave made to the master again tot the slave */
if (preg_match(sprintf("/%s/",$options['slave']), $call['source_ip'])) {
echo "Skipping slave log entry: $url\n";
return;
}
/* Don't send calls to the slave that already failed on master */
if (intval(trim($call['source_result']))!== 200) {
echo sprintf("Skipping failed master log entry (%s)\n", $call['source_result']);
return;
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, false);
// curl_setopt($ch, CURLOPT_POSTFIELDS, $post_arr);
// curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT , 3);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 3000);
curl_setopt($ch, CURLOPT_USERAGENT, 'php solr slave tester');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$curl_info = curl_getinfo($ch);
if (!$curl_info['http_code']==200) {
echo "ERROR ON SLAVE\n"; exit;
} else {
echo "Slave ok\n";
}
}
/* command line argument functions below */
function cliargs_print_usage_and_exit($cliargs) {
print "Usage:\n";
foreach ($cliargs as $long => $arginfo) {
$short = $arginfo['short'];
$type = $arginfo['type'];
$required = ($type=='required');
$optional = ($type=='optional');
$description = $arginfo['description'];
print "\t-$short/--$long ";
if ($optional||$required) {
print "<value> ";
}
print ": $description";
if ($required) {
print " (required)";
}
print "\n";
}
exit();
}
function cliargs_strstartswith($source, $prefix) {
return strncmp($source, $prefix, strlen($prefix)) == 0;
}
function cliargs_get_options($cliargs) {
global $argv;
global $argc;
$options = array('unnamed' => array());
for ($index=1; $index<$argc; $index+=1) {
$currentarg = strtolower($argv[$index]);
$argparts = preg_split('/=/', $currentarg);
//print_r($argparts); exit;
$namepart = $argparts[0];
if (cliargs_strstartswith($namepart, '--')) {
$longname = substr($namepart, 2);
} else if (cliargs_strstartswith($namepart, '-')) {
$shortname = substr($namepart, 1);
$longname = $shortname;
foreach ($cliargs as $name => $info) {
if ($shortname===$info['short']) {
$longname = $name;
break;
}
}
} else {
$longname = 'unnamed';
}
if ($longname=='unnamed') {
$options['unnamed'][] = $namepart;
} else {
if (empty($cliargs[$longname])) {
print "Unknown argument '$longname'\n";
cliargs_print_usage_and_exit($cliargs);
}
$arginfo = $cliargs[$longname];
$argtype = $arginfo['type'];
if ($argtype==='switch') {
$value = true;
} else if (isset($argparts[1])) {
$value = $argparts[1];
} else if (($index+1)<$argc) {
$value = $argv[$index+1];
$index += 1;
} else {
print "Missing value after '$longname'\n";
cliargs_print_usage_and_exit($cliargs);
}
$options[$longname] = $value;
}
}
foreach ($cliargs as $longname => $arginfo) {
$type = $arginfo['type'];
if (!isset($options[$longname])) {
if ($type=='required') {
print("Missing required value for '$longname'\n");
cliargs_print_usage_and_exit($cliargs);
}
else if ($type=='optional') {
if (!isset($arginfo['default'])) {
die('Missing default value for '.$longname . PHP_EOL);
}
$options[$longname] = $arginfo['default'];
} else if ($type=='switch') {
$options[$longname] = false;
}
}
}
foreach ($options as $longname => $value) {
if ($longname!='unnamed'){
if (strlen(trim($value))==0) {
unset($options[$longname]);
//print "Length '$longname' = " . strlen($value) . "\n";
}
}
}
unset($options['unnamed']);
return $options;
}
?>