HivePaaS
Docker Swarm 기반 오픈소스 자체 호스팅 PaaS, SSL·백업·자동 확장 지원
Go 언어로 구현된 SNMP 라이브러리
An SNMP library written in Go
개발 도구Go · ShellGitHub @gosnmp직접 설치운영자 미확인
써 보셨어요? 한 번만 눌러 주세요. 로그인은 필요 없어요.
개인 계정의 공개 저장소 소유자는 GitHub로 로그인해 운영자를 확인하고 소개를 직접 관리할 수 있습니다. 관리자 승인은 필요하지 않습니다.
로그인한 뒤 확인 파일을 만들어 이 저장소의 기본 브랜치에 30분 안에 커밋해 주세요. 다른 브랜치나 포크의 파일은 인정하지 않습니다. 파일을 올린 뒤 아래에서 확인하면 관리자 승인 없이 관리할 수 있습니다. 자격을 유지하려면 파일을 남겨 두세요.
로그인이 필요하면 로그인 화면으로 이동합니다. 로그인 후 이곳으로 돌아와 원하는 확인 버튼을 다시 눌러 주세요.
설치형 프로젝트는 배포 도메인 없이도 확인할 수 있습니다. 개인 저장소는 GitHub 로그인으로, 조직 저장소는 위의 저장소 확인 파일로 직접 확인해 주세요.
메이커가 쓴 소개는 위의 한 줄이 전부입니다. 더 자세한 내용은 저장소 README에서 읽을 수 있습니다. 저장소 README ↗
README 전체를 불러오는 중입니다.
README 전문 보기 ↗GoSNMP is an SNMP client library fully written in Go. It provides Get, GetNext, GetBulk, Walk, BulkWalk, Set and Traps. It supports IPv4 and IPv6, using SNMPv1, SNMPv2c or SNMPv3. Builds are tested against linux/amd64 and linux/386.
gosnmp was started by Andreas Louca, then completely rewritten by Sonia Hamilton (2012-2020), and now ownership has been transferred to the community at gosnmp/gosnmp.
For support and help, join us in the #snmp channel of Gophers Slack.
GoSNMP has the following SNMP functions:
GoSNMP has the following helper functions:
*big.Intgosnmp/gosnmp has completely diverged from alouca/gosnmp, your code will require modification in these (and other) locations:
Connect uses the GoSNMP struct;
gosnmp.Default is provided for you to build on.gosnmp.LoggerInterface interface; otherwise
debugging will disabled.type LoggerInterface interface {
Print(v ...interface{})
Printf(format string, v ...interface{})
}
To enable logging, you must call gosnmp.NewLogger() function, and pass a pointer to your logging interface, for example with standard *log.Logger:
gosnmp.Default.Logger = gosnmp.NewLogger(log.New(os.Stdout, "", 0))
or
g := &gosnmp.GoSNMP{
...
Logger: gosnmp.NewLogger(log.New(os.Stdout, "", 0)),
}
You can completely remove the logging code from your application using the golang build tag "gosnmp_nodebug", for example:
go build -tags gosnmp_nodebug
This will completely disable the logging of the gosnmp library, even if the logger interface is specified in the code. This provides a small performance improvement.
go get github.com/gosnmp/gosnmp
https://pkg.go.dev/github.com/gosnmp/gosnmp
Here is examples/example/main.go, demonstrating how to use GoSNMP:
// Default is a pointer to a GoSNMP struct that contains sensible defaults
// eg port 161, community public, etc
g.Default.Target = "192.168.1.10"
err := g.Default.Connect()
if err != nil {
log.Fatalf("Connect() err: %v", err)
}
defer g.Default.Conn.Close()
oids := []string{"1.3.6.1.2.1.1.4.0", "1.3.6.1.2.1.1.7.0"}
result, err2 := g.Default.Get(oids) // Get() accepts up to g.MAX_OIDS
if err2 != nil {
log.Fatalf("Get() err: %v", err2)
}
for i, variable := range result.Variables {
fmt.Printf("%d: oid: %s ", i, variable.Name)
// the Value of each variable returned by Get() implements
// interface{}. You could do a type switch...
switch variable.Type {
case g.OctetString:
bytes := variable.Value.([]byte)
fmt.Printf("string: %s\n", string(bytes))
default:
// ... or often you're just interested in numeric values.
// ToBigInt() will return the Value as a BigInt, for plugging
// into your calculations.
fmt.Printf("number: %d\n", g.ToBigInt(variable.Value))
}
}
Running this example gives the following output (from my printer):
% go run example.go
0: oid: 1.3.6.1.2.1.1.4.0 string: Administrator
1: oid: 1.3.6.1.2.1.1.7.0 number: 104
examples/example2.go is similar to example.go, however it uses a
custom &GoSNMP rather than g.Defaultexamples/walkexample.go demonstrates using BulkWalkexamples/example3.go demonstrates SNMPv3examples/trapserver.go demonstrates writing an SNMP v2c trap serverI don't have any plans to write a mib parser. Others have suggested https://github.com/sleepinggenius2/gosmi
Contributions are welcome, especially ones that have packet captures (see below).
If you've never contributed to a Go project before, here is an example workflow.
go get github.com/gosnmp/gosnmpcd $GOPATH/src/github.com/gosnmp/gosnmpgit remote rename origin upstreamgit remote add origin [email protected]:<your-github-username>/gosnmp.gitgit checkout -b developmentgit push -u origin development (setup where you push to, check it works)Create your packet captures in the following way:
Expected output, obtained via an snmp command. For example:
% snmpget -On -v2c -c public 203.50.251.17 1.3.6.1.2.1.1.7.0 \
1.3.6.1.2.1.2.2.1.2.6 1.3.6.1.2.1.2.2.1.5.3
.1.3.6.1.2.1.1.7.0 = INTEGER: 78
.1.3.6.1.2.1.2.2.1.2.6 = STRING: GigabitEthernet0
.1.3.6.1.2.1.2.2.1.5.3 = Gauge32: 4294967295
A packet capture, obtained while running the snmpget. For example:
sudo tcpdump -s 0 -i eth0 -w foo.pcap host 203.50.251.17 and port 161
Rane's document SNMP: Simple? Network Management Protocol was useful when learning the SNMP protocol.
Please create an issue on Github with packet captures (upload capture to Google Drive, Dropbox, or similar) containing samples of missing BER types, or of any other bugs you find. If possible, please include 2 or 3 examples of the missing/faulty BER type.
The following BER types have been implemented:
Local testing in Docker
docker build -t gosnmp/gosnmp:latest .
docker run -it gosnmp/gosnmp:latest
or
export GOSNMP_TARGET=1.2.3.4
export GOSNMP_PORT=161
export GOSNMP_TARGET_IPV4=1.2.3.4
export GOSNMP_PORT_IPV4=161
export GOSNMP_TARGET_IPV6='0:0:0:0:0:ffff:102:304'
export GOSNMP_PORT_IPV6=161
make test test-race
Tests are grouped as follows:
marshal_test.gomisc_test.gogosnmp_api_test.gogeneric_e2e_test.goThe generic end-to-end integration test generic_e2e_test.go should
work against any SNMP MIB-2 compliant host (e.g. a router, NAS box, printer).
Mocks were generated using:
mockgen -source=interface.go -destination=mocks/gosnmp_mock.go -package=mocks
However they're currently removed, as they were breaking linting.
To profile cpu usage:
go test -cpuprofile cpu.out
go test -c
go tool pprof gosnmp.test cpu.out
To profile memory usage:
go test -memprofile mem.out
go test -c
go tool pprof gosnmp.test mem.out
To check test coverage:
go get github.com/axw/gocov/gocov
go get github.com/matm/gocov-html
gocov test github.com/gosnmp/gosnmp | gocov-html > gosnmp.html && firefox gosnmp.html &
To measure the performance of password hash caching:
Password hash caching can be disabled during benchmark tests by using the golang build tag "gosnmp_nopwdcache", so:
go build -tags gosnmp_nopwdcache -bench=Benchmark.*Hash
will benchmark the code without password hash caching. Removing the tag will run the benchmark with caching enabled (default behavior of package).
Parts of the code are taken from the Golang project (specifically some functions for unmarshaling BER responses), which are under the same terms and conditions as the Go language. The rest of the code is under a BSD license.
See the LICENSE file for more details.
The remaining code is Copyright 2012 the GoSNMP Authors - see AUTHORS.md for a list of authors.