snmp
This commit is contained in:
74
src/udi/handlers/snmp/snmp.go
Normal file
74
src/udi/handlers/snmp/snmp.go
Normal file
@ -0,0 +1,74 @@
|
||||
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"`
|
||||
DiffValue string `json:"diffValue"`
|
||||
}
|
||||
|
||||
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()
|
||||
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 = make(map[string]interface{})
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user