feat: 监控架构优化 — 模型层 + Prometheus/Zabbix/SNMP pkg + Dockerfile升级Go1.24

整合 /srv/监控架构优化方案 核心代码:

后端模型(4个新文件):
- monitor.go: MonitorTarget(统一监控目标) + MonitorMetric(指标定义) + MetricValue(时序数据) + MetricHistory
- snmp.go: SNMPDevice + SNMPTemplate + OID模板管理
- site.go: SiteTask(拨测任务) + SiteResult(拨测结果)
- alert_v2.go: 增强告警(多条件规则/模板/升级策略/静默)

后端 pkg(3个包):
- pkg/prometheus/client.go (531行): PromQL查询/Range/Labels/Rules
- pkg/zabbix/client.go: Zabbix API (Host/Item/Trigger/History)
- pkg/snmp/collector.go (621行): gosnmp Walk/BulkWalk/自动发现

基础设施:
- Dockerfile: Go 1.21→1.24 (gosnmp依赖)
- Agent安装弹框: SSH一键安装Tab + server上下文
- 服务器列表操作栏: 安装按钮条件显示/隐藏

Handler层因类型不兼容暂时跳过,模型和客户端包已编译通过。
This commit is contained in:
attacker committed 2026-06-18 11:52:10 +00:00
1 parent f3a9e5e8d0
commit 24fa17c2a8
13 files changed
+3427 -98

No files matched your search

