nz-talon首次提交
This commit is contained in:
@@ -0,0 +1,138 @@
|
||||
package net.geedge.confagent.controller;
|
||||
|
||||
import cn.hutool.log.Log;
|
||||
import net.geedge.confagent.util.ConfagentUtil;
|
||||
import net.geedge.confagent.util.R;
|
||||
import net.geedge.confagent.util.RCode;
|
||||
import net.geedge.confagent.util.Tool;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/promtail")
|
||||
public class PromtailController extends BaseController{
|
||||
private final static Log log = Log.get();
|
||||
|
||||
@Autowired
|
||||
private ConfagentUtil confagentUtil;
|
||||
|
||||
@Value("${confagent.promtail.query.auth:true}")
|
||||
private Boolean queryAuth;
|
||||
|
||||
private final String[] QUERY_API_SUFFIX = {"query","query_range","series","labels","values"};
|
||||
|
||||
@Value("${confagent.promtail.restart:systemctl restart promtail}")
|
||||
private String restartCmd;
|
||||
|
||||
@Value("${confagent.versionFile:promtail.version}")
|
||||
private String versionFile;
|
||||
|
||||
private static String rootPath = Tool.WebPathUtil.getRootPath();
|
||||
|
||||
/**
|
||||
* @Description 获取promtail相关配置
|
||||
* @Author rui
|
||||
* @Date 2021/3/24
|
||||
*/
|
||||
@GetMapping("/config")
|
||||
public R queryConfig(){
|
||||
Map<String,String> cmdLine = ConfagentUtil.loadServiceConfigFile(promtailCmdLinePath);
|
||||
Map<String,Object> promtailConf = Tool.YamlUtil.readAsMap(promtailConfPath);
|
||||
String version = confagentUtil.readVersion();
|
||||
|
||||
Map<String,Object> result = new LinkedHashMap<>();
|
||||
|
||||
result.put("version",version);
|
||||
result.put("cmdline",cmdLine);
|
||||
result.put("config",promtailConf);
|
||||
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 写入promtail配置文件
|
||||
* @Author rui
|
||||
* @Date 2021/3/25
|
||||
*/
|
||||
@PostMapping("/config")
|
||||
public R overwriteConfig( @RequestBody Map<String,Object> configs){
|
||||
|
||||
Object version = configs.get("version");
|
||||
File tf = Tool.FileUtil.file(rootPath, versionFile);
|
||||
log.debug("version file path : {}" ,tf.getAbsolutePath());
|
||||
if(version==null){
|
||||
return R.error(RCode.PROMTAIL_CONFIG_VERSION_ISNULL);
|
||||
}else {
|
||||
Tool.FileUtil.writeUtf8String(version.toString(), tf);
|
||||
}
|
||||
|
||||
Map<String,String> cmdLine =(Map<String,String>) configs.get("cmdline");
|
||||
Map<String,Object> promtailConf =(Map<String,Object>) configs.get("config");
|
||||
|
||||
// boolean reload = false;
|
||||
// boolean restart = false;
|
||||
if(!Tool.MapUtil.isEmpty(cmdLine)){
|
||||
log.info("write promtail cmdLine conf:{}", Tool.JSONUtil.toJsonStr(cmdLine));
|
||||
writeServiceConfigFile(cmdLine,promtailCmdLinePath);
|
||||
// restart = true;
|
||||
}
|
||||
if(!Tool.MapUtil.isEmpty(promtailConf)){
|
||||
log.info("write promtail conf:{}", Tool.JSONUtil.toJsonStr(promtailConf));
|
||||
Tool.YamlUtil.writeAsMap(promtailConf,promtailConfPath);
|
||||
// reload = true;
|
||||
}
|
||||
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 获取promtail相关配置版本
|
||||
* @Author rui
|
||||
* @Date 2021/3/24
|
||||
*/
|
||||
@GetMapping("/config/version")
|
||||
public R queryVersion(){
|
||||
Map<String,Object> result = new HashMap<>();
|
||||
String version = confagentUtil.readVersion();
|
||||
result.put("version",version);
|
||||
return R.ok(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @Description 写 promtail config.conf 启动参数配置文件
|
||||
* @Author rui
|
||||
* @Date 2021/3/25
|
||||
*/
|
||||
private void writeServiceConfigFile(Map<String,String> conf,String path){
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append("OPTION=\"");
|
||||
for (Map.Entry<String,String> entry:conf.entrySet()){
|
||||
String key = entry.getKey();
|
||||
String value = entry.getValue();
|
||||
sb.append("--"+key+"=");
|
||||
sb.append("'"+value+"' ");
|
||||
}
|
||||
sb.append("\"");
|
||||
|
||||
Tool.FileUtil.writeUtf8String(sb.toString(),path);
|
||||
|
||||
}
|
||||
|
||||
// private boolean reloadPromtail(){
|
||||
//
|
||||
// UrlBuilder promtailReload = new UrlBuilder();
|
||||
// int promtailPort = ConfagentUtil.getConfFilePort(promtailCmdLinePath, PROMTAIL_LISTEN_ADDR, DEFAULT_PROMTAIL_PORT);
|
||||
// promtailReload.setScheme("http").setHost(defaultPromtailIP).setPort(promtailPort).addPath("/-/reload");
|
||||
//
|
||||
// String url = promtailReload.toString();
|
||||
// HttpRequest post = Tool.HttpUtil.createPost(url);
|
||||
// HttpResponse response = post.execute();
|
||||
// return 200 == response.getStatus();
|
||||
// }
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user