Skip to content

Instantly share code, notes, and snippets.

@iMilnb
Created December 21, 2017 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iMilnb/523c70c9744dcf2d437e5ca9915f866d to your computer and use it in GitHub Desktop.
Save iMilnb/523c70c9744dcf2d437e5ca9915f866d to your computer and use it in GitHub Desktop.
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