77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package snmp
|
|
|
|
import (
|
|
"time"
|
|
"log"
|
|
"encoding/json"
|
|
"udi/config"
|
|
"udi/handlers/handler"
|
|
"udi/database"
|
|
)
|
|
|
|
|
|
type SnmpHandler struct {
|
|
handler.CommonHandler
|
|
dbh *database.DatabaseHandle
|
|
}
|
|
|
|
type endpoint_t struct {
|
|
Label string `json:"label"`
|
|
Variable string `json:"variable"`
|
|
Value string `json:"value"`
|
|
}
|
|
|
|
type observation_t struct {
|
|
Device string `json:"device"`
|
|
Label string `json:"label"`
|
|
Variables map[string]endpoint_t `json:"variables"`
|
|
}
|
|
|
|
|
|
func New(id string, config config.HandlerConfigT) handler.Handler {
|
|
t := &SnmpHandler {
|
|
}
|
|
t.Id = id
|
|
t.dbh = database.NewDatabaseHandle()
|
|
log.Printf("Handler SNMP %d initialized", id)
|
|
return t
|
|
}
|
|
|
|
func (self *SnmpHandler) Handle(message handler.MessageT) {
|
|
// log.Printf("Handler SNMP %d processing %s -> %s", self.Id, message.Topic, message.Payload)
|
|
|
|
var observation observation_t
|
|
err := json.Unmarshal([]byte(message.Payload), &observation)
|
|
if err != nil {
|
|
self.Lost("Unable to parse payload into Observation struct", err, message)
|
|
return
|
|
}
|
|
|
|
var measurement database.Measurement
|
|
measurement.Time = time.Now()
|
|
|
|
measurement.Application = "SNMP"
|
|
measurement.Device = observation.Device
|
|
|
|
measurement.Attributes = map[string]interface{} {
|
|
"Label": observation.Label,
|
|
}
|
|
|
|
measurement.Values = make(map[string]database.VariableType)
|
|
for k, v := range observation.Variables {
|
|
measurement.Values[k] = database.VariableType {
|
|
Label: v.Label,
|
|
Variable: v.Variable,
|
|
Unit: "",
|
|
Value: v.Value,
|
|
}
|
|
}
|
|
|
|
// log.Printf("Prepared measurement item: %s", measurement)
|
|
|
|
self.dbh.StoreMeasurement(&measurement)
|
|
self.S()
|
|
}
|
|
|
|
|