UserTrafficAbnormalAutoWarning.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\ServerChan;
  4. use Illuminate\Console\Command;
  5. use App\Http\Models\Config;
  6. use App\Http\Models\User;
  7. use App\Http\Models\UserTrafficHourly;
  8. use Cache;
  9. use Log;
  10. class UserTrafficAbnormalAutoWarning extends Command
  11. {
  12. protected $signature = 'userTrafficAbnormalAutoWarning';
  13. protected $description = '用户流量异常警告';
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. }
  18. public function handle()
  19. {
  20. $jobStartTime = microtime(true);
  21. // 1小时内流量异常用户(多往前取5分钟,防止数据统计任务执行时间过长导致没有数据)
  22. $userTotalTrafficList = UserTrafficHourly::query()->where('node_id', 0)->where('total', '>', 104857600)->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))->groupBy('user_id')->selectRaw("user_id, sum(total) as totalTraffic")->get(); // 只统计100M以上的记录,加快查询速度
  23. if (!$userTotalTrafficList->isEmpty()) {
  24. $title = "流量异常用户提醒";
  25. $config = $this->systemConfig();
  26. foreach ($userTotalTrafficList as $vo) {
  27. $user = User::query()->where('id', $vo->user_id)->first();
  28. // 通过ServerChan发微信消息提醒管理员
  29. if ($vo->totalTraffic > ($config['traffic_ban_value'] * 1024 * 1024 * 1024) && $config['is_server_chan'] && $config['server_chan_key']) {
  30. $traffic = UserTrafficHourly::query()->where('node_id', 0)->where('user_id', $vo->user_id)->where('created_at', '>=', date('Y-m-d H:i:s', time() - 3900))->selectRaw("user_id, sum(`u`) as totalU, sum(`d`) as totalD, sum(total) as totalTraffic")->first();
  31. $content = "用户**{$user->username}(ID:{$user->id})**,最近1小时**上传流量:{$this->flowAutoShow($traffic->totalU)},下载流量:{$this->flowAutoShow($traffic->totalD)},共计:{$this->flowAutoShow($traffic->totalTraffic)}**。";
  32. $serverChan = new ServerChan();
  33. $serverChan->send($title, $content);
  34. }
  35. }
  36. }
  37. $jobEndTime = microtime(true);
  38. $jobUsedTime = round(($jobEndTime - $jobStartTime) , 4);
  39. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  40. }
  41. // 系统配置
  42. private function systemConfig()
  43. {
  44. $config = Config::query()->get();
  45. $data = [];
  46. foreach ($config as $vo) {
  47. $data[$vo->name] = $vo->value;
  48. }
  49. return $data;
  50. }
  51. // 根据流量值自动转换单位输出
  52. private function flowAutoShow($value = 0)
  53. {
  54. $kb = 1024;
  55. $mb = 1048576;
  56. $gb = 1073741824;
  57. $tb = $gb * 1024;
  58. $pb = $tb * 1024;
  59. if (abs($value) > $pb) {
  60. return round($value / $pb, 2) . "PB";
  61. } elseif (abs($value) > $tb) {
  62. return round($value / $tb, 2) . "TB";
  63. } elseif (abs($value) > $gb) {
  64. return round($value / $gb, 2) . "GB";
  65. } elseif (abs($value) > $mb) {
  66. return round($value / $mb, 2) . "MB";
  67. } elseif (abs($value) > $kb) {
  68. return round($value / $kb, 2) . "KB";
  69. } else {
  70. return round($value, 2) . "B";
  71. }
  72. }
  73. }