Alarm.php 3.1 KB

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