MonitorAlertEvent.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace App\Module\ThirdParty\Events;
  3. use App\Module\ThirdParty\Models\ThirdPartyService;
  4. use App\Module\ThirdParty\Models\ThirdPartyMonitor;
  5. use Illuminate\Foundation\Events\Dispatchable;
  6. use Illuminate\Queue\SerializesModels;
  7. /**
  8. * 监控告警事件
  9. *
  10. * 当第三方服务监控检测到异常时触发此事件
  11. */
  12. class MonitorAlertEvent
  13. {
  14. use Dispatchable, SerializesModels;
  15. /**
  16. * 第三方服务实例
  17. *
  18. * @var ThirdPartyService
  19. */
  20. public ThirdPartyService $service;
  21. /**
  22. * 监控记录实例
  23. *
  24. * @var ThirdPartyMonitor
  25. */
  26. public ThirdPartyMonitor $monitor;
  27. /**
  28. * 告警类型
  29. *
  30. * @var string
  31. */
  32. public string $alertType;
  33. /**
  34. * 告警级别
  35. *
  36. * @var string
  37. */
  38. public string $alertLevel;
  39. /**
  40. * 告警消息
  41. *
  42. * @var string
  43. */
  44. public string $alertMessage;
  45. /**
  46. * 监控数据
  47. *
  48. * @var array
  49. */
  50. public array $monitorData;
  51. /**
  52. * 告警时间
  53. *
  54. * @var \Carbon\Carbon
  55. */
  56. public \Carbon\Carbon $alertTime;
  57. /**
  58. * 额外数据
  59. *
  60. * @var array
  61. */
  62. public array $metadata;
  63. // 告警类型常量
  64. public const ALERT_TYPE_HEALTH = 'health'; // 健康检查告警
  65. public const ALERT_TYPE_PERFORMANCE = 'performance'; // 性能告警
  66. public const ALERT_TYPE_AVAILABILITY = 'availability'; // 可用性告警
  67. public const ALERT_TYPE_ERROR_RATE = 'error_rate'; // 错误率告警
  68. public const ALERT_TYPE_RESPONSE_TIME = 'response_time'; // 响应时间告警
  69. // 告警级别常量
  70. public const LEVEL_INFO = 'INFO';
  71. public const LEVEL_WARNING = 'WARNING';
  72. public const LEVEL_CRITICAL = 'CRITICAL';
  73. /**
  74. * 创建事件实例
  75. *
  76. * @param ThirdPartyService $service
  77. * @param ThirdPartyMonitor $monitor
  78. * @param string $alertType
  79. * @param string $alertLevel
  80. * @param string $alertMessage
  81. * @param array $monitorData
  82. * @param array $metadata
  83. */
  84. public function __construct(
  85. ThirdPartyService $service,
  86. ThirdPartyMonitor $monitor,
  87. string $alertType,
  88. string $alertLevel,
  89. string $alertMessage,
  90. array $monitorData = [],
  91. array $metadata = []
  92. ) {
  93. $this->service = $service;
  94. $this->monitor = $monitor;
  95. $this->alertType = $alertType;
  96. $this->alertLevel = $alertLevel;
  97. $this->alertMessage = $alertMessage;
  98. $this->monitorData = $monitorData;
  99. $this->alertTime = now();
  100. $this->metadata = $metadata;
  101. }
  102. /**
  103. * 获取事件数据数组
  104. *
  105. * @return array
  106. */
  107. public function toArray(): array
  108. {
  109. return [
  110. 'service_id' => $this->service->id,
  111. 'service_code' => $this->service->code,
  112. 'service_name' => $this->service->name,
  113. 'monitor_id' => $this->monitor->id,
  114. 'alert_type' => $this->alertType,
  115. 'alert_level' => $this->alertLevel,
  116. 'alert_message' => $this->alertMessage,
  117. 'monitor_data' => $this->monitorData,
  118. 'alert_time' => $this->alertTime->toISOString(),
  119. 'metadata' => $this->metadata,
  120. ];
  121. }
  122. /**
  123. * 判断是否为严重告警
  124. *
  125. * @return bool
  126. */
  127. public function isCritical(): bool
  128. {
  129. return $this->alertLevel === self::LEVEL_CRITICAL;
  130. }
  131. /**
  132. * 判断是否为警告告警
  133. *
  134. * @return bool
  135. */
  136. public function isWarning(): bool
  137. {
  138. return $this->alertLevel === self::LEVEL_WARNING;
  139. }
  140. /**
  141. * 获取告警标题
  142. *
  143. * @return string
  144. */
  145. public function getAlertTitle(): string
  146. {
  147. $serviceName = $this->service->name;
  148. $typeLabel = $this->getAlertTypeLabel();
  149. return "第三方服务监控告警 - {$serviceName} ({$typeLabel})";
  150. }
  151. /**
  152. * 获取告警类型标签
  153. *
  154. * @return string
  155. */
  156. public function getAlertTypeLabel(): string
  157. {
  158. return match ($this->alertType) {
  159. self::ALERT_TYPE_HEALTH => '健康检查',
  160. self::ALERT_TYPE_PERFORMANCE => '性能监控',
  161. self::ALERT_TYPE_AVAILABILITY => '可用性监控',
  162. self::ALERT_TYPE_ERROR_RATE => '错误率监控',
  163. self::ALERT_TYPE_RESPONSE_TIME => '响应时间监控',
  164. default => '未知类型',
  165. };
  166. }
  167. /**
  168. * 获取详细告警信息
  169. *
  170. * @return array
  171. */
  172. public function getDetailedInfo(): array
  173. {
  174. $info = [
  175. 'service' => [
  176. 'id' => $this->service->id,
  177. 'code' => $this->service->code,
  178. 'name' => $this->service->name,
  179. 'type' => $this->service->type,
  180. 'status' => $this->service->status,
  181. ],
  182. 'alert' => [
  183. 'type' => $this->alertType,
  184. 'type_label' => $this->getAlertTypeLabel(),
  185. 'level' => $this->alertLevel,
  186. 'message' => $this->alertMessage,
  187. 'time' => $this->alertTime->toISOString(),
  188. ],
  189. 'monitor' => [
  190. 'id' => $this->monitor->id,
  191. 'check_type' => $this->monitor->check_type,
  192. 'status' => $this->monitor->status,
  193. 'response_time' => $this->monitor->response_time,
  194. 'checked_at' => $this->monitor->checked_at?->toISOString(),
  195. ],
  196. 'data' => $this->monitorData,
  197. 'metadata' => $this->metadata,
  198. ];
  199. return $info;
  200. }
  201. /**
  202. * 获取建议操作
  203. *
  204. * @return array
  205. */
  206. public function getSuggestedActions(): array
  207. {
  208. $actions = [];
  209. switch ($this->alertType) {
  210. case self::ALERT_TYPE_HEALTH:
  211. $actions[] = '检查服务健康状态';
  212. $actions[] = '验证服务配置';
  213. $actions[] = '检查网络连接';
  214. if ($this->isCritical()) {
  215. $actions[] = '启用备用服务';
  216. $actions[] = '通知相关人员';
  217. }
  218. break;
  219. case self::ALERT_TYPE_PERFORMANCE:
  220. $actions[] = '分析性能瓶颈';
  221. $actions[] = '检查服务负载';
  222. $actions[] = '优化调用频率';
  223. if ($this->isCritical()) {
  224. $actions[] = '考虑降级服务';
  225. }
  226. break;
  227. case self::ALERT_TYPE_AVAILABILITY:
  228. $actions[] = '检查服务可用性';
  229. $actions[] = '验证服务状态';
  230. $actions[] = '检查依赖服务';
  231. if ($this->isCritical()) {
  232. $actions[] = '切换到备用服务';
  233. }
  234. break;
  235. case self::ALERT_TYPE_ERROR_RATE:
  236. $actions[] = '分析错误日志';
  237. $actions[] = '检查调用参数';
  238. $actions[] = '验证认证信息';
  239. if ($this->isCritical()) {
  240. $actions[] = '暂停相关调用';
  241. }
  242. break;
  243. case self::ALERT_TYPE_RESPONSE_TIME:
  244. $actions[] = '检查网络延迟';
  245. $actions[] = '分析服务响应';
  246. $actions[] = '优化请求参数';
  247. if ($this->isCritical()) {
  248. $actions[] = '增加超时时间';
  249. $actions[] = '考虑异步处理';
  250. }
  251. break;
  252. }
  253. return $actions;
  254. }
  255. /**
  256. * 判断是否需要立即处理
  257. *
  258. * @return bool
  259. */
  260. public function requiresImmediateAction(): bool
  261. {
  262. return $this->isCritical() && in_array($this->alertType, [
  263. self::ALERT_TYPE_HEALTH,
  264. self::ALERT_TYPE_AVAILABILITY,
  265. ]);
  266. }
  267. }