This repository has been archived by the owner on Apr 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
157 lines (121 loc) · 4.63 KB
/
index.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
<?php
define('INDEX_PHP_PASSTHROUGH', true);
require_once '../vendor/colinmollenhour/credis/Client.php';
$redisClient = new Credis_Client();
$processId = getmypid();
$requestUri = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] ? 'https' : 'http') . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
class CachedProcInfo {
public $pid = null;
public $running = false;
public $requestUri = null;
public $requestMethod = null;
public $startedAt = null;
public $endedAt = null;
public $clientInfo = null;
public function getRunTime(): float {
$now = microtime(true);
$then = $this->startedAt;
if ($this->endedAt) {
$now = $this->endedAt;
}
return round(($now - $then), 5);
}
public function getLastSeen(): string {
$lastSeen = $this->startedAt;
if ($this->endedAt) {
$lastSeen = $this->endedAt;
}
return date('H:i:s', $lastSeen);
}
public function getStatus(): string {
if (!file_exists("/proc/{$this->pid}")) {
if ($this->endedAt) {
$val = "✓ ☠ Process finished (last completed in {$this->getRunTime()} sec)";
} else {
$val = "☠ Process is dead (killed)";
}
return $val;
}
if (!$this->running) {
if ($this->endedAt) {
return "✓ Idle. Completed in {$this->getRunTime()} sec";
} else {
return "☠ Completed (killed / unknown end time)";
}
}
return "👣 Running for {$this->getRunTime()} sec";
}
public function render(): void {
echo PHP_EOL;
echo "PID:\t\t{$this->pid}" . PHP_EOL;
echo "Status:\t\t{$this->getStatus()}" . PHP_EOL;
echo "URL:\t\t{$this->requestUri}" . PHP_EOL;
echo "Method:\t\t{$this->requestMethod}" . PHP_EOL;
echo "Client:\t\t{$this->clientInfo}" . PHP_EOL;
echo "Last seen:\t{$this->getLastSeen()}" . PHP_EOL;
}
};
$cacheKey = "fpm_proc_{$processId}";
$cacheCurrent = $redisClient->get($cacheKey);
if ($cacheCurrent) {
$cacheCurrent = unserialize($cacheCurrent);
}
if (!($cacheCurrent instanceof CachedProcInfo)) {
$cacheCurrent = new CachedProcInfo();
}
$cacheCurrent->pid = $processId;
$cacheCurrent->requestUri = $requestUri;
$cacheCurrent->startedAt = microtime(true);
$cacheCurrent->endedAt = null;
$cacheCurrent->running = true;
$cacheCurrent->requestMethod = $_SERVER['REQUEST_METHOD'];
$cacheCurrent->clientInfo = $_SERVER['REMOTE_ADDR'] . " ({$_SERVER['HTTP_USER_AGENT']})";
$redisClient->set($cacheKey, serialize($cacheCurrent));
$redisClient->expire($cacheKey, 900); // keep running proc info for up to 15 minutes
register_shutdown_function(function () use ($redisClient, $cacheCurrent, $cacheKey) {
$cacheCurrent->endedAt = microtime(true);
$cacheCurrent->running = false;
$redisClient->set($cacheKey, serialize($cacheCurrent));
$redisClient->expire($cacheKey, 120); // keep complete proc info for 2 minutes
});
if ($_SERVER['REQUEST_URI'] === '/passthrough-status') {
// Debug mode: List FPM processes
$procKeys = $redisClient->keys('fpm_proc_*');
$procObjs = [];
$procObjsRunning = [];
$procObjsStopped = [];
foreach ($procKeys as $procKey) {
$processInfo = $redisClient->get($procKey);
$processInfo = unserialize($processInfo);
if (!$processInfo instanceof CachedProcInfo) {
continue;
}
$procObjs[] = $processInfo;
if ($processInfo->running) {
$procObjsRunning[] = $processInfo;
} else {
$procObjsStopped[] = $processInfo;
}
}
$procCountTotal = count($procObjs);
$procCountActive = count($procObjsRunning);
$procCountStopped = count($procObjsStopped);
// Output
header('Content-Type: text/plain; charset=utf8');
echo 'Passthrough php-fpm process info' . PHP_EOL . PHP_EOL;
echo " [{$procCountTotal}]\tTotal process count" . PHP_EOL;
echo " [{$procCountActive}]\tRunning process count" . PHP_EOL;
echo " [{$procCountStopped}]\tCompleted process count" . PHP_EOL;
echo PHP_EOL;
echo '----- Active processes -----' . PHP_EOL;
foreach ($procObjsRunning as $processInfo) {
$processInfo->render();
}
echo PHP_EOL;
echo '----- Completed processes -----' . PHP_EOL;
foreach ($procObjsStopped as $processInfo) {
$processInfo->render();
}
} else {
require_once "index.passthrough.php";
}