forked from wang4386/CDT-Monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAliyunTrafficCheck.php
More file actions
603 lines (504 loc) · 23.3 KB
/
AliyunTrafficCheck.php
File metadata and controls
603 lines (504 loc) · 23.3 KB
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
<?php
require 'vendor/autoload.php';
require_once 'Database.php';
require_once 'ConfigManager.php';
require_once 'AliyunService.php';
require_once 'NotificationService.php';
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;
class AliyunTrafficCheck
{
private $db;
private $configManager;
private $aliyunService;
private $notificationService;
private $initError = null;
const KEEP_ALIVE_COOLDOWN = 1800;
public function __construct()
{
try {
$this->db = new Database();
$this->configManager = new ConfigManager($this->db);
$this->aliyunService = new AliyunService();
$this->notificationService = new NotificationService();
// 注入配置到通知服务
$this->notificationService->setConfig($this->configManager->getAllSettings());
} catch (Exception $e) {
$this->initError = $e->getMessage();
}
}
public function getInitError()
{
return $this->initError;
}
public function isInitialized()
{
if ($this->initError) return false;
return $this->configManager->isInitialized();
}
public function getAdminPassword()
{
return $this->configManager->get('admin_password', '');
}
public function login($password)
{
$ip = $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$ip = trim($ips[0]);
}
$attempts = $this->db->getRecentFailedAttempts($ip, 900);
if ($attempts >= 5) {
$this->db->addLog('warning', "登录被锁定: IP {$ip} 尝试次数过多");
throw new Exception("错误次数过多,请 15 分钟后再试。");
}
$adminPass = $this->getAdminPassword();
if (empty($adminPass)) return false;
if (hash_equals((string)$adminPass, (string)$password)) {
$this->db->clearLoginAttempts($ip);
$this->db->addLog('info', "管理员登录成功 [IP: {$ip}]");
return true;
}
$this->db->recordLoginAttempt($ip);
$this->db->addLog('warning', "管理员登录失败 [IP: {$ip}]");
return false;
}
public function setup($data)
{
if ($this->initError) throw new Exception($this->initError);
if ($this->isInitialized()) return false;
return $this->configManager->updateConfig($data);
}
public function updateConfig($data)
{
$success = $this->configManager->updateConfig($data);
if ($success) {
$this->notificationService->setConfig($this->configManager->getAllSettings());
}
return $success;
}
public function getConfigForFrontend()
{
if ($this->initError) return [];
$settings = $this->configManager->getAllSettings();
$accounts = $this->configManager->getAccounts();
$config = [
'admin_password' => $settings['admin_password'] ?? '',
'traffic_threshold' => (int)($settings['traffic_threshold'] ?? 95),
'enable_schedule_email' => ($settings['enable_schedule_email'] ?? '0') === '1',
'shutdown_mode' => $settings['shutdown_mode'] ?? 'KeepCharging',
'threshold_action' => $settings['threshold_action'] ?? 'stop_and_notify',
'keep_alive' => ($settings['keep_alive'] ?? '0') === '1',
'api_interval' => (int)($settings['api_interval'] ?? 600),
'Notification' => [
'email' => $settings['notify_email'] ?? '',
'host' => $settings['notify_host'] ?? '',
'port' => $settings['notify_port'] ?? 465,
'username' => $settings['notify_username'] ?? '',
'password' => $settings['notify_password'] ?? '',
'secure' => $settings['notify_secure'] ?? 'ssl',
],
'Accounts' => []
];
foreach ($accounts as $row) {
$config['Accounts'][] = [
'AccessKeyId' => $row['access_key_id'],
'AccessKeySecret' => $row['access_key_secret'],
'regionId' => $row['region_id'],
'instanceId' => $row['instance_id'],
'maxTraffic' => (float)$row['max_traffic'],
'schedule' => [
'enabled' => $row['schedule_enabled'] == 1,
'startTime' => $row['start_time'],
'stopTime' => $row['stop_time']
]
];
}
return $config;
}
// --- 修改:支持按 Tab 获取日志 ---
public function getSystemLogs($tab = 'action')
{
if ($this->initError) return [];
if ($tab === 'heartbeat') {
// 心跳日志:只看 heartbeat 类型
$types = ['heartbeat'];
} else {
// 动作日志:只看 info 和 warning,排除 error (超时/接口错误)
$types = ['info', 'warning'];
}
// 仅返回最近 20 条
$logs = $this->db->getLogsByTypes($types, 20);
foreach ($logs as &$log) {
$log['time_str'] = date('Y-m-d H:i:s', $log['created_at']);
}
return $logs;
}
// --- 新增:清空日志并重排 ID ---
public function clearSystemLogs($tab = 'action')
{
if ($this->initError) return false;
$result = false;
if ($tab === 'heartbeat') {
$result = $this->db->clearLogsByTypes(['heartbeat']);
} else {
$result = $this->db->clearLogsByTypes(['info', 'warning', 'error']);
}
// 关键改动:清空后立即重排剩余 ID
if ($result) {
$this->db->reorderLogsIds();
}
return $result;
}
public function getAccountHistory($id)
{
if ($this->initError) return [];
$account = $this->configManager->getAccountById($id);
if (!$account) return ['error' => 'Account not found'];
$ak = $account['access_key_id'];
$rawHourly = $this->db->getHourlyStats($ak);
$chartHourly = [];
foreach ($rawHourly as $row) {
$chartHourly[] = [
'time' => date('H:00', $row['recorded_at']),
'full_time' => date('Y-m-d H:i', $row['recorded_at']),
'value' => round($row['traffic'], 3)
];
}
$rawDaily = $this->db->getDailyStats($ak);
$chartDaily = [];
foreach ($rawDaily as $row) {
$chartDaily[] = [
'date' => date('Y-m-d', $row['recorded_at']),
'value' => round($row['traffic'], 3)
];
}
return [
'history_24h' => $chartHourly,
'history_30d' => $chartDaily
];
}
// --- 核心监控逻辑 ---
public function monitor()
{
if ($this->initError) return "Error: " . $this->initError;
// 优化:分级清理日志
// 普通/重要日志保留 30 天,高频心跳日志仅保留 3 天
$this->db->pruneLogs(30, 3);
// 关键改动:每次清理后重排 ID,保证 ID 永远紧凑
$this->db->reorderLogsIds();
$this->db->pruneStats();
// 优化:每天凌晨 04:xx 执行一次 VACUUM 整理数据库碎片
if (date('H') === '04' && date('i') === '00') {
$this->db->vacuum();
}
$logs = [];
$currentUserTime = date('H:i');
$currentTime = time();
$threshold = (int)$this->configManager->get('traffic_threshold', 95);
$shutdownMode = $this->configManager->get('shutdown_mode', 'KeepCharging');
$thresholdAction = $this->configManager->get('threshold_action', 'stop_and_notify');
$keepAlive = $this->configManager->get('keep_alive', '0') === '1';
$userInterval = (int)$this->configManager->get('api_interval', 600);
$accounts = $this->configManager->getAccounts();
foreach ($accounts as $account) {
$logPrefix = "[{$account['access_key_id']}]";
$actions = [];
$forceRefresh = false;
$statusTransformed = false;
// 1. 定时任务
if ($account['schedule_enabled'] == 1) {
if ($account['start_time'] && $currentUserTime === $account['start_time']) {
if ($this->safeControlInstance($account, 'start')) {
$actions[] = "定时启动";
$this->db->addLog('info', "执行定时启动 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("定时启动", $account, "计划任务已触发,实例正在启动。");
$this->logMailResult($mailRes, $account['access_key_id']);
$forceRefresh = true;
$statusTransformed = true;
}
}
if ($account['stop_time'] && $currentUserTime === $account['stop_time']) {
if ($this->safeControlInstance($account, 'stop', $shutdownMode)) {
$actions[] = "定时停止({$shutdownMode})";
$this->db->addLog('info', "执行定时停止 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("定时停止", $account, "计划任务已触发,实例已停止。");
$this->logMailResult($mailRes, $account['access_key_id']);
$forceRefresh = true;
$statusTransformed = true;
}
}
}
// 2. 自适应心跳
$lastUpdate = $account['updated_at'] ?? 0;
$cachedStatus = $account['instance_status'] ?? 'Unknown';
$isTransientState = in_array($cachedStatus, ['Starting', 'Stopping', 'Pending', 'Unknown']);
$currentInterval = ($isTransientState || $statusTransformed) ? 60 : $userInterval;
$shouldCheckApi = $forceRefresh || (($currentTime - $lastUpdate) > $currentInterval);
if (date('i') === '00') {
$shouldCheckApi = true;
}
$newUpdateTime = $currentTime;
if ($shouldCheckApi) {
$newTraffic = $this->safeGetTraffic($account);
$status = $this->safeGetInstanceStatus($account);
if ($status === 'Unknown') {
usleep(500000);
$status = $this->safeGetInstanceStatus($account);
}
if ($newTraffic < 0) {
$traffic = $account['traffic_used'];
$apiStatusLog = "流量API异常";
$newUpdateTime = $lastUpdate;
} else {
$traffic = $newTraffic;
$apiStatusLog = "已更新";
$this->db->addHourlyStat($account['access_key_id'], $traffic);
$this->db->addDailyStat($account['access_key_id'], $traffic);
}
if ($status === 'Unknown') {
$newUpdateTime = $lastUpdate;
$apiStatusLog .= "(状态Unknown)";
} else {
$apiStatusLog .= in_array($status, ['Starting', 'Stopping', 'Pending']) ? " [过渡态]" : " [稳定态]";
}
$this->configManager->updateAccountStatus($account['id'], $traffic, $status, $newUpdateTime);
} else {
$traffic = $account['traffic_used'];
$status = $account['instance_status'];
$timeLeft = $currentInterval - ($currentTime - $lastUpdate);
$apiStatusLog = "缓存({$timeLeft}s)";
}
$maxTraffic = $account['max_traffic'];
$usagePercent = ($maxTraffic > 0) ? round(($traffic / $maxTraffic) * 100, 2) : 0;
$trafficDesc = "流量:{$usagePercent}%";
$isOverThreshold = $usagePercent >= $threshold;
// 3. 流量熔断
if ($isOverThreshold) {
$trafficDesc .= "[警告]";
if ($shouldCheckApi) {
if ($thresholdAction === 'stop_and_notify') {
if ($status !== 'Stopped') {
if ($this->safeControlInstance($account, 'stop', $shutdownMode)) {
$actions[] = "超限关机";
$this->db->addLog('warning', "流量超限自动关机 [{$account['access_key_id']}] 使用率:{$usagePercent}%");
$this->configManager->updateAccountStatus($account['id'], $traffic, 'Stopping', $currentTime);
$status = 'Stopping';
}
}
} else {
$actions[] = "超限告警";
$this->db->addLog('warning', "流量超限触发告警 [{$account['access_key_id']}] 使用率:{$usagePercent}%");
}
$mailRes = $this->notificationService->sendTrafficWarning($account['access_key_id'], $traffic, $usagePercent, implode(',', $actions), $threshold);
$this->logMailResult($mailRes, $account['access_key_id']);
}
}
// 4. 保活逻辑
if ($keepAlive && $account['schedule_enabled'] == 1 && !$isOverThreshold) {
if ($this->isTimeInRange($currentUserTime, $account['start_time'], $account['stop_time'])) {
if ($status === 'Stopped') {
$lastKeepAlive = $account['last_keep_alive_at'] ?? 0;
$timeSinceLast = $currentTime - $lastKeepAlive;
if ($timeSinceLast > self::KEEP_ALIVE_COOLDOWN) {
if ($this->safeControlInstance($account, 'start')) {
$actions[] = "保活启动";
$this->db->addLog('info', "执行保活启动 [{$account['access_key_id']}]");
$mailRes = $this->notificationService->notifySchedule("保活启动", $account, "检测到实例在工作时段非预期关机,已尝试自动启动。");
$this->logMailResult($mailRes, $account['access_key_id']);
$this->configManager->updateLastKeepAlive($account['id'], $currentTime);
$this->configManager->updateAccountStatus($account['id'], $traffic, 'Starting', $currentTime);
$status = 'Starting';
}
} else {
$cooldownLeft = ceil((self::KEEP_ALIVE_COOLDOWN - $timeSinceLast) / 60);
$apiStatusLog .= " [保活冷却:{$cooldownLeft}m]";
}
}
}
}
if ($statusTransformed) {
$tempStatus = in_array("定时启动", $actions) ? 'Starting' : 'Stopping';
$this->configManager->updateAccountStatus($account['id'], $traffic, $tempStatus, $currentTime);
$apiStatusLog .= " -> 强制过渡态";
}
$actionLog = empty($actions) ? "无动作" : implode(", ", $actions);
$logLine = sprintf("%s %s | %s | %s | %s", $logPrefix, $actionLog, $trafficDesc, $status, $apiStatusLog);
// --- 修改:将心跳日志写入数据库 ---
$this->db->addLog('heartbeat', $logLine);
$logs[] = $logLine;
}
$this->configManager->updateLastRunTime(time());
return implode(PHP_EOL, $logs);
}
public function getStatusForFrontend()
{
if ($this->initError) return ['error' => $this->initError];
$data = [];
$threshold = (int)$this->configManager->get('traffic_threshold', 95);
$userInterval = (int)$this->configManager->get('api_interval', 600);
$currentTime = time();
$accounts = $this->configManager->getAccounts();
foreach ($accounts as $account) {
$lastUpdate = $account['updated_at'] ?? 0;
$cachedStatus = $account['instance_status'] ?? 'Unknown';
$newUpdateTime = $currentTime;
$isTransientState = in_array($cachedStatus, ['Starting', 'Stopping', 'Pending', 'Unknown']);
$checkInterval = $isTransientState ? 60 : $userInterval;
if (($currentTime - $lastUpdate) > $checkInterval) {
$newTraffic = $this->safeGetTraffic($account);
$status = $this->safeGetInstanceStatus($account);
if ($status === 'Unknown') {
usleep(500000);
$status = $this->safeGetInstanceStatus($account);
}
if ($newTraffic < 0) {
$traffic = $account['traffic_used'];
$newUpdateTime = $lastUpdate;
} else {
$traffic = $newTraffic;
$this->db->addHourlyStat($account['access_key_id'], $traffic);
$this->db->addDailyStat($account['access_key_id'], $traffic);
}
if ($status === 'Unknown') {
$newUpdateTime = $lastUpdate;
}
$this->configManager->updateAccountStatus($account['id'], $traffic, $status, $newUpdateTime);
} else {
$traffic = $account['traffic_used'];
$status = $account['instance_status'];
}
$usagePercent = ($account['max_traffic'] > 0) ? round(($traffic / $account['max_traffic']) * 100, 2) : 0;
$isFull = $usagePercent >= $threshold;
$data[] = [
'id' => $account['id'],
'account' => substr($account['access_key_id'], 0, 7) . '***',
'flow_total' => (float)$account['max_traffic'],
'flow_used' => round($traffic, 2),
'percentageOfUse' => $usagePercent,
'region' => $account['region_id'],
'regionName' => $this->getRegionName($account['region_id']),
'rate95' => $isFull,
'threshold' => $threshold,
'instanceStatus' => $status,
'lastUpdated' => date('Y-m-d H:i:s', $lastUpdate > 0 ? $lastUpdate : $currentTime)
];
}
return [
'data' => $data,
'system_last_run' => $this->configManager->getLastRunTime()
];
}
public function refreshAccount($id)
{
if ($this->initError) return false;
$targetAccount = $this->configManager->getAccountById($id);
if (!$targetAccount) return false;
$currentTime = time();
$traffic = $this->safeGetTraffic($targetAccount);
$status = $this->safeGetInstanceStatus($targetAccount);
if ($traffic < 0) {
$traffic = $targetAccount['traffic_used'];
} else {
$this->db->addHourlyStat($targetAccount['access_key_id'], $traffic);
$this->db->addDailyStat($targetAccount['access_key_id'], $traffic);
}
return $this->configManager->updateAccountStatus($id, $traffic, $status, $currentTime);
}
public function sendTestEmail($to)
{
return $this->notificationService->sendTestEmail($to);
}
private function logMailResult($result, $key)
{
if ($result === true) {
$this->db->addLog('info', "邮件发送成功 [$key]");
} elseif ($result !== false && $result !== true) {
$this->db->addLog('warning', "邮件发送失败 [$key]: " . strip_tags($result));
}
}
private function safeGetTraffic($account)
{
try {
return $this->aliyunService->getTraffic($account['access_key_id'], $account['access_key_secret']);
} catch (ClientException $e) {
$code = $e->getErrorCode();
$this->db->addLog('error', "流量查询配置错误: " . ($code ?: "鉴权失败"));
return -1;
} catch (ServerException $e) {
$this->db->addLog('error', "流量查询失败: 阿里云接口超时");
return -1;
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'cURL error') !== false) {
$this->db->addLog('error', "流量查询失败: 网络连接超时");
} else {
$this->db->addLog('error', "流量查询失败: 系统未知错误");
}
return -1;
}
}
private function safeGetInstanceStatus($account)
{
try {
return $this->aliyunService->getInstanceStatus($account);
} catch (\Exception $e) {
if (strpos($e->getMessage(), 'cURL error') !== false) {
} elseif ($e instanceof ClientException) {
$this->db->addLog('error', "实例状态查询配置错误: 鉴权失败");
} else {
}
return 'Unknown';
}
}
private function safeControlInstance($account, $action, $shutdownMode = 'KeepCharging')
{
try {
return $this->aliyunService->controlInstance($account, $action, $shutdownMode);
} catch (ClientException $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 权限不足或配置错误");
return false;
} catch (ServerException $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 阿里云服务无响应");
return false;
} catch (\Exception $e) {
$this->db->addLog('error', "实例操作失败 [{$action}]: 无法连接API");
return false;
}
}
private function isTimeInRange($current, $start, $end) {
if (!$start || !$end) return false;
if ($start < $end) {
return $current >= $start && $current < $end;
} else {
return $current >= $start || $current < $end;
}
}
private function getRegionName($regionId)
{
$regions = [
'cn-hongkong' => '中国香港',
'ap-southeast-1' => '新加坡',
'us-west-1' => '美国(硅谷)',
'us-east-1' => '美国(弗吉尼亚)',
'cn-hangzhou' => '华东1(杭州)',
'cn-shanghai' => '华东2(上海)',
'cn-qingdao' => '华北1(青岛)',
'cn-beijing' => '华北2(北京)',
'cn-zhangjiakou' => '华北3(张家口)',
'cn-huhehaote' => '华北5(呼和浩特)',
'cn-wulanchabu' => '华北6(乌兰察布)',
'cn-shenzhen' => '华南1(深圳)',
'cn-heyuan' => '华南2(河源)',
'cn-guangzhou' => '华南3(广州)',
'cn-chengdu' => '西南1(成都)',
'ap-northeast-1' => '日本(东京)',
];
return $regions[$regionId] ?? $regionId;
}
public function renderTemplate() {
if (!file_exists('template.html')) return "File not found";
ob_start();
include 'template.html';
return ob_get_clean();
}
}