|
1
|
|
|
#!/bin/php |
|
2
|
|
|
<?php |
|
3
|
|
|
|
|
4
|
|
|
//prevent non cli access |
|
5
|
|
|
if(php_sapi_name()!=='cli') exit(); |
|
6
|
|
|
|
|
7
|
|
|
$dir = dirname(dirname(__FILE__)); |
|
8
|
|
|
class_exists('Setup', false) or include($dir.'/classes/Setup.class.php'); |
|
9
|
|
|
class_exists('Utilities', false) or include($dir.'/classes/Utilities.class.php'); |
|
10
|
|
|
class_exists('_MySQL', false) or include($dir.'/classes/_MySQL.class.php'); |
|
11
|
|
|
class_exists('_MeasurePerformance', false) or include($dir.'/classes/_MeasurePerformance.class.php'); |
|
12
|
|
|
|
|
13
|
|
|
$options = getopt("p:i:"); |
|
14
|
|
|
$processType = isset($options['p']) ? $options['p'] : 'main'; |
|
15
|
|
|
$parentProcessId = isset($options['i']) ? (int)$options['i'] : 0; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
switch ($processType) { |
|
19
|
|
|
case 'main'; |
|
20
|
|
|
main(); |
|
21
|
|
|
break; |
|
22
|
|
|
case 'monitorProcessWatch'; |
|
23
|
|
|
monitorProcessWatch($parentProcessId); |
|
24
|
|
|
break; |
|
25
|
|
|
default: |
|
26
|
|
|
_Logging::appLog("Invalid process requested in main script"); |
|
27
|
|
|
exit(); |
|
28
|
|
|
break; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
function main(){ |
|
33
|
|
|
// start process watch |
|
34
|
|
|
$parentProcessId = getmypid(); |
|
35
|
|
|
if($parentProcessId == false || $parentProcessId == 0) { |
|
|
|
|
|
|
36
|
|
|
_Logging::appLog("Parent process couldnt get pid"); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$userProcessId = 0; |
|
40
|
|
|
$monitorProcessesId = 0; |
|
41
|
|
|
|
|
42
|
|
|
$mysql = new _MySQL(); |
|
43
|
|
|
|
|
44
|
|
|
// control |
|
45
|
|
|
while (true) { |
|
46
|
|
|
try{ |
|
47
|
|
|
$mysql->connect(Setup::$connectionArray); |
|
48
|
|
|
if(!Utilities::is_process_running($userProcessId)){ |
|
49
|
|
|
$userCheck = $mysql->runQueryReturnVar("select username from users where beenChecked = 0"); |
|
50
|
|
View Code Duplication |
if($userCheck!==false){ |
|
51
|
|
|
$cmd = 'php '.dirname(__FILE__).'/userJob.php -i '.$parentProcessId; |
|
52
|
|
|
$userProcessId = Utilities::run_in_background($cmd); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
} catch (Exception $e) { |
|
56
|
|
|
_Logging::appLog($e->getMessage()); |
|
57
|
|
|
} |
|
58
|
|
View Code Duplication |
if(!Utilities::is_process_running($monitorProcessesId)){ |
|
59
|
|
|
$cmd = 'php '.dirname(__FILE__).'/blacklistmonitor.php -p monitorProcessWatch -i '.$parentProcessId; |
|
60
|
|
|
$monitorProcessesId = Utilities::run_in_background($cmd); |
|
61
|
|
|
} |
|
62
|
|
|
sleep(15);//15 seconds |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
function monitorProcessWatch($parentProcessId){ |
|
68
|
|
|
|
|
69
|
|
|
$m = new _MeasurePerformance(); |
|
70
|
|
|
$mysql = new _MySQL(); |
|
71
|
|
|
$mysql->connect(Setup::$connectionArray); |
|
72
|
|
|
|
|
73
|
|
|
$parallelProcessesMonitors = Setup::$settings['max_monitor_processes']; |
|
74
|
|
|
|
|
75
|
|
|
$monitorProcesses = array(); |
|
76
|
|
|
$processCountMonitors = 0; |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$ipDomain = false; |
|
79
|
|
|
|
|
80
|
|
|
while (true) { |
|
81
|
|
|
// are we still running? |
|
82
|
|
|
if(!Utilities::is_process_running($parentProcessId)){ |
|
83
|
|
|
_Logging::appLog("Parent Stopped - monitorStartWatch exited"); |
|
84
|
|
|
exit(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$processCountMonitors = count($monitorProcesses); |
|
88
|
|
|
|
|
89
|
|
|
if ($processCountMonitors < $parallelProcessesMonitors){ |
|
90
|
|
|
$ipDomain = Utilities::getNextMonitor($mysql); |
|
91
|
|
|
if ($ipDomain!==false) { |
|
92
|
|
|
// start it |
|
93
|
|
|
$cmd = 'php '.dirname(__FILE__).'/monitorJob.php -h '.escapeshellarg($ipDomain); |
|
94
|
|
|
$pid = Utilities::run_in_background($cmd); |
|
95
|
|
|
$m->work(1); |
|
96
|
|
|
$monitorProcesses[] = $pid; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// was there any work? |
|
101
|
|
|
if($ipDomain===false){ |
|
102
|
|
|
sleep(10);//10 seconds |
|
103
|
|
|
}else{ |
|
104
|
|
|
usleep(10000);//ideal time 10ms |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// delete finished processes |
|
108
|
|
|
for ($x = 0; $x < $processCountMonitors; $x++) { |
|
109
|
|
|
if(isset($monitorProcesses[$x])){ |
|
110
|
|
|
if(!Utilities::is_process_running($monitorProcesses[$x])){ |
|
111
|
|
|
unset($monitorProcesses[$x]); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
// fix array index |
|
117
|
|
|
$monitorProcesses = array_values($monitorProcesses); |
|
118
|
|
|
|
|
119
|
|
|
$processCountMonitors = count($monitorProcesses); |
|
120
|
|
|
|
|
121
|
|
|
//randomly reset counter every now and then |
|
122
|
|
|
if(mt_rand(1,2000)==1){ |
|
123
|
|
|
$m->endWork(); |
|
124
|
|
|
_Logging::appLog("App Avg Hosts/sec: {$m->avgPerformance}\tMonitor Threads: $processCountMonitors/$parallelProcessesMonitors"); |
|
125
|
|
|
$m = new _MeasurePerformance(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|