提交 d6deb6eb authored 作者: chenshiying's avatar chenshiying

[新增] 优化logstash 脚本

上级 8237481f
......@@ -40,4 +40,5 @@ build/
**/src/main/resources/bin/jenkinsGit/**
**/src/main/resources/bin/jenkinsPush/**
**/src/main/resources/bin/elk/**
**/src/main/resources/bin/elkold/**
......@@ -221,6 +221,9 @@ pssh -h /home/soft/deploy/server/host_ip_push_script.txt -i bash /home/soft/logs
pssh -h /home/soft/deploy/server/host_ip_push_script.txt -i bash /home/soft/logstash-stop-business.sh
pssh -h /home/soft/deploy/server/host_ip_push_script.txt -i bash /home/soft/logstash-start-business.sh
pssh -h /home/soft/deploy/server/host_ip_push_script.txt -i bash /home/soft/logstash-stop-dutyreport.sh
pssh -h /home/soft/deploy/server/host_ip_push_script.txt -i bash /home/soft/logstash-start-dutyreport.sh
```
......
package com.loit.common.script;
import com.loit.common.script.dto.BackendFrontEnum;
import com.loit.common.script.dto.DeployInfoDataDTO;
import com.loit.common.utils.ListUtil;
import com.loit.common.utils.StringUtils;
import com.loit.common.utils.excel.ImportExcel;
import com.loit.common.utils.file.FileUtils;
import com.loit.common.utils.freemarker.FreeMarkerUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.http.entity.ContentType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.*;
@Slf4j
public class GeneratorLogstashScriptNew {
protected static String root_path = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\bin";
protected static String root_path_full = root_path + "\\elk";
protected static String time_loit_servers = "portal,getway,notice,file,version,gray-server,mobile-portal";
protected static String duty_report_servers = "duty-report";
private static Map<String, List<DeployInfoDataDTO>> backendInstanceDtoMap = new LinkedHashMap<>();
public static void main(String[] args) {
try {
String filePathStr = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\deployInfo.xlsx";
File pdfFile = new File(filePathStr);
FileInputStream fileInputStream = new FileInputStream(pdfFile);
MultipartFile multipartFile = new MockMultipartFile(pdfFile.getName(), pdfFile.getName(),
ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
ImportExcel ei = new ImportExcel(multipartFile, 1, 0);
List<DeployInfoDataDTO> grayVersionDataDTOList = ei.getDataList(DeployInfoDataDTO.class);
if (ListUtil.isEmpty(grayVersionDataDTOList)) {
return;
}
//对前端实例进行分组
for (DeployInfoDataDTO deployDataDTO : grayVersionDataDTOList) {
BackendFrontEnum backendFrontType = BackendFrontEnum.getEnumByCode(deployDataDTO.getBackendFrontType());
if (!BackendFrontEnum.BACKEND.equals(backendFrontType)) {
continue;
}
String fixedIp = deployDataDTO.getFixedIp();
List<DeployInfoDataDTO> deployInfoDataDTOS = backendInstanceDtoMap.get(fixedIp);
if (deployInfoDataDTOS == null) {
deployInfoDataDTOS = new ArrayList<>();
}
deployInfoDataDTOS.add(deployDataDTO);
backendInstanceDtoMap.put(fixedIp, deployInfoDataDTOS);
}
//生成前端nginx 配置文件
backendInstanceDtoMap.forEach((fixedIp, deployInfoList) -> {
if (StringUtils.isBlank(fixedIp)) {
return;
}
String fileDir = root_path_full + "\\" + fixedIp;
File folder = new File(fileDir + "\\elkconfig");
if (!folder.exists()) {
folder.mkdirs();
}
List<DeployInfoDataDTO> portalServerList = new ArrayList<>();
List<DeployInfoDataDTO> businessServerList = new ArrayList<>();
List<DeployInfoDataDTO> dutyReportServerList = new ArrayList<>();
for (DeployInfoDataDTO dataDTO : deployInfoList) {
if (!"backend".equals(dataDTO.getBackendFrontType())) {
continue;
}
if (time_loit_servers.contains(dataDTO.getServiceName())) {
portalServerList.add(dataDTO);
} else if (duty_report_servers.equals(dataDTO.getServiceName())) { //勤务报备
dutyReportServerList.add(dataDTO);
} else {
businessServerList.add(dataDTO);
}
}
if (CollectionUtils.isNotEmpty(portalServerList)) {
buildServerConf(fileDir, portalServerList, "portal");
}
if (CollectionUtils.isNotEmpty(businessServerList)) {
buildServerConf(fileDir, businessServerList, "business");
}
if (CollectionUtils.isNotEmpty(dutyReportServerList)) {
buildServerConf(fileDir, dutyReportServerList, "dutyreport");
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
public static void buildServerConf(String fileDir, List<DeployInfoDataDTO> list, String folderName) {
List<Map<String, String>> mapList = new ArrayList<>();
for (DeployInfoDataDTO dataDTO : list) {
Map<String, String> map = new HashMap<>();
map.put("deployPath", dataDTO.getDeployPath());
map.put("serviceName", dataDTO.getServiceName());
mapList.add(map);
}
buildBackendConfig(fileDir, mapList, folderName);
buildBashConfig(fileDir, folderName);
}
/**
* 生成logstash-es-sone.conf 及其 sh
*/
private static void buildBackendConfig(String fileDir, List<Map<String, String>> mapList, String folderName) {
String fileName = "logstash-es-sone-" + folderName + ".conf";
String logstashFileName = fileDir + "\\elkconfig" + "\\" + fileName;
Map model = new HashMap();
model.put("mapList", mapList);
if ("portal".equals(folderName)) {
model.put("esindex", "portal");
} else if ("dutyreport".equals(folderName)) {
model.put("esindex", "dutyreport");
} else {
model.put("esindex", "sone");
}
String result = FreeMarkerUtils.process("sh\\logstash-conf.ftl", model);
FileUtils.write(logstashFileName, result);
}
/**
* 生成logstash 启动 停止 脚本
*
* @param folderName
* @throws IOException
*/
private static void buildBashConfig(String fileDir, String folderName) {
String fileName = "logstash-es-sone-" + folderName + ".conf";
String startFileName = "logstash-start-" + folderName + ".sh";
String stopFileName = "logstash-stop-" + folderName + ".sh";
String logstashStartFileName = fileDir + "\\" + startFileName;
String logstashStopFileName = fileDir + "\\" + stopFileName;
Map model = new HashMap();
model.put("fileName", fileName);
model.put("folderName", folderName);
String startResult = FreeMarkerUtils.process("sh\\logstash-start.ftl", model);
FileUtils.write(logstashStartFileName, startResult);
String stopResult = FreeMarkerUtils.process("sh\\logstash-stop.ftl", model);
FileUtils.write(logstashStopFileName, stopResult);
}
}
......@@ -25,12 +25,12 @@ import java.util.*;
* logstash-es-portal.conf
*/
@Slf4j
public class GeneratorLogstashScript {
public class GeneratorLogstashScriptOld {
protected static String root_path = "F:\\9Git140\\loit-build-common\\loit-build-component\\loit-build-deploy-env\\src\\main\\resources\\bin";
protected static String root_path_full = root_path + "\\elk";
protected static String root_path_full = root_path + "\\elkold";
protected static String time_loit_servers = "portal,getway,notice,file,version,gray-server,mobile-portal";
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论