AutoCheckNodeStatusJob.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\SsNode;
  7. use App\Http\Models\SsNodeInfo;
  8. use App\Http\Models\EmailLog;
  9. use App\Mail\nodeCrashWarning;
  10. use Cache;
  11. use Mail;
  12. use Log;
  13. class AutoCheckNodeStatusJob extends Command
  14. {
  15. protected $signature = 'autoCheckNodeStatusJob';
  16. protected $description = '自动监测节点是否宕机';
  17. protected $cacheKey = 'node_shutdown_warning_';
  18. public function __construct()
  19. {
  20. parent::__construct();
  21. }
  22. public function handle()
  23. {
  24. $config = $this->systemConfig();
  25. $nodeList = SsNode::query()->where('status', 1)->get();
  26. foreach ($nodeList as $node) {
  27. // 10分钟内无节点信息则认为是宕机,因为每个节点的负载信息最多保存10分钟
  28. $node_info = SsNodeInfo::query()->where('node_id', $node->id)->where('log_time', '>=', strtotime("-10 minutes"))->orderBy('id', 'desc')->first();
  29. if (empty($node_info) || empty($node_info->load)) {
  30. // 10分钟内已发警告,则不再发
  31. if (Cache::has($this->cacheKey . $node->id)) {
  32. continue;
  33. }
  34. $title = "节点宕机警告";
  35. $content = "系统监测到节点【{$node->name}】({$node->server})可能宕机了,请及时检查。";
  36. // 发邮件通知管理员
  37. if ($config['is_node_crash_warning']) {
  38. if ($config['crash_warning_email']) {
  39. try {
  40. Mail::to($config['crash_warning_email'])->send(new nodeCrashWarning($config['website_name'], $node->name, $node->server));
  41. $this->sendEmailLog(1, $title, $content);
  42. } catch (\Exception $e) {
  43. $this->sendEmailLog(1, $title, $content, 0, $e->getMessage());
  44. }
  45. }
  46. // 通过ServerChan发微信消息提醒管理员
  47. if ($config['is_server_chan'] && $config['server_chan_key']) {
  48. $serverChan = new ServerChan();
  49. $result = $serverChan->send($title, $content, $config['server_chan_key']);
  50. if ($result->errno > 0) {
  51. $this->sendEmailLog(1, '[ServerChan]' . $title, $content);
  52. } else {
  53. $this->sendEmailLog(1, '[ServerChan]' . $title, $content, 0, $result->errmsg);
  54. }
  55. }
  56. // 写入发信缓存
  57. Cache::put($this->cacheKey . $node->id, $node->name . '(' . $node->server . ')', 10);
  58. }
  59. }
  60. }
  61. Log::info('定时任务:' . $this->description);
  62. }
  63. /**
  64. * 写入邮件发送日志
  65. * @param int $user_id 接收者用户ID
  66. * @param string $title 标题
  67. * @param string $content 内容
  68. * @param int $status 投递状态
  69. * @param string $error 投递失败时记录的异常信息
  70. */
  71. private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  72. {
  73. $emailLogObj = new EmailLog();
  74. $emailLogObj->user_id = $user_id;
  75. $emailLogObj->title = $title;
  76. $emailLogObj->content = $content;
  77. $emailLogObj->status = $status;
  78. $emailLogObj->error = $error;
  79. $emailLogObj->created_at = date('Y-m-d H:i:s');
  80. $emailLogObj->save();
  81. }
  82. // 系统配置
  83. private function systemConfig()
  84. {
  85. $config = Config::query()->get();
  86. $data = [];
  87. foreach ($config as $vo) {
  88. $data[$vo->name] = $vo->value;
  89. }
  90. return $data;
  91. }
  92. }