1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Go言語のSNMPパッケージgosnmpでsnmpsetを作る方法

Posted at

はじめに

Go言語のSNMPパッケージgosnmpを紹介するシリーズの7回目です。
前回(1年以上前)は、snmpwalkの作り方について書きました。

今回はsnmpsetコマンドの作り方です。

snmpsetコマンドについて

snmpsetは、NET-SNMPのコマンドです。MIBを設定(set)をするためのコマンドです。設定の変更はSNMP以外の方法で実施する方が多いので利用されることは少ないと思います。

$ snmpset -v 2c -c private 192.168.1.210 sysLocation.0 s test
SNMPv2-MIB::sysLocation.0 = STRING: test

のように使用します。

snmpsetをgosnmpを使って作る

先程のsnnpsetコマンドをgosnmp

で作ると

package main

import (
	"fmt"
	"log"
	"os"
	"time"

	g "github.com/gosnmp/gosnmp"
)

func main() {
	var Client = &g.GoSNMP{
		Target:    "192.168.1.210",
		Port:      161,
		Community: "private",
		Version:   g.Version2c,
		Timeout:   time.Duration(2) * time.Second,
		Logger:    g.NewLogger(log.New(os.Stdout, "", 0)),
	}
	err := Client.Connect()
	if err != nil {
		log.Fatalf("Connect() err: %v", err)
	}
	defer Client.Conn.Close()
	var mySnmpPDU = []g.SnmpPDU{{
		Name:  "1.3.6.1.2.1.1.6.0",
		Type:  g.OctetString,
		Value: []byte("test1"),
	}}
	setResult, setErr := Client.Set(mySnmpPDU)
	if setErr != nil {
		log.Fatalf("SNMP set() fialed due to err: %v", setErr)
	}
	for i, variable := range setResult.Variables {
		fmt.Printf("%d: oid: %s ", i, variable.Name)

		switch variable.Type {
		case g.OctetString:
			fmt.Printf("string: %s\n", string(variable.Value.([]byte)))
		default:
			fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))
		}
	}
}

のようになります。

実行すると

$go run main.go
SEND INIT
SENDING PACKET: Version:2c, MsgFlags:NoAuthNoPriv, SecurityModel:SnmpV3SecurityModel(0), SecurityParameters:, ContextEngineID:, ContextName:, Community:public, PDUType:SetRequest, MsgID:0, RequestID:236079856, MsgMaxSize:0, Error:NoError, ErrorIndex:0, NonRepeaters:0, MaxRepetitions:0, Variables:[{[116 101 115 116 49] 1.3.6.1.2.1.1.6.0 OctetString}]
WAITING RESPONSE...
GET RESPONSE OK: [48 46 2 1 1 4 6 112 117 98 108 105 99 162 33 2 4 14 18 74 240 2 1 0 2 1 0 48 19 48 17 6 8 43 6 1 2 1 1 6 0 4 5 116 101 115 116 49]
Packet sanity verified, we got all the bytes (48)
parseRawField: version
Parsed version 1
parseRawField: community
Parsed community private
UnmarshalPayload Meet PDUType 0x476574526573706f6e7365. Offset 13
getResponseLength: 35
parseRawField: request id
requestID: 236079856
parseRawField: error-status
errorStatus: 0
parseRawField: error index
error-index: 0
vblLength: 21
parseRawField: OID
OID: .1.3.6.1.2.1.1.6.0
decodeValue: type is OctetString
decodeValue: value is []byte{0x74, 0x65, 0x73, 0x74, 0x31}
0: oid: .1.3.6.1.2.1.1.6.0 string: test

SETするMIBのオブジェクト名をわかりやすくする方法

をみてください。

NET-SNMPのエージェントでsetのテストする時の注意点

NET-SNMPのSNMPエージェントでテストする時の注意点を書いておきます。

エージェントの設定ファイルにSET可能なCommunity名privateを追加します。

/ect/snmp/snmpd.conf
view   all  included   .1.3.6.1
rwcommunity private  default    -V all

もう一つ、sysLocationなどをsnmpd.confで設定するとSET可能なCommutiy名を指定してもエラーで失敗します。

/etc/snmp/snmpd.conf
sysLocation    YMI2F

を追加

$snmpset -v 2c -c private 192.168.1.210 sysLocation.0 s test
Error in packet.
Reason: notWritable (That object does not support modification)
Failed object: SNMPv2-MIB::sysLocation.0

Go言語で作ったプログラムの場合は、

$go run main.go
SEND INIT
SENDING PACKET: Version:2c, MsgFlags:NoAuthNoPriv, SecurityModel:SnmpV3SecurityModel(0), SecurityParameters:, ContextEngineID:, ContextName:, Community:public, PDUType:SetRequest, MsgID:0, RequestID:2074898788, MsgMaxSize:0, Error:NoError, ErrorIndex:0, NonRepeaters:0, MaxRepetitions:0, Variables:[{[116 101 115 116 49] 1.3.6.1.2.1.1.6.0 OctetString}]
WAITING RESPONSE...
GET RESPONSE OK: [48 46 2 1 1 4 6 112 117 98 108 105 99 162 33 2 4 123 172 113 100 2 1 17 2 1 1 48 19 48 17 6 8 43 6 1 2 1 1 6 0 4 5 116 101 115 116 49]
Packet sanity verified, we got all the bytes (48)
parseRawField: version
Parsed version 1
parseRawField: community
Parsed community private
UnmarshalPayload Meet PDUType 0x476574526573706f6e7365. Offset 13
getResponseLength: 35
parseRawField: request id
requestID: 2074898788
parseRawField: error-status
errorStatus: 17
parseRawField: error index
error-index: 1
vblLength: 21
parseRawField: OID
OID: .1.3.6.1.2.1.1.6.0
decodeValue: type is OctetString
decodeValue: value is []byte{0x74, 0x65, 0x73, 0x74, 0x31}
0: oid: .1.3.6.1.2.1.1.6.0 string: old

エラーになっています。

parseRawField: error-status
errorStatus: 17
parseRawField: error index
error-index: 1

余談

TWSNMP FKとFCのMIBブラウザーにSNMP Set機能を付けました。この機能をつけるために学習した内容をこの記事に書いています。

image.png

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?