AutoCheckNodeStatusJob.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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分钟内无节点负载信息则认为是宕机
  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}】({$node->ip})**可能宕机,请及时检查。";
  36. // 发邮件通知管理员
  37. if ($config['is_node_crash_warning'] && $config['crash_warning_email']) {
  38. try {
  39. Mail::to($config['crash_warning_email'])->send(new nodeCrashWarning($config['website_name'], $node->name, $node->server));
  40. $this->sendEmailLog(1, $title, $content);
  41. } catch (\Exception $e) {
  42. $this->sendEmailLog(1, $title, $content, 0, $e->getMessage());
  43. }
  44. }
  45. // 通过ServerChan发微信消息提醒管理员
  46. if ($config['is_server_chan'] && $config['server_chan_key']) {
  47. $serverChan = new ServerChan();
  48. $serverChan->send($title, $content);
  49. }
  50. // 写入发信缓存
  51. Cache::put($this->cacheKey . $node->id, $node->name . '(' . $node->server . ')', 10);
  52. }
  53. }
  54. Log::info('定时任务:' . $this->description);
  55. }
  56. /**
  57. * 写入邮件发送日志
  58. *
  59. * @param int $user_id 接收者用户ID
  60. * @param string $title 标题
  61. * @param string $content 内容
  62. * @param int $status 投递状态
  63. * @param string $error 投递失败时记录的异常信息
  64. */
  65. private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  66. {
  67. $emailLogObj = new EmailLog();
  68. $emailLogObj->user_id = $user_id;
  69. $emailLogObj->title = $title;
  70. $emailLogObj->content = $content;
  71. $emailLogObj->status = $status;
  72. $emailLogObj->error = $error;
  73. $emailLogObj->created_at = date('Y-m-d H:i:s');
  74. $emailLogObj->save();
  75. }
  76. // 系统配置
  77. private function systemConfig()
  78. {
  79. $config = Config::query()->get();
  80. $data = [];
  81. foreach ($config as $vo) {
  82. $data[$vo->name] = $vo->value;
  83. }
  84. return $data;
  85. }
  86. }