Sin descripción

ErrorLogMonitor.php 6.3KB

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