diff options
| author | tanghao <admin@LAPTOP-QCSKVLI9> | 2021-02-05 09:30:37 +0800 |
|---|---|---|
| committer | tanghao <admin@LAPTOP-QCSKVLI9> | 2021-02-05 09:30:37 +0800 |
| commit | 6cdb57536914e136fab093e1daf40c98a95a3b85 (patch) | |
| tree | 8b7ef742610aeaaff332c075278a899101db739c | |
| parent | 1e360e1ad3056b1f8b6e10222f156ff0830332a0 (diff) | |
fix: 修复http usage检测问题
| -rw-r--r-- | src/main/java/com/nis/job/ConfagentJob.java | 26 | ||||
| -rw-r--r-- | src/main/java/com/nis/server/SNMPTrapServer.java | 18 |
2 files changed, 27 insertions, 17 deletions
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java index c9fcc8d..1823e14 100644 --- a/src/main/java/com/nis/job/ConfagentJob.java +++ b/src/main/java/com/nis/job/ConfagentJob.java @@ -139,18 +139,18 @@ public class ConfagentJob extends QuartzJobBean { ResponseEntity<String> responseEntity = null; try { RestTemplate restTemplate = new RestTemplate(); -// responseEntity = restTemplate.postForEntity(String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD), null, String.class); +// responseEntity = restTemplate.postForEntity(String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD), null, String.class); int port= Integer.parseInt(promServerPort); String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString(); responseEntity = ReflectUtil.invoke(restTemplate, "postForEntity", url, null, String.class, Collections.emptyMap()); } catch (RestClientException e) { - logger.error("热加载 global prometheus api 接口失败,API 地址是:" + String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); + logger.error("热加载 global prometheus api 接口失败,API 地址是:" + String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); } catch (MalformedURLException e) { logger.error(e); } if (responseEntity != null && responseEntity.getStatusCodeValue() == 200) { - logger.info(String.format("http://%s:%s", ipaddr, promServerPort) + " global prometheus 热加载配置文件成功"); + logger.info(String.format("%s:%s", ipaddr, promServerPort) + " global prometheus 热加载配置文件成功"); } } } catch (Exception e) { @@ -175,12 +175,12 @@ public class ConfagentJob extends QuartzJobBean { String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString(); responseEntity = ReflectUtil.invoke(restTemplate, "postForEntity", url, null, String.class, Collections.emptyMap()); } catch (RestClientException e) { - logger.error("热加载 per-datacenter prometheus api 接口失败,API 地址是:" + String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); + logger.error("热加载 per-datacenter prometheus api 接口失败,API 地址是:" + String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); } catch (MalformedURLException e) { logger.error(e); } if (responseEntity != null && responseEntity.getStatusCodeValue() == 200) { - logger.info(String.format("http://%s:%s", ipaddr, promServerPort) + " per datacenter prometheus 热加载配置文件成功"); + logger.info(String.format("%s:%s", ipaddr, promServerPort) + " per datacenter prometheus 热加载配置文件成功"); } } } catch (Exception e) { @@ -202,12 +202,12 @@ public class ConfagentJob extends QuartzJobBean { ResponseEntity<String> responseEntity = null; try { RestTemplate restTemplate = new RestTemplate(); - responseEntity = restTemplate.postForEntity(String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD), null, String.class); + responseEntity = restTemplate.postForEntity(String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD), null, String.class); } catch (RestClientException e) { - logger.error("热加载 prometheusSettingHandle 接口失败,API 地址是:" + String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); + logger.error("热加载 prometheusSettingHandle 接口失败,API 地址是:" + String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); } if (responseEntity != null && responseEntity.getStatusCodeValue() == 200) { - logger.info(String.format("http://%s:%s", ipaddr, promServerPort) + " prometheusSettingHandle 热加载配置文件成功"); + logger.info(String.format("%s:%s", ipaddr, promServerPort) + " prometheusSettingHandle 热加载配置文件成功"); } } } catch (Exception e) { @@ -243,12 +243,12 @@ public class ConfagentJob extends QuartzJobBean { String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString(); responseEntity = ReflectUtil.invoke(restTemplate, "postForEntity", url, null, String.class, Collections.emptyMap()); } catch (RestClientException e) { - logger.error("热加载 thanos rule api 接口失败,API 地址是:" + String.format("http://%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); + logger.error("热加载 thanos rule api 接口失败,API 地址是:" + String.format("%s:%s%s", ipaddr, promServerPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); } catch (MalformedURLException e) { logger.error(e); } if (responseEntity != null && responseEntity.getStatusCodeValue() == 200) { - logger.info(String.format("http://%s:%s", ipaddr, promServerPort) + " thanos rule 热加载配置文件成功"); + logger.info(String.format("%s:%s", ipaddr, promServerPort) + " thanos rule 热加载配置文件成功"); } } } @@ -411,17 +411,17 @@ public class ConfagentJob extends QuartzJobBean { ResponseEntity<String> responseEntity = null; try { RestTemplate restTemplate = new RestTemplate(); -// responseEntity = restTemplate.postForEntity(String.format("http://%s:%s%s", ipaddr, snmpPort, Constant.API_RELOAD), null, String.class); +// responseEntity = restTemplate.postForEntity(String.format("%s:%s%s", ipaddr, snmpPort, Constant.API_RELOAD), null, String.class); int port = Integer.parseInt(snmpPort); String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString(); responseEntity = ReflectUtil.invoke(restTemplate, "postForEntity", url, null, String.class, Collections.emptyMap()); } catch (RestClientException e) { - logger.error("热加载 snmp_exporter api 接口失败,API 地址是:" + String.format("http://%s:%s%s", ipaddr, snmpPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); + logger.error("热加载 snmp_exporter api 接口失败,API 地址是:" + String.format("%s:%s%s", ipaddr, snmpPort, Constant.API_RELOAD) + ",错误信息是:" + e.getMessage(), e); } catch (MalformedURLException e) { logger.error(e); } if (responseEntity != null && responseEntity.getStatusCodeValue() == 200) { - logger.info(String.format("http://%s:%s", ipaddr, snmpPort) + " snmp_exporter 热加载配置文件成功"); + logger.info(String.format("%s:%s", ipaddr, snmpPort) + " snmp_exporter 热加载配置文件成功"); } } } diff --git a/src/main/java/com/nis/server/SNMPTrapServer.java b/src/main/java/com/nis/server/SNMPTrapServer.java index c47dcf4..2dbd02e 100644 --- a/src/main/java/com/nis/server/SNMPTrapServer.java +++ b/src/main/java/com/nis/server/SNMPTrapServer.java @@ -1,5 +1,7 @@ package com.nis.server;
+import cn.hutool.core.net.NetUtil;
+import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import com.alibaba.fastjson.JSON;
@@ -9,6 +11,8 @@ import com.nis.entity.*; import com.nis.service.AlertRuleService;
import com.nis.service.SysConfigService;
import com.nis.util.DateUtil;
+
+import org.apache.commons.lang3.StringUtils;
import org.snmp4j.*;
import org.snmp4j.mp.MPv1;
import org.snmp4j.mp.MPv2c;
@@ -294,10 +298,16 @@ public class SNMPTrapServer implements CommandResponder,ApplicationRunner{// imp HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity entity =new HttpEntity(mess,headers);
- String url="http://"+alertApi+alertPathPrefix+"/api/v1/alerts";
- Object postForEntity = restTemplate.postForEntity(url, entity,Object.class);
- logger.info("post success info {}",JSON.toJSON(postForEntity));
- confagentMetrics.snmpCounter.increment();
+// String url="http://"+alertApi+alertPathPrefix+"/api/v1/alerts";
+ if(StringUtils.isNotBlank(alertApi)) {
+ String[] alertApis = alertApi.split(":");
+ alertApi=alertApis[0];
+ Integer alertPort=Integer.valueOf(alertApis[1]);
+ String url = UrlBuilder.create().setScheme("http").setHost(alertApi).setPort(alertPort).appendPath(alertPathPrefix+"/api/v1/alerts").toString();
+ Object postForEntity = restTemplate.postForEntity(url, entity,Object.class);
+ logger.info("post success info {}",JSON.toJSON(postForEntity));
+ confagentMetrics.snmpCounter.increment();
+ }
}
catch (Exception exception)
{
|
