仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ jk_monitor.php 4.9 KB · 83 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2/*
3 * 用户端 URL 监控和通知任务
4 * 建议每15秒执行一次:/jk_monitor.php?my=后台API密钥
5 */
6include("./MPHX/common.php");
7include_once("./MPHX/monitor.function.php");
8if(($_GET['my'] ?? '') != $conf['api']) exit('密钥错误');
9monitor_ensure_tables($DB);
10
11$now = date('Y-m-d H:i:s');
12$tasks = $DB->get_all_prepare("SELECT * FROM MN_monitor_task WHERE enabled='true' AND (next_run IS NULL OR next_run='' OR next_run<=?) ORDER BY id ASC LIMIT 50", [$now]) ?: [];
13$done = 0;
14foreach($tasks as $task) {
15 if (($task['task_type'] ?? 'url') === 'resource') {
16 $userRow = $DB->get_row_prepare("SELECT * FROM MN_zj WHERE user=? limit 1", [$task['user']]);
17 $percent = $userRow ? monitor_resource_percent($userRow, $task['resource_type']) : null;
18 $ok = $percent !== null && !monitor_resource_exceeds_threshold($percent, $task['resource_threshold']);
19 $result = [
20 'ok' => $ok,
21 'code' => 0,
22 'time' => 0,
23 'error' => $percent === null ? '无法读取资源用量' : (monitor_resource_name($task['resource_type']).'已使用'.$percent.'%,超过阈值'.$task['resource_threshold'].'%'),
24 'body' => ''
25 ];
26 } else {
27 $result = monitor_check_url($task);
28 }
29 $status = $result['ok'] ? 'ok' : 'fail';
30 $fail_count = $result['ok'] ? 0 : ((int)$task['fail_count'] + 1);
31 $notified = 'false';
32 $excerpt = substr($result['body'], 0, 500);
33 if(!$result['ok'] && $fail_count >= (int)$task['fail_threshold']) {
34 $title = '监控异常:'.$task['name'];
35 $content = ($task['task_type'] ?? 'url') === 'resource'
36 ? '资源监控:'.monitor_resource_name($task['resource_type']).','.$result['error']
37 : 'URL:'.$task['url'].' 状态码:'.$result['code'].' 耗时:'.$result['time'].'ms 错误:'.$result['error'];
38 monitor_add_notice($DB, $task['user'], 'monitor', $title, $content, 'warning');
39 $u = $DB->get_row_prepare("SELECT mailuser FROM MN_zj WHERE user=? limit 1", [$task['user']]);
40 if(($task['notify_email'] ?? 'true') === 'true' && !empty($u['mailuser'])) monitor_send_mail($u['mailuser'], $title, $content);
41 $notified = 'true';
42 }
43 if($result['ok'] && ($task['last_status'] ?? '') === 'fail') {
44 $recover = ($task['task_type'] ?? 'url') === 'resource'
45 ? '资源监控已恢复正常:'.monitor_resource_name($task['resource_type'])
46 : 'URL:'.$task['url'].' 已恢复正常,状态码:'.$result['code'].',耗时:'.$result['time'].'ms';
47 monitor_add_notice($DB, $task['user'], 'monitor', '监控恢复:'.$task['name'], $recover, 'success');
48 }
49 $DB->query_prepare("INSERT INTO MN_monitor_log (task_id,user,url,http_code,response_time,check_status,error_message,response_excerpt,notified,created_at) VALUES (?,?,?,?,?,?,?,?,?,?)", [$task['id'],$task['user'],$task['url'],$result['code'],$result['time'],$status,$result['error'],$excerpt,$notified,$now]);
50 $next = date('Y-m-d H:i:s', time()+monitor_normalize_interval($task['task_type'] ?? 'url', $task['interval_seconds']));
51 $DB->query_prepare("UPDATE MN_monitor_task SET last_run=?,next_run=?,last_status=?,last_code=?,last_error=?,fail_count=? WHERE id=?", [$now,$next,$status,$result['code'],$result['error'],$fail_count,$task['id']]);
52 $done++;
53}
54
55// 到期提醒、空间/流量通知(每天最多按文案去重一次)
56$users = $DB->get_all_prepare("SELECT * FROM MN_zj WHERE qk!='false' LIMIT 1000") ?: [];
57foreach($users as $u) {
58 if(!empty($u['datae']) && $u['datae']!='0000-00-00') {
59 $days = floor((strtotime($u['datae']) - time()) / 86400);
60 if(in_array($days, [7,3,1,0], true)) {
61 $title = '主机到期提醒';
62 $content = '您的主机将在 '.$days.' 天后到期,到期时间:'.$u['datae'];
63 $exists = $DB->get_row_prepare("SELECT id FROM MN_notice_log WHERE user=? AND title=? AND content=? AND created_at>=? limit 1", [$u['user'],$title,$content,date('Y-m-d 00:00:00')]);
64 if(!$exists) monitor_add_notice($DB, $u['user'], 'expire', $title, $content, 'warning');
65 }
66 }
67 $ll = json_decode($u['llmax'] ?? '', true) ?: [];
68 if(isset($ll['max'],$ll['dq']) && (float)$ll['max']>0) {
69 $percent = round($ll['dq'] / ((float)$ll['max']*1024*1024*1024) * 100, 2);
70 if($percent >= 80) {
71 $title = '流量使用提醒';
72 $content = '您的本月流量已使用 '.$percent.'%,请留意剩余流量。';
73 $exists = $DB->get_row_prepare("SELECT id FROM MN_notice_log WHERE user=? AND title=? AND created_at>=? limit 1", [$u['user'],$title,date('Y-m-d 00:00:00')]);
74 if(!$exists) monitor_add_notice($DB, $u['user'], 'traffic', $title, $content, $percent>=100?'danger':'warning');
75 }
76 }
77}
78
79if (function_exists('mnbt_do_action')) {
80 mnbt_do_action('cron', ['source' => 'jk_monitor', 'done' => $done]);
81}
82
83echo '执行完成,检测任务:'.$done;