AutoReportNode.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Components\Helpers;
  4. use App\Components\ServerChan;
  5. use App\Http\Models\SsNode;
  6. use App\Http\Models\SsNodeTrafficDaily;
  7. use Illuminate\Console\Command;
  8. use Log;
  9. class AutoReportNode extends Command
  10. {
  11. protected $signature = 'autoReportNode';
  12. protected $description = '自动报告节点昨日使用情况';
  13. protected static $systemConfig;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. self::$systemConfig = Helpers::systemConfig();
  18. }
  19. public function handle()
  20. {
  21. $jobStartTime = microtime(true);
  22. if (self::$systemConfig['node_daily_report']) {
  23. $nodeList = SsNode::query()->where('status', 1)->get();
  24. if (!$nodeList->isEmpty()) {
  25. $msg = "|节点|上行流量|下行流量|合计|\r\n| :------ | :------ | :------ |\r\n";
  26. foreach ($nodeList as $node) {
  27. $log = SsNodeTrafficDaily::query()
  28. ->where('node_id', $node->id)
  29. ->where('created_at', '>=', date('Y-m-d 00:00:00', strtotime("-1 day")))
  30. ->where('created_at', '<=', date('Y-m-d 23:59:59', strtotime("-1 day")))
  31. ->first();
  32. $msg .= '|' . $node->name . '|' . flowAutoShow($log->u) . '|' . flowAutoShow($log->d) . '|' . $log->traffic . "\r\n";
  33. }
  34. $this->notifyMasterByServerchan('节点日报', $msg);
  35. }
  36. }
  37. $jobEndTime = microtime(true);
  38. $jobUsedTime = round(($jobEndTime - $jobStartTime), 4);
  39. Log::info('执行定时任务【' . $this->description . '】,耗时' . $jobUsedTime . '秒');
  40. }
  41. /**
  42. * 通过ServerChan发微信消息提醒管理员
  43. *
  44. * @param string $title 消息标题
  45. * @param string $content 消息内容
  46. *
  47. * @throws \GuzzleHttp\Exception\GuzzleException
  48. */
  49. private function notifyMasterByServerchan($title, $content)
  50. {
  51. if (self::$systemConfig['is_server_chan'] && self::$systemConfig['server_chan_key']) {
  52. ServerChan::send($title, $content);
  53. }
  54. }
  55. }