告警中心 - 新增统一告警(alert_unified)、监控告警规则(monitor_alert_rule)、指标目录(alert_metric_catalog) - 通知策略与消息分发(alert_notify_policy/dispatch/message)、静默规则 - 告警引擎按阈值/SNMP 拆分(alert_engine_monitor/snmp) 监控采集 - SNMP 常量/模板与历史指标(snmp templates/constants、snmp_metrics_history、snmp_collect) - 中间件监控 Redis 采集与种子(middleware_monitor_redis/seed) - 站点探测 pkg(siteprobe) + 站点视图(site_view) 自动化任务 - cron 调度器(cronscheduler)、脚本运行器(scriptrunner)、系统任务(systask)及运行记录(cron_job_run、system_task) 审计与凭证 - 审计设置(audit_setting) + 历史清理(auditcleaner) 前端 - 监控页面(host/middleware/site/snmp)拆分为子模块并大屏化 - 告警中心多 Tab(规则/指标目录/通知策略/静默)、任务管理组件重构、Agent CLI Markdown 渲染 - 移除旧脚本卡片/详情/日志组件,统一为 ScriptTable/ScriptCodeEditor/TerminalOutput Co-Authored-By: Claude <noreply@anthropic.com>
108 lines
3.1 KiB
Go
108 lines
3.1 KiB
Go
package siteprobe
|
|
|
|
import (
|
|
"context"
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"net"
|
|
"net/url"
|
|
"strings"
|
|
"time"
|
|
|
|
"ezops/model"
|
|
)
|
|
|
|
// parseSSLTarget 解析 SSL 拨测地址,支持 example.com:443 / https://example.com // Logan.Li
|
|
func parseSSLTarget(raw string) (host, port string, err error) {
|
|
raw = strings.TrimSpace(raw)
|
|
if raw == "" {
|
|
return "", "", fmt.Errorf("SSL 地址不能为空")
|
|
}
|
|
lower := strings.ToLower(raw)
|
|
// 兼容 https:example.com(单斜杠写法)
|
|
if strings.HasPrefix(lower, "https:") && !strings.HasPrefix(lower, "https://") {
|
|
raw = "https://" + strings.TrimSpace(raw[len("https:"):])
|
|
lower = strings.ToLower(raw)
|
|
}
|
|
if strings.HasPrefix(lower, "http://") || strings.HasPrefix(lower, "https://") || strings.HasPrefix(lower, "ssl://") {
|
|
u, parseErr := url.Parse(raw)
|
|
if parseErr != nil || u.Host == "" {
|
|
return "", "", fmt.Errorf("SSL 地址格式无效: %s", raw)
|
|
}
|
|
if h, p, splitErr := net.SplitHostPort(u.Host); splitErr == nil {
|
|
return h, p, nil
|
|
}
|
|
defPort := "443"
|
|
if u.Scheme == "http" {
|
|
defPort = "80"
|
|
}
|
|
return u.Hostname(), defPort, nil
|
|
}
|
|
// 去掉路径后缀,如 example.com:443/foo
|
|
if idx := strings.Index(raw, "/"); idx >= 0 {
|
|
raw = raw[:idx]
|
|
}
|
|
return splitHostPort(raw, "443")
|
|
}
|
|
|
|
// probeSSL 纯 TLS 握手检测证书(不发起 HTTP 请求)// Logan.Li
|
|
func probeSSL(ctx context.Context, task model.SiteTask) ProbeOutcome {
|
|
start := time.Now()
|
|
host, port, err := parseSSLTarget(task.URL)
|
|
if err != nil {
|
|
return failOutcome(start, err.Error())
|
|
}
|
|
addr := net.JoinHostPort(host, port)
|
|
d := &net.Dialer{Timeout: time.Until(deadlineFromCtx(ctx))}
|
|
conn, err := tls.DialWithDialer(d, "tcp", addr, &tls.Config{
|
|
ServerName: host,
|
|
InsecureSkipVerify: !task.VerifySSL,
|
|
})
|
|
elapsed := msSince(start)
|
|
if err != nil {
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
return ProbeOutcome{Status: model.SiteResultTimeout, TotalTime: elapsed, ResponseTime: elapsed, ErrorMessage: "TLS 握手超时"}
|
|
}
|
|
return failOutcome(start, fmt.Sprintf("TLS 握手失败: %s", err.Error()))
|
|
}
|
|
defer conn.Close()
|
|
|
|
remote := ""
|
|
if addr := conn.RemoteAddr(); addr != nil {
|
|
remote = addr.String()
|
|
}
|
|
out := ProbeOutcome{
|
|
Status: model.SiteResultSuccess,
|
|
ResponseTime: elapsed,
|
|
TotalTime: elapsed,
|
|
RemoteIP: remote,
|
|
}
|
|
applyCertsToOutcome(&out, conn.ConnectionState().PeerCertificates)
|
|
return out
|
|
}
|
|
|
|
// applyCertsToOutcome 从 TLS 证书链填充到期信息 // Logan.Li
|
|
func applyCertsToOutcome(out *ProbeOutcome, certs []*x509.Certificate) {
|
|
if len(certs) == 0 {
|
|
return
|
|
}
|
|
cert := certs[0]
|
|
expiry := cert.NotAfter
|
|
days := int(time.Until(expiry).Hours() / 24)
|
|
out.SSLCertExpiry = &expiry
|
|
out.SSLDaysRemaining = days
|
|
out.SSLCertIssuer = cert.Issuer.CommonName
|
|
if out.SSLCertIssuer == "" {
|
|
out.SSLCertIssuer = cert.Issuer.String()
|
|
}
|
|
out.SSLCertSubject = cert.Subject.CommonName
|
|
if out.SSLCertSubject == "" {
|
|
out.SSLCertSubject = cert.Subject.String()
|
|
}
|
|
}
|
|
|
|
// IsSSLProbeTask 是否为 SSL 证书拨测任务 // Logan.Li
|
|
func IsSSLProbeTask(task model.SiteTask) bool {
|
|
return strings.EqualFold(string(task.TaskType), "ssl")
|
|
} |