gosnmp
1// Copyright 2012 The GoSNMP Authors. All rights reserved. Use of this
2// source code is governed by a BSD-style license that can be found in the
3// LICENSE file.
4
5/*
6Example usage and output:
7
8$ GOSNMP_TARGET=10.12.0.1 GOSNMP_PORT=161 go run main.go
9SEND INIT
10SENDING PACKET: gosnmp.SnmpPacket{Version:0x1, MsgFlags:0x0, SecurityModel:0x0, SecurityParameters:gosnmp.SnmpV3SecurityParameters(nil), ContextEngineID:"", ContextName:"", Community:"public", PDUType:0xa0, MsgID:0x0, RequestID:0x48ef671d, MsgMaxSize:0x0, Error:0x0, ErrorIndex:0x0, NonRepeaters:0x0, MaxRepetitions:0x0, Variables:[]gosnmp.SnmpPDU{gosnmp.SnmpPDU{Name:"1.3.6.1.2.1.1.4.0", Type:0x5, Value:interface {}(nil)}, gosnmp.SnmpPDU{Name:"1.3.6.1.2.1.1.7.0", Type:0x5, Value:interface {}(nil)}}, Logger:gosnmp.Logger(nil), SnmpTrap:gosnmp.SnmpTrap{Variables:[]gosnmp.SnmpPDU(nil), IsInform:false, Enterprise:"", AgentAddress:"", GenericTrap:0, SpecificTrap:0, Timestamp:0x0}}
11WAITING RESPONSE...
122021/01/03 13:31:15 Query latency in seconds: 0.000625777
13GET RESPONSE OK: [48 63 2 1 1 4 6 112 117 98 108 105 99 162 50 2 4 72 239 103 29 2 1 0 2 1 0 48 36 48 19 6 8 43 6 1 2 1 1 4 0 4 7 99 111 110 116 97 99 116 48 13 6 8 43 6 1 2 1 1 7 0 2 1 78]
14Packet sanity verified, we got all the bytes (65)
15parseRawField: version
16Parsed version 1
17parseRawField: community
18Parsed community public
19UnmarshalPayload Meet PDUType 0xa2. Offset 13
20getResponseLength: 52
21parseRawField: request id
22requestID: 1223649053
23parseRawField: error-status
24errorStatus: 0
25parseRawField: error index
26error-index: 0
27vblLength: 38
28parseRawField: OID
29OID: .1.3.6.1.2.1.1.4.0
30decodeValue: msg: value
31decodeValue: type is OctetString
32decodeValue: value is []byte{0x63, 0x6f, 0x6e, 0x74, 0x61, 0x63, 0x74}
33parseRawField: OID
34OID: .1.3.6.1.2.1.1.7.0
35decodeValue: msg: value
36decodeValue: type is Integer
37decodeValue: value is 78
380: oid: .1.3.6.1.2.1.1.4.0 string: contact
391: oid: .1.3.6.1.2.1.1.7.0 number: 78
40
41*/
42
43package main44
45import (46"fmt"47"log"48"os"49"strconv"50"time"51
52g "github.com/gosnmp/gosnmp"53)
54
55func main() {56
57// get Target and Port from environment58envTarget := os.Getenv("GOSNMP_TARGET")59envPort := os.Getenv("GOSNMP_PORT")60if len(envTarget) <= 0 {61log.Fatalf("environment variable not set: GOSNMP_TARGET")62}63if len(envPort) <= 0 {64log.Fatalf("environment variable not set: GOSNMP_PORT")65}66port, _ := strconv.ParseUint(envPort, 10, 16)67
68// Build our own GoSNMP struct, rather than using g.Default.69// Do verbose logging of packets.70params := &g.GoSNMP{71Target: envTarget,72Port: uint16(port),73Community: "public",74Version: g.Version2c,75Timeout: time.Duration(2) * time.Second,76Logger: g.NewLogger(log.New(os.Stdout, "", 0)),77}78err := params.Connect()79if err != nil {80log.Fatalf("Connect() err: %v", err)81}82defer params.Conn.Close()83
84// Function handles for collecting metrics on query latencies.85var sent time.Time86params.OnSent = func(x *g.GoSNMP) {87sent = time.Now()88}89params.OnRecv = func(x *g.GoSNMP) {90log.Println("Query latency in seconds:", time.Since(sent).Seconds())91}92
93oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}94result, err2 := params.Get(oids) // Get() accepts up to g.MAX_OIDS95if err2 != nil {96log.Fatalf("Get() err: %v", err2)97}98
99for i, variable := range result.Variables {100fmt.Printf("%d: oid: %s ", i, variable.Name)101
102// the Value of each variable returned by Get() implements103// interface{}. You could do a type switch...104switch variable.Type {105case g.OctetString:106fmt.Printf("string: %s\n", string(variable.Value.([]byte)))107default:108// ... or often you're just interested in numeric values.109// ToBigInt() will return the Value as a BigInt, for plugging110// into your calculations.111fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))112}113}114}
115