2015-11-06 09:27:17 +00:00
|
|
|
package amon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/internal"
|
2017-02-04 15:58:02 +00:00
|
|
|
"github.com/influxdata/telegraf/registry/outputs"
|
2015-11-06 09:27:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Amon struct {
|
|
|
|
ServerKey string
|
|
|
|
AmonInstance string
|
2015-11-10 21:40:39 +00:00
|
|
|
Timeout internal.Duration
|
2015-11-06 09:27:17 +00:00
|
|
|
|
|
|
|
client *http.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
var sampleConfig = `
|
2016-02-18 21:26:51 +00:00
|
|
|
## Amon Server Key
|
2015-11-06 09:27:17 +00:00
|
|
|
server_key = "my-server-key" # required.
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Amon Instance URL
|
2015-11-06 09:27:17 +00:00
|
|
|
amon_instance = "https://youramoninstance" # required
|
|
|
|
|
2016-02-18 21:26:51 +00:00
|
|
|
## Connection timeout.
|
2015-11-06 09:27:17 +00:00
|
|
|
# timeout = "5s"
|
|
|
|
`
|
|
|
|
|
|
|
|
type TimeSeries struct {
|
|
|
|
Series []*Metric `json:"series"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Metric struct {
|
|
|
|
Metric string `json:"metric"`
|
2016-01-27 23:15:14 +00:00
|
|
|
Points [1]Point `json:"metrics"`
|
2015-11-06 09:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Point [2]float64
|
|
|
|
|
|
|
|
func (a *Amon) Connect() error {
|
|
|
|
if a.ServerKey == "" || a.AmonInstance == "" {
|
|
|
|
return fmt.Errorf("serverkey and amon_instance are required fields for amon output")
|
|
|
|
}
|
|
|
|
a.client = &http.Client{
|
|
|
|
Timeout: a.Timeout.Duration,
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func (a *Amon) Write(metrics []telegraf.Metric) error {
|
|
|
|
if len(metrics) == 0 {
|
2015-11-06 09:27:17 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
ts := TimeSeries{}
|
2015-12-19 21:19:43 +00:00
|
|
|
tempSeries := []*Metric{}
|
|
|
|
metricCounter := 0
|
2015-12-19 21:08:31 +00:00
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, m := range metrics {
|
|
|
|
mname := strings.Replace(m.Name(), "_", ".", -1)
|
|
|
|
if amonPts, err := buildMetrics(m); err == nil {
|
2015-12-19 21:08:31 +00:00
|
|
|
for fieldName, amonPt := range amonPts {
|
|
|
|
metric := &Metric{
|
|
|
|
Metric: mname + "_" + strings.Replace(fieldName, "_", ".", -1),
|
|
|
|
}
|
|
|
|
metric.Points[0] = amonPt
|
2015-12-19 21:19:43 +00:00
|
|
|
tempSeries = append(tempSeries, metric)
|
|
|
|
metricCounter++
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2015-11-06 09:27:17 +00:00
|
|
|
} else {
|
2016-09-30 21:37:56 +00:00
|
|
|
log.Printf("I! unable to build Metric for %s, skipping\n", m.Name())
|
2015-11-06 09:27:17 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-19 21:08:31 +00:00
|
|
|
|
2015-12-19 21:19:43 +00:00
|
|
|
ts.Series = make([]*Metric, metricCounter)
|
2015-11-06 09:27:17 +00:00
|
|
|
copy(ts.Series, tempSeries[0:])
|
|
|
|
tsBytes, err := json.Marshal(ts)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to marshal TimeSeries, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
req, err := http.NewRequest("POST", a.authenticatedUrl(), bytes.NewBuffer(tsBytes))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to create http.Request, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
req.Header.Add("Content-Type", "application/json")
|
|
|
|
|
|
|
|
resp, err := a.client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error POSTing metrics, %s\n", err.Error())
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode < 200 || resp.StatusCode > 209 {
|
|
|
|
return fmt.Errorf("received bad status code, %d\n", resp.StatusCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Amon) SampleConfig() string {
|
|
|
|
return sampleConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Amon) Description() string {
|
|
|
|
return "Configuration for Amon Server to send metrics to."
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Amon) authenticatedUrl() string {
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s/api/system/%s", a.AmonInstance, a.ServerKey)
|
|
|
|
}
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
func buildMetrics(m telegraf.Metric) (map[string]Point, error) {
|
|
|
|
ms := make(map[string]Point)
|
|
|
|
for k, v := range m.Fields() {
|
2015-12-19 21:08:31 +00:00
|
|
|
var p Point
|
|
|
|
if err := p.setValue(v); err != nil {
|
2016-01-27 23:15:14 +00:00
|
|
|
return ms, fmt.Errorf("unable to extract value from Fields, %s", err.Error())
|
2015-12-19 21:08:31 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
p[0] = float64(m.Time().Unix())
|
|
|
|
ms[k] = p
|
2015-11-06 09:27:17 +00:00
|
|
|
}
|
2016-01-27 23:15:14 +00:00
|
|
|
return ms, nil
|
2015-11-06 09:27:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Point) setValue(v interface{}) error {
|
|
|
|
switch d := v.(type) {
|
|
|
|
case int:
|
|
|
|
p[1] = float64(int(d))
|
|
|
|
case int32:
|
|
|
|
p[1] = float64(int32(d))
|
|
|
|
case int64:
|
|
|
|
p[1] = float64(int64(d))
|
|
|
|
case float32:
|
|
|
|
p[1] = float64(d)
|
|
|
|
case float64:
|
|
|
|
p[1] = float64(d)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("undeterminable type")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Amon) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
2016-01-27 21:21:36 +00:00
|
|
|
outputs.Add("amon", func() telegraf.Output {
|
2015-11-06 09:27:17 +00:00
|
|
|
return &Amon{}
|
|
|
|
})
|
|
|
|
}
|