当前位置:   article > 正文

skywalking中新增,修改,删除alarm-setting.yml文件_skywalking alarm-settings.yml

skywalking alarm-settings.yml

1:直接上代码,controller层

  1. 一:controller层
  2. @GetMapping("/insertAlarmYaml")
  3. public boolean insertAlarmYaml(@ApiIgnore AlarmRuleDTO alarmRuleDTO) throws ServiceException {
  4. try {
  5. Properties properties = new Properties();
  6. properties.load(new InputStreamReader(Object.class.getResourceAsStream("/AlarmSettingPath.properties"), "GBK"));
  7. String ymlPath = properties.getProperty("alarmRulePath");
  8. return alarmQueryService.insertYaml(alarmRuleDTO, ymlPath);
  9. } catch (Exception e) {
  10. log.error(e.getMessage());
  11. return false;
  12. }
  13. }
  14. 二:service层
  15. /**
  16. * 新增告警规则配置
  17. */
  18. boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) throws ServiceException;
  19. 三:service实现层
  20. public boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) throws ServiceException {
  21. Reader applicationReader = getReader();
  22. YamlUpdateUtil alarmInsert = new YamlUpdateUtil(applicationReader);
  23. return alarmInsert.insertAlarmConfig(alarmRuleDTO, ymlPath);
  24. }
  25. public Reader getReader() {
  26. Reader applicationReader;
  27. try {
  28. applicationReader = ResourceUtils.read("alarm-settings.yml");
  29. } catch (FileNotFoundException e) {
  30. throw new ServiceException("can't load alarm-settings.yml");
  31. }
  32. return applicationReader;
  33. }
  34. 四:增删改的操作都是在我自己写的工具类完成的,这里把整个工具类代码附上
  35. public class YamlUpdateUtil {
  36. private Map yamlData;
  37. private final static DumperOptions OPTIONS = new DumperOptions();
  38. //以块读取yml文件
  39. static {
  40. OPTIONS.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
  41. OPTIONS.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
  42. OPTIONS.setPrettyFlow(false);
  43. }
  44. public YamlUpdateUtil(Reader io) {
  45. Yaml yaml = new Yaml(new SafeConstructor());
  46. yamlData = (Map) yaml.load(io);
  47. }
  48. /**
  49. * 新增告警规则配置
  50. */
  51. public boolean insertAlarmConfig(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
  52. if (Objects.nonNull(yamlData)) {
  53. return insertYaml(alarmRuleDTO, ymlPath);
  54. }
  55. return false;
  56. }
  57. /**
  58. * 更新告警规则配置
  59. */
  60. public boolean updateAlarmConfig(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
  61. if (Objects.nonNull(yamlData)) {
  62. return updateAlarmYaml(alarmRuleDTO, ymlPath);
  63. }
  64. return false;
  65. }
  66. /**
  67. * 删除告警规则配置
  68. */
  69. public boolean removeAlarmConfig(String ruleName, String ymlPath) {
  70. if (Objects.nonNull(yamlData)) {
  71. return removeAlarmYaml(ruleName, ymlPath);
  72. }
  73. return false;
  74. }
  75. /**
  76. * 更新告警规则配置文件,根据告警规则名更新
  77. */
  78. private boolean updateAlarmYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
  79. Map ruleMap = (Map) yamlData.get("rules");
  80. if (null == ruleMap) {
  81. return false;
  82. }
  83. ruleMap.forEach((key, value) -> {
  84. if (((String) key).endsWith("_rule")) {
  85. //根据告警规则名判断需要更新的规则属性
  86. if (((String) key).equalsIgnoreCase(alarmRuleDTO.getAlarmRuleName())) {
  87. Map ymlValue = (Map) value;
  88. if (alarmRuleDTO.getMetricsName() != null) {
  89. ymlValue.put("metrics-name", alarmRuleDTO.getMetricsName());
  90. }
  91. if (alarmRuleDTO.getOp() != null) {
  92. ymlValue.put("op", alarmRuleDTO.getOp());
  93. }
  94. if (alarmRuleDTO.getThreshold() != null) {
  95. ymlValue.put("threshold", alarmRuleDTO.getThreshold());
  96. }
  97. if (alarmRuleDTO.getPeriod() != null) {
  98. ymlValue.put("period", alarmRuleDTO.getPeriod());
  99. }
  100. if (alarmRuleDTO.getCount() != null) {
  101. ymlValue.put("count", alarmRuleDTO.getCount());
  102. }
  103. if (alarmRuleDTO.getSilencePeriod() != null) {
  104. ymlValue.put("silence-period", alarmRuleDTO.getSilencePeriod());
  105. }
  106. if (alarmRuleDTO.getMessage() != null) {
  107. ymlValue.put("message", alarmRuleDTO.getMessage());
  108. }
  109. yamlData.put("rules", ruleMap);
  110. Yaml yaml = new Yaml(OPTIONS);
  111. try {
  112. FileWriter writer = new FileWriter(ymlPath);
  113. yaml.dump(yamlData, writer);
  114. writer.flush();
  115. } catch (IOException e) {
  116. log.error(e.getMessage());
  117. }
  118. }
  119. }
  120. });
  121. return true;
  122. }
  123. /**
  124. * 新增规则告警,告警名称为必输字段且必须以_rule结尾
  125. */
  126. private boolean insertYaml(AlarmRuleDTO alarmRuleDTO, String ymlPath) {
  127. Map ruleMap = (Map) yamlData.get("rules");
  128. //如果规则一开始是空的,这里应该先创建一个规则
  129. if (null == ruleMap) {
  130. ruleMap = new HashMap();
  131. yamlData.put("rules", ruleMap);
  132. }
  133. String ruleName = alarmRuleDTO.getAlarmRuleName();
  134. //如果传入的规则名称存在,直接返回失败
  135. if (ruleMap.containsKey(ruleName)) {
  136. log.error("INSERT:传入的规则名称存在,请核对");
  137. return false;
  138. }
  139. //放入新增的规则属性
  140. Map<String, Object> ymlValue = new HashMap<>();
  141. ymlValue.put("metrics-name", alarmRuleDTO.getMetricsName());
  142. ymlValue.put("op", alarmRuleDTO.getOp());
  143. ymlValue.put("threshold", alarmRuleDTO.getThreshold());
  144. ymlValue.put("period", alarmRuleDTO.getPeriod());
  145. ymlValue.put("count", alarmRuleDTO.getCount());
  146. ymlValue.put("silence-period", alarmRuleDTO.getSilencePeriod());
  147. ymlValue.put("message", alarmRuleDTO.getMessage());
  148. ruleMap.put(ruleName, ymlValue);
  149. Yaml yaml = new Yaml(OPTIONS);
  150. try {
  151. FileWriter writer = new FileWriter(ymlPath);
  152. yaml.dump(yamlData, writer);
  153. writer.flush();
  154. return true;
  155. } catch (IOException e) {
  156. log.error(e.getMessage());
  157. }
  158. return false;
  159. }
  160. /**
  161. * 根据告警规则名删除规则配置
  162. */
  163. private boolean removeAlarmYaml(String ruleName, String ymlPath) {
  164. Map ruleMap = (Map) yamlData.get("rules");
  165. //如果规则为空,则返回失败
  166. if (null == ruleMap) {
  167. return false;
  168. }
  169. //如果传入的规则名称不存在,直接返回失败
  170. if (!ruleMap.containsKey(ruleName)) {
  171. log.error("REMOVE:传入的规则名称不存在,请核对!");
  172. return false;
  173. }
  174. //根据规则名称删除相应配置
  175. ruleMap.remove(ruleName);
  176. Yaml yaml = new Yaml(OPTIONS);
  177. try {
  178. FileWriter writer = new FileWriter(ymlPath);
  179. yaml.dump(yamlData, writer);
  180. writer.flush();
  181. return true;
  182. } catch (IOException e) {
  183. log.error(e.getMessage());
  184. }
  185. return false;
  186. }
  187. }

 *目前存在的问题的,对yml文件实现的增删改操作之后,要求实现动态加载,目前尚未完成,望各位对这块熟悉的大神能指点一二

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/236439
推荐阅读
相关标签
  

闽ICP备14008679号