暫無描述

Monitor.php 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php namespace App\Console\Commands;
  2. use Illuminate\Console\Command;
  3. use Illuminate\Support\Facades\DB;
  4. use App\libs\sms;
  5. define("YP_SMS_KEY", "fbdb5f2ddae13c2f4a592348bfe52137");
  6. define('YP_SMS_YHQ', '73a74eb72c42b765669acd8e94096b9f');
  7. define("YP_SMS_KEY_FAMLI",'995629e02beaaf47118b84ac19c4b5b9');
  8. define("YP_VOICE_URL", "http://voice.yunpian.com/v2/voice/send.json");
  9. define("YP_TPL_URL", "https://sms.yunpian.com/v2/sms/tpl_single_send.json");
  10. define("YP_TPL_ID", "3310818");
  11. class Monitor extends Command {
  12. protected $signature = 'Monitor';
  13. // protected $phone = '13161864516';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = '监控template5分钟内点击量';
  20. public function handle() {
  21. set_time_limit(0);
  22. ini_set('memory_limit', '1024M');
  23. $this->monitor();
  24. }
  25. public function monitor() {
  26. #统计一定时间区间内的uv频率
  27. $startTime = strtotime(date('Y-m-d 09:00:00',time()));
  28. $endTime = strtotime(date('Y-m-d 17:00:00',time()));
  29. $currentTime = time();
  30. if($startTime > $currentTime ||
  31. $endTime < $currentTime ||
  32. (date('w',$currentTime)==6) ||
  33. (date('w',$currentTime) == 0)){
  34. return false;
  35. }
  36. $fiveMinuteBefore = date('Y-m-d H:i:s',strtotime('-5 minutes'));
  37. $pv = DB::table('templates_log')->where('create_time','<=',date('Y-m-d H:i:s',$currentTime))->where('create_time','>=',$fiveMinuteBefore)->count();
  38. if($pv <= 1){
  39. #发送验证码报警
  40. self::sendMsg('13161864516');
  41. self::sendMsg('18410900527');
  42. error_log(date('Y-m-d H:i:s',time()).' pv:'.$pv." \n ",3,"/log/seafood_log/script_success/".date('Y-m-d',time()).'Monitor.log');
  43. }
  44. }
  45. private static function init(){
  46. $ch = curl_init();
  47. /* 设置验证方式 */
  48. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept:text/plain;charset=utf-8',
  49. 'Content-Type:application/x-www-form-urlencoded', 'charset=utf-8'));
  50. /* 设置返回结果为流 */
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52. /* 设置超时时间*/
  53. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  54. /* 设置通信方式 */
  55. curl_setopt($ch, CURLOPT_POST, 1);
  56. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  57. return $ch;
  58. }
  59. public static function sendMsg($phone){
  60. $tpl_id = YP_TPL_ID;
  61. $ch=self::init();
  62. //$data=array('tpl_id' => $tpl_id,'text'=>$text,'apikey'=>YP_SMS_KEY,'mobile'=>$phone);
  63. $data = [
  64. 'apikey' => YP_SMS_KEY,
  65. 'mobile' => $phone,
  66. 'tpl_id' => $tpl_id,
  67. ];
  68. $json_data = self::tpl_send($ch,$data);
  69. //print_r($json_data); ******************************maybe影响验证码发出
  70. $array = json_decode($json_data,true);
  71. // echo '<pre>';print_r($array);
  72. curl_close($ch);
  73. return $array;
  74. }
  75. private static function tpl_send($ch,$data){
  76. curl_setopt ($ch, CURLOPT_URL, YP_TPL_URL);
  77. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
  78. $result = curl_exec($ch);
  79. $error = curl_error($ch);
  80. self::checkErr($result,$error);
  81. return $result;
  82. }
  83. private static function checkErr($result,$error) {
  84. if($result === false)
  85. {
  86. echo 'Curl error: ' . $error;
  87. }
  88. // else
  89. // {
  90. // echo '操作完成没有任何错误';
  91. // }
  92. }
  93. }