Skip to content

Commit

Permalink
Add missing trap oid
Browse files Browse the repository at this point in the history
  • Loading branch information
titilambert committed Sep 28, 2016
1 parent 4a0e7b0 commit 28f0c16
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,7 @@ func (a *AlertNode) handleSnmpTrap(snmpTrap *pipeline.SnmpTrapHandler, ad *Alert
}

err := a.et.tm.SnmpTrapService.Alert(
snmpTrap.TrapOid,
tmpDataList,
ad.Level,
)
Expand Down
6 changes: 4 additions & 2 deletions pipeline/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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"`
Expand All @@ -1317,7 +1319,7 @@ type SnmpTrapHandler struct {
//<b>{{ .Message }}</b>
//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" }}' )
Expand Down
33 changes: 19 additions & 14 deletions services/snmptrap/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion task_master.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 28f0c16

Please sign in to comment.