83 lines
2.0 KiB
Go
83 lines
2.0 KiB
Go
package controllers
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"git.apinb.com/bsm-sdk/core/infra"
|
|
"git.apinb.com/ops/logs/internal/impl"
|
|
"git.apinb.com/ops/logs/internal/models"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type trapShieldOption struct {
|
|
OIDPrefix string `json:"oid_prefix"`
|
|
Name string `json:"name"`
|
|
Vendor string `json:"vendor"`
|
|
Source string `json:"source"`
|
|
}
|
|
|
|
// ListTrapShieldOptions 返回 Trap 字典和已接收 Trap 中可用于屏蔽配置的类型。
|
|
func ListTrapShieldOptions(ctx *gin.Context) {
|
|
var dictionary []models.TrapDictionaryEntry
|
|
if err := impl.DBService.Order("vendor asc, name asc, id asc").Find(&dictionary).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
|
|
options := make([]trapShieldOption, 0, len(dictionary))
|
|
seen := make(map[string]struct{}, len(dictionary))
|
|
for _, entry := range dictionary {
|
|
oid := normalizeTrapOIDInput(entry.OID)
|
|
if oid == "" {
|
|
oid = normalizeTrapOIDInput(entry.OIDPrefix)
|
|
}
|
|
if oid == "" {
|
|
continue
|
|
}
|
|
if _, exists := seen[oid]; exists {
|
|
continue
|
|
}
|
|
seen[oid] = struct{}{}
|
|
name := strings.TrimSpace(entry.Name)
|
|
if name == "" {
|
|
name = strings.TrimSpace(entry.Title)
|
|
}
|
|
if name == "" {
|
|
name = oid
|
|
}
|
|
options = append(options, trapShieldOption{
|
|
OIDPrefix: oid,
|
|
Name: name,
|
|
Vendor: strings.TrimSpace(entry.Vendor),
|
|
Source: "dictionary",
|
|
})
|
|
}
|
|
|
|
var receivedOIDs []string
|
|
if err := impl.DBService.Model(&models.LogEvent{}).
|
|
Where("source_kind = ? AND trap_o_id <> ?", "snmp_trap", "").
|
|
Distinct("trap_o_id").
|
|
Order("trap_o_id asc").
|
|
Pluck("trap_o_id", &receivedOIDs).Error; err != nil {
|
|
infra.Response.Error(ctx, err)
|
|
return
|
|
}
|
|
for _, rawOID := range receivedOIDs {
|
|
oid := normalizeTrapOIDInput(rawOID)
|
|
if oid == "" {
|
|
continue
|
|
}
|
|
if _, exists := seen[oid]; exists {
|
|
continue
|
|
}
|
|
seen[oid] = struct{}{}
|
|
options = append(options, trapShieldOption{
|
|
OIDPrefix: oid,
|
|
Name: "已接收 Trap",
|
|
Source: "received",
|
|
})
|
|
}
|
|
|
|
infra.Response.Success(ctx, gin.H{"items": options})
|
|
}
|