-
Notifications
You must be signed in to change notification settings - Fork 341
/
main.go
116 lines (100 loc) · 2.75 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2020 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.
package main
import (
"fmt"
"log"
"net"
"time"
gosnmp "github.com/gosnmp/gosnmp"
)
func main() {
log.Println("Starting")
var port uint16 = 2162
go Start(fmt.Sprintf("tcp://0.0.0.0:%d", port))
gosnmp.Default.Target = "127.0.0.1"
gosnmp.Default.Transport = "tcp"
gosnmp.Default.Port = port
gosnmp.Default.Community = "public"
gosnmp.Default.Version = gosnmp.Version1
time.Sleep(time.Duration(1) * time.Second)
oid := gosnmp.SnmpPDU{
Name: "1.3.6.1.2.1.1.6",
Type: gosnmp.ObjectIdentifier,
Value: "1.3.6.1.2.1.1.6.10",
}
oid1 := gosnmp.SnmpPDU{
Name: "1.3.6.1.2.1.1.7",
Type: gosnmp.OctetString,
Value: "Testing TCP trap...",
}
oid2 := gosnmp.SnmpPDU{
Name: "1.3.6.1.2.1.1.8",
Type: gosnmp.Integer,
Value: 123,
}
cou := 5
for cou > 0 {
time.Sleep(time.Duration(1) * time.Second)
err := gosnmp.Default.Connect()
if err != nil {
log.Fatal(err)
}
defer gosnmp.Default.Conn.Close()
//RebuildCron()
log.Printf("Running (%d)\n", cou)
trap := gosnmp.SnmpTrap{
Variables: []gosnmp.SnmpPDU{oid, oid1, oid2},
Enterprise: ".1.3.6.1.6.3.1.1.5.1",
AgentAddress: "127.0.0.1",
GenericTrap: 0,
SpecificTrap: 0,
Timestamp: 300,
}
_, err = gosnmp.Default.SendTrap(trap)
if err != nil {
log.Fatalf("SendTrap() err: %v\n", err)
}
cou--
}
//time.Sleep(time.Duration(10) * time.Second)
log.Println("Stop...")
}
// Start SNMP server
func Start(address string) {
log.Printf("Starting SNMP TRAP Server on: %s\n", address)
tl := gosnmp.NewTrapListener()
tl.OnNewTrap = myTrapHandlerTCP
tl.Params = gosnmp.Default
err := tl.Listen(address)
if err != nil {
time.Sleep(1 * time.Second)
log.Fatalf("Error in TRAP listen: %s\n", err)
}
}
func myTrapHandlerTCP(packet *gosnmp.SnmpPacket, addr *net.UDPAddr) {
log.Printf("SNMP trap received from: %s:%d. Community:%s, SnmpVersion:%s\n",
addr.IP, addr.Port, packet.Community, packet.Version)
for i, variable := range packet.Variables {
var val string
switch variable.Type {
case gosnmp.OctetString:
val = string(variable.Value.([]byte))
case gosnmp.ObjectIdentifier:
val = fmt.Sprintf("%s", variable.Value)
case gosnmp.TimeTicks:
a := gosnmp.ToBigInt(variable.Value)
val = fmt.Sprintf("%d", (*a).Int64())
case gosnmp.Null:
val = ""
default:
// ... or often you're just interested in numeric values.
// ToBigInt() will return the Value as a BigInt, for plugging
// into your calculations.
a := gosnmp.ToBigInt(variable.Value)
val = fmt.Sprintf("%d", (*a).Int64())
}
log.Printf("- oid[%d]: %s (%s) = %v \n", i, variable.Name, variable.Type, val)
}
}