package com.nis.util; import cn.hutool.core.io.FileUtil; import cn.hutool.core.util.RuntimeUtil; import cn.hutool.log.Log; import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; import org.apache.commons.io.IOUtils; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import java.io.*; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Set; public class YamlUtil { private static Log logger = Log.get(); /** * snmp配置相关 * @param param * @param ymlPath * @return */ @SuppressWarnings("unchecked") public static boolean snmpYmlHandle(Set param,String ymlPath) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml y = new Yaml(options); boolean flag=false; if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) { OutputStream out = null; OutputStreamWriter osw = null; try { File file = FileUtil.file(ymlPath); 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 (UnsupportedEncodingException e) { logger.error("snmpYmlHandle error : "+e.getMessage(),e); } finally { if(out!=null) { IOUtils.closeQuietly(out); } if(osw!=null) { IOUtils.closeQuietly(osw); } } } return flag; } /** * datacenter相关配置 * @param param * @param ymlPath * @return */ @SuppressWarnings("unchecked") public static boolean subYmlHandle(Map param,String ymlPath,String ruleYmlPath) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml y = new Yaml(options); boolean flag=false; if(param!=null && ymlPath != null && ymlPath.length() != 0) { FileInputStream fileInputStream =null; OutputStream out = null; OutputStreamWriter osw = null; try { File file = FileUtil.file(ymlPath); /**fileInputStream = new FileInputStream(file); Map map = y.loadAs(fileInputStream, Map.class); if(map==null) { map=new HashMap(); } map.remove("rule_files"); map.put("scrape_configs", param);**/ //修改 rule.yml配置文件 File ruleYmlfile = FileUtil.file(ruleYmlPath); if(ruleYmlfile.exists()){ // 文件存在则删除文件信息 ruleYmlfile.delete(); } out=FileUtil.getOutputStream(file); osw=new OutputStreamWriter(out, "UTF-8"); y.dump(param,osw); flag = true; } catch (UnsupportedEncodingException e) { logger.error("subYmlHandle error : "+e.getMessage(),e); } finally { if(fileInputStream!=null) { IOUtils.closeQuietly(fileInputStream); } if(out!=null) { IOUtils.closeQuietly(out); } if(osw!=null) { IOUtils.closeQuietly(osw); } } } return flag; } /** * global相关配置设置 * @param param * @param ymlPath * @param ruleYmlPath * @return */ @SuppressWarnings("unchecked") public static boolean centerYmlHandle(Map param,String ymlPath,String ruleYmlPath){ DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml y = new Yaml(options); boolean flag=false; if(param!=null && !param.isEmpty() && ymlPath != null && ymlPath.length() != 0) { // FileInputStream fileInputStream =null; BufferedInputStream inputStream = null; OutputStream out = null; OutputStreamWriter osw = null; // FileInputStream fileInputStream2 =null; BufferedInputStream inputStream2 = null; OutputStream out2 = null; OutputStreamWriter osw2 = null; try { //修改 rule.yml配置文件 File ruleYmlfile = FileUtil.file(ruleYmlPath); if(!ruleYmlfile.exists()){ // 文件不存在 创建文件 ruleYmlfile.createNewFile(); } inputStream2 = FileUtil.getInputStream(ruleYmlfile); Map map2 = y.loadAs(inputStream2, Map.class); if(map2==null) { map2=new HashMap(); } map2.put("groups", param.get("groups")); out2 = FileUtil.getOutputStream(ruleYmlfile); osw2 = new OutputStreamWriter(out2, "UTF-8"); y.dump(map2,osw2); //去除告警规则相关内容 param.remove("groups"); // 修改prometheus.yml配置文件 File ymlfile = FileUtil.file(ymlPath); /**fileInputStream = new FileInputStream(ymlfile); Map map = y.loadAs(fileInputStream, Map.class); if(map==null) { map=new HashMap(); } map.put("scrape_configs", param.get("scrape_configs")); map.put("alerting", param.get("alerting")); map.put("rule_files", param.get("rule_files"));**/ out = FileUtil.getOutputStream(ymlfile); osw = new OutputStreamWriter(out, "UTF-8"); y.dump(param,osw); flag = true; } catch (UnsupportedEncodingException e) { logger.error("centerYmlHandle error : "+e.getMessage(),e); } catch (FileNotFoundException e) { logger.error("centerYmlHandle error : "+e.getMessage(),e); } catch (IOException e) { logger.error("centerYmlHandle error : "+e.getMessage(),e); } finally { if(inputStream!=null) { IOUtils.closeQuietly(inputStream); } if(out!=null) { IOUtils.closeQuietly(out); } if(osw!=null) { IOUtils.closeQuietly(osw); } if(inputStream2!=null) { IOUtils.closeQuietly(inputStream2); } if(out2!=null) { IOUtils.closeQuietly(out2); } if(osw2!=null) { IOUtils.closeQuietly(osw2); } } } return flag; } /** * thanos rule 角色所需 rule.yml 配置文件处理 * * @param param * @param ruleYmlPath * @return */ public static boolean thanosRuleYmlHandle(Map param, String ruleYmlPath) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml y = new Yaml(options); boolean flag = false; if (param != null && !param.isEmpty()) { FileInputStream fileInputStream = null; OutputStream out = null; try { // 修改 rule.yml配置文件 File ruleYmlfile = FileUtil.file(ruleYmlPath); if (!ruleYmlfile.exists()) { // 文件不存在 创建文件 ruleYmlfile.createNewFile(); } FileInputStream fileInputStream2 = new FileInputStream(ruleYmlfile); Map map2 = y.loadAs(fileInputStream2, Map.class); if (map2 == null) { map2 = new HashMap(); } map2.put("groups", param.get("groups")); OutputStream out2 = FileUtil.getOutputStream(ruleYmlfile); OutputStreamWriter osw2 = new OutputStreamWriter(out2, "UTF-8"); y.dump(map2, osw2); flag = true; } catch (UnsupportedEncodingException e) { logger.error("thanosRuleYmlHandle error : "+e.getMessage(),e); } catch (FileNotFoundException e) { logger.error("thanosRuleYmlHandle error : "+e.getMessage(),e); } catch (IOException e) { logger.error("thanosRuleYmlHandle error : "+e.getMessage(),e); } finally { if (fileInputStream != null) { IOUtils.closeQuietly(fileInputStream); } if (out != null) { IOUtils.closeQuietly(out); } } } return flag; } /** * prometheus启动项修改 * @param filePath * @param param * @return */ public static boolean prometheusStartHandle(String filePath,String param) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); boolean flag=false; RandomAccessFile raf = null; try { raf = new RandomAccessFile(FileUtil.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"});*/ RuntimeUtil.exec("systemctl daemon-reload"); RuntimeUtil.exec("systemctl stop prometheus"); RuntimeUtil.exec("systemctl start prometheus"); flag=true; } catch (FileNotFoundException e) { logger.error("prometheusStartHandle error : FileNotFoundException "+e.getMessage(),e); } catch (IOException e) { logger.error("prometheusStartHandle error : IOException "+e.getMessage(),e); } finally { if(raf!=null) { IOUtils.closeQuietly(raf); } } return flag; } /** * prometheus全局配置修改 * @param param * @return */ @SuppressWarnings("unchecked") public static boolean prometheusSettingHandle(Map param, String ymlPath) { DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml y = new Yaml(options); 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 = FileUtil.file(ymlPath); fileInputStream = new FileInputStream(ymlfile); Map map = y.loadAs(fileInputStream, Map.class); if(map==null) { return false; } Map object = (Map) map.get("global"); if(ObjectUtils.isEmpty(object)) { object = new LinkedHashMap(); map.put("global", object); } String scrapeInterval = param.get(Constant.SCRAPE_INTERVAL) + "s"; String scrapeTimeout =param.get(Constant.SCRAPE_TIMEOUT) + "s"; object.put(Constant.SCRAPE_INTERVAL, scrapeInterval); object.put(Constant.EVALUATION_INTERVAL, scrapeInterval); object.put(Constant.SCRAPE_TIMEOUT,scrapeTimeout); out = new FileOutputStream(ymlfile); osw = new OutputStreamWriter(out, "UTF-8"); y.dump(map,osw); flag = true; } catch (FileNotFoundException e) { logger.error("prometheusSettingHandle error : "+e.getMessage(),e); } catch (UnsupportedEncodingException e) { logger.error("prometheusSettingHandle error : "+e.getMessage(),e); } finally { if(fileInputStream!=null) { IOUtils.closeQuietly(fileInputStream); } if(out!=null) { IOUtils.closeQuietly(out); } if(osw!=null) { IOUtils.closeQuietly(osw); } } } return flag; } }