| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace common\helpers;
- class Alarm
- {
- // 预警平台Domain
- private static $url = 'http://ibs-cn.elken.com:8017';
- // 推送预警信息API
- private static $reportApi = '/api/report';
- public static function reportAlarm($message)
- {
- // 业务环境白名单
- // $allowPlatform = \Yii::$app->params['allowPlatform'];
- // 是否开启预警信息推送
- $reportAlarmOpen = Cache::getSystemConfig()['reportAlarmOpen']['VALUE'];
- // 业务平台token
- $message['platform-id'] = \Yii::$app->params['alarmAccessToken'];
- // 堆栈ID
- $message['trace-id'] = $message['trace-id'] ?? Tool::generateId();
- // 级别
- $message['stance'] = $message['stance'] ?? 2;
- // 类型
- $message['brand'] = $message['brand'] ?? 'OTA';
- // 日志入库
- // Tool::alarmCall($message);
- // 日志写文件
- LoggerTool::error($message);
- // 业务环境过滤
- if ($reportAlarmOpen /**&& (count($allowPlatform) == 0 || in_array($_SERVER['HTTP_HOST'], $allowPlatform))*/) {
- $result = self::curl(json_encode($message));
- if ($result['code'] != 200) {
- // 重新推送一次,如果失败,写错误日志
- $result = self::curl(json_encode($message));
- if ($result['code'] != 200) {
- LoggerTool::error(['预警信息上报平台失败. traceId【' . $message['trace-id'] . '】', $result]);
- return false;
- }
- }
- LoggerTool::notice(['信息推送预警平台成功.', 'platformID: .' . $message['platform-id'], 'traceID: . ' . $message['trace-id']]);
- } else {
- $reason = '';
- if ($reportAlarmOpen == '0') {
- $reason = '上报预警平台状态不允许(reportAlarmOpen=关闭)';
- }
- // if (!count($allowPlatform)) {
- // $reason = '主机地址不在白名单中(host=' . $_SERVER['HTTP_HOST'] . ')';
- // }
- LoggerTool::error('预警信息上报平台取消. traceId【' . $message['trace-id'] . '】. 原因:' . $reason);
- }
- }
- private static function curl($post_string)
- {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, self::$url . self::$reportApi);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
- curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $response = curl_exec($ch);
- curl_close($ch);
- if (curl_errno($ch)) {
- // 请求失败,返回错误信息
- return ['code' => 500, 'message' => curl_error($ch)];
- }
- return ['code' => 200, 'message' => json_decode($response, true)];
- }
- }
|