diff --git a/alert.go b/alert.go index a1b7bb0d11..c267317d9f 100644 --- a/alert.go +++ b/alert.go @@ -1121,6 +1121,7 @@ func (a *AlertNode) handleSnmpTrap(snmpTrap *pipeline.SnmpTrapHandler, ad *Alert } err := a.et.tm.SnmpTrapService.Alert( + snmpTrap.TrapOid, tmpDataList, ad.Level, ) diff --git a/pipeline/alert.go b/pipeline/alert.go index 1f2bc795ec..a6316912d4 100644 --- a/pipeline/alert.go +++ b/pipeline/alert.go @@ -1284,9 +1284,10 @@ type TalkHandler struct { // Send alerts to `target-ip:target-port` on OID '1.3.6.1.2.1.1.7' // // tick:property -func (a *AlertNode) SnmpTrap(dataList ...[]interface{}) *SnmpTrapHandler { +func (a *AlertNode) SnmpTrap(trapOid string, dataList ...[]interface{}) *SnmpTrapHandler { snmpTrap := &SnmpTrapHandler{ AlertNode: a, + TrapOid: trapOid, DataList: dataList, } a.SnmpTrapHandlers = append(a.SnmpTrapHandlers, snmpTrap) @@ -1298,6 +1299,7 @@ func (a *AlertNode) SnmpTrap(dataList ...[]interface{}) *SnmpTrapHandler { type SnmpTrapHandler struct { *AlertNode + TrapOid string // List of trap data. // tick:ignore DataList [][]interface{} `tick:"Data"` @@ -1317,7 +1319,7 @@ type SnmpTrapHandler struct { //{{ .Message }} //Value: {{ index .Fields "value" }} //''') -// .snmpTrap() +// .snmpTrap('1.3.6.1.4.1.1') // .data('1.3.6.1.4.1.1.5', 's', '{{ .Level }}' ) // .data('1.3.6.1.4.1.1.6', 's', '50' ) // .data('1.3.6.1.4.1..1.7', 's', '{{ index .Fields "used_percent" }}' ) diff --git a/services/snmptrap/service.go b/services/snmptrap/service.go index eb9aaf8611..be245a441b 100644 --- a/services/snmptrap/service.go +++ b/services/snmptrap/service.go @@ -42,7 +42,7 @@ func (s *Service) StateChangesOnly() bool { return s.stateChangesOnly } -func (s *Service) Alert(dataList [][]interface{}, level kapacitor.AlertLevel) error { +func (s *Service) Alert(trapOid string, dataList [][]interface{}, level kapacitor.AlertLevel) error { // SNMP target address address := s.targetIp + ":" + strconv.Itoa(s.targetPort) // SNMP version @@ -66,47 +66,52 @@ func (s *Service) Alert(dataList [][]interface{}, level kapacitor.AlertLevel) er }) var varBinds snmpgo.VarBinds + // Add trap oid + varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSysUpTime, snmpgo.NewTimeTicks(1000))) + oid, _ := snmpgo.NewOid(trapOid) + varBinds = append(varBinds, snmpgo.NewVarBind(snmpgo.OidSnmpTrap, oid)) + // Add Data for _, data := range dataList { - oid_str := data[0].(string) - oid_type_raw := data[1].(string) - oid, _ := snmpgo.NewOid(oid_str) + oidStr := data[0].(string) + oidTypeRaw := data[1].(string) + oid, _ := snmpgo.NewOid(oidStr) // http://docstore.mik.ua/orelly/networking_2ndEd/snmp/ch10_03.htm - switch oid_type_raw { + switch oidTypeRaw { case "a": return errors.New("Snmptrap Datatype 'IP address' not supported") case "c": - oid_value, err := strconv.ParseInt(data[2].(string), 10, 64) + oidValue, err := strconv.ParseInt(data[2].(string), 10, 64) if err != nil { return err } - varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewCounter64(uint64(oid_value)))) + varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewCounter64(uint64(oidValue)))) case "d": return errors.New("Snmptrap Datatype 'Decimal string' not supported") case "i": - oid_value, err := strconv.ParseInt(data[2].(string), 10, 64) + oidValue, err := strconv.ParseInt(data[2].(string), 10, 64) if err != nil { return err } - varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewInteger(int32(oid_value)))) + varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewInteger(int32(oidValue)))) case "n": varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewNull())) case "o": return errors.New("Snmptrap Datatype 'Object ID' not supported") case "s": - oid_value := []byte(data[2].(string)) - varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewOctetString(oid_value))) + oidValue := []byte(data[2].(string)) + varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewOctetString(oidValue))) case "t": - oid_value, err := strconv.ParseInt(data[2].(string), 10, 64) + oidValue, err := strconv.ParseInt(data[2].(string), 10, 64) if err != nil { return err } - varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewTimeTicks(uint32(oid_value)))) + varBinds = append(varBinds, snmpgo.NewVarBind(oid, snmpgo.NewTimeTicks(uint32(oidValue)))) case "u": return errors.New("Snmptrap Datatype 'Unsigned integer' not supported") case "x": return errors.New("Snmptrap Datatype 'Hexadecimal string' not supported") default: - return errors.New("Snmptrap Datatype not supported: " + oid_type_raw) + return errors.New("Snmptrap Datatype not supported: " + oidTypeRaw) } } diff --git a/task_master.go b/task_master.go index 82870fa84c..10fe7509a1 100644 --- a/task_master.go +++ b/task_master.go @@ -84,7 +84,7 @@ type TaskMaster struct { } SnmpTrapService interface { StateChangesOnly() bool - Alert(dataList [][]interface{}, level AlertLevel) error + Alert(trapOid string, dataList [][]interface{}, level AlertLevel) error } TelegramService interface { Global() bool