Skip to content

Instantly share code, notes, and snippets.

@pteich
Last active January 16, 2021 18:35
Show Gist options
  • Save pteich/961d1a0226959502e3cb to your computer and use it in GitHub Desktop.
Save pteich/961d1a0226959502e3cb to your computer and use it in GitHub Desktop.
Request Listeners Stats from Icecast
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"time"
)
// SourceListmounts holds single listener elements from Icecast XML
type SourceListmounts struct {
Mount string `xml:"mount,attr"`
Listeners int32 `xml:"listeners"`
Connected int32 `xml:"connected"`
ContentType string `xml:"content-type"`
}
// Listmounts main structure of icecast XML
type Listmounts struct {
Sources []SourceListmounts `xml:"source"`
}
// this function will be executet as go routine for every host
func checkIcecastListmounts(host string, timestamp int64) {
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/admin/listmounts", host), nil)
if err != nil {
check(err)
return
}
req.SetBasicAuth(conf.Icecast.Username, conf.Icecast.Password)
startTime := time.Now()
icecastResponse, err := httpClientIcecast.Do(req)
if icecastResponse == nil {
check(err)
return
}
if err != nil {
check(err)
return
}
elapsedTime := time.Since(startTime)
defer icecastResponse.Body.Close()
var listmounts Listmounts
if body, err := ioutil.ReadAll(icecastResponse.Body); err == nil {
if err := xml.Unmarshal(body, &listmounts); err != nil {
check(err)
return
}
} else {
check(err)
return
}
// create buffer for series data
buffer := new(bytes.Buffer)
// write data for connect duration
buffer.WriteString(fmt.Sprintf("response,host=%s value=%v %d\n", host, elapsedTime.Seconds()*1000, timestamp))
var total int = 0
// iterate over all source nodes
for _, sources := range listmounts.Sources {
// write listener count to measure "listeners" with tags host and mount (host-name and mount-name)
buffer.WriteString(fmt.Sprintf("listeners,host=%s,mount=%s value=%di %d\n", host, sources.Mount, sources.Listeners, timestamp))
total += sources.Listeners
}
// write total listener for host
buffer.WriteString(fmt.Sprintf("listenerstotal,host=%s value=%di %d\n", host, total, timestamp))
// write buffer to channel so it can be send to InfluxDB
seriesChannel <- buffer
}
@dgagnon-rdx
Copy link

Hi, How it's works? I'm interested in giving it a try but I'm new to influx/telegraf so I don't know how to use an external plugin that isn't build in. Also, how can I configure it so it can read my url. Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment