summaryrefslogtreecommitdiff
path: root/src/main/java/com/nis/util/YamlUtil.java
blob: 3ca661581572b776c0d27ed989c58dcd7dcaad65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
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<String> 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<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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<String,Object> 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<String, Object> 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<String, Object> 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<String, Object> 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<String,Object> 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;
	}
}