No Description

ErrorLogMonitor.php 6.0KB

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