Skip to content

Instantly share code, notes, and snippets.

@iMilnb iMilnb/bitstamp.go
Created Dec 21, 2017

Embed
What would you like to do?
collectd go plugin to fetch Bitstamp prices
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
You can’t perform that action at this time.