fix: require trap rule before alerting

This commit is contained in:
zxr
2026-07-18 10:21:25 +08:00
parent b07661383a
commit 6d5f680386

View File

@@ -498,21 +498,7 @@ func (e *Engine) HandleTrap(addr *net.UDPAddr, pkt *gosnmp.SnmpPacket) {
rules := e.trapRules
e.mu.RUnlock()
var matched *models.TrapRule
for i := range rules {
if trapRuleMatches(&rules[i], trapOID, fp) {
matched = &rules[i]
break
}
}
if matched == nil && dict != nil && strings.TrimSpace(dict.SeverityCode) != "" {
matched = &models.TrapRule{
AlertName: firstNonEmpty(firstNonEmpty(dict.Name, dict.Title), "SNMP Trap"),
SeverityCode: dict.SeverityCode,
PolicyID: 0,
}
}
matched := firstMatchingTrapRule(rules, trapOID, fp)
if matched == nil {
rawBytes, mErr := json.Marshal(fp)
if mErr != nil {
@@ -677,6 +663,15 @@ func trapRuleMatches(rule *models.TrapRule, trapOID, varbindFP string) bool {
return true
}
func firstMatchingTrapRule(rules []models.TrapRule, trapOID, varbindFP string) *models.TrapRule {
for i := range rules {
if trapRuleMatches(&rules[i], trapOID, varbindFP) {
return &rules[i]
}
}
return nil
}
func firstNonEmpty(a, b string) string {
if strings.TrimSpace(a) != "" {
return a