|
@@ -0,0 +1,188 @@
|
|
1
|
+<?php namespace App\Console\Commands;
|
|
2
|
+
|
|
3
|
+use Illuminate\Console\Command;
|
|
4
|
+use Illuminate\Support\Facades\DB;
|
|
5
|
+use App\libs\sms;
|
|
6
|
+
|
|
7
|
+class ErrorLogMonitor extends Command {
|
|
8
|
+
|
|
9
|
+ /**
|
|
10
|
+ * The console command name.
|
|
11
|
+ *
|
|
12
|
+ * @var string
|
|
13
|
+ */
|
|
14
|
+ protected $name = 'ErrorLogMonitor';
|
|
15
|
+ protected $YP_TPL_ID = '3390076';
|
|
16
|
+
|
|
17
|
+ /**
|
|
18
|
+ * The console command description.
|
|
19
|
+ *
|
|
20
|
+ * @var string
|
|
21
|
+ */
|
|
22
|
+ protected $description = '监控最近3分钟错误日志';
|
|
23
|
+
|
|
24
|
+ public function handle()
|
|
25
|
+ {
|
|
26
|
+ //本地测试路径
|
|
27
|
+// $path = dirname(__FILE__).'\..\..\..\public';
|
|
28
|
+$path = '\log\seafood_log\error';
|
|
29
|
+ $fileName = '\\'.date('Y-m-d',time()).'.txt';
|
|
30
|
+ //判断文件是否存在
|
|
31
|
+ $fileName = $path.$fileName;
|
|
32
|
+ $fileRes = file_exists($fileName);
|
|
33
|
+ if($fileRes){
|
|
34
|
+ //读取文件内容
|
|
35
|
+ $contents = $this->tailWithSkip($fileName);
|
|
36
|
+ $arr = json_decode($contents,true);
|
|
37
|
+ $time = strtotime($arr['time']);
|
|
38
|
+
|
|
39
|
+ //判断最后一行时间是否在3分钟内
|
|
40
|
+ if(time()-$time <= 180){
|
|
41
|
+ //报警
|
|
42
|
+ $this->sendMsg('13161864516');
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+ }
|
|
46
|
+
|
|
47
|
+ //获取文件最后一行内容
|
|
48
|
+ public function tailWithSkip($filepath, $lines = 1, $skip = 0, $adaptive = true)
|
|
49
|
+ {
|
|
50
|
+ // Open file
|
|
51
|
+ $f = @fopen($filepath, "rb");
|
|
52
|
+ if (@flock($f, LOCK_SH) === false) return false;
|
|
53
|
+ if ($f === false) return false;
|
|
54
|
+
|
|
55
|
+ // Sets buffer size, according to the number of lines to retrieve.
|
|
56
|
+ // This gives a performance boost when reading a few lines from the file.
|
|
57
|
+ $max=max($lines, $skip);
|
|
58
|
+ if (!$adaptive) $buffer = 4096;
|
|
59
|
+ else $buffer = ($max < 2 ? 64 : ($max < 10 ? 512 : 4096));
|
|
60
|
+
|
|
61
|
+ // Jump to last character
|
|
62
|
+ fseek($f, -1, SEEK_END);
|
|
63
|
+
|
|
64
|
+ // Read it and adjust line number if necessary
|
|
65
|
+ // (Otherwise the result would be wrong if file doesn't end with a blank line)
|
|
66
|
+ if (fread($f, 1) == "\n") {
|
|
67
|
+ if ($skip > 0) { $skip++; $lines--; }
|
|
68
|
+ } else {
|
|
69
|
+ $lines--;
|
|
70
|
+ }
|
|
71
|
+
|
|
72
|
+ // Start reading
|
|
73
|
+ $output = '';
|
|
74
|
+ $chunk = '';
|
|
75
|
+ // While we would like more
|
|
76
|
+ while (ftell($f) > 0 && $lines >= 0) {
|
|
77
|
+ // Figure out how far back we should jump
|
|
78
|
+ $seek = min(ftell($f), $buffer);
|
|
79
|
+
|
|
80
|
+ // Do the jump (backwards, relative to where we are)
|
|
81
|
+ fseek($f, -$seek, SEEK_CUR);
|
|
82
|
+
|
|
83
|
+ // Read a chunk
|
|
84
|
+ $chunk = fread($f, $seek);
|
|
85
|
+
|
|
86
|
+ // Calculate chunk parameters
|
|
87
|
+ $count = substr_count($chunk, "\n");
|
|
88
|
+ $strlen = mb_strlen($chunk, '8bit');
|
|
89
|
+
|
|
90
|
+ // Move the file pointer
|
|
91
|
+ fseek($f, -$strlen, SEEK_CUR);
|
|
92
|
+
|
|
93
|
+ if ($skip > 0) { // There are some lines to skip
|
|
94
|
+ if ($skip > $count) { $skip -= $count; $chunk=''; } // Chunk contains less new line symbols than
|
|
95
|
+ else {
|
|
96
|
+ $pos = 0;
|
|
97
|
+
|
|
98
|
+ while ($skip > 0) {
|
|
99
|
+ if ($pos > 0) $offset = $pos - $strlen - 1; // Calculate the offset - NEGATIVE position of last new line symbol
|
|
100
|
+ else $offset=0; // First search (without offset)
|
|
101
|
+
|
|
102
|
+ $pos = strrpos($chunk, "\n", $offset); // Search for last (including offset) new line symbol
|
|
103
|
+
|
|
104
|
+ if ($pos !== false) $skip--; // Found new line symbol - skip the line
|
|
105
|
+ else break; // "else break;" - Protection against infinite loop (just in case)
|
|
106
|
+ }
|
|
107
|
+ $chunk=substr($chunk, 0, $pos); // Truncated chunk
|
|
108
|
+ $count=substr_count($chunk, "\n"); // Count new line symbols in truncated chunk
|
|
109
|
+ }
|
|
110
|
+ }
|
|
111
|
+
|
|
112
|
+ if (strlen($chunk) > 0) {
|
|
113
|
+ // Add chunk to the output
|
|
114
|
+ $output = $chunk . $output;
|
|
115
|
+ // Decrease our line counter
|
|
116
|
+ $lines -= $count;
|
|
117
|
+ }
|
|
118
|
+ }
|
|
119
|
+
|
|
120
|
+ // While we have too many lines
|
|
121
|
+ // (Because of buffer size we might have read too many)
|
|
122
|
+ while ($lines++ < 0) {
|
|
123
|
+ // Find first newline and remove all text before that
|
|
124
|
+ $output = substr($output, strpos($output, "\n") + 1);
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ // Close file and return
|
|
128
|
+ @flock($f, LOCK_UN);
|
|
129
|
+ fclose($f);
|
|
130
|
+ return trim($output);
|
|
131
|
+ }
|
|
132
|
+
|
|
133
|
+ private function init(){
|
|
134
|
+ $ch = curl_init();
|
|
135
|
+ /* 设置验证方式 */
|
|
136
|
+ curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
|
|
137
|
+ 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
|
|
138
|
+ /* 设置返回结果为流 */
|
|
139
|
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
140
|
+
|
|
141
|
+ /* 设置超时时间*/
|
|
142
|
+ curl_setopt($ch, CURLOPT_TIMEOUT, 10);
|
|
143
|
+
|
|
144
|
+ /* 设置通信方式 */
|
|
145
|
+ curl_setopt($ch, CURLOPT_POST, 1);
|
|
146
|
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
147
|
+ return $ch;
|
|
148
|
+
|
|
149
|
+ }
|
|
150
|
+
|
|
151
|
+ public function sendMsg($phone){
|
|
152
|
+
|
|
153
|
+ $tpl_id = $this->YP_TPL_ID;
|
|
154
|
+ $ch=$this->init();
|
|
155
|
+ //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
|
|
156
|
+ $data = [
|
|
157
|
+ 'apikey' => YP_SMS_KEY,
|
|
158
|
+ 'mobile' => $phone,
|
|
159
|
+ 'tpl_id' => $tpl_id,
|
|
160
|
+ ];
|
|
161
|
+ $json_data = $this->tpl_send($ch,$data);
|
|
162
|
+ //print_r($json_data); ******************************maybe影响验证码发出
|
|
163
|
+ $array = json_decode($json_data,true);
|
|
164
|
+ // echo '<pre>';print_r($array);
|
|
165
|
+ curl_close($ch);
|
|
166
|
+ return $array;
|
|
167
|
+ }
|
|
168
|
+
|
|
169
|
+ private function tpl_send($ch,$data){
|
|
170
|
+ curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
|
|
171
|
+ curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
|
|
172
|
+ $result = curl_exec($ch);
|
|
173
|
+ $error = curl_error($ch);
|
|
174
|
+ $this->checkErr($result,$error);
|
|
175
|
+ return $result;
|
|
176
|
+ }
|
|
177
|
+
|
|
178
|
+ private static function checkErr($result,$error) {
|
|
179
|
+ if($result === false)
|
|
180
|
+ {
|
|
181
|
+ echo 'Curl error: ' . $error;
|
|
182
|
+ }
|
|
183
|
+// else
|
|
184
|
+// {
|
|
185
|
+// echo '操作完成没有任何错误';
|
|
186
|
+// }
|
|
187
|
+ }
|
|
188
|
+}
|