summaryrefslogtreecommitdiff
path: root/src/main/java/com/nis
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/com/nis')
-rw-r--r--src/main/java/com/nis/job/ConfagentJob.java29
-rw-r--r--src/main/java/com/nis/server/SNMPTrapServer.java4
-rw-r--r--src/main/java/com/nis/util/LogPrintUtil.java14
-rw-r--r--src/main/java/com/nis/util/MybatisPlusConfig.java28
-rw-r--r--src/main/java/com/nis/util/RuntimeUtil.java14
-rw-r--r--src/main/java/com/nis/util/YamlUtil.java33
6 files changed, 55 insertions, 67 deletions
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java
index 1823e14..6d1194c 100644
--- a/src/main/java/com/nis/job/ConfagentJob.java
+++ b/src/main/java/com/nis/job/ConfagentJob.java
@@ -1,6 +1,7 @@
package com.nis.job;
import cn.hutool.core.io.FileUtil;
+import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
@@ -141,13 +142,12 @@ public class ConfagentJob extends QuartzJobBean {
RestTemplate restTemplate = new RestTemplate();
// 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();
+ //String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ String url = UrlBuilder.create().setScheme("http").setHost(ipaddr).setPort(port).appendPath(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("%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("%s:%s", ipaddr, promServerPort) + " global prometheus 热加载配置文件成功");
@@ -172,13 +172,12 @@ public class ConfagentJob extends QuartzJobBean {
RestTemplate restTemplate = new RestTemplate();
// responseEntity = ReflectUtil.invoke(restTemplate,"postForEntity",new UrlBuilder().setScheme("http").setHost(ipaddr).setPort(port).addPath(Constant.API_RELOAD).toString(), null, String.class);;
int port= Integer.parseInt(promServerPort);
- String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ //String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ String url = UrlBuilder.create().setScheme("http").setHost(ipaddr).setPort(port).appendPath(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("%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("%s:%s", ipaddr, promServerPort) + " per datacenter prometheus 热加载配置文件成功");
}
@@ -240,13 +239,12 @@ public class ConfagentJob extends QuartzJobBean {
try {
RestTemplate restTemplate = new RestTemplate();
int port= Integer.parseInt(promServerPort);
- String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ //String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ String url = UrlBuilder.create().setScheme("http").setHost(ipaddr).setPort(port).appendPath(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("%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("%s:%s", ipaddr, promServerPort) + " thanos rule 热加载配置文件成功");
}
@@ -413,13 +411,12 @@ public class ConfagentJob extends QuartzJobBean {
RestTemplate restTemplate = new RestTemplate();
// 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();
+ //String url = new URL("http", ipaddr, port, Constant.API_RELOAD).toString();
+ String url = UrlBuilder.create().setScheme("http").setHost(ipaddr).setPort(port).appendPath(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("%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("%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 2dbd02e..03666e6 100644
--- a/src/main/java/com/nis/server/SNMPTrapServer.java
+++ b/src/main/java/com/nis/server/SNMPTrapServer.java
@@ -1,5 +1,6 @@
package com.nis.server;
+import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.net.NetUtil;
import cn.hutool.core.net.url.UrlBuilder;
import cn.hutool.core.util.StrUtil;
@@ -311,8 +312,7 @@ public class SNMPTrapServer implements CommandResponder,ApplicationRunner{// imp
}
catch (Exception exception)
{
- exception.printStackTrace();
- logger.error("Error parsing UDP trap: {}",exception.getMessage());
+ logger.error("Error parsing UDP trap: {}",ExceptionUtil.getMessage(exception));
}finally{
}
diff --git a/src/main/java/com/nis/util/LogPrintUtil.java b/src/main/java/com/nis/util/LogPrintUtil.java
deleted file mode 100644
index 1289766..0000000
--- a/src/main/java/com/nis/util/LogPrintUtil.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package com.nis.util;
-
-
-import java.io.PrintWriter;
-import java.io.StringWriter;
-
-
-public class LogPrintUtil {
- public static String print(Throwable t){
- StringWriter sw = new StringWriter();
- t.printStackTrace(new PrintWriter(sw));
- return sw.toString();
- }
-}
diff --git a/src/main/java/com/nis/util/MybatisPlusConfig.java b/src/main/java/com/nis/util/MybatisPlusConfig.java
index 5853006..c3b1026 100644
--- a/src/main/java/com/nis/util/MybatisPlusConfig.java
+++ b/src/main/java/com/nis/util/MybatisPlusConfig.java
@@ -1,8 +1,10 @@
package com.nis.util;
+import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.apache.ibatis.mapping.DatabaseIdProvider;
@@ -56,19 +58,23 @@ public class MybatisPlusConfig {
try {
databaseId = databaseIdProvider.getDatabaseId(dataSource);
} catch (SQLException e) {
- logger.debug(LogPrintUtil.print(e));
+ logger.debug(ExceptionUtil.getMessage(e));
}
- switch (databaseId){
- case Constant.MYSQL:
- return String.format("select seq_nextval('%s')", incrementerName);
- case Constant.POSTGRESQL:
- return String.format("select seq_nextval('%s')", incrementerName);
- case Constant.ORACLE:
- return String.format("select %s.nextval from dual", incrementerName);
- default:
- return String.format("select seq_nextval('%s')", incrementerName);
- }
+ if(ObjectUtils.isNotEmpty(databaseId)) {
+ switch (databaseId){
+ case Constant.MYSQL:
+ return String.format("select seq_nextval('%s')", incrementerName);
+ case Constant.POSTGRESQL:
+ return String.format("select seq_nextval('%s')", incrementerName);
+ case Constant.ORACLE:
+ return String.format("select %s.nextval from dual", incrementerName);
+ default:
+ return String.format("select seq_nextval('%s')", incrementerName);
+ }
+ }else {
+ return String.format("select seq_nextval('%s')", incrementerName);
+ }
}
}
}
diff --git a/src/main/java/com/nis/util/RuntimeUtil.java b/src/main/java/com/nis/util/RuntimeUtil.java
index d89a642..65b581a 100644
--- a/src/main/java/com/nis/util/RuntimeUtil.java
+++ b/src/main/java/com/nis/util/RuntimeUtil.java
@@ -1,9 +1,12 @@
package com.nis.util;
+import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
+import cn.hutool.log.Log;
+
import org.apache.commons.io.IOUtils;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+/*import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;*/
import java.io.*;
import java.util.UUID;
@@ -16,7 +19,8 @@ import java.util.UUID;
*/
public class RuntimeUtil {
// Log
- private static Log log = LogFactory.getLog(RuntimeUtil.class);
+// private static Log log = LogFactory.getLog(RuntimeUtil.class);
+ private static Log log = Log.get();
// 当前Runtime
private static Runtime runtime = Runtime.getRuntime();
@@ -142,12 +146,10 @@ public class RuntimeUtil {
if (currentProcess != null) {
currentProcess.destroy();
- currentProcess = null;
}
if (nextProcess != null) {
nextProcess.destroy();
- nextProcess = null;
}
}
@@ -252,7 +254,7 @@ public class RuntimeUtil {
private static void write(OutputStream out, byte[] buffer, int length) {
try {
out.write(buffer, 0, length);
- } catch (IOException e) {LogPrintUtil.print(e);
+ } catch (IOException e) {ExceptionUtil.getMessage(e);
}
}
diff --git a/src/main/java/com/nis/util/YamlUtil.java b/src/main/java/com/nis/util/YamlUtil.java
index 93ab6cd..3ca6615 100644
--- a/src/main/java/com/nis/util/YamlUtil.java
+++ b/src/main/java/com/nis/util/YamlUtil.java
@@ -35,15 +35,13 @@ public class YamlUtil {
OutputStreamWriter osw = null;
try {
File file = FileUtil.file(ymlPath);
- out=new FileOutputStream(file);
+ out=FileUtil.getOutputStream(file);
osw=new OutputStreamWriter(out, "UTF-8");
for(String s : param) {
Object load = JSON.parse(s);
y.dump(load,osw);
}
flag = true;
- } catch (FileNotFoundException e) {
- logger.error("snmpYmlHandle error : "+e.getMessage(),e);
} catch (UnsupportedEncodingException e) {
logger.error("snmpYmlHandle error : "+e.getMessage(),e);
} finally {
@@ -90,13 +88,11 @@ public class YamlUtil {
ruleYmlfile.delete();
}
- out=new FileOutputStream(file);
+ out=FileUtil.getOutputStream(file);
osw=new OutputStreamWriter(out, "UTF-8");
y.dump(param,osw);
flag = true;
- } catch (FileNotFoundException e) {
- logger.error("subYmlHandle error : "+e.getMessage(),e);
} catch (UnsupportedEncodingException e) {
logger.error("subYmlHandle error : "+e.getMessage(),e);
} finally {
@@ -128,10 +124,12 @@ public class YamlUtil {
Yaml y = new Yaml(options);
boolean flag=false;
if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
- FileInputStream fileInputStream =null;
+// FileInputStream fileInputStream =null;
+ BufferedInputStream inputStream = null;
OutputStream out = null;
OutputStreamWriter osw = null;
- FileInputStream fileInputStream2 =null;
+// FileInputStream fileInputStream2 =null;
+ BufferedInputStream inputStream2 = null;
OutputStream out2 = null;
OutputStreamWriter osw2 = null;
try {
@@ -141,13 +139,13 @@ public class YamlUtil {
// 文件不存在 创建文件
ruleYmlfile.createNewFile();
}
- fileInputStream2 = new FileInputStream(ruleYmlfile);
- Map<String,Object> map2 = y.loadAs(fileInputStream2, Map.class);
+ inputStream2 = FileUtil.getInputStream(ruleYmlfile);
+ Map<String,Object> map2 = y.loadAs(inputStream2, Map.class);
if(map2==null) {
map2=new HashMap();
}
map2.put("groups", param.get("groups"));
- out2 = new FileOutputStream(ruleYmlfile);
+ out2 = FileUtil.getOutputStream(ruleYmlfile);
osw2 = new OutputStreamWriter(out2, "UTF-8");
y.dump(map2,osw2);
//去除告警规则相关内容
@@ -163,7 +161,7 @@ public class YamlUtil {
map.put("alerting", param.get("alerting"));
map.put("rule_files", param.get("rule_files"));**/
- out = new FileOutputStream(ymlfile);
+ out = FileUtil.getOutputStream(ymlfile);
osw = new OutputStreamWriter(out, "UTF-8");
y.dump(param,osw);
@@ -176,8 +174,8 @@ public class YamlUtil {
} catch (IOException e) {
logger.error("centerYmlHandle error : "+e.getMessage(),e);
} finally {
- if(fileInputStream!=null) {
- IOUtils.closeQuietly(fileInputStream);
+ if(inputStream!=null) {
+ IOUtils.closeQuietly(inputStream);
}
if(out!=null) {
IOUtils.closeQuietly(out);
@@ -185,8 +183,8 @@ public class YamlUtil {
if(osw!=null) {
IOUtils.closeQuietly(osw);
}
- if(fileInputStream2!=null) {
- IOUtils.closeQuietly(fileInputStream2);
+ if(inputStream2!=null) {
+ IOUtils.closeQuietly(inputStream2);
}
if(out2!=null) {
IOUtils.closeQuietly(out2);
@@ -228,7 +226,7 @@ public class YamlUtil {
map2 = new HashMap();
}
map2.put("groups", param.get("groups"));
- OutputStream out2 = new FileOutputStream(ruleYmlfile);
+ OutputStream out2 = FileUtil.getOutputStream(ruleYmlfile);
OutputStreamWriter osw2 = new OutputStreamWriter(out2, "UTF-8");
y.dump(map2, osw2);
flag = true;
@@ -259,7 +257,6 @@ public class YamlUtil {
public static boolean prometheusStartHandle(String filePath,String param) {
DumperOptions options = new DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
- Yaml y = new Yaml(options);
boolean flag=false;
RandomAccessFile raf = null;
try {