仰望星辰工作室

better-staridc-MNBT

better-staridc-MNBT/ user/api/monitor.php 4.8 KB · 83 行 原始文件
Z zfhsh first commit 1 天前
1<?php
2$monitor_actions = ['monitor_save','monitor_del','monitor_toggle','notice_read'];
3if(!in_array($egn, $monitor_actions, true)) return;
4include_once("../MPHX/monitor.function.php");
5monitor_ensure_tables($DB);
6
7if($egn=='monitor_save') {
8 $id = intval($_POST['id'] ?? 0);
9 $name = trim($_POST['name'] ?? '');
10 $task_type = trim($_POST['task_type'] ?? 'url');
11 $url = trim($_POST['url'] ?? '');
12 $resource_type = trim($_POST['resource_type'] ?? '');
13 $resource_threshold = max(1, min(100, intval($_POST['resource_threshold'] ?? 80)));
14 $method = monitor_normalize_method($_POST['method'] ?? 'GET');
15 if(!in_array($task_type, ['url','resource'], true)) $task_type = 'url';
16 $interval = monitor_normalize_interval($task_type, $_POST['interval_seconds'] ?? 60);
17 $timeout = max(1, min(30, intval($_POST['timeout_seconds'] ?? 10)));
18 $status_rule = trim($_POST['status_rule'] ?? 'eq');
19 $status_value = trim($_POST['status_value'] ?? '200');
20 $content_rule = trim($_POST['content_rule'] ?? 'none');
21 $content_value = trim($_POST['content_value'] ?? '');
22 $fail_threshold = max(1, intval($_POST['fail_threshold'] ?? 1));
23 $notify_email = ($_POST['notify_email'] ?? 'true') === 'true' ? 'true' : 'false';
24 $enabled = ($_POST['enabled'] ?? 'true') === 'true' ? 'true' : 'false';
25 if($name==='') json_exit('任务名称不能为空');
26 if($task_type === 'url' && $url==='') json_exit('URL不能为空');
27 if($task_type === 'resource' && !in_array($resource_type, ['web','sql','traffic'], true)) json_exit('请选择资源类型');
28 if($task_type === 'url' && !monitor_is_safe_url($url)) json_exit('URL格式错误或不允许访问内网地址');
29 if($task_type === 'resource') {
30 $url = '';
31 $method = 'GET';
32 $timeout = 10;
33 $status_rule = 'eq';
34 $status_value = '200';
35 $content_rule = 'none';
36 $content_value = '';
37 $fail_threshold = 1;
38 }
39 $now = date('Y-m-d H:i:s');
40 $next = date('Y-m-d H:i:s', time()+$interval);
41 if($id>0) {
42 $row = $DB->get_row_prepare("SELECT id FROM MN_monitor_task WHERE id=? AND user=? limit 1", [$id, $yhc['user']]);
43 if(!$row) json_exit('任务不存在');
44 $ok = $DB->query_prepare("UPDATE MN_monitor_task SET name=?,task_type=?,url=?,resource_type=?,resource_threshold=?,method=?,interval_seconds=?,timeout_seconds=?,status_rule=?,status_value=?,content_rule=?,content_value=?,fail_threshold=?,notify_email=?,enabled=?,next_run=?,updated_at=? WHERE id=? AND user=?", [$name,$task_type,$url,$resource_type,$resource_threshold,$method,$interval,$timeout,$status_rule,$status_value,$content_rule,$content_value,$fail_threshold,$notify_email,$enabled,$next,$now,$id,$yhc['user']]);
45 json_exit($ok?'保存成功':'保存失败');
46 } else {
47 $task_count = $DB->count_prepare("SELECT count(*) FROM MN_monitor_task WHERE user=?", [$yhc['user']]);
48 if($task_count >= 5) json_exit('每个用户最多只能添加5个监控任务');
49 $ok = $DB->query_prepare("INSERT INTO MN_monitor_task (user,name,task_type,url,resource_type,resource_threshold,method,interval_seconds,timeout_seconds,status_rule,status_value,content_rule,content_value,fail_threshold,notify_email,enabled,next_run,created_at,updated_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)", [$yhc['user'],$name,$task_type,$url,$resource_type,$resource_threshold,$method,$interval,$timeout,$status_rule,$status_value,$content_rule,$content_value,$fail_threshold,$notify_email,$enabled,$next,$now,$now]);
50 json_exit($ok?'添加成功':'添加失败');
51 }
52 return;
53}
54
55if($egn=='monitor_del') {
56 $id = intval($_POST['id'] ?? 0);
57 if($id<=0) json_exit('参数错误');
58 $ok = $DB->query_prepare("DELETE FROM MN_monitor_task WHERE id=? AND user=?", [$id, $yhc['user']]);
59 $DB->query_prepare("DELETE FROM MN_monitor_log WHERE task_id=? AND user=?", [$id, $yhc['user']]);
60 json_exit($ok?'删除成功':'删除失败');
61 return;
62}
63
64if($egn=='monitor_toggle') {
65 $id = intval($_POST['id'] ?? 0);
66 $enabled = ($_POST['enabled'] ?? 'true') === 'true' ? 'true' : 'false';
67 if($id<=0) json_exit('参数错误');
68 $next = date('Y-m-d H:i:s', time()+60);
69 $ok = $DB->query_prepare("UPDATE MN_monitor_task SET enabled=?,next_run=?,updated_at=? WHERE id=? AND user=?", [$enabled,$next,date('Y-m-d H:i:s'),$id,$yhc['user']]);
70 json_exit($ok?'修改成功':'修改失败');
71 return;
72}
73
74if($egn=='notice_read') {
75 $id = intval($_POST['id'] ?? 0);
76 if($id>0) {
77 $ok = $DB->query_prepare("UPDATE MN_notice_log SET is_read='true' WHERE id=? AND user=?", [$id, $yhc['user']]);
78 } else {
79 $ok = $DB->query_prepare("UPDATE MN_notice_log SET is_read='true' WHERE user=?", [$yhc['user']]);
80 }
81 json_exit($ok?'操作成功':'操作失败');
82 return;
83}