Alarm.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace common\helpers;
  3. class Alarm
  4. {
  5. // 预警平台Domain
  6. private static string $url = 'http://ibs-cn.elken.com:8017';
  7. // 推送预警信息API
  8. private static string $reportApi = '/api/report';
  9. // 业务环境白名单
  10. private static array $allowPlatform = ['fapi.ekhkad.com', 'bapi.ekhkad.com'];
  11. public static function reportAlarm($message)
  12. {
  13. // 是否开启预警信息推送
  14. $reportAlarmOpen = Cache::getSystemConfig()['reportAlarmOpen']['VALUE'];
  15. // 业务平台token
  16. $message['platform-id'] = \Yii::$app->params['alarmAccessToken'];
  17. // 日志入库
  18. Tool::alarmCall($message);
  19. // 业务环境过滤
  20. if ($reportAlarmOpen && (count(self::$allowPlatform) == 0 || in_array($_SERVER['HTTP_HOST'], self::$allowPlatform))) {
  21. $result = self::curl(json_encode($message));
  22. if ($result['code'] != 200) {
  23. // 重新推送一次,如果失败,写错误日志
  24. $result = self::curl(json_encode($message));
  25. if ($result['code'] != 200) {
  26. LoggerTool::error('预警信息上报平台失败. traceId【' . $message['trace-id'] . '】');
  27. }
  28. }
  29. } else {
  30. if ($reportAlarmOpen == '0') {
  31. $reason = '上报预警平台状态不允许(reportAlarmOpen=关闭)';
  32. } else {
  33. $reason = '主机地址不在白名单中(host=' . $_SERVER['HTTP_HOST'] . ')';
  34. }
  35. LoggerTool::error('预警信息上报平台取消. traceId【' . $message['trace-id'] . '】. 原因:' . $reason);
  36. }
  37. }
  38. private static function curl($post_string)
  39. {
  40. $ch = curl_init();
  41. curl_setopt($ch, CURLOPT_URL, self::$url . self::$reportApi);
  42. curl_setopt($ch, CURLOPT_POST, 1);
  43. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  44. curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
  45. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  47. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  48. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  49. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  50. $response = curl_exec($ch);
  51. curl_close($ch);
  52. return json_decode($response, true);
  53. }
  54. }