文章目录
- 前言
- 服务启动时序图
- 一、项目架构与技术选型
- 项目背景
- 核心思想
- 实现过程简述
- 1. 技术栈
- 2.目录结构
- 主要功能
- 1. 配置热加载与参数解析
- 2. 巡检任务与业务逻辑
- 3. 报告生成与美化
- 4. 日志系统与统一输出
- 三、使用说明
- 1、配置文件
- 2.快速使用
- 6.巡检日志示例
- 7.最终效果展示
前言
在企业 IT 运维场景中,定期巡检服务器、数据库、中间件、K8S 集群等基础设施的健康状态,是保障业务稳定运行的关键环节,以及重要业务接口都是保障业务稳定运行的关键。但传统的人工巡检效率低、易遗漏,而且人工巡检时间常,因此诞生了自动化巡检系统。本文将详细介绍一个基于 Go 语言开发的运维巡检系统(opsxj),并结合源码解析其架构设计、核心功能与工程实践。
服务启动时序图
一、项目架构与技术选型
项目背景
OpsXJ 是一款面向企业级 IT 运维场景的自动化巡检工具,支持多类型资源的健康巡检、异常检测、趋势分析,
并可一键生成结构化的 PDF/HTML 巡检报告。
平台适用于服务器、中间件、应用、备份、域名、SSL证书、K8S 等多种运维对象,极大提升了运维效率和规范性。
核心思想
- 自动化:通过配置驱动,自动批量巡检各类 IT 资源,减少人工干预。
- 结构化:巡检结果统一结构化输出,便于分析、归档和报告生成。
- 可视化:自动生成趋势图、得分表、异常明细,报告一目了然。
- 一键报告:支持 HTML/PDF 格式报告导出,满足审计和管理需求。
实现过程简述
- 配置驱动:所有巡检对象、阈值、账号等均通过
config.yaml
配置,灵活扩展。 - 巡检执行:后端基于 Go 并发调度,自动批量巡检所有配置资源,聚合结果。
- 趋势与历史:每日巡检后自动追加历史数据(如
server_alarm_history.json
),用于趋势图生成。 - 报告生成:
- HTML 报告:
/api/v1/inspect/report/html
,结构化展示所有巡检内容、趋势图、得分、异常明细等。 - PDF 报告:
/api/v1/inspect/report/pdf
,自动调用 wkhtmltopdf 工具将 HTML 转为 PDF,支持中文字体。
- HTML 报告:
- 接口开放:支持 RESTful API 调用,便于集成自动化运维流程。
1. 技术栈
后端语言:Go1.23.7(Golang)
Web 框架:Gin
配置管理:Viper
命令行工具:Cobra
日志系统:Zap
K8S 客户端:官方 client-go
前端报告:HTML/CSS 动态生成
依赖管理:Go Modules + Vendor
2.目录结构
cmd/
启动入口与服务初始化api/
路由与接口实现internal/
核心业务逻辑与巡检实现pkg/
工具库、报告生成、日志等static/
静态资源(趋势图、PDF等)config.yaml
配置文件middleware_info.txt
中间件信息文件server_info.txt
服务器信息文件# 二、核心功能与实现
主要功能
-
多资源类型巡检
- 支持服务器、应用系统、中间件、备份、域名、SSL证书、K8S等多类型资源的健康巡检。
- 支持端口连通性、资源阈值(CPU/内存/磁盘)、服务状态、证书/域名到期等多维度检查。
-
自动趋势分析
- 自动记录每日告警历史,生成 CPU/内存/磁盘等趋势图,辅助容量与风险分析。
-
结构化报告生成
- 自动生成分组、带表头的巡检明细表格。
- 支持 HTML 在线浏览和 PDF 一键导出,报告内容完整、格式美观,支持中文。
-
异常与建议输出
- 自动归纳异常项,输出针对性的运维建议,便于快速定位和处理问题。
1. 配置热加载与参数解析
使用 Viper 统一管理配置,支持 YAML 格式,支持热加载(WatchConfig)。
通过 Cobra 实现命令行参数解析,支持 --config 等自定义参数,便于灵活部署。
2. 巡检任务与业务逻辑
K8S 集群巡检:通过 client-go 获取 Pod、Node、Service 状态,支持与 kubectl get pod 一致的详细状态判断。
应用接口巡检:支持 GET/POST 请求,校验响应码、内容、JSON 字段等,适配登录接口、Token 校验等场景。
MySQL 主从巡检:动态解析 SHOW SLAVE STATUS 字段,兼容不同 MySQL 版本,重点关注 Seconds_Behind_Master。
MySQL备份巡检:支持多种日志文件名模式,自动匹配前一天的备份文件,灵活适配实际运维需求。
服务器基础资源巡检:支持设置阈值以及被巡检的磁盘名称,超过阈值则记录并再pdf巡检报告中体现.
中间件服务巡检:通过socket的方式轻松探测端口是否存在
公网域名巡检:通过golang中的whois库实现对域名过期时间巡检
SSL证书巡检:解析证书对应的域名,实现获取ssl证书的过期时间
3. 报告生成与美化
动态生成 HTML 巡检报告,采用卡片式布局,分区展示巡检内容与结果。
折线图(CPU/内存/磁盘)美观优化,区分颜色、加粗线条、标注数据点。
表格自适应宽度,长错误信息自动换行,提升可读性。
4. 日志系统与统一输出
业务日志与 Gin 访问日志统一采用 Zap,输出 JSON 格式,便于后续分析与检索。
支持日志文件分割与归档,保障日志安全与可追溯性。
三、使用说明
1、配置文件
config.yaml是服务启动的主配置文件,包含了服务启动端口、生成的pdf名称、巡检人名称、服务器资源阈值配置、数据库备份巡检配置、k8s巡检配置、应用接口巡检配置、域名巡检配置、SSL证书巡检配置、日志保留天数配置
如果要取消某一项巡检,则将其置空就行,参考下方的DOMAIN和SSL_CERT
APPLICATION:- object_name : 'test1系统' # 应用名1url : 'https://xxxx.xxxx/getList?startTime=&endTime=&pageSize=10&pageNo=1' # 应用url1code : 200 #状态码method : 'GET' # 新增headers:xx : xx- object_name : 'test2系统' # 应用名3url : 'http://10.10.10.13:34//sysUser/listData' # 应用url3code : 200 # 应用名3method : 'POST' # 新增token : 'TOKEN' # 应用token3content_type : 'application/json' # 新增body : '{"name":"张三"}' # 新增headers :X-Request-Id: 'abc123'User-Agent: 'opsxj-checker/1.0'response_result: '{"code":200,"message":"success"}'RESOURCE:types:- 'cpu' - 'memory'- 'disk'cpu_threshold: 80memory_threshold: 80disk_threshold: 80mounted_on: ['/export','/']BACKUP:- object_name : 'mysql1' # 数据库名称type : 'Mysql' # 数据库类型instance_host : '11.137.20.8' # 实例地址instance_port : '3306' # 实例端口ssh_user : 'root' # ssh用户ssh_port : 22 # ssh端口ssh_password : '' # ssh密码# full_backup_3306_202506182200*.loginstance_backup_log : 'data/mybackup/my${instance_port}/full_backup_${instance_port}_${date8}${hour}${minute}*.log'# mysql_backup_3306_250618_22*.log#instance_backup_log : 'data/mybackup/my${instance_port}/mysql_backup_${instance_port}_${date6}_${hour}*.log'backup_time: '22:00'log_keywords:- 'completed OK!'- 'end tar successfull'DOMAIN:- object_name : '' # 名称domain : '' # 域名- object_name : '' # 名称domain : '' # 域名SSL_CERT:- object_name : '' # 证书名称cert_domain : '' # 证书对应的二级域名K8S:- object_name : 'k8s集群' # 名称type : 'K8S' # 类型kubeconfig : '/root/.kube/config' # kubeconfig文件路径master_host : '10.17.10.8' # 主节点地址ssh_port : 22 # ssh端口ssh_user : 'root' # ssh用户ssh_password : '' # ssh密码LOG:level: 'DEBUG'filename: 'inspect.log'max_size: 100 # 100MBmax_age: 10 # 保留10个备份max_backups: 10 # 最多保留10个备份DEFAULT:listenAddr: ":8080"concurrency: 10 #并发10data_name: '巡检报告.pdf'project_name: 'test项目'inspector_name: '张三' # 巡检人
server_info.txt文件主要用于保存服务器信息,模块名称不变就为all即可
[all]
# 服务器名称 ip ssh端口 用户名 密码
hbase_server_3 172.16.1.1 22 root password
hbase_server_3 172.16.1.1 23 root password
middleware_info.txt用户保存中间件信息,支持多个模块。符合ansible格式标准
//TODO 中间件巡检只需要名称、ip、port 目前加载用户名和密码是为了解决检查数据统一使用
[mysql]
# 名称 IP 端口 数据库用户名 数据库密码 用于检查主从
mysql1 11.16.1.1 3306 root password
mysql2 11.16.1.2 3306 root password
mysql3 11.16.1.3 3306 root password[postgresql]
pg1 11.17.55.86 5432 root password
pg2 11.17.55.87 5432 root password
pg3 11.17.55.88 5432 root password[nginx]
#其余密码随便写
nginx1 17.27.55.86 4323 root password
nginx2 17.27.55.87 5432 root password[bigdata_hdfs]
hdfs1 12.27.55.86 9873 root password
hdfs2 12.27.55.86 6785 root password
2.快速使用
-
安装依赖
- 安装wkhtmltopdf工具依赖 yum install -y xorg-x11-fonts-75dpi.noarch xorg-x11-fonts-Type1.noarch
- 安装 wkhtmltopdf(用于 HTML 转 PDF)
- 服务器需安装中文字体(如 SimSun.ttf、YaheiMono.ttf)
- cd /usr/share/fonts/ mkdir simsun mkdir yaheimono 将SimSun.ttf YaheiMono.ttf上传到对应的目录下
-
配置资源 --放置同一目录下
- 编辑
config.yaml
,配置所有需要巡检的url、备份、证书、域名、k8s等信息。 - 编辑
middleware_info.txt
,配置所有需要巡检的中间件信息。 - 编辑
server_info.txt
,配置所有需要巡检的服务器信息。
- 编辑
-
启动服务并输出PDF巡检报告
第一种方式go run -mod=vendor cmd/main.go curl -OJ 'http://localhosts:8080/api/v1/inspect/report/pdf'
第二种方式
go build -o opsxj cmd/main.go ./opsxj --config=config.yaml --inspect # 启动服务并自动执行一次全量巡检并输出PDF报告
-
巡检与导出
- 浏览器访问 HTML 报告:
http://localhost:8080/api/v1/inspect/report/html
(默认巡检所有类型并以html形式展示再浏览器中) - 生成 PDF 报告:
curl -OJ http://localhost:8080/api/v1/inspect/report/pdf
(默认巡检所有类型并生成pdf巡检报告)
./opsxj --config=config.yaml --inspect
(启动服务并巡检所有类型生成pdf巡检报告) - 导出PDF报告
生成的pdf巡检报告位于同一目录下的static目录
- 浏览器访问 HTML 报告:
-
单项巡检(返回 对应的JSON格式巡检结果)
服务器巡检
curl http://localhost:8080/api/v1/inspect?type=server{"type": "服务器","total": 2,"normal": 0,"abnormal": 2,"details": [{"Abnormal": 2,"fail_detail": "服务器21.16.1.1 端口22检测失败\n服务器21.17.1.1 端口23检测失败","inspection_type": "服务器","normal": 0,"total": 2}]
}
应用接口巡检
curl http://localhost:8080/api/v1/inspect?type=application
{"type": "业务","total": 24,"normal": 0,"abnormal": 24,"details": [{"fail_detail": "context deadline exceeded (Client.Timeout exceeded while awaiting headers)","inspection_type": "APPLICATION","name": "test1系统","status": "异常","url": "https://12.28.24.87/getList?startTime=&endTime=&pageSize=10&pageNo=1"},{"fail_detail": "dial tcp: lookup 11.11.11.11: no such host","inspection_type": "APPLICATION","name": "test2系统","status": "异常","url": "http://11.11.11.11/api/share/b61e38d4f14f4"}]
中间件巡检
curl http://localhost:8080/api/v1/inspect?type=middleware
{"type": "服务器","total": 2,"normal": 0,"abnormal": 2,"details": [{"Abnormal": 2,"fail_detail": "服务器172.16.1.1 端口23检测失败\n服务器172.16.1.1 端口22检测失败","inspection_type": "服务器","normal": 0,"total": 2}]
}
k8s集群巡检
curl http://localhost:8080/api/v1/inspect?type=k8s{"type": "K8S","total": 3,"normal": 0,"abnormal": 3,"details": [{"Abnormal": 1,"fail_detail": "kubeconfig连接失败: CreateFile conf/kubeconfig.yaml: The system cannot find the path specified.","inspection_type": "K8S","name": "component","normal": 0,"total": 0},{"Abnormal": 1,"fail_detail": "kubeconfig连接失败: CreateFile conf/kubeconfig.yaml: The system cannot find the path specified.","inspection_type": "K8S","name": "pod","normal": 0,"total": 0},{"Abnormal": 1,"fail_detail": "kubeconfig连接失败: CreateFile conf/kubeconfig.yaml: The system cannot find the path specified.","inspection_type": "K8S","name": "certs","normal": 0,"total": 0}]
}
域名巡检
curl http://localhost:8080/api/v1/inspect?type=domain
{"type": "域名","total": 2,"normal": 2,"abnormal": 0,"details": [{"expire_time": "2028-10-11 11:05:17","fail_detail": "","inspection_type": "DOMAIN","is_renew": "N","name": "baidu.com"},{"expire_time": "2032-09-13 04:00:00","fail_detail": "","inspection_type": "DOMAIN","is_renew": "N","name": "tencent.com"}]
}
ssl证书巡检
curl http://localhost:8080/api/v1/inspect?type=ssl_cert{"type": "SSL证书","total": 1,"normal": 1,"abnormal": 0,"details": [{"expire_time": "2026-05-13 06:45:06","fail_detail": "","inspection_type": "SSL_CERT","is_renew": "N","name": "crxn.cn"}]
}
数据库备份巡检
curl http://localhost:8080/api/v1/inspect?type=backup
{"type": "Mysql备份","total": 1,"normal": 0,"abnormal": 1,"details": [{"Abnormal": 1,"fail_detail": "数据库11.137.20.8:3306 端口检测失败","inspection_type": "BACKUP","name": "mysql1","normal": 0,"total": 1}]
}
6.巡检日志示例
巡检日志为同一目录下的inspect.log
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"logger/zap.go:32","msg":"init logger success"}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":" ____ __ __ _ "}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":" / __ \\____ _____/ /_/ /_ __(_)___ _ "}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":" / / / / __ \\/ ___/ __/ / / / / / __ '/"}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":"/ /_/ / /_/ / / / /_/ / /_/ / / /_/ / "}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":"\\____/ .___/_/ \\__/_/\\__,_/_/\\__,_/ "}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"utils/logo.go:15","msg":" /_/ "}
{"level":"INFO","time":"2025-06-18 11:19:43","caller":"app/server.go:154","msg":"Success starting opsxj server running on"}
{"level":"INFO","time":"2025-06-18 11:20:08","caller":"ansible/parser.go:34","msg":"开始解析服务器配置文件: ./server_info.txt"}
{"level":"DEBUG","time":"2025-06-18 11:20:08","caller":"ansible/parser.go:59","msg":"发现模块: all"}
{"level":"INFO","time":"2025-06-18 11:20:08","caller":"ansible/parser.go:97","msg":"成功解析配置文件,共发现 2 个服务器"}
7.最终效果展示
生成的PDF文件
以上就是所有内容,目前已适用于所维护的项目中,后续有新功能会持续迭代代码。目前代码和工具暂不考虑开源,如有需要可私信~~支持定开