-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy patherrorHandler.php
More file actions
77 lines (77 loc) · 2.82 KB
/
Copy patherrorHandler.php
File metadata and controls
77 lines (77 loc) · 2.82 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
<?php
/**
* Created by PhpStorm.
* User: zhezhao
* Date: 2016/9/28
* Time: 14:54
*/
require_once 'mailto.php';
$errorList = array();
//从队列取出错误信息
$redis = new Redis();
if($redis->connect('127.0.0.1', 6379) && $redis->auth("hfcasnic")){
while ($row = $redis->rPop("errorList")){
$errorList[] = json_decode($row,true);
}
$redis->close();
}else{
writeText('redis打开失败');
}
//进行错误处理
foreach ($errorList as $errorItem){
$level = $errorItem['level'];
$client_id = $errorItem['client_id'];
$msg = $errorItem['msg'];
$stime = $errorItem['stime'];
//短信、邮件报警
$report_level = array('fatal');
if(in_array($level,$report_level)){
//获取邮件收件人,短信收件人
$mysqli = new mysqli("127.0.0.1","root","root","zabbix");
if($mysqli->connect_errno){ //连接成功errno应该为0
writeText('Connect Error:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$mysql_result = $mysqli->query("SELECT * FROM `alert_setting` WHERE `item` IN ('email','message')");
$item = array();
while ($row = $mysql_result->fetch_assoc()){
$item[$row['item']] = $row['value'];
}
$mailto = $item['email'];
$smsto = $item['message'];
$mysql_result->free();
$mysqli->close();
//发送邮件、短信
$err_msg = '';
$err_msg .= "来源ID:".$client_id."<br/>";
$err_msg .= "错误等级:".$level."<br/>";
$err_msg .= "发生时间:".date("Y-m-d H-i-s",$stime)."<br/>";
$err_msg .= "错误详细:".$msg."<br/>";
$subject = "错误收集接口报警";
sendmailto($mailto,$subject,$err_msg);
writeText("mailto ".$mailto);
writeText("send message to ".$smsto);
}
//写入mysql数据库
$mysqli = new mysqli("127.0.0.1","root","root","zabbix");
if($mysqli->connect_errno){ //连接成功errno应该为0
writeText('Connect Error:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf8');
$prepare_sql = "INSERT INTO `collected_error`(`occur_time`,`client_id`,`level`,`msg`,`mail`,`sms`,`handle_time`,`finished`) VALUES (?,?,?,?,?,?,?,?)";
$mysqli_stmt = $mysqli->prepare($prepare_sql);
$finished = 1;
$mysqli_stmt->bind_param("sssssssi",date("Y-m-d H-i-s",$stime),$client_id,$level,$msg,$mailto,$smsto,date("Y-m-d H-i-s"),$finished);
if($mysqli_stmt->execute()){
$id = $mysqli_stmt->insert_id;
}else{
writeText("Insert Error:".date("Y-m-d H-i-s",$stime).",$client_id,$level,$msg");
}
$mysqli->close();
}
//写入文本文件中的日志
function writeText($str){
$content = date("Y-m-d H:i:s");
$content = "[".$content."]".$str."\r\n";
file_put_contents("/var/log/errorHandler.log",$content,FILE_APPEND);
}