Golang SNMP Development Learning Note (i)

Source: Internet
Author: User
Tags snmp snmpv3
This is a creation in Article, where the information may have evolved or changed.

Overview

SONIAH/GOSNMP is a pure Go language-written SNMP package, based on ALOUCA/GOSNMP development. Currently provides getrequest, GetNext, Getbulk, walk, and setrequest functions, supports IPV4 and IPV6, and supports SNMPV2C and SNMPv3. GOSNMP is still in development, currently does not support Trap,walk and setrequest is not perfect, but as a general network monitoring use is sufficient, if necessary can also be directly modified to supplement the source code.
This article describes the Getrequest, GetNext, and getbulk operations of SONIAH/GOSNMP.

Resources

    • Document: HTTPS://GODOC.ORG/GITHUB.COM/SONIAH/GOSNMP
    • Source: Https://github.com/soniah/gosnmp

Installation

get github.com/soniah/gosnmp

Example 1. Getrequest

 PackageMainImport("FMT"    "Log"G"Github.com/soniah/gosnmp")funcMain () {//Default is a pointer-a GOSNMP struct that contains    //sensible defaults eg Port 161, community public, etcG.default.target ="172.18.0.2"G.default.community ="Community"ERR: = G.default.connect ()ifErr! =Nil{log. Fatalf ("Connect () Err:%v", err)}deferG.default.conn.close () OIDs: = []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"} result, err: = G.default.get (OIDs)//Get () accepts up to G.max_oids    ifErr! =Nil{log. Fatalf ("Get () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d. OID:%s", I, V.name)//The Value of each variable returned by Get () implements        //interface{}. You could do a type switch ...        Switchv.type{ CaseG.octetstring:fmt. Printf ("string:%s\n",string(V.value. ([]byte)))default://... or often you ' re just interested in numeric values.            //Tobigint () would return the Value as a BigInt, for plugging            //into your calculations.Fmt. Printf ("Number:%d\n", G.tobigint (V.value))}}}

Output Result:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S26521. oid: .1.3.6.1.2.1.1.7.0 number: 3

Example 2. Construct GOSNMP structure by oneself

 PackageMainImport("FMT"    "Log"    "Time"G"Github.com/soniah/gosnmp")funcMain () {//Build our own gosnmp struct, rather than using G.defaultSNMP: = &g.gosnmp{Target:"172.18.0.2", Port:161, Community:"Community", VERSION:G.VERSION2C, Timeout:time. Duration(2) * time. Second,} ERR: = SNMP. Connect ()ifErr! =Nil{log. Fatalf ("Connect () Err:%v", err)}deferSnmp. Conn.close () OIDs: = []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"} result, err: = SNMP. Get (OIDs)ifErr! =Nil{log. Fatalf ("Get () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d. OID:%s", I, V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("string:%s\n",string(V.value. ([]byte)))default: FMT. Printf ("Number:%d\n", G.tobigint (V.value))}}}

Output Result:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S26521. oid: .1.3.6.1.2.1.1.7.0 number: 3

Example 3. SNMPv3

 PackageMainImport("FMT"    "Log"    "Time"G"Github.com/soniah/gosnmp")funcMain () {snmp: = &gosnmp{Target:"172.17.0.10", Port:161, Community:"Community", Version:g.version3, Timeout:time. Duration( in) * time. Second, Securitymodel:g.usersecuritymodel, Msgflags:g.authpriv,//authentication and encryptionSecurityparameters: &g.usmsecurityparameters {UserName:"User", Authenticationprotocol:g.sha, Authenticationpassphrase:"Password", Privacyprotocol:g.des, Privacypassphrase:"Password",},} err: = SNMP. Connect ()ifErr! =Nil{log. Fatalf ("Connect () Err:%v", err)}deferSnmp. Conn.close () OIDs: = []string{"1.3.6.1.2.1.1.5.0","1.3.6.1.2.1.1.7.0"} result, err: = SNMP. Get (OIDs)ifErr! =Nil{log. Fatalf ("Get () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d. OID:%s", I, V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("string:%s\n",string(V.value. ([]byte)))default: FMT. Printf ("Number:%d\n", G.tobigint (V.value))}}}

TODO: Sample 3 code is not validated.

Example 4. GetNext

 PackageMainImport("FMT"    "Log"G"Github.com/soniah/gosnmp")funcMain () {g.default.target ="172.18.0.2"G.default.community ="Community"ERR: = G.default.connect ()ifErr! =Nil{log. Fatalf ("Connect () Err:%v", err)}deferG.default.conn.close () OIDs: = []string{"1.3.6.1.2.1.1.5","1.3.6.1.2.1.2.2.1.10"} result, err: = G.default.getnext (OIDs)ifErr! =Nil{log. Fatalf ("Get () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d. OID:%s", I, V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("string:%s\n",string(V.value. ([]byte)))default: FMT. Printf ("Number:%d\n", G.tobigint (V.value))}}}

Output Result:

0. oid: .1.3.6.1.2.1.1.5.0 string: YD_WLZX1F_S26521. oid: .1.3.6.1.2.1.2.2.1.10.1 number: 144611

Example 5. Getbulk

 PackageMainImport("FMT"    "Log"G"Github.com/soniah/gosnmp")funcMain () {g.default.target ="172.18.0.2"G.default.community ="Community"ERR: = G.default.connect ()ifErr! =Nil{log. Fatalf ("Connect () Err:%v", err)}deferG.default.conn.close () OIDs: = []string{"1.3.6.1.2.1.1.2",//sysobjectid        "1.3.6.1.2.1.1.5",//sysname        "1.3.6.1.2.1.2.2.1.1",//ifindex        "1.3.6.1.2.1.2.2.1.10",//ifinoctets        "1.3.6.1.2.1.2.2.1.16",//ifoutoctets}//The first 2 OIDs do not repeat, after 3 repeats 10 times, equivalent to perform 5 times GetNextresult, err: = G.default.getbulk (OIDs,2,5)ifErr! =Nil{log. Fatalf ("Get () Err:%v", err)} forI, V: =RangeResult. Variables {fmt. Printf ("%d:%s =", I, V.name)Switchv.type{ CaseG.octetstring:fmt. Printf ("%s\n",string(V.value. ([]byte)))default: FMT. Printf ("%d\n", G.tobigint (V.value))}}}

Output Result:

0:. 1.3.6.1.2.1.1.2.0 = 01:. 1.3.6.1.2.1.1.5.0 = yd_wlzx1f_s26522:. 1.3.6.1.2.1.2.2.1.1.1 = 13:. 1.3.6.1.2.1.2.2.1.10.1 = 1446114:. 1.3.6.1.2.1.2.2.1.16.1 = 5393062845:. 1.3.6.1.2.1.2.2.1.1.2 = 26:. 1.3.6.1.2.1.2.2.1.10.2 = 07:. 1.3.6.1.2.1.2.2.1.16.2 = 08:. 1.3.6.1.2.1.2.2.1.1.3 = 39:. 1.3.6.1.2.1.2.2.1.10.3 = 70688630Ten:. 1.3.6.1.2.1.2.2.1.16.3 = 216291621 One:. 1.3.6.1.2.1.2.2.1.1.4 = 4 A:. 1.3.6.1.2.1.2.2.1.10.4 = 129777391 -:. 1.3.6.1.2.1.2.2.1.16.4 = 1052726443 -:. 1.3.6.1.2.1.2.2.1.1.5 = 5 the:. 1.3.6.1.2.1.2.2.1.10.5 = 169733222 -:. 1.3.6.1.2.1.2.2.1.16.5 = 641244267
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.