1
0

AutoCheckNodeStatusJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. $serverChan->send($title, $content);
  50. }
  51. // 写入发信缓存
  52. Cache::put($this->cacheKey . $node->id, $node->name . '(' . $node->server . ')', 10);
  53. }
  54. }
  55. }
  56. Log::info('定时任务:' . $this->description);
  57. }
  58. /**
  59. * 写入邮件发送日志
  60. * @param int $user_id 接收者用户ID
  61. * @param string $title 标题
  62. * @param string $content 内容
  63. * @param int $status 投递状态
  64. * @param string $error 投递失败时记录的异常信息
  65. */
  66. private function sendEmailLog($user_id, $title, $content, $status = 1, $error = '')
  67. {
  68. $emailLogObj = new EmailLog();
  69. $emailLogObj->user_id = $user_id;
  70. $emailLogObj->title = $title;
  71. $emailLogObj->content = $content;
  72. $emailLogObj->status = $status;
  73. $emailLogObj->error = $error;
  74. $emailLogObj->created_at = date('Y-m-d H:i:s');
  75. $emailLogObj->save();
  76. }
  77. // 系统配置
  78. private function systemConfig()
  79. {
  80. $config = Config::query()->get();
  81. $data = [];
  82. foreach ($config as $vo) {
  83. $data[$vo->name] = $vo->value;
  84. }
  85. return $data;
  86. }
  87. }