Skip to content

Commit

Permalink
First version
Browse files Browse the repository at this point in the history
  • Loading branch information
grongor committed Jun 30, 2020
1 parent cd70dd6 commit dc27b54
Show file tree
Hide file tree
Showing 24 changed files with 2,978 additions and 2 deletions.
61 changes: 61 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
name: CI

on:
push:
pull_request:

jobs:
build:
name: Build
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- name: Install dependencies
run: sudo apt update && sudo apt install libsnmp-dev
- name: Build
run: go build ./cmd/snmp-proxy

test:
name: Test
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- name: Install dependencies
run: sudo apt update && sudo apt install libsnmp-dev python3-pip snmp-mibs-downloader
- name: Install snmpsim
run: sudo pip3 install snmpsim
- name: Run tests
run: make test

coverage:
name: Code Coverage
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
- name: Install dependencies
run: sudo apt update && sudo apt install libsnmp-dev python3-pip snmp-mibs-downloader
- name: Install snmpsim
run: sudo pip3 install snmpsim
- name: Download go-acc
run: GO111MODULE=off go get -u github.com/ory/go-acc
- name: Generate Code Coverage
run: $(go env GOPATH)/bin/go-acc --covermode set --output coverage.cov ./...
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.cov

lint:
name: Lint
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v2
- name: Install dependencies
run: sudo apt update && sudo apt install libsnmp-dev
- name: golangci-lint
uses: golangci/golangci-lint-action@v1
with:
version: v1.27
25 changes: 25 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Release

on:
push:
tags:
- v*

jobs:
release:
name: Release
runs-on: ubuntu-18.04
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- uses: actions/setup-go@v2
- name: Install dependencies
run: sudo apt update && sudo apt install libsnmp-dev
- name: GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
args: release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/bin
/dist

/config.toml
/snmp-proxy
60 changes: 60 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
run:
modules-download-mode: readonly

linters:
enable-all: true
disable:
- depguard
- dogsled
- funlen
- godox
- gofmt
- golint
- gomnd
- interfacer

linters-settings:
gocritic:
disabled-checks:
- captLocal

govet:
enable-all: true
disable:
- shadow

lll:
tab-width: 4

wsl:
allow-cuddle-declarations: true

issues:
max-issues-per-linter: 0
max-same-issues: 0
exclude-rules:
# ignore unchecked errors and missing Close() calls in tests
- path: _test\.go
linters: [errcheck, bodyclose]
# ignore recommendations for variable names, e.g id => ID, uri => URI, ...
- text: "^ST1003:"
linters: [stylecheck]
# ignore line length limit in tests
- path: _test\.go
text: line is \d+ characters
linters: [lll]
# ignore defer cuddle in tests
- path: _test\.go
text: only one cuddle assignment allowed before defer statement
linters: [wsl]
# ignore expressions after assignment in tests
- path: _test\.go
text: only cuddled expressions if assigning variable or using from line above
linters: [wsl]
# ignore scopelint in tests for "test" variable (probably bug, maybe remove this later)
- path: _test\.go
text: Using the variable on range scope `test` in function literal
linters: [scopelint]
# ignore goerr113 dynamic errors definition error...not sure how to approach this correctly now
- text: do not define dynamic errors, use wrapped static errors instead
linters: [goerr113]
23 changes: 23 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
archives:
- files:
- LICENSE
- README.md
- config.toml.dist
- id: binary
format: binary
name_template: snmp-proxy

builds:
- main: ./cmd/snmp-proxy
binary: snmp-proxy
env:
- CGO_ENABLED=1
goos:
- linux
goarch:
- amd64

checksum:
name_template: 'checksums.txt'

changelog:
32 changes: 32 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
export BIN = ${PWD}/bin
export GOBIN = $(BIN)

.PHONY: build
build:
for CMD in `ls cmd`; do \
go build ./cmd/$$CMD; \
done

.PHONY: check
check: lint test

