Skip to content

Commit

Permalink
Merge pull request #298 from telenornms/fix/snmp_improvement
Browse files Browse the repository at this point in the history
Fixes snmp sender, adding support for overriding snmpTrapOID and actually sending the pdus
  • Loading branch information
KristianLyng authored Oct 9, 2023
2 parents be0fd69 + 7aee9ff commit 4b86732
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 106 deletions.
113 changes: 57 additions & 56 deletions sender/snmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package sender

import (
"fmt"
"reflect"
"sync"
"time"

Expand All @@ -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" {
Expand All @@ -48,64 +51,62 @@ 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 {
x.r.Do(func() {
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
}
81 changes: 31 additions & 50 deletions sender/snmp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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)

Expand Down

0 comments on commit 4b86732

Please sign in to comment.