AutoCheckNodeStatusJob.php 3.8 KB

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