UserTrafficAbnormalWarningJob.php 3.5 KB

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