2015-01-07 02:28:34 +00:00
|
|
|
package collectd
|
2015-01-07 23:10:27 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/kimor79/gollectd"
|
|
|
|
)
|
|
|
|
|
2015-01-08 21:36:07 +00:00
|
|
|
// TODO corylanou: This needs to be made a public `main.Point` so we can share this across packages.
|
2015-01-07 23:10:27 +00:00
|
|
|
type Metric struct {
|
|
|
|
Name string
|
|
|
|
Tags map[string]string
|
|
|
|
Value interface{}
|
|
|
|
Timestamp time.Time
|
|
|
|
}
|
|
|
|
|
2015-01-08 21:36:07 +00:00
|
|
|
func Unmarshal(data *gollectd.Packet) []Metric {
|
|
|
|
// Prefer high resolution timestamp.
|
|
|
|
var timeStamp time.Time
|
|
|
|
if data.TimeHR > 0 {
|
2015-01-08 21:37:10 +00:00
|
|
|
// TimeHR is "near" nanosecond measurement, but not exactly nanasecond time
|
2015-01-08 21:36:07 +00:00
|
|
|
// Since we store time in microseconds, we round here (mostly so tests will work easier)
|
|
|
|
sec := data.TimeHR >> 30
|
|
|
|
// Shifting, masking, and dividing by 1 billion to get nanoseconds.
|
|
|
|
nsec := ((data.TimeHR & 0x3FFFFFFF) << 30) / 1000 / 1000 / 1000
|
|
|
|
timeStamp = time.Unix(int64(sec), int64(nsec)).UTC().Round(time.Microsecond)
|
|
|
|
} else {
|
|
|
|
// If we don't have high resolution time, fall back to basic unix time
|
|
|
|
timeStamp = time.Unix(int64(data.Time), 0).UTC().Round(time.Microsecond)
|
2015-01-07 23:10:27 +00:00
|
|
|
}
|
|
|
|
|
2015-01-08 21:36:07 +00:00
|
|
|
var m []Metric
|
2015-01-07 23:10:27 +00:00
|
|
|
for i, _ := range data.Values {
|
|
|
|
metric := Metric{Name: fmt.Sprintf("%s_%s", data.Plugin, data.Values[i].Name)}
|
|
|
|
metric.Value = data.Values[i].Value
|
2015-01-08 21:36:07 +00:00
|
|
|
metric.Timestamp = timeStamp
|
2015-01-07 23:10:27 +00:00
|
|
|
|
|
|
|
if data.Hostname != "" {
|
|
|
|
metric.Tags["host"] = data.Hostname
|
|
|
|
}
|
|
|
|
if data.PluginInstance != "" {
|
|
|
|
metric.Tags["instance"] = data.PluginInstance
|
|
|
|
}
|
|
|
|
if data.Type != "" {
|
|
|
|
metric.Tags["type"] = data.Type
|
|
|
|
}
|
|
|
|
if data.TypeInstance != "" {
|
|
|
|
metric.Tags["type_instance"] = data.TypeInstance
|
|
|
|
}
|
|
|
|
m = append(m, metric)
|
|
|
|
}
|
2015-01-08 21:36:07 +00:00
|
|
|
return m
|
2015-01-07 23:10:27 +00:00
|
|
|
}
|