-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
58 lines (48 loc) · 1.28 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2012 The GoSNMP Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
/*
The developer of the trapserver code (https://github.com/jda) says "I'm working
on the best level of abstraction but I'm able to receive traps from a Cisco
switch and Net-SNMP".
Pull requests welcome.
*/
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
g "github.com/gosnmp/gosnmp"
)
func main() {
flag.Usage = func() {
fmt.Printf("Usage:\n")
fmt.Printf(" %s\n", filepath.Base(os.Args[0]))
flag.PrintDefaults()
}
tl := g.NewTrapListener()
tl.OnNewTrap = myTrapHandler
tl.Params = g.Default
tl.Params.Logger = g.NewLogger(log.New(os.Stdout, "", 0))
err := tl.Listen("0.0.0.0:162")
if err != nil {
log.Panicf("error in listen: %s", err)
}
}
func myTrapHandler(packet *g.SnmpPacket, addr *net.UDPAddr) {
log.Printf("got trapdata from %s\n", addr.IP)
for _, v := range packet.Variables {
switch v.Type {
case g.OctetString:
s := v.Value.([]uint8)
fmt.Println("-----------------------", string(s))
b := v.Value.([]byte)
fmt.Printf("OID: %s, string: %x\n", v.Name, b)
default:
log.Printf("trap: %+v\n", v)
}
}
}