.PHONY: lint
lint: $(BIN)/golangci-lint
$(BIN)/golangci-lint run

.PHONY: fix
fix: $(BIN)/golangci-lint
$(BIN)/golangci-lint run --fix

.PHONY: test
test:
timeout 300 go test ./...
timeout 300 go test --race ./...
timeout 300 go test --count 100 ./...

.PHONY: clean
clean:
rm -rf bin

$(BIN)/golangci-lint:
curl --retry 5 -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.27.0
109 changes: 107 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,107 @@
# go-snmp-proxy
Proxy server for SNMP request, written in Go
go-snmp-proxy
=============

[![Coverage Status](https://coveralls.io/repos/github/grongor/go-snmp-proxy/badge.svg)](https://coveralls.io/github/grongor/go-snmp-proxy)

HTTP proxy server for SNMP requests, written in Go.

Use cases
---------

- reduce time for devices with latency issues (remote locations, bad connection, ...)
- fetching large SNMP tree of a server in Asia from data center in US might take a long time
- remove dependency on SNMP from your code
- don't want to/can't install SNMP tooling, PHP extension,... ? This solves it gracefully.
- use HTTP authentication for your SNMP requests
- encrypt "SNMP traffic" between the client and the server
- bypass firewall
- ...post your use-case via issue :-)

Only SNMP versions 1 and 2c are supported. If you want support for version 3, please, send a pull request.

Clients
=======

| Language | Link |
|----------|------------------------------------|
| PHP | https://github.com/simPod/PHP-SNMP |

Feel free to create an issue to include your implementation here.

How it works
------------

The application provides a single HTTP endpoint `/snmp-proxy`, which accepts POST requests:
```json
{
"request_type": "getNext",
"host": "192.168.1.1",
"community": "public",
"oids": [
".1.2.3",
".4.5.6"
],
"version": "2c",
"timeout": 10,
"retries": 3
}
```

It will then issue SNMP requests based on the given JSON request, convert the result into JSON and send it back
to the client. Response might look like this:
```json
{
"result": [
".1.2.3.4.5",
123,
".4.5.6.7.8",
"lorem"
]
}
```

Result is an array instead of a map because maps in Go aren't ordered (and overcoming this would unnecessarily
complicated), and the order is also not guaranteed by the JSON format.

If there is an error, response will be as follows:
```json
{
"error": "description of what happened"
}
```

Some errors are "standardized" and expected:
- no such instance
- no such object
- end of mib

The rest of the errors just describe what unexpected happened.

MIBs
----

The application will try to find all installed MIBs and parse the DisplayHint information for OctetString types
so that it knows how to format them. MIB parsing was inspired by
[Prometheus SNMP exporter generator](https://github.com/prometheus/snmp_exporter/tree/master/generator). Thanks!

In case that OID is of the type OctetString, and it isn't found in the MIBs, then we try to detect whether the string
is printable (utf8 valid + all characters are printable). If it isn't, it's formatted as `AB C0 D5 D6...`.

Metrics
-------

If you set Metrics.Listen address in the config, the application will expose Prometheus metrics on given address[:port],
specifically on `GET /metrics`. These metrics contain all essential information about Go runtime, and a histogram
of `POST /snmp-proxy` requests (count, durations).

Shared libraries
----------------

You will have to install a shared library for the `snmp-proxy`: `libsnmp` (contained in `libsnmp-dev`).

Binaries available in the [Releases](https://github.com/grongor/go-snmp-proxy/releases) will be built on the Ubuntu LTS
and thus compatible with the version of this library available in the Ubuntu LTS (and stable Debian).
If you that doesn't match your target system, you can:
- install the expected version of `libsnmp` (you will usually have the newer version, but it should be possible
to install older one)
- build the `snmp-proxy` yourself in the same environment as you expected the `snmp-proxy` to run
Loading

0 comments on commit dc27b54

Please sign in to comment.