-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Amon output #350
Closed
Closed
Add Amon output #350
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d315f3
Amon output
martinrusev ab86ed2
Register Amon
martinrusev d3ae5ad
Update amon output
martinrusev 1c92cfb
Fix indentation
martinrusev 9030e8b
Update identation
martinrusev f3ef4e6
Update tests
martinrusev 1f5bef1
Spelling + Readme/Changelog updates
martinrusev 9cf7a54
server_key typo
martinrusev d9d461c
Sync with upstream
martinrusev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Amon Output Plugin | ||
|
||
This plugin writes to [Amon](https://www.amon.cx) | ||
and requires an `serverkey` and `amoninstance` URL which can be obtained [here](https://www.amon.cx/docs/monitoring/) | ||
for the account. | ||
|
||
If the point value being sent cannot be converted to a float64, the metric is skipped. | ||
|
||
Metrics are grouped by converting any `_` characters to `.` in the Point Name. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package amon | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/influxdb/influxdb/client/v2" | ||
"github.com/influxdb/telegraf/duration" | ||
"github.com/influxdb/telegraf/outputs" | ||
) | ||
|
||
type Amon struct { | ||
ServerKey string | ||
AmonInstance string | ||
Timeout duration.Duration | ||
|
||
client *http.Client | ||
} | ||
|
||
var sampleConfig = ` | ||
# Amon Server Key | ||
serverkey = "my-server-key" # required. | ||
|
||
# Amon Insance URL | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. spelling |
||
amoninstance = "https://youramoninstance" # required | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
# Connection timeout. | ||
# timeout = "5s" | ||
` | ||
|
||
type TimeSeries struct { | ||
Series []*Metric `json:"series"` | ||
} | ||
|
||
type Metric struct { | ||
Metric string `json:"metric"` | ||
Points [1]Point `json:"points"` | ||
} | ||
|
||
type Point [2]float64 | ||
|
||
func (a *Amon) Connect() error { | ||
if a.ServerKey == "" || a.AmonInstance == "" { | ||
return fmt.Errorf("serverkey and amoninstance are required fields for amon output") | ||
} | ||
a.client = &http.Client{ | ||
Timeout: a.Timeout.Duration, | ||
} | ||
return nil | ||
} | ||
|
||
func (a *Amon) Write(points []*client.Point) error { | ||
if len(points) == 0 { | ||
return nil | ||
} | ||
ts := TimeSeries{} | ||
var tempSeries = make([]*Metric, len(points)) | ||
var acceptablePoints = 0 | ||
for _, pt := range points { | ||
metric := &Metric{ | ||
Metric: strings.Replace(pt.Name(), "_", ".", -1), | ||
} | ||
if p, err := buildPoint(pt); err == nil { | ||
metric.Points[0] = p | ||
tempSeries[acceptablePoints] = metric | ||
acceptablePoints += 1 | ||
} else { | ||
log.Printf("unable to build Metric for %s, skipping\n", pt.Name()) | ||
} | ||
} | ||
ts.Series = make([]*Metric, acceptablePoints) | ||
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) | ||
} | ||
|
||
func buildPoint(pt *client.Point) (Point, error) { | ||
var p Point | ||
if err := p.setValue(pt.Fields()["value"]); err != nil { | ||
return p, fmt.Errorf("unable to extract value from Fields, %s", err.Error()) | ||
} | ||
p[0] = float64(pt.Time().Unix()) | ||
return p, nil | ||
} | ||
|
||
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() { | ||
outputs.Add("amon", func() outputs.Output { | ||
return &Amon{} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,163 @@ | ||
package amon | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/influxdb/telegraf/testutil" | ||
|
||
"github.com/influxdb/influxdb/client/v2" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
fakeServerKey = "123456" | ||
fakeAmonInstance = "https://demo.amon.cx" | ||
) | ||
|
||
func TestUriOverride(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
json.NewEncoder(w).Encode(`{"status":"ok"}`) | ||
})) | ||
defer ts.Close() | ||
|
||
a := &Amon{ | ||
ServerKey: fakeServerKey, | ||
AmonInstance: fakeAmonInstance, | ||
} | ||
|
||
err := a.Connect() | ||
require.NoError(t, err) | ||
err = a.Write(testutil.MockBatchPoints().Points()) | ||
require.NoError(t, err) | ||
} | ||
|
||
func TestAuthenticatedUrl(t *testing.T) { | ||
a := &Amon{ | ||
ServerKey: fakeServerKey, | ||
AmonInstance: fakeAmonInstance, | ||
} | ||
|
||
authUrl := a.authenticatedUrl() | ||
assert.EqualValues(t, fmt.Sprintf("%s/api/system/%s", fakeAmonInstance, fakeServerKey), authUrl) | ||
} | ||
|
||
func TestBuildPoint(t *testing.T) { | ||
tags := make(map[string]string) | ||
var tagtests = []struct { | ||
ptIn *client.Point | ||
outPt Point | ||
err error | ||
}{ | ||
{ | ||
client.NewPoint( | ||
"test1", | ||
tags, | ||
map[string]interface{}{"value": 0.0}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
0.0, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test2", | ||
tags, | ||
map[string]interface{}{"value": 1.0}, | ||
time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2010, time.December, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
1.0, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test3", | ||
tags, | ||
map[string]interface{}{"value": 10}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
10.0, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test4", | ||
tags, | ||
map[string]interface{}{"value": int32(112345)}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
112345.0, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test5", | ||
tags, | ||
map[string]interface{}{"value": int64(112345)}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
112345.0, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test6", | ||
tags, | ||
map[string]interface{}{"value": float32(11234.5)}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
11234.5, | ||
}, | ||
nil, | ||
}, | ||
{ | ||
client.NewPoint( | ||
"test7", | ||
tags, | ||
map[string]interface{}{"value": "11234.5"}, | ||
time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), | ||
), | ||
Point{ | ||
float64(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC).Unix()), | ||
11234.5, | ||
}, | ||
fmt.Errorf("unable to extract value from Fields, undeterminable type"), | ||
}, | ||
} | ||
for _, tt := range tagtests { | ||
pt, err := buildPoint(tt.ptIn) | ||
if err != nil && tt.err == nil { | ||
t.Errorf("%s: unexpected error, %+v\n", tt.ptIn.Name(), err) | ||
} | ||
if tt.err != nil && err == nil { | ||
t.Errorf("%s: expected an error (%s) but none returned", tt.ptIn.Name(), tt.err.Error()) | ||
} | ||
if !reflect.DeepEqual(pt, tt.outPt) && tt.err == nil { | ||
t.Errorf("%s: \nexpected %+v\ngot %+v\n", tt.ptIn.Name(), tt.outPt, pt) | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be
server_key