-
Notifications
You must be signed in to change notification settings - Fork 7
/
users_used_quota.php
391 lines (351 loc) · 12.6 KB
/
users_used_quota.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
<?php
$serverName = $db_host;
$userName = $db_user;
$password = $db_pass;
$dbName = "root_cwp";
// Create connection
$conn = new mysqli($serverName, $userName, $password, $dbName);
class CwpUsersUsedQuota
{
private $conn;
public function __construct($conn)
{
$this->conn = $conn;
}
public function calculate()
{
$quota['home'] = $this->getUsedHomeQuota();
$quota['mysql'] = $this->getUsedMysqlQuota();
$emailQuota = $this->getUsedEmailQuota();
$usersEmailDomainsPost = $this->getUserEmailDomainsPost();
$quota['email'] = $this->joinEmailDomainsWithQuota($usersEmailDomainsPost, $emailQuota);
return $quota;
}
public function getUsedHomeQuota()
{
$homeQuota = shell_exec("du -bs /home/*");
$homeQuotaAll = explode("\n", $homeQuota);
$quota = [];
foreach ($homeQuotaAll as $homeQuotaInfo) {
if (!empty($homeQuotaInfo)) {
$userQuotaInfo = explode('/', trim($homeQuotaInfo), 2);
$userName = trim(str_replace('home/', '', $userQuotaInfo[1]));
$userQuota = trim($userQuotaInfo[0]);
$quota[$userName] = (int) $userQuota;
}
}
return $quota;
}
public function getUsedMysqlQuota()
{
$allQuota = shell_exec("du -bs /var/lib/mysql/*");
$allQuotaRows = explode("\n", $allQuota);
$quota = [];
foreach ($allQuotaRows as $rowQuotaInfo) {
if (!empty($rowQuotaInfo)) {
$userQuotaInfo = explode('/', trim($rowQuotaInfo), 2);
$fileQuota = trim($userQuotaInfo[0]);
$userNameDb = trim(str_replace('var/lib/mysql/', '', $userQuotaInfo[1]));
$userNameDbExploded = explode('_', trim($userNameDb), 2);
$userName = $userNameDbExploded[0];
if (isset($userNameDbExploded[1])) {
$dbName = $userNameDbExploded[1];
};
if (isset($quota[$userName])) {
if (isset($dbName)) {
$quota[$userName]['db'][$dbName] = (int) $fileQuota;
}
$quota[$userName]['db_quota'] += (int) $fileQuota;
$quota[$userName]['db_count']++;
} else {
if (isset($dbName)) {
$quota[$userName]['db'][$dbName] = (int) $fileQuota;
}
$quota[$userName]['db_quota'] = (int) $fileQuota;
$quota[$userName]['db_count'] = 1;
}
unset($dbName);
}
}
return $quota;
}
public function getUsedEmailQuota()
{
$emailQuota = shell_exec("du -bs /var/vmail/*");
$emailQuotaAll = explode("\n", $emailQuota);
$quota = [];
foreach ($emailQuotaAll as $emailQuotaInfo) {
if (!empty($emailQuotaInfo)) {
$domainQuotaInfo = explode('/', trim($emailQuotaInfo), 2);
$domainName = trim(str_replace('var/vmail/', '', $domainQuotaInfo[1]));
$emailQuota = trim($domainQuotaInfo[0]);
$quota[$domainName] = (int) $emailQuota;
}
}
return $quota;
}
public function getUserEmailDomainsPost()
{
$sql = "
SELECT
cu.username,
cu.domain,
pm.local_part AS post
FROM
root_cwp.user cu,
postfix.mailbox pm
WHERE
pm.domain = cu.domain
UNION ALL
SELECT
cd.user,
cd.domain,
pm.local_part
FROM
root_cwp.domains cd,
postfix.mailbox pm
WHERE
pm.domain = cd.domain
UNION ALL
SELECT
cs.user,
CONCAT(cs.subdomain, '.', cs.domain),
pm.local_part
FROM
root_cwp.subdomains cs,
postfix.mailbox pm
WHERE
pm.domain = CONCAT(cs.subdomain, '.', cs.domain)
";
$result = $this->conn->query($sql);
$usersDomains = [];
while ($row = $result->fetch_assoc()) {
if (!isset($usersDomains['user'][$row['username']][$row['domain']])) {
$usersDomains['user'][$row['username']][$row['domain']] = 1;
} else {
$usersDomains['user'][$row['username']][$row['domain']] += 1;
}
$usersDomains['domain'][$row['domain']] = $row['username'];
}
return $usersDomains;
}
public function joinEmailDomainsWithQuota(array $usersDomains, array $emailQuota)
{
$data = [];
foreach ($emailQuota as $domain => $quota) {
if (!isset($usersDomains['domain'][$domain])) {
continue;
}
$user = $usersDomains['domain'][$domain];
if (isset($data[$user])) {
$data[$user]['quota'] += (int) $quota;
} else {
$data[$user]['quota'] = (int) $quota;
}
$data[$user]['count'] = (int) $usersDomains['user'][$user][$domain];
}
return $data;
}
}
$cwpUsersQuota = new CwpUsersUsedQuota($conn);
$quota = $cwpUsersQuota->calculate();
?>
<div id="tablecontainer">
<?php
$sql = "SELECT u.*, p.package_name, p.disk_quota FROM user u, packages p WHERE p.id = u.package ORDER by u.id ASC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$meta = $stmt->result_metadata();
$result = null;
$params = null;
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array([
$stmt,
'bind_result',
], $params);
while ($stmt->fetch()) {
foreach ($row as $key => $val) {
$c[$key] = $val;
}
$result[] = $c;
}
$stmt->close();
?>
<table border=1 id="dbtable">
<tr>
<th class="center">LP.</th>
<th class="center">User Name</th>
<th class="center">Domain</th>
<th class="center">Package name</th>
<th class="center">Package quota</th>
<th class="center">Home dir quota</th>
<th class="center">MySql quota</th>
<th class="center">Mail quota</th>
<th class="center">Used quota</th>
<th class="center">Free quota</th>
<th class="center">Percent</th>
</tr>
<?php
$allAccounts = count($result);
$sum['home'] = 0;
$sum['email'] = 0;
$sum['mysql']['count'] = 0;
$sum['mysql']['quota'] = 0;
$sum['all'] = 0;
$sum['free'] = 0;
for ($i = 0; $i <= $allAccounts - 1; $i++) :
$userName = $result[$i]['username'];
$domain = $result[$i]['domain'];
?>
<tr>
<td class="right"><?php echo $i + 1 ?>.</td>
<td class="left"><?php echo $userName ?></td>
<td class="left"><?php echo $domain ?></td>
<td class="left"><?php echo($result[$i]['package_name']) ?></td>
<td class="right"><?php echo round($result[$i]['disk_quota'] / 1024, 2) ?> GB</td>
<td class="right">
<?php
$sum['home'] += $quota['home'][$userName];
echo round($quota['home'][$userName] / 1024 / 1024 / 1024, 2);
?> GB
</td>
<td class="right">
<?php
if (isset($quota['mysql'][$userName])) {
$sum['mysql']['count'] += $quota['mysql'][$userName]['db_count'];
$sum['mysql']['quota'] += $quota['mysql'][$userName]['db_quota'];
echo round($quota['mysql'][$userName]['db_quota'] / 1024 / 1024 / 1024, 2);
echo ' GB ';
echo "[{$quota['mysql'][$userName]['db_count']}]";
} else {
echo 0 . ' GB';
}
?>
</td>
<td class="right">
<?php
if (isset($quota['email'][$userName])) {
$sum['email'] += $quota['email'][$userName]['quota'];
echo round($quota['email'][$userName]['quota'] / 1024 / 1024, 2);
echo ' MB [' . $quota['email'][$userName]['count'] .']' ;
} else {
echo 0 . ' MB [0]';
}
?>
</td>
<td class="right">
<?php
$homeQuota = 0;
if (isset($quota['home'][$userName])) {
$homeQuota = $quota['home'][$userName];
}
$mysqlQuota = 0;
if (isset($quota['mysql'][$userName])) {
$mysqlQuota = $quota['mysql'][$userName]['db_quota'];
}
$emailQuota = 0;
if (isset($quota['email'][$userName]['quota'])) {
$emailQuota = $quota['email'][$userName]['quota'];
}
$allQuota = $homeQuota + $mysqlQuota + $emailQuota;
$sum['all'] += $allQuota;
echo round($allQuota / 1024 / 1024 / 1024, 2);
echo ' GB ';
?>
</td>
<td class="right">
<?php
$packageMaxQuotaBytes = $result[$i]['disk_quota'] * 1024 * 1024;
$freeQuota = $packageMaxQuotaBytes - $allQuota;
$sum['free'] += $freeQuota;
echo round($freeQuota / 1024 / 1024 / 1024, 2);
echo ' GB ';
?>
</td>
<td>
<?php
$packageMaxQuotaBytes = $result[$i]['disk_quota'] * 1024 * 1024;
if ($packageMaxQuotaBytes == "0") {
$usedQuotaProgress = 0;
echo "[Unlimited]";
} else {
$usedQuotaPercent = round($allQuota * 100 / $packageMaxQuotaBytes, 2);
$usedQuotaProgress = round($allQuota * 100 / $packageMaxQuotaBytes, 0);
echo "[$usedQuotaPercent %]";
}
$progressBarClass = 'progressBarGreen';
if ($usedQuotaProgress > 50) {
$progressBarClass = 'progressBarOrange';
}
if ($usedQuotaProgress > 90) {
$progressBarClass = 'progressBarRed';
}
if ($usedQuotaProgress > 100) {
$usedQuotaProgress = 100;
}
?>
<div class="progressBox">
<div class='<?= $progressBarClass; ?>' style="width:<?= $usedQuotaProgress; ?>px;"></div>
</div>
</td>
</tr>
<?php endfor; ?>
<thead>
<tr>
<td colspan="2" class="left">Total users: <?= $allAccounts; ?></td>
<td colspan="3" class="right">Sum:</td>
<td class="right">
<?php echo round($sum['home'] / 1024 / 1024 / 1024, 2); ?> GB
</td>
<td class="right">
<?php echo round($sum['mysql']['quota'] / 1024 / 1024 / 1024, 2); ?> GB
[<?php echo $sum['mysql']['count']; ?>]
</td>
<td class="right">
<?php echo round($sum['email'] / 1024 / 1024 / 1024, 2); ?> GB
</td>
<td class="right">
<?php echo round($sum['all'] / 1024 / 1024 / 1024, 2); ?> GB
</td>
<td class="right">
<?php echo round($sum['free'] / 1024 / 1024 / 1024, 2); ?> GB
</td>
<td></td>
</tr>
</thead>
</table>
</div>
<style type="text/css">
#dbtable {
width: 100%;
margin-bottom: 20px;
}
#dbtable td, #dbtable th {
padding: 6px;
}
.center {
text-align: center;
}
.right {
text-align: right;
}
.progressBox {
width: 100px;
height: 10px;
border: 1px solid;
}
.progressBarGreen {
height: 8px;
background-color: green;
}
.progressBarOrange {
height: 8px;
background-color: orange;
}
.progressBarRed {
height: 8px;
background-color: red;
}
</style>