123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- <?php namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\DB;
- use App\libs\sms;
- class ErrorLogMonitor extends Command {
- /**
- * The console command name.
- *
- * @var string
- */
- protected $name = 'ErrorLogMonitor';
- protected $YP_TPL_ID = '3390076';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '监控最近3分钟错误日志';
- public function handle()
- {
- //本地测试路径
- // $path = dirname(__FILE__).'\..\..\..\public';
- $path1 = '/log/seafood_log/error';
- $res1 = $this->chargeFile($path1);
- $path2 = '/log/seafood_log/script_error';
- $res2 = $this->chargeFile($path2);
- if($res1 || $res2){
- $this->sendMsg('13161864516');
- $this->sendMsg('18501257479');
- $this->sendMsg('18410900527');
- }
- }
- public function chargeFile($path){
- $fileName = '/'.date('Y-m-d',time()).'.txt';
- //判断文件是否存在
- $fileName = $path.$fileName;
- $fileRes = file_exists($fileName);
- if($fileRes){
- //读取文件内容
- $contents = $this->tailWithSkip($fileName);
- $arr = json_decode($contents,true);
- $time = strtotime($arr['time']);
- //判断最后一行时间是否在3分钟内
- if(time()-$time <= 180){
- //报警
- return true;
- }
- }
- return false;
- }
- //获取文件最后一行内容
- public function tailWithSkip($filepath, $lines = 1, $skip = 0, $adaptive = true)
- {
- // Open file
- $f = @fopen($filepath, "rb");
- if (@flock($f, LOCK_SH) === false) return false;
- if ($f === false) return false;
- // Sets buffer size, according to the number of lines to retrieve.
- // This gives a performance boost when reading a few lines from the file.
- $max=max($lines, $skip);
- if (!$adaptive) $buffer = 4096;
- else $buffer = ($max < 2 ? 64 : ($max < 10 ? 512 : 4096));
- // Jump to last character
- fseek($f, -1, SEEK_END);
- // Read it and adjust line number if necessary
- // (Otherwise the result would be wrong if file doesn't end with a blank line)
- if (fread($f, 1) == "\n") {
- if ($skip > 0) { $skip++; $lines--; }
- } else {
- $lines--;
- }
- // Start reading
- $output = '';
- $chunk = '';
- // While we would like more
- while (ftell($f) > 0 && $lines >= 0) {
- // Figure out how far back we should jump
- $seek = min(ftell($f), $buffer);
- // Do the jump (backwards, relative to where we are)
- fseek($f, -$seek, SEEK_CUR);
- // Read a chunk
- $chunk = fread($f, $seek);
- // Calculate chunk parameters
- $count = substr_count($chunk, "\n");
- $strlen = mb_strlen($chunk, '8bit');
- // Move the file pointer
- fseek($f, -$strlen, SEEK_CUR);
- if ($skip > 0) { // There are some lines to skip
- if ($skip > $count) { $skip -= $count; $chunk=''; } // Chunk contains less new line symbols than
- else {
- $pos = 0;
- while ($skip > 0) {
- if ($pos > 0) $offset = $pos - $strlen - 1; // Calculate the offset - NEGATIVE position of last new line symbol
- else $offset=0; // First search (without offset)
- $pos = strrpos($chunk, "\n", $offset); // Search for last (including offset) new line symbol
- if ($pos !== false) $skip--; // Found new line symbol - skip the line
- else break; // "else break;" - Protection against infinite loop (just in case)
- }
- $chunk=substr($chunk, 0, $pos); // Truncated chunk
- $count=substr_count($chunk, "\n"); // Count new line symbols in truncated chunk
- }
- }
- if (strlen($chunk) > 0) {
- // Add chunk to the output
- $output = $chunk . $output;
- // Decrease our line counter
- $lines -= $count;
- }
- }
- // While we have too many lines
- // (Because of buffer size we might have read too many)
- while ($lines++ < 0) {
- // Find first newline and remove all text before that
- $output = substr($output, strpos($output, "\n") + 1);
- }
- // Close file and return
- @flock($f, LOCK_UN);
- fclose($f);
- return trim($output);
- }
- private function init(){
- $ch = curl_init();
- /* 设置验证方式 */
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
- 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
- /* 设置返回结果为流 */
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- /* 设置超时时间*/
- curl_setopt($ch, CURLOPT_TIMEOUT, 10);
- /* 设置通信方式 */
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- return $ch;
- }
- public function sendMsg($phone){
- $tpl_id = $this->YP_TPL_ID;
- $ch=$this->init();
- //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
- $data = [
- 'apikey' => YP_SMS_KEY,
- 'mobile' => $phone,
- 'tpl_id' => $tpl_id,
- ];
- $json_data = $this->tpl_send($ch,$data);
- //print_r($json_data); ******************************maybe影响验证码发出
- $array = json_decode($json_data,true);
- // echo '<pre>';print_r($array);
- curl_close($ch);
- return $array;
- }
- private function tpl_send($ch,$data){
- curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
- curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
- $result = curl_exec($ch);
- $error = curl_error($ch);
- $this->checkErr($result,$error);
- return $result;
- }
- private static function checkErr($result,$error) {
- if($result === false)
- {
- echo 'Curl error: ' . $error;
- }
- // else
- // {
- // echo '操作完成没有任何错误';
- // }
- }
- }
|