Alarm.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. // 堆栈ID
  21. $message['trace-id'] = $message['trace-id'] ?? Tool::generateId();
  22. // 级别
  23. $message['stance'] = $message['stance'] ?? 2;
  24. // 类型
  25. $message['brand'] = $message['brand'] ?? 'OTA';
  26. // 日志入库
  27. // Tool::alarmCall($message);
  28. // 日志写文件
  29. LoggerTool::error($message);
  30. // 业务环境过滤
  31. if ($reportAlarmOpen /**&& (count($allowPlatform) == 0 || in_array($_SERVER['HTTP_HOST'], $allowPlatform))*/) {
  32. self::curl(json_encode($message));
  33. LoggerTool::notice(['信息推送预警平台成功.', 'platformID: .' . $message['platform-id'], 'traceID: . ' . $message['trace-id']]);
  34. } else {
  35. $reason = '';
  36. if ($reportAlarmOpen == '0') {
  37. $reason = '上报预警平台状态不允许(reportAlarmOpen=关闭)';
  38. }
  39. // if (!count($allowPlatform)) {
  40. // $reason = '主机地址不在白名单中(host=' . $_SERVER['HTTP_HOST'] . ')';
  41. // }
  42. LoggerTool::error('预警信息上报平台取消. traceId【' . $message['trace-id'] . '】. 原因:' . $reason);
  43. }
  44. }
  45. private static function curl($post_string)
  46. {
  47. $ch = curl_init();
  48. curl_setopt($ch, CURLOPT_URL, self::$url . self::$reportApi);
  49. curl_setopt($ch, CURLOPT_POST, 1);
  50. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  51. curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
  52. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
  53. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  54. // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
  55. curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
  56. curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
  57. $response = curl_exec($ch);
  58. curl_close($ch);
  59. return ['code' => 200, 'message' => json_decode($response, true)];
  60. }
  61. }