diff options
| author | shizhendong <[email protected]> | 2020-10-30 09:55:21 +0800 |
|---|---|---|
| committer | shizhendong <[email protected]> | 2020-10-30 09:55:21 +0800 |
| commit | 5df48e1c6829b97767af88d2dbdf8e345c797eb7 (patch) | |
| tree | 738deb30cf239644d029341199f79119ec4668e0 | |
| parent | 3f90c278645922eb528baedbbdc469f2903223b7 (diff) | |
fix: 修改 prometheus 本地保存时间方式
| -rw-r--r-- | src/main/java/com/nis/job/ConfagentJob.java | 72 |
1 files changed, 65 insertions, 7 deletions
diff --git a/src/main/java/com/nis/job/ConfagentJob.java b/src/main/java/com/nis/job/ConfagentJob.java index cf0a13d..8082c58 100644 --- a/src/main/java/com/nis/job/ConfagentJob.java +++ b/src/main/java/com/nis/job/ConfagentJob.java @@ -21,7 +21,7 @@ import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.web.client.RestClientException; import org.springframework.web.client.RestTemplate; -import java.io.StringReader; +import java.io.*; import java.util.*; import java.util.stream.Collectors; @@ -205,15 +205,22 @@ public class ConfagentJob extends QuartzJobBean { integer = confEvents.get(Constant.STORAGE_LOCAL_RETENTION); logger.info("prometheusStartHandle integer {}", integer); - ConfEvent confEvent = confEventService.queryConfEvent("storage_local_retention"); + ConfEvent confEvent = confEventService.queryConfEvent(Constant.STORAGE_LOCAL_RETENTION); if (integer != null && !integer.equals(confEvent.getValue())) { - String param = sysConfigService.queryValueByParamkey(Constant.STORAGE_LOCAL_RETENTION); - logger.info("prometheusStartHandle param {}", param); - boolean prometheusStartHandle = YamlUtil.prometheusStartHandle(prometheusServicePath, param); - logger.info("prometheusStartHandle result {}", JSON.toJSON(prometheusStartHandle)); + String retentionDays = sysConfigService.queryValueByParamkey(Constant.STORAGE_LOCAL_RETENTION); + logger.info("prometheus 数据本地保存天数是 {}", retentionDays); + // boolean prometheusStartHandle = YamlUtil.prometheusStartHandle(prometheusServicePath, param); + boolean prometheusStartHandle = this.handleStorageLocalRetention(prometheusServicePath, retentionDays); + logger.info("prometheus 配置本地保存天数" + (prometheusStartHandle ? "成功" : "失败")); } else { - logger.info("prometheusStartHandle not start because of no change"); + logger.info("prometheeus 本地保存天数没有改变,不进行更新"); } + } else { + // 第一次启动的时候读取数据库配置进行更新 + String retentionDays = sysConfigService.queryValueByParamkey(Constant.STORAGE_LOCAL_RETENTION); + logger.info("prometheus 数据本地保存天数是 {}", retentionDays); + boolean prometheusStartHandle = this.handleStorageLocalRetention(prometheusServicePath, retentionDays); + logger.info("prometheus 配置本地保存天数" + (prometheusStartHandle ? "成功" : "失败")); } } catch (Exception e) { logger.error("prometheus startup setting error : ", e); @@ -793,4 +800,55 @@ public class ConfagentJob extends QuartzJobBean { return null; } } + + public boolean handleStorageLocalRetention(String filePath, String retentionDays) { + boolean flag = false; + // 首先获取 启动项 配置文件路径 + String startParameterConfigPath = this.startParameterConfigPath(filePath); + logger.info("获取到的 prometheus 启动项配置文件地址是:" + startParameterConfigPath); + try (RandomAccessFile raf = new RandomAccessFile(new File(startParameterConfigPath), "rw");) { + String readLine = null; + long lastPoint = 0; //记住上一次的偏移量 + while ((readLine = raf.readLine()) != null) { + final long ponit = raf.getFilePointer(); + if (readLine.startsWith("OPTION")) { + String replace = readLine.replaceAll("--storage.tsdb.retention.time=\\d+d", "--storage.tsdb.retention.time=" + retentionDays + "d"); + raf.seek(lastPoint); + raf.writeBytes(replace); + } + lastPoint = ponit; + } + + 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) { + logger.error("获取prometheus启动项配置文件失败,未找到该文件,路径有误,错误信息是:"+e.getMessage(), e); + } catch (IOException e) { + logger.error("处理prometheus启动项配置文件失败,错误信息是:"+e.getMessage(), e); + } + return flag; + } + + private String startParameterConfigPath(String filePath) { + String readLine = ""; + try (RandomAccessFile raf = new RandomAccessFile(new File(filePath), "rw");) { + while ((readLine = raf.readLine()) != null) { + if (readLine.startsWith("EnvironmentFile")) { + break; + } + } + if(StringUtils.isNotEmpty(readLine)){ + String[] split = readLine.split("="); + readLine = split[1]; + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + logger.error("获取prometheus.services失败,未找到该文件,路径有误,错误信息是:"+e.getMessage(), e); + } catch (IOException e) { + logger.error("处理prometheus.service文件失败,错误信息是:"+e.getMessage(), e); + } + return readLine; + } } |