+355
View File
@@ -0,0 +1,355 @@
package models
import (
"time"
)
// ============================================
// MonitorTarget 监控目标(主机/中间件/设备/站点)
// ============================================
// TargetType 监控目标类型
type TargetType string
const (
TargetTypeHost TargetType = "host" // 主机
TargetTypeMiddleware TargetType = "middleware" // 中间件
TargetTypeDevice TargetType = "device" // 网络设备
TargetTypeSite TargetType = "site" // 站点
TargetTypeDatabase TargetType = "database" // 数据库
TargetTypeContainer TargetType = "container" // 容器
)
// TargetStatus 监控目标状态
type TargetStatus string
const (
TargetStatusOnline TargetStatus = "online" // 在线
TargetStatusOffline TargetStatus = "offline" // 离线
TargetStatusUnknown TargetStatus = "unknown" // 未知
)
// MonitorTarget 监控目标表
// 存储所有被监控的对象,包括主机、中间件、网络设备、站点等
type MonitorTarget struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(128);not null;index;comment:目标名称" json:"name"`
Type TargetType `gorm:"type:varchar(32);not null;index;comment:目标类型" json:"type"`
Status TargetStatus `gorm:"type:varchar(16);default:'unknown';index;comment:状态" json:"status"`
IP string `gorm:"type:varchar(64);comment:IP地址" json:"ip,omitempty"`
Port int `gorm:"default:0;comment:端口" json:"port,omitempty"`
AgentID string `gorm:"type:varchar(64);index;comment:Agent标识" json:"agent_id,omitempty"`
OS string `gorm:"type:varchar(64);comment:操作系统" json:"os,omitempty"`
OSVersion string `gorm:"type:varchar(64);comment:OS版本" json:"os_version,omitempty"`
Arch string `gorm:"type:varchar(16);comment:系统架构" json:"arch,omitempty"`
Kernel string `gorm:"type:varchar(64);comment:内核版本" json:"kernel,omitempty"`
HostGroupID uint `gorm:"index;comment:主机组ID" json:"host_group_id,omitempty"`
Tags string `gorm:"type:varchar(512);comment:标签JSON" json:"tags,omitempty"`
CollectMode string `gorm:"type:varchar(16);default:'agent';comment:采集模式:agent/snmp/api" json:"collect_mode"`
SNMPConfig string `gorm:"type:text;comment:SNMP配置JSON" json:"snmp_config,omitempty"`
APIConfig string `gorm:"type:text;comment:API配置JSON" json:"api_config,omitempty"`
Address string `gorm:"type:varchar(255);comment:监控地址(IP或域名)" json:"address,omitempty"`
Protocol string `gorm:"type:varchar(32);comment:协议类型(http/https/tcp/udp/icmp/snmp)" json:"protocol,omitempty"`
Timeout int `gorm:"default:30;comment:超时时间(秒)" json:"timeout,omitempty"`
Config string `gorm:"type:text;comment:通用配置JSON" json:"config,omitempty"`
Interval int `gorm:"default:60;comment:采集间隔(秒)" json:"interval"`
Labels string `gorm:"type:varchar(512);comment:标签JSON" json:"labels,omitempty"`
Description string `gorm:"type:varchar(256);comment:描述" json:"description,omitempty"`
CreatedAt time.Time `gorm:"not null;index;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
DeletedAt *time.Time `gorm:"index;comment:删除时间" json:"deleted_at,omitempty"`
LastSeenAt *time.Time `gorm:"index;comment:最后在线时间" json:"last_seen_at,omitempty"`
// 关联关系
HostGroup *HostGroup `gorm:"foreignKey:HostGroupID" json:"host_group,omitempty"`
}
// TableName 指定表名
func (MonitorTarget) TableName() string {
return "monitor_targets"
}
// ============================================
// MonitorMetric 监控指标定义
// ============================================
// MetricType 指标类型
type MetricType string
const (
MetricTypeGauge MetricType = "gauge" // 瞬时值
MetricTypeCounter MetricType = "counter" // 计数器
MetricTypeSummary MetricType = "summary" // 摘要
)
// DataSourceType 数据来源类型
type DataSourceType string
const (
DataSourceAgent DataSourceType = "agent" // Agent采集
DataSourceSNMP DataSourceType = "snmp" // SNMP采集
DataSourceAPI DataSourceType = "api" // API采集
DataSourcePrometheus DataSourceType = "prometheus" // Prometheus
DataSourceZabbix DataSourceType = "zabbix" // Zabbix
)
// MonitorMetric 监控指标定义表
// 定义系统中所有可采集的监控指标元数据
type MonitorMetric struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(128);not null;uniqueIndex:uk_metric_name;comment:指标名称" json:"name"`
DisplayName string `gorm:"type:varchar(128);not null;comment:显示名称" json:"display_name"`
Description string `gorm:"type:varchar(256);comment:指标描述" json:"description,omitempty"`
MetricType MetricType `gorm:"type:varchar(16);not null;comment:指标类型: gauge/counter/summary" json:"metric_type"`
Unit string `gorm:"type:varchar(32);comment:单位" json:"unit,omitempty"`
DataSource DataSourceType `gorm:"type:varchar(16);not null;comment:数据来源" json:"data_source"`
CollectExpr string `gorm:"type:varchar(256);comment:采集表达式/OID/PromQL" json:"collect_expr,omitempty"`
Interval int `gorm:"default:60;comment:采集间隔(秒)" json:"interval"`
Labels string `gorm:"type:varchar(512);comment:指标标签定义JSON" json:"labels,omitempty"`
ValueType string `gorm:"type:varchar(16);default:'float64';comment:值类型" json:"value_type"`
Category string `gorm:"type:varchar(64);comment:指标分类" json:"category,omitempty"`
IsDefault bool `gorm:"default:true;comment:是否默认启用" json:"is_default"`
IsInternal bool `gorm:"default:false;comment:是否内置指标" json:"is_internal"`
Status int `gorm:"default:1;comment:状态: 1启用 0禁用" json:"status"`
Icon string `gorm:"type:varchar(64);comment:图标" json:"icon,omitempty"`
SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
DeletedAt *time.Time `gorm:"index;comment:删除时间" json:"deleted_at,omitempty"`
}
// TableName 指定表名
func (MonitorMetric) TableName() string {
return "monitor_metrics"
}
// ============================================
// MetricValue 指标值(时序数据)
// ============================================
// MetricValue 指标值表(按时间分区的时序数据)
// 存储采集到的实际指标数值,支持高并发写入和时序查询
type MetricValue struct {
ID uint64 `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
MetricID uint `gorm:"not null;index:idx_metric_time;comment:指标ID" json:"metric_id"`
TargetID uint `gorm:"not null;index:idx_target_metric;comment:目标ID" json:"target_id"`
Instance string `gorm:"type:varchar(128);index;comment:实例标识" json:"instance,omitempty"`
Value float64 `gorm:"not null;comment:指标值" json:"value"`
RawValue string `gorm:"type:text;comment:原始值(用于字符串类型)" json:"raw_value,omitempty"`
Labels string `gorm:"type:varchar(512);index;comment:标签JSON" json:"labels,omitempty"`
Timestamp int64 `gorm:"not null;index:idx_metric_time;comment:时间戳(秒)" json:"timestamp"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
// 关联关系
Metric *MonitorMetric `gorm:"foreignKey:MetricID" json:"metric,omitempty"`
Target *MonitorTarget `gorm:"foreignKey:TargetID" json:"target,omitempty"`
}
// TableName 指定表名
func (MetricValue) TableName() string {
return "metric_values"
}
// MetricHistory 指标历史数据(简化版,供监控查询使用)
type MetricHistory struct {
ID uint64 `gorm:"primaryKey;autoIncrement" json:"id"`
TargetID uint `gorm:"index;not null" json:"target_id"`
MetricName string `gorm:"type:varchar(128);index" json:"metric_name"`
Value float64 `json:"value"`
Timestamp int64 `gorm:"index;not null" json:"timestamp"`
}
func (MetricHistory) TableName() string {
return "metric_histories"
}
// MetricValueQuery 指标值查询参数
type MetricValueQuery struct {
MetricID uint `json:"metric_id" form:"metric_id"`
TargetID uint `json:"target_id" form:"target_id"`
Instance string `json:"instance" form:"instance"`
StartTime int64 `json:"start_time" form:"start_time"`
EndTime int64 `json:"end_time" form:"end_time"`
Step int `json:"step" form:"step"` // 聚合步长(秒)
Limit int `json:"limit" form:"limit"`
}
// MetricAggResult 指标聚合结果
type MetricAggResult struct {
Timestamp int64 `json:"timestamp"`
Value float64 `json:"value"`
Count int64 `json:"count,omitempty"`
Min float64 `json:"min,omitempty"`
Max float64 `json:"max,omitempty"`
Avg float64 `json:"avg,omitempty"`
}
// ============================================
// MetricTag 指标标签
// ============================================
// MetricTag 指标标签定义表
// 定义指标维度标签,用于多维数据分析和过滤
type MetricTag struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
MetricID uint `gorm:"not null;index:idx_metric_tag;comment:指标ID" json:"metric_id"`
TagKey string `gorm:"type:varchar(64);not null;index:idx_metric_tag;comment:标签键" json:"tag_key"`
TagValue string `gorm:"type:varchar(128);index;comment:标签值" json:"tag_value"`
Description string `gorm:"type:varchar(128);comment:描述" json:"description,omitempty"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
}
// TableName 指定表名
func (MetricTag) TableName() string {
return "metric_tags"
}
// ============================================
// MonitorDashboard 监控大屏配置
// ============================================
// DashboardType 大屏类型
type DashboardType string
const (
DashboardTypeSystem DashboardType = "system" // 系统内置
DashboardTypeCustom DashboardType = "custom" // 自定义
DashboardTypeShared DashboardType = "shared" // 共享
)
// MonitorDashboard 监控大屏配置表
// 定义监控大屏的元数据配置
type MonitorDashboard struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(128);not null;comment:大屏名称" json:"name"`
Type DashboardType `gorm:"type:varchar(16);default:'custom';comment:大屏类型" json:"type"`
Description string `gorm:"type:varchar(256);comment:描述" json:"description,omitempty"`
Layout string `gorm:"type:text;comment:布局配置JSON" json:"layout,omitempty"`
Theme string `gorm:"type:varchar(32);default:'dark';comment:主题: dark/light" json:"theme"`
RefreshRate int `gorm:"default:30;comment:自动刷新间隔(秒)" json:"refresh_rate"`
IsDefault bool `gorm:"default:false;comment:是否默认大屏" json:"is_default"`
IsPublic bool `gorm:"default:false;comment:是否公开" json:"is_public"`
CreatedBy uint `gorm:"index;comment:创建人ID" json:"created_by"`
Status int `gorm:"default:1;comment:状态: 1启用 0禁用" json:"status"`
SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
DeletedAt *time.Time `gorm:"index;comment:删除时间" json:"deleted_at,omitempty"`
// 关联关系
Panels []DashboardPanel `gorm:"foreignKey:DashboardID" json:"panels,omitempty"`
}
// TableName 指定表名
func (MonitorDashboard) TableName() string {
return "monitor_dashboards"
}
// ============================================
// DashboardPanel 大屏面板
// ============================================
// PanelType 面板类型
type PanelType string
const (
PanelTypeChart PanelType = "chart" // 图表
PanelTypeGauge PanelType = "gauge" // 仪表盘
PanelTypeCounter PanelType = "counter" // 计数器
PanelTypeTable PanelType = "table" // 表格
PanelTypeStatus PanelType = "status" // 状态
PanelTypeTopology PanelType = "topology" // 拓扑
PanelTypeLog PanelType = "log" // 日志
PanelTypeAlertList PanelType = "alert_list" // 告警列表
PanelTypeHeatmap PanelType = "heatmap" // 热力图
PanelTypeText PanelType = "text" // 文本
)
// ChartType 图表类型
type ChartType string
const (
ChartTypeLine ChartType = "line" // 折线
ChartTypeBar ChartType = "bar" // 柱状
ChartTypeArea ChartType = "area" // 面积
ChartTypePie ChartType = "pie" // 饼图
ChartTypeScatter ChartType = "scatter" // 散点
)
// DashboardPanel 大屏面板配置表
// 定义监控大屏中的各个面板配置
type DashboardPanel struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
DashboardID uint `gorm:"not null;index;comment:大屏ID" json:"dashboard_id"`
Title string `gorm:"type:varchar(128);not null;comment:面板标题" json:"title"`
Description string `gorm:"type:varchar(256);comment:描述" json:"description,omitempty"`
PanelType PanelType `gorm:"type:varchar(16);not null;comment:面板类型" json:"panel_type"`
ChartType ChartType `gorm:"type:varchar(16);comment:图表类型" json:"chart_type,omitempty"`
// 位置布局配置
X int `gorm:"default:0;comment:网格X坐标" json:"x"`
Y int `gorm:"default:0;comment:网格Y坐标" json:"y"`
Width int `gorm:"default:6;comment:宽度(网格列数)" json:"width"`
Height int `gorm:"default:4;comment:高度(网格行数)" json:"height"`
// 数据源配置
DataSource string `gorm:"type:varchar(16);comment:数据源类型" json:"data_source,omitempty"`
MetricID uint `gorm:"comment:关联指标ID" json:"metric_id,omitempty"`
TargetID uint `gorm:"comment:关联目标ID" json:"target_id,omitempty"`
QueryExpr string `gorm:"type:text;comment:查询表达式" json:"query_expr,omitempty"`
QueryConfig string `gorm:"type:text;comment:查询配置JSON" json:"query_config,omitempty"`
// 展示配置
TimeRange string `gorm:"type:varchar(16);default:'1h';comment:时间范围" json:"time_range"`
RefreshRate int `gorm:"default:0;comment:刷新间隔(秒),0表示继承大屏" json:"refresh_rate"`
Thresholds string `gorm:"type:text;comment:阈值配置JSON" json:"thresholds,omitempty"`
Colors string `gorm:"type:text;comment:颜色配置JSON" json:"colors,omitempty"`
Legend string `gorm:"type:varchar(64);comment:图例配置" json:"legend,omitempty"`
Unit string `gorm:"type:varchar(32);comment:显示单位" json:"unit,omitempty"`
Decimals int `gorm:"default:2;comment:小数位数" json:"decimals"`
// 状态
Status int `gorm:"default:1;comment:状态: 1启用 0禁用" json:"status"`
SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
}
// TableName 指定表名
func (DashboardPanel) TableName() string {
return "dashboard_panels"
}
// PanelQueryConfig 面板查询配置
type PanelQueryConfig struct {
Metrics []string `json:"metrics"` // 指标名称列表
Targets []uint `json:"targets"` // 目标ID列表
Instances []string `json:"instances"` // 实例列表
Aggregations []string `json:"aggregations"` // 聚合函数
GroupBy []string `json:"group_by"` // 分组标签
Filters map[string]string `json:"filters"` // 过滤条件
Step int `json:"step"` // 步长
LegendFormat string `json:"legend_format"` // 图例格式
}
// ThresholdConfig 阈值配置
type ThresholdConfig struct {
Critical float64 `json:"critical"` // 严重
Warning float64 `json:"warning"` // 警告
Info float64 `json:"info"` // 提示
Unit string `json:"unit"` // 单位
}
// HostGroup 主机组
type HostGroup struct {
ID uint `gorm:"primaryKey;autoIncrement;comment:主键ID" json:"id"`
Name string `gorm:"type:varchar(128);not null;uniqueIndex;comment:组名称" json:"name"`
Description string `gorm:"type:varchar(256);comment:描述" json:"description,omitempty"`
ParentID uint `gorm:"index;comment:父组ID" json:"parent_id,omitempty"`
SortOrder int `gorm:"default:0;comment:排序" json:"sort_order"`
CreatedAt time.Time `gorm:"not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"not null;comment:更新时间" json:"updated_at"`
DeletedAt *time.Time `gorm:"index;comment:删除时间" json:"deleted_at,omitempty"`
}
// TableName 指定表名
func (HostGroup) TableName() string {
return "host_groups"
}