Created
December 21, 2017 22:22
-
-
Save iMilnb/523c70c9744dcf2d437e5ca9915f866d to your computer and use it in GitHub Desktop.
collectd go plugin to fetch Bitstamp prices
This file contains hidden or 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
package main | |
import ( | |
"encoding/json" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"strconv" | |
"time" | |
"collectd.org/api" | |
"collectd.org/exec" | |
"collectd.org/plugin" | |
) | |
func errHandle(errMsg error) { | |
if errMsg != nil { | |
log.Fatal(errMsg) | |
} | |
} | |
func bitstampFetch(url string) string { | |
resp, getErr := http.Get(url) | |
errHandle(getErr) | |
body, readErr := ioutil.ReadAll(resp.Body) | |
errHandle(readErr) | |
res := make(map[string]string) | |
errHandle(json.Unmarshal(body, &res)) | |
return res["last"] | |
} | |
type Bitstamp struct{} | |
func (Bitstamp) Read() error { | |
var url string | |
var v string | |
var l float64 | |
var convErr error | |
pairs := []string{"ethusd", "xrpusd", "ltcusd", "btcusd"} | |
for _, v = range pairs { | |
url = "https://www.bitstamp.net/api/v2/ticker/" + v + "/" | |
l, convErr = strconv.ParseFloat(bitstampFetch(url), 64) | |
errHandle(convErr) | |
vl := api.ValueList{ | |
Identifier: api.Identifier{ | |
Host: exec.Hostname(), | |
Plugin: "bitstamp", | |
PluginInstance: v, | |
Type: "gauge", | |
}, | |
Time: time.Now(), | |
Interval: 60 * time.Second, | |
Values: []api.Value{api.Gauge(l)}, | |
} | |
if err := plugin.Write(&vl); err != nil { | |
plugin.Error(err) | |
} | |
} | |
return nil | |
} | |
func init() { | |
plugin.RegisterRead("bitstamp", &Bitstamp{}) | |
} | |
func main() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment