76 lines
2.7 KiB
Go
76 lines
2.7 KiB
Go
package controllers
|
||
|
||
import (
|
||
"encoding/json"
|
||
"fmt"
|
||
"regexp"
|
||
"strings"
|
||
|
||
"git.apinb.com/ops/logs/internal/models"
|
||
)
|
||
|
||
func validateSyslogRule(rule *models.SyslogRule) error {
|
||
regexFields := []struct {
|
||
name string
|
||
pattern string
|
||
}{
|
||
{name: "keyword_regex", pattern: rule.KeywordRegex},
|
||
{name: "message_regex", pattern: rule.MessageRegex},
|
||
{name: "resource_uid_extract_regex", pattern: rule.ResourceUIDExtractRegex},
|
||
}
|
||
for _, field := range regexFields {
|
||
if err := validateOptionalRegex(field.name, field.pattern); err != nil {
|
||
return err
|
||
}
|
||
}
|
||
|
||
if raw := strings.TrimSpace(rule.SeverityMappingJSON); raw != "" {
|
||
var mapping map[string]any
|
||
if err := json.Unmarshal([]byte(raw), &mapping); err != nil {
|
||
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象:%v;请修正 severity_mapping_json 后重试", err)
|
||
}
|
||
if mapping == nil {
|
||
return fmt.Errorf("severity_mapping_json 必须是字符串到字符串的 JSON 对象,不能是 null;请改为 JSON 对象后重试")
|
||
}
|
||
for pattern, severity := range mapping {
|
||
if _, ok := severity.(string); !ok {
|
||
return fmt.Errorf("severity_mapping_json 的映射键 %q 对应值必须是字符串;请修正该映射值后重试", pattern)
|
||
}
|
||
if _, err := regexp.Compile(pattern); err != nil {
|
||
return fmt.Errorf("severity_mapping_json 的映射键 %q 不是有效正则表达式:%v;请修正该映射键后重试", pattern, err)
|
||
}
|
||
}
|
||
}
|
||
|
||
if strings.TrimSpace(rule.DeviceNameContains) == "" &&
|
||
strings.TrimSpace(rule.SourceMatch) == "" &&
|
||
strings.TrimSpace(rule.KeywordRegex) == "" &&
|
||
strings.TrimSpace(rule.MessageRegex) == "" {
|
||
return fmt.Errorf("Syslog 规则的匹配条件全部为空,运行时永远不会命中;请至少填写 device_name_contains、source_match、keyword_regex、message_regex 中的一项")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validateTrapRule(rule *models.TrapRule) error {
|
||
if strings.TrimSpace(rule.VarbindMatchRegex) != "" {
|
||
if _, err := regexp.Compile(rule.VarbindMatchRegex); err != nil {
|
||
return fmt.Errorf("varbind_match_regex 不是有效正则表达式:%v;请修正 varbind_match_regex 后重试", err)
|
||
}
|
||
}
|
||
if strings.TrimSpace(rule.OIDPrefix) == "" && strings.TrimSpace(rule.VarbindMatchRegex) == "" {
|
||
return fmt.Errorf("Trap 规则的匹配条件全部为空,运行时永远不会命中;请至少填写 oid_prefix、varbind_match_regex 中的一项")
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func validateOptionalRegex(field, pattern string) error {
|
||
pattern = strings.TrimSpace(pattern)
|
||
if pattern == "" {
|
||
return nil
|
||
}
|
||
if _, err := regexp.Compile(pattern); err != nil {
|
||
return fmt.Errorf("%s 不是有效正则表达式:%v;请修正 %s 后重试", field, err, field)
|
||
}
|
||
return nil
|
||
}
|