-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqq.php
121 lines (117 loc) · 4.51 KB
/
qq.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
<?php
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
// www.boxmoe.com //
////////////////////////////////////////////////////////////////////
$config = json_decode(file_get_contents(__DIR__ . "/config.json"), true);
//日志输出,看需求不需要就注释
$logPath = __DIR__ . "/web_status.log";
$curl = curl_init();
//错误输出
function errLog($msg)
{
global $logPath;
$message = "[错误]" . date("[y-m-d H:i:s]") . " $msg\n";
error_log($message, 3, $logPath);
//网页输出,按需不需要就注释
echo "<p id='err'>".$message."</p>" ;
}
//正常输出
function infoLog($msg)
{
global $logPath;
$message = "[正常]" . date("[y-m-d H:i:s]") . " $msg\n";
error_log($message, 3, $logPath);
//网页输出,按需不需要就注释
echo "<p id='info'>".$message."</p>\n";
}
//开始QQ机器人推送
function boxmoe_msg_qq($qq, $msg)
{
$message = "$msg";
$time = "[检测时间:". date("y-m-d H:i:s")."]";
$desp = $message . "\n".$time;
// 封装,推送到 QQ
$postdata = http_build_query(
array(
'message' => $desp
)
);
// 执行POST请求
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return $result = file_get_contents('http://127.0.0.1:5700/send_private_msg?user_id='.$qq.'', false, $context);
}
//开始监控
function startMonitor()
{
global $config, $curl;
$failedSites = [];
foreach ($config['websites'] as $website) {
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_HEADER => 1,
CURLOPT_URL => $website['url'],
CURLOPT_SSL_VERIFYPEER => 0,
CURLOPT_SSL_VERIFYHOST => 0,
));
$result = curl_exec($curl);
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($result === false || $code >= 400) {
errLog(
"[Web异常] " .
"[监控网站]" . $website['url'] . " " .
"[网站状态] $code " . curl_error($curl)
);
$failedSites[] = [
"website" => $website,
"err_msg" => curl_error($curl),
"code" => $code,
];
} else {
infoLog(
"[检测正常] [监控网站] " . $website['url']." [网站状态] ".$code
);
}
}
if (count($failedSites) && count($config['qq'])) {
$eMessage = "Web异常通知 \n";
foreach ($failedSites as $i => $fail) {
$eMessage .= "$i.\n";
$eMessage .= "\t[监控节点] " . $fail['website']['title'] . "\n" .
"\t[监控网站] " . $fail['website']['url'] . "\n" .
"\t[异常状态] (" . $fail['code'] . ") " . $fail['err_msg'] . "\n";
}
//通知QQ推送异常消息
foreach ($config['qq'] as $qq) {
boxmoe_msg_qq($qq, $eMessage);
}
}
}
startMonitor();
curl_close($curl);