diff --git a/sender/snmp.go b/sender/snmp.go index 2a445002..75fc9936 100644 --- a/sender/snmp.go +++ b/sender/snmp.go @@ -2,7 +2,6 @@ package sender import ( "fmt" - "reflect" "sync" "time" @@ -11,17 +10,21 @@ import ( ) type SNMP struct { - Port uint16 `doc:"Snmp port"` - Community string `doc:"Snmp communit field"` - Version string `doc:"Snmp version possible values: 2c, 3"` - Target string `doc:"Snmp target"` - Oidmap map[string]interface{} `doc:"Snmp oid to json field mapping"` - Timeout uint `doc:"Snmp timeout, default 5 seconds"` - r sync.Once - err error - g *gosnmp.GoSNMP + Port uint16 `doc:"Snmp port"` + Community string `doc:"Snmp communit field"` + Version string `doc:"Snmp version possible values: 2c, 3"` + Target string `doc:"Snmp target"` + Oidmap map[string]interface{} `doc:"Snmp oid to json field mapping"` + Timeout uint `doc:"Snmp timeout, default 5 seconds"` + r sync.Once + err error + g *gosnmp.GoSNMP + SnmpTrapOID string `doc:"Value of the snmp trap oid pdu"` } +/* + * SNMP trap sender + */ func (x *SNMP) init() { var version gosnmp.SnmpVersion if x.Version == "2c" { @@ -48,7 +51,7 @@ func (x *SNMP) init() { } x.g.Target = x.Target - x.g.Connect() + x.err = x.g.Connect() } func (x *SNMP) Send(c *skogul.Container) error { @@ -56,56 +59,54 @@ func (x *SNMP) Send(c *skogul.Container) error { x.init() }) - if x.err != nil { - return x.err + var pdutypes []gosnmp.SnmpPDU + + if x.SnmpTrapOID != "" { + pdutypes = append(pdutypes, gosnmp.SnmpPDU{ + Value: x.SnmpTrapOID, + Type: gosnmp.ObjectIdentifier, + Name: ".1.3.6.1.6.3.1.1.4.1.0", + }) } - pdutypes := []gosnmp.SnmpPDU{} - - for _, m := range c.Metrics { - for j, i := range m.Data { - var pdutype gosnmp.SnmpPDU - - pduName := fmt.Sprintf("%s", x.Oidmap[j]) - - switch reflect.TypeOf(i).Kind() { - case reflect.String: - pdutype = gosnmp.SnmpPDU{ - Value: i, - Name: pduName, - Type: gosnmp.OctetString, - } - case reflect.Bool: - pdutype = gosnmp.SnmpPDU{ - Value: i, - Name: pduName, - Type: gosnmp.Boolean, - } - case reflect.Int: - pdutype = gosnmp.SnmpPDU{ - Value: i, - Name: pduName, - Type: gosnmp.Integer, - } - default: + m := c.Metrics[0] + + for j, i := range m.Data { + var pdutype gosnmp.SnmpPDU + + pduName := fmt.Sprintf("%s", x.Oidmap[j]) + + switch i.(type) { + case string: + pdutype = gosnmp.SnmpPDU{ + Value: i, + Name: pduName, + Type: gosnmp.OctetString, } - pdutypes = append(pdutypes, pdutype) - - trap := gosnmp.SnmpTrap{} - trap.Variables = pdutypes - trap.IsInform = false - trap.Enterprise = "no" - trap.AgentAddress = "localhost" - trap.GenericTrap = 1 - trap.SpecificTrap = 1 - _, err := x.g.SendTrap(trap) - - if err != nil { - x.err = err + case bool: + pdutype = gosnmp.SnmpPDU{ + Value: i, + Name: pduName, + Type: gosnmp.Boolean, } + case float64: + k := int(i.(float64)) + pdutype = gosnmp.SnmpPDU{ + Value: k, + Name: pduName, + Type: gosnmp.Integer, + } + default: } - + pdutypes = append(pdutypes, pdutype) } - return nil + trap := gosnmp.SnmpTrap{} + trap.Variables = pdutypes + trap.IsInform = false + trap.Enterprise = "no" + trap.AgentAddress = "localhost" + _, err := x.g.SendTrap(trap) + + return err } diff --git a/sender/snmp_test.go b/sender/snmp_test.go index 17a28ab2..e9bf51e0 100644 --- a/sender/snmp_test.go +++ b/sender/snmp_test.go @@ -16,60 +16,41 @@ import ( func TestSnmpSenderTest(t *testing.T) { t.Skip() - c, _ := loadJsonFile(t, "ble ble") + c, _ := loadJsonFile(t, "") sconf := fmt.Sprintln(` -{ - "receivers": { - "http": { - "type": "http", - "address": "localhost:1337", - "handlers": { - "/": "h" + { + "receivers": { + "http": { + "type": "http", + "address": ":11111", + "handlers": { + "/": "h" + } } - } - }, - "transformers": { - "flatten_data": { - "type": "data", - "flattenSeparator": "drop", - "flatten": [ - [ - "kek" - ] - ] }, - "remove_data": { - "type": "data", - "remove": [ - "kek" - ] - } - }, - "handlers": { - "h": { - "parser": "skogul", - "transformers": [ - "now", - "flatten_data", - "remove_data" - ], - "sender": "snmp" - } - }, - "senders": { - "snmp": { - "type": "snmp", - "port": 7331, - "community": "xxx", - "version": "2c", - "target": "localhost", - "oidmap": { - "kek": "1.3.3.7", + "transformers": {}, + "handlers": { + "h": { + "parser": "skogul", + "transformers": [ + "now" + ], + "sender": "print" } - } - } -}`) + }, + "senders": { + "snmp": { + "type": "snmp", + "port": 1337, + "community": "public", + "version": "2c", + "target": "localhost", + "oidmap": {} + } + } + } + `) conf, err := config.Bytes([]byte(sconf)) @@ -91,7 +72,7 @@ func TestSnmpSenderTest(t *testing.T) { } go httpRcv.Start() - time.Sleep(time.Duration(20 * time.Millisecond)) + time.Sleep(time.Duration(5 * time.Second)) err = snmpSender.Send(c)