summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortanghao <default@DESKTOP-7FEGRP2>2020-04-10 18:11:42 +0800
committertanghao <default@DESKTOP-7FEGRP2>2020-04-10 18:11:42 +0800
commit20fdf88407ddc02ae5bb8fbdc3fa6cdce1eabb05 (patch)
tree8876c076c89b8c04c8bca62adc3919b978011cf5
parent128ee8eed673a8321d99e94bb4c0fc5acc9f4572 (diff)
feat: prometheus抓取数据以及保存数据时间根据数据库设置动态设定
-rw-r--r--src/main/java/com/nis/dao/SysConfigDao.java1
-rw-r--r--src/main/java/com/nis/job/ConfagentJob.java45
-rw-r--r--src/main/java/com/nis/service/SysConfigService.java2
-rw-r--r--src/main/java/com/nis/service/impl/SysConfigServiceImpl.java5
-rw-r--r--src/main/java/com/nis/util/Constant.java10
-rw-r--r--src/main/java/com/nis/util/YamlUtil.java132
-rw-r--r--src/main/resources/application.yml3
-rw-r--r--src/main/resources/mapper/SysConfigDao.xml4
8 files changed, 193 insertions, 9 deletions
diff --git a/src/main/java/com/nis/dao/SysConfigDao.java b/src/main/java/com/nis/dao/SysConfigDao.java
index e4ddb04..2a6dba7 100644
--- a/src/main/java/com/nis/dao/SysConfigDao.java
+++ b/src/main/java/com/nis/dao/SysConfigDao.java
@@ -11,4 +11,5 @@ public interface SysConfigDao {
List<SysConfig> selectList();
+ String selectValueByParamkey(String paramKey);
}
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java
index 32ee0c5..9436d36 100644
--- a/src/main/java/com/nis/job/ConfagentJob.java
+++ b/src/main/java/com/nis/job/ConfagentJob.java
@@ -70,6 +70,8 @@ public class ConfagentJob extends QuartzJobBean {
private String metricsPath;
@Value("${confagent.snmpYmlPath}")
private String snmpYmlPath;
+ @Value("${confagent.prometheusServicePath}")
+ private String prometheusServicePath;
@Autowired
private RedisTemplate<Object,Object> redisTemplate;
@@ -86,7 +88,9 @@ public class ConfagentJob extends QuartzJobBean {
logger.info("The local ymlPath info {}",JSON.toJSON(ymlPath));
logger.info("The local ruleYmlPath info {}",JSON.toJSON(ruleYmlPath));
-
+
+
+
if(promserver!=null) {
// 判断本次表格数据与之前数据是否有变化判断是否执行操作
boolean changeFlag = confEventService.compareData(confEvents);
@@ -99,12 +103,12 @@ public class ConfagentJob extends QuartzJobBean {
Map centerResult = centerHandle();
logger.debug("center result info {} ",JSON.toJSON(centerResult));
if(centerResult!=null) {
- //获取yml文件信息替换对应内容
try {
+ //获取yml文件信息替换对应内容 热加载
boolean handleResult = YamlUtil.centerYmlHandle(centerResult,ymlPath,ruleYmlPath);
logger.info("yamlHanlde center result {}",JSON.toJSON(handleResult));
} catch (Exception e) {
- logger.error("yamlHanlde center error");
+ logger.error("yamlHanlde center error",e);
e.printStackTrace();
}
}
@@ -112,16 +116,47 @@ public class ConfagentJob extends QuartzJobBean {
List result = subHandle(promserver);
logger.debug("subHandle list info {} ",JSON.toJSON(result));
if(result!=null) {
- //获取yml文件信息替换对应内容
try {
+ //获取yml文件信息替换对应内容 热加载
boolean handleResult = YamlUtil.subYmlHandle(result,ymlPath);
logger.info("yamlHanlde sub result {}",JSON.toJSON(handleResult));
} catch (Exception e) {
- logger.error("yamlHanlde sub error");
+ logger.error("yamlHanlde sub error",e);
e.printStackTrace();
}
}
}
+
+ //prometheus全局配置修改 热加载
+ try {
+ String scrapeInterval = sysConfigService.queryValueByParamkey(Constant.SCRAPE_INTERVAL);
+ Map param =new HashMap();
+ param.put(Constant.SCRAPE_INTERVAL,scrapeInterval);
+ boolean prometheusSettingHandle = YamlUtil.prometheusSettingHandle(param,ymlPath);
+ logger.info("prometheusSettingHandle result {}",JSON.toJSON(prometheusSettingHandle));
+ }catch(Exception e) {
+ logger.error("prometheus global setting error : ",e);
+ e.printStackTrace();
+ }
+
+ //prometheus启动项相关修改 每次需要判断是否更改来确定是否执行方法 会关闭prometheus并重启
+ try {
+ Integer integer = null;
+ if(confEvents!=null) {
+ integer = confEvents.get(Constant.STORAGE_LOCAL_RETENTION);
+ }
+ String param = sysConfigService.queryValueByParamkey(Constant.STORAGE_LOCAL_RETENTION);
+ if(integer!=null && !integer.equals(Integer.valueOf(param))) {
+ boolean prometheusStartHandle = YamlUtil.prometheusStartHandle(prometheusServicePath, param);
+ logger.info("prometheusStartHandle result {}",JSON.toJSON(prometheusStartHandle));
+ }else {
+ logger.info("prometheusStartHandle not start because of no change");
+ }
+ }catch(Exception e){
+ logger.error("prometheus startup setting error : ",e);
+ e.printStackTrace();
+ }
+
//处理完信息配置后 将confEvent数据保存 留待下个周期比较判断
confEvents=confEventService.queryConfEventMap();
}else {
diff --git a/src/main/java/com/nis/service/SysConfigService.java b/src/main/java/com/nis/service/SysConfigService.java
index 1a142fe..65ee610 100644
--- a/src/main/java/com/nis/service/SysConfigService.java
+++ b/src/main/java/com/nis/service/SysConfigService.java
@@ -6,4 +6,6 @@ import com.nis.entity.SysConfig;
public interface SysConfigService {
public List<SysConfig> queryList();
+
+ public String queryValueByParamkey(String paramKey);
}
diff --git a/src/main/java/com/nis/service/impl/SysConfigServiceImpl.java b/src/main/java/com/nis/service/impl/SysConfigServiceImpl.java
index 44ea0de..404a558 100644
--- a/src/main/java/com/nis/service/impl/SysConfigServiceImpl.java
+++ b/src/main/java/com/nis/service/impl/SysConfigServiceImpl.java
@@ -19,4 +19,9 @@ public class SysConfigServiceImpl implements SysConfigService {
public List<SysConfig> queryList() {
return sysConfigDao.selectList();
}
+
+ @Override
+ public String queryValueByParamkey(String paramKey) {
+ return sysConfigDao.selectValueByParamkey(paramKey);
+ }
}
diff --git a/src/main/java/com/nis/util/Constant.java b/src/main/java/com/nis/util/Constant.java
index 73b527c..d0050a6 100644
--- a/src/main/java/com/nis/util/Constant.java
+++ b/src/main/java/com/nis/util/Constant.java
@@ -53,6 +53,16 @@ public class Constant {
public static final String ASC = "asc";
/**
+ * prometheus全局抓取数据时间
+ */
+ public static final String SCRAPE_INTERVAL="scrape_interval";
+
+ /**
+ * prometheus文件保存时间天数
+ */
+ public static final String STORAGE_LOCAL_RETENTION="storage_local_retention";
+
+ /**
* 当前数据库类型
*/
public static final String DB_TYPE;
diff --git a/src/main/java/com/nis/util/YamlUtil.java b/src/main/java/com/nis/util/YamlUtil.java
index a1a97d1..3e8d572 100644
--- a/src/main/java/com/nis/util/YamlUtil.java
+++ b/src/main/java/com/nis/util/YamlUtil.java
@@ -1,13 +1,16 @@
package com.nis.util;
+import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
+import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
+import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
@@ -16,6 +19,8 @@ import java.util.Map;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
+import com.alibaba.fastjson.JSON;
+
public class YamlUtil {
private static Yaml y;
@@ -26,10 +31,16 @@ public class YamlUtil {
y = new Yaml(options);
}
+ /**
+ * snmp配置相关
+ * @param param
+ * @param ymlPath
+ * @return
+ */
@SuppressWarnings("unchecked")
public static boolean snmpYmlHandle(List<String> param,String ymlPath) {
boolean flag=false;
- if(!param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
+ if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
OutputStream out = null;
OutputStreamWriter osw = null;
try {
@@ -66,10 +77,16 @@ public class YamlUtil {
return flag;
}
+ /**
+ * datacenter相关配置
+ * @param param
+ * @param ymlPath
+ * @return
+ */
@SuppressWarnings("unchecked")
public static boolean subYmlHandle(List<Object> param,String ymlPath) {
boolean flag=false;
- if(!param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
+ if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
FileInputStream fileInputStream =null;
OutputStream out = null;
OutputStreamWriter osw = null;
@@ -119,10 +136,17 @@ public class YamlUtil {
return flag;
}
+ /**
+ * global相关配置设置
+ * @param param
+ * @param ymlPath
+ * @param ruleYmlPath
+ * @return
+ */
@SuppressWarnings("unchecked")
public static boolean centerYmlHandle(Map<String,Object> param,String ymlPath,String ruleYmlPath){
boolean flag=false;
- if(!param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
+ if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) {
FileInputStream fileInputStream =null;
OutputStream out = null;
OutputStreamWriter osw = null;
@@ -191,7 +215,109 @@ public class YamlUtil {
return flag;
}
+ /**
+ * prometheus启动项修改
+ * @param filePath
+ * @param param
+ * @return
+ */
+ public static boolean prometheusStartHandle(String filePath,String param) {
+ boolean flag=false;
+ RandomAccessFile raf = null;
+ try {
+ raf = new RandomAccessFile(new File(filePath), "rw");
+ String readLine = null;
+ long lastPoint = 0; //记住上一次的偏移量
+ while((readLine=raf.readLine())!=null) {
+ final long ponit = raf.getFilePointer();
+ if(readLine.startsWith("ExecStart")) {
+ String replace = readLine.replaceAll("--storage.tsdb.retention.time=\\d+d", "--storage.tsdb.retention.time="+param+"d");
+ raf.seek(lastPoint);
+ raf.writeBytes(replace);
+ }
+ lastPoint = ponit;
+ }
+ raf.close();
+ RuntimeUtil.run(null, null, new String[]{"/bin/sh", "-c","systemctl daemon-reload"});
+ RuntimeUtil.run(null, null, new String[]{"/bin/sh", "-c","systemctl stop prometheus"});
+ RuntimeUtil.run(null, null, new String[]{"/bin/sh", "-c","systemctl start prometheus"});
+ flag=true;
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if(raf!=null) {
+ try {
+ raf.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ return flag;
+ }
+ /**
+ * prometheus全局配置修改
+ * @param param
+ * @param ymlPath
+ * @param ruleYmlPath
+ * @return
+ */
+ @SuppressWarnings("unchecked")
+ public static boolean prometheusSettingHandle(Map<String,Object> param,String ymlPath){
+ boolean flag=false;
+ if(param!=null && !param.isEmpty() &&ymlPath != null && ymlPath.length() != 0) { //
+ FileInputStream fileInputStream =null;
+ OutputStream out = null;
+ OutputStreamWriter osw = null;
+ try {
+ // 修改prometheus.yml配置文件
+ File ymlfile = new File(ymlPath);
+ fileInputStream = new FileInputStream(ymlfile);
+ Map<String,Object> map = y.loadAs(fileInputStream, Map.class);
+ if(map==null) {
+ return false;
+ }
+ Map object = (Map) map.get("global");
+ object.put(Constant.SCRAPE_INTERVAL, param.get(Constant.SCRAPE_INTERVAL)+"s");
+ out = new FileOutputStream(ymlfile);
+ osw = new OutputStreamWriter(out, "UTF-8");
+ y.dump(map,osw);
+
+ RuntimeUtil.run(null, null, new String[]{"/bin/sh", "-c","ps -ef |grep prometheus|grep -v grep|awk \'{print $2}\'|xargs kill -hup"});
+ flag = true;
+ osw.close();
+ } catch (Exception e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } finally {
+ if(fileInputStream!=null) {
+ try {
+ fileInputStream.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if(out!=null) {
+ try {
+ out.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ if(osw!=null) {
+ try {
+ osw.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+ }
+ return flag;
+ }
/*public static void main(String[] args) throws Exception {
// File file = new File("F:/workspace/nz-web/nz-admin/tmp/promRuleCheck1574925561786.yml");
File file = new File("F:/prometheus.yml");
diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml
index af16249..8e55c52 100644
--- a/src/main/resources/application.yml
+++ b/src/main/resources/application.yml
@@ -37,10 +37,11 @@ spring:
confagent:
- ipaddr: 192.168.40.126
+ ipaddr: 192.168.40.247
ymlPath: F:/prometheus.yml
ruleYmlPath: F:/rule.yml
snmpYmlPath: F:/snmp.yml
+ prometheusServicePath: F:/prometheus.service
prometheusPort: 9090
## job config
jobCron: 0/15 * * * * ?
diff --git a/src/main/resources/mapper/SysConfigDao.xml b/src/main/resources/mapper/SysConfigDao.xml
index 4b92e6b..5ef43b2 100644
--- a/src/main/resources/mapper/SysConfigDao.xml
+++ b/src/main/resources/mapper/SysConfigDao.xml
@@ -13,4 +13,8 @@
select * from sys_config where status=1
</select>
+ <select id="selectValueByParamkey" resultType="java.lang.String">
+ select param_value from sys_config where status=1 and param_key= #{paramKey}
+ </select>
+
</mapper> \ No newline at end of file