Compare commits

..

No commits in common. "f2d0d1ca941f5e6d9e0dfe8810389e4fba95b870" and "c05cf671cf075380668c5a931d4d4c7079e06634" have entirely different histories.

2 changed files with 75 additions and 21 deletions

View File

@ -0,0 +1,64 @@
# snmp-mqtt
This project is directly derived from https://github.com/dchote/snmp-mqtt
A simple go app that reads SNMP values and publishes it to the specified MQTT endpoint at the specified interval.
In constrast to the original version, all configuration must be provided via an environment variable containing
a JSON string.
The MQTT configuration is part of this JSON string.
The topic has been moved to the MQTT configuration and there is now only one topic for all endpoints and variables.
The published message on MQTT looks like this:
```
{"device":"172.16.3.1","label":"router","variables":{"lan-in":{"label":"lan-in","variable":".1.3.6.1.2.1.31.1.1.1.6.2","value":"979673705579"},"lan-out":{"label":"lan-out","variable":".1.3.6.1.2.1.31.1.1.1.10.2","value":"1813410276168"},"wan-in":{"label":"wan-in","variable":".1.3.6.1.2.1.31.1.1.1.6.4","value":"83591215399"},"wan-out":{"label":"wan-out","variable":".1.3.6.1.2.1.31.1.1.1.10.4","value":"83741895468"}}}
```
```
export SNMP_MQTT_CONF=$(cat config.json)
./smq
```
An example config.json file:
```
{
"mqtt": {
"broker": "mqtt://172.23.1.102:1883",
"tlsEnable": "false",
"topic": "snmp"
},
"interval": 10,
"snmpEndpoints": [
{
"endpoint": "172.16.3.1",
"label": "router",
"community": "public",
"oidTopics": [
{
"oid": ".1.3.6.1.2.1.31.1.1.1.6.4",
"label": "wan-in",
"diff": "true"
},
{
"oid": ".1.3.6.1.2.1.31.1.1.1.10.4",
"label": "wan-out",
"diff": "true"
},
{
"oid": ".1.3.6.1.2.1.31.1.1.1.6.2",
"label": "lan-in",
"diff": "true"
},
{
"oid": ".1.3.6.1.2.1.31.1.1.1.10.2",
"label": "lan-out",
"diff": "true"
}
]
}
]
}
```

View File

@ -3,7 +3,6 @@ package tsmq
import ( import (
"log" "log"
"time" "time"
"strconv"
"encoding/json" "encoding/json"
"tsm/config" "tsm/config"
@ -13,15 +12,14 @@ import (
) )
type variable_t struct { type variable_t struct {
Label string `json:"label"` Name string `json:"variable"`
Variable string `json:"variable"` Value float64 `json:"value"`
Value string `json:"value"`
Unit string `json:"unit"` Unit string `json:"unit"`
Status string `json:"status"` Status string `json:"status"`
} }
type message_t struct { type message_t struct {
Device string `json:"device"` Server string `json:"server"`
Label string `json:"label"` Label string `json:"label"`
Variables map[string]variable_t `json:"variables"` Variables map[string]variable_t `json:"variables"`
} }
@ -34,36 +32,28 @@ func Start() {
log.Println("Polling server " + server.Name) log.Println("Polling server " + server.Name)
message := message_t { message := message_t {
Device: server.Name, Server: server.Name,
Label: server.Label, Label: server.Label,
Variables: make(map[string]variable_t), Variables: make(map[string]variable_t),
} }
label := "rootdisp"
status := "Ok" status := "Ok"
rootdisp := 0.0 value := 0.0
stratum := 0
resp, err := ntp.Query(server.Name) resp, err := ntp.Query(server.Name)
if err != nil { if err != nil {
status = "Error" status = "Error"
} else { } else {
rootdisp = resp.RootDispersion.Seconds() * 1000 value = resp.RootDispersion.Seconds() * 1000
stratum = int(resp.Stratum)
} }
message.Variables["rootdisp"] = variable_t { v := variable_t {
Label: "rootdisp", Name: label,
Variable: "", Value: value,
Value: strconv.FormatFloat(rootdisp, 'f', 4, 64),
Unit: "ms",
Status: status,
}
message.Variables["stratum"] = variable_t {
Label: "stratum",
Variable: "",
Value: strconv.Itoa(stratum),
Unit: "ms", Unit: "ms",
Status: status, Status: status,
} }
message.Variables[label] = v
j, err := json.Marshal(message) j, err := json.Marshal(message)