package com.ruoyi.util; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateExceptionHandler; import java.io.*; import java.nio.charset.StandardCharsets; import java.util.*; public class ServerOpsDocGenerator { public static void main(String[] args) throws Exception { generateServerOpsDoc(); } public static void generateServerOpsDoc() throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_2_3_32); cfg.setClassForTemplateLoading(ServerOpsDocGenerator.class, "/static"); cfg.setDefaultEncoding("UTF-8"); cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); Map data = new HashMap<>(); data.put("projectName", "产品库存管理系统"); data.put("projectVersion", "V1.0"); data.put("docDate", "2026年6月"); // 服务器清单 List> servers = new ArrayList<>(); servers.add(createServer("SVR-APP-01", "应用服务器", "192.168.1.101", "8核", "16GB", "500GB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-APP-02", "应用服务器", "192.168.1.102", "8核", "16GB", "500GB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-DB-01", "数据库主库", "192.168.1.201", "8核", "32GB", "1TB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-DB-02", "数据库从库", "192.168.1.202", "8核", "32GB", "1TB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-REDIS-01", "Redis节点1", "192.168.1.301", "4核", "8GB", "200GB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-REDIS-02", "Redis节点2", "192.168.1.302", "4核", "8GB", "200GB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-REDIS-03", "Redis节点3", "192.168.1.303", "4核", "8GB", "200GB SSD", "CentOS 7.9", "运行中")); servers.add(createServer("SVR-FILE-01", "文件服务器", "192.168.1.401", "4核", "8GB", "2TB HDD", "CentOS 7.9", "磁盘告警")); servers.add(createServer("SVR-AI-01", "AI推理服务器", "192.168.1.501", "16核", "64GB", "1TB SSD", "Ubuntu 22.04", "运行中")); servers.add(createServer("SVR-NGINX-01", "Nginx代理", "192.168.1.11", "4核", "8GB", "200GB SSD", "CentOS 7.9", "运行中")); data.put("servers", servers); // 运维进度跟踪 List> progress = new ArrayList<>(); progress.add(createProgress("2026-05-20", "服务器初始环境配置", "安装JDK25、Nginx、Docker", "100%", "")); progress.add(createProgress("2026-05-21", "数据库主从搭建", "MySQL8.0主从复制配置", "100%", "")); progress.add(createProgress("2026-05-22", "Redis集群部署", "3节点哨兵模式部署", "100%", "")); progress.add(createProgress("2026-05-23", "应用服务部署", "SpringBoot应用双节点部署", "100%", "")); progress.add(createProgress("2026-05-24", "SSL证书配置", "Nginx HTTPS配置", "100%", "")); progress.add(createProgress("2026-05-25", "监控系统搭建", "Prometheus+Grafana部署", "90%", "告警规则待完善")); progress.add(createProgress("2026-05-26", "日志收集系统", "ELK日志平台搭建", "80%", "Kibana仪表盘待配置")); progress.add(createProgress("2026-05-27", "备份策略配置", "数据库自动备份脚本配置", "100%", "")); progress.add(createProgress("2026-05-28", "安全加固", "防火墙规则、SSH加固", "85%", "端口扫描待完成")); progress.add(createProgress("2026-05-29", "压力测试", "JMeter性能压测", "100%", "并发指标达标")); progress.add(createProgress("2026-05-30", "灾备演练", "数据库主从切换演练", "100%", "切换时间30秒内")); data.put("progress", progress); // 服务器巡检记录 List> inspections = new ArrayList<>(); inspections.add(createInspection("2026-05-20", "SVR-APP-01", "CPU 35%, 内存 52%, 磁盘 40%", "正常", "")); inspections.add(createInspection("2026-05-20", "SVR-APP-02", "CPU 30%, 内存 48%, 磁盘 38%", "正常", "")); inspections.add(createInspection("2026-05-20", "SVR-DB-01", "CPU 45%, 内存 68%, 磁盘 55%", "正常", "")); inspections.add(createInspection("2026-05-20", "SVR-DB-02", "CPU 20%, 内存 60%, 磁盘 52%", "正常", "")); inspections.add(createInspection("2026-05-20", "SVR-REDIS-01", "CPU 15%, 内存 40%, 磁盘 25%", "正常", "")); inspections.add(createInspection("2026-05-25", "SVR-APP-01", "CPU 42%, 内存 58%, 磁盘 43%", "正常", "")); inspections.add(createInspection("2026-05-25", "SVR-DB-01", "CPU 55%, 内存 72%, 磁盘 60%", "关注", "内存使用率偏高,建议关注")); inspections.add(createInspection("2026-05-25", "SVR-FILE-01", "CPU 20%, 内存 35%, 磁盘 85%", "告警", "磁盘使用率达85%,需扩容")); inspections.add(createInspection("2026-05-30", "SVR-APP-01", "CPU 38%, 内存 55%, 磁盘 45%", "正常", "")); inspections.add(createInspection("2026-05-30", "SVR-DB-01", "CPU 48%, 内存 70%, 磁盘 58%", "正常", "内存已优化")); inspections.add(createInspection("2026-05-30", "SVR-FILE-01", "CPU 18%, 内存 32%, 磁盘 88%", "告警", "磁盘持续增长,需尽快扩容")); inspections.add(createInspection("2026-05-30", "SVR-AI-01", "CPU 65%, 内存 78%, 磁盘 45%", "关注", "GPU使用率正常,CPU负载偏高")); data.put("inspections", inspections); // 数据库备份记录 List> backups = new ArrayList<>(); backups.add(createBackup("2026-05-20", "全量备份", "product_inventory", "5.2GB", "成功", "02:00", "02:15")); backups.add(createBackup("2026-05-21", "增量备份", "product_inventory", "380MB", "成功", "02:00", "02:08")); backups.add(createBackup("2026-05-22", "增量备份", "product_inventory", "420MB", "成功", "02:00", "02:09")); backups.add(createBackup("2026-05-23", "全量备份", "product_inventory", "5.5GB", "成功", "02:00", "02:20")); backups.add(createBackup("2026-05-24", "增量备份", "product_inventory", "350MB", "成功", "02:00", "02:07")); backups.add(createBackup("2026-05-25", "增量备份", "product_inventory", "400MB", "成功", "02:00", "02:08")); backups.add(createBackup("2026-05-26", "增量备份", "product_inventory", "390MB", "失败", "02:00", "02:03")); backups.add(createBackup("2026-05-26", "增量备份", "product_inventory", "395MB", "成功", "03:00", "03:08")); backups.add(createBackup("2026-05-27", "全量备份", "product_inventory", "5.8GB", "成功", "02:00", "02:22")); backups.add(createBackup("2026-05-28", "增量备份", "product_inventory", "410MB", "成功", "02:00", "02:09")); data.put("backups", backups); // 故障/事件记录 List> incidents = new ArrayList<>(); incidents.add(createIncident("INC-001", "2026-05-22 14:30", "SVR-DB-01", "MySQL连接数异常升高", "连接池泄漏", "调整连接池参数,重启服务", "2026-05-22 15:10", "已解决", "周XX")); incidents.add(createIncident("INC-002", "2026-05-24 09:15", "SVR-APP-01", "应用响应超时", "线程池耗尽", "扩大线程池,增加超时时间", "2026-05-24 09:50", "已解决", "周XX")); incidents.add(createIncident("INC-003", "2026-05-26 02:03", "SVR-DB-01", "数据库备份失败", "磁盘空间不足", "清理归档日志,释放空间", "2026-05-26 03:00", "已解决", "周XX")); incidents.add(createIncident("INC-004", "2026-05-28 11:20", "SVR-REDIS-02", "Redis哨兵切换异常", "网络抖动导致误判", "排查网络,调整哨兵超时参数", "2026-05-28 11:50", "已解决", "周XX")); incidents.add(createIncident("INC-005", "2026-05-30 08:45", "SVR-FILE-01", "磁盘使用率超85%", "日志文件未轮转", "启用日志轮转,清理旧日志", "2026-05-30 09:30", "处理中", "周XX")); incidents.add(createIncident("INC-006", "2026-05-30 16:00", "SVR-AI-01", "GPU驱动异常导致推理失败", "驱动版本不兼容", "回滚GPU驱动版本", "2026-05-30 17:30", "已解决", "孙XX")); data.put("incidents", incidents); // 补丁/更新记录 List> patches = new ArrayList<>(); patches.add(createPatch("2026-05-20", "SVR-APP-01/02", "JDK 25.0.1 安全更新", "安全补丁", "成功", "")); patches.add(createPatch("2026-05-22", "全部服务器", "OpenSSL 3.0.15 升级", "安全补丁", "成功", "修复高危漏洞CVE-2026-XXXX")); patches.add(createPatch("2026-05-24", "SVR-NGINX-01", "Nginx 1.26.2 版本升级", "版本升级", "成功", "")); patches.add(createPatch("2026-05-26", "SVR-DB-01/02", "MySQL 8.0.38 补丁更新", "安全补丁", "成功", "先升级从库,验证后切换升级主库")); patches.add(createPatch("2026-05-28", "SVR-AI-01", "CUDA Toolkit 12.6 更新", "版本升级", "成功", "")); patches.add(createPatch("2026-05-30", "全部服务器", "系统内核安全更新", "安全补丁", "进行中", "需逐台重启,正在分批执行")); data.put("patches", patches); // 变更记录 List> changes = new ArrayList<>(); changes.add(createChange("CHG-001", "2026-05-22", "数据库连接池参数调整", "SVR-DB-01", "连接数50到100", "周XX", "已完成", "")); changes.add(createChange("CHG-002", "2026-05-23", "Nginx新增负载均衡规则", "SVR-NGINX-01", "增加A/B测试路由", "周XX", "已完成", "")); changes.add(createChange("CHG-003", "2026-05-25", "Redis内存策略调整", "SVR-REDIS-01/02/03", "maxmemory从4G调整为6G", "周XX", "已完成", "")); changes.add(createChange("CHG-004", "2026-05-27", "数据库备份策略调整", "SVR-DB-01", "从每日全量改为周三六全量+日增量", "周XX", "已完成", "")); changes.add(createChange("CHG-005", "2026-05-29", "应用JVM参数优化", "SVR-APP-01/02", "堆内存8G到12G,GC策略调整", "王XX", "已完成", "")); data.put("changes", changes); // 待办事项 List> actionItems = new ArrayList<>(); actionItems.add(createActionItem("1", "SVR-FILE-01磁盘扩容至4TB", "周XX", "2026-06-05")); actionItems.add(createActionItem("2", "完成ELK日志平台Kibana仪表盘配置", "周XX", "2026-06-03")); actionItems.add(createActionItem("3", "完善Prometheus告警规则", "周XX", "2026-06-04")); actionItems.add(createActionItem("4", "完成全部服务器内核安全更新", "周XX", "2026-06-02")); actionItems.add(createActionItem("5", "编写运维手册和应急预案", "周XX", "2026-06-10")); actionItems.add(createActionItem("6", "配置异地备份同步任务", "周XX", "2026-06-08")); data.put("actionItems", actionItems); // 运维统计 data.put("serverCount", "10"); data.put("runningCount", "8"); data.put("alertCount", "2"); data.put("uptimeRate", "99.95%"); data.put("totalInspections", "12"); data.put("totalBackups", "10"); data.put("backupSuccessRate", "90%"); data.put("totalIncidents", "6"); data.put("solvedIncidents", "5"); data.put("totalPatches", "6"); data.put("completedPatches", "5"); // 运维结论 data.put("opsConclusion", "整体运维状况良好,服务器可用率达到99.95%。SVR-FILE-01磁盘告警需尽快处理,已完成日志轮转缓解压力,仍需扩容。备份策略已优化,26日备份失败已处理。安全补丁分批执行中,预计6月2日前全部完成。"); data.put("riskWarning", "磁盘告警(SVR-FILE-01)、内存偏高(SVR-DB-01)、内核补丁待完成(3台)"); Template template = cfg.getTemplate("服务器运维记录.xml"); StringWriter out = new StringWriter(); template.process(data, out); String outputPath = "D:/牛马/文档/服务器运维记录.docx"; File outputFile = new File(outputPath); outputFile.getParentFile().mkdirs(); try (FileOutputStream fos = new FileOutputStream(outputFile); OutputStreamWriter osw = new OutputStreamWriter(fos, StandardCharsets.UTF_8)) { osw.write(out.toString()); } System.out.println("服务器运维记录生成成功: " + outputPath); } private static Map createServer(String id, String type, String ip, String cpu, String memory, String disk, String os, String status) { Map map = new HashMap<>(); map.put("id", id); map.put("type", type); map.put("ip", ip); map.put("cpu", cpu); map.put("memory", memory); map.put("disk", disk); map.put("os", os); map.put("status", status); return map; } private static Map createProgress(String date, String task, String content, String rate, String remark) { Map map = new HashMap<>(); map.put("date", date); map.put("task", task); map.put("content", content); map.put("rate", rate); map.put("remark", remark); return map; } private static Map createInspection(String date, String server, String metrics, String result, String remark) { Map map = new HashMap<>(); map.put("date", date); map.put("server", server); map.put("metrics", metrics); map.put("result", result); map.put("remark", remark); return map; } private static Map createBackup(String date, String type, String database, String size, String status, String startTime, String endTime) { Map map = new HashMap<>(); map.put("date", date); map.put("type", type); map.put("database", database); map.put("size", size); map.put("status", status); map.put("startTime", startTime); map.put("endTime", endTime); return map; } private static Map createIncident(String no, String time, String server, String desc, String cause, String solution, String resolveTime, String status, String handler) { Map map = new HashMap<>(); map.put("no", no); map.put("time", time); map.put("server", server); map.put("desc", desc); map.put("cause", cause); map.put("solution", solution); map.put("resolveTime", resolveTime); map.put("status", status); map.put("handler", handler); return map; } private static Map createPatch(String date, String server, String content, String type, String result, String remark) { Map map = new HashMap<>(); map.put("date", date); map.put("server", server); map.put("content", content); map.put("type", type); map.put("result", result); map.put("remark", remark); return map; } private static Map createChange(String no, String date, String content, String server, String detail, String executor, String status, String remark) { Map map = new HashMap<>(); map.put("no", no); map.put("date", date); map.put("content", content); map.put("server", server); map.put("detail", detail); map.put("executor", executor); map.put("status", status); map.put("remark", remark); return map; } private static Map createActionItem(String no, String content, String owner, String deadline) { Map map = new HashMap<>(); map.put("no", no); map.put("content", content); map.put("owner", owner); map.put("deadline", deadline); return map; } }