Last active
November 2, 2015 16:01
-
-
Save pteich/4fb1a912cdee66eb81a6 to your computer and use it in GitHub Desktop.
Request Wowza Stream Listeners
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
package main | |
import ( | |
"encoding/xml" | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
"net/url" | |
httpDigestAuth "github.com/pteich/http-digest-auth-client" | |
) | |
var DigestAuth *httpDigestAuth.DigestHeaders | |
type WowzaStreamdetails struct { | |
Url string `xml:"Name" json:"-"` | |
Listeners int `xml:"SessionsTotal" json:"-"` | |
} | |
type WowzaStreams struct { | |
Stream []WowzaStreamdetails `xml:"VHost>Application>ApplicationInstance>Stream"` | |
} | |
func checkWowzaListmounts(host string, timestamp int64) { | |
req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/connectioncounts.xml", host), nil) | |
check(err) | |
// apply HTTP digest authentication | |
DigestAuth = &httpDigestAuth.DigestHeaders{} | |
DigestAuth, err = DigestAuth.Auth(conf.Wowza.Username, conf.Wowza.Password, fmt.Sprintf("http://%s/connectioncounts.xml", host)) | |
check(err) | |
DigestAuth.ApplyAuth(req) | |
resp, err := httpClientIcecast.Do(req) | |
if resp == nil { | |
check(err) | |
return | |
} | |
if err != nil { | |
check(err) | |
return | |
} | |
defer resp.Body.Close() | |
if body, err := ioutil.ReadAll(resp.Body); err == nil { | |
var streams WowzaStreams | |
if err := xml.Unmarshal(body, &streams); err != nil { | |
check(err) | |
return | |
} | |
buffer := new(bytes.Buffer) | |
var total int = 0 | |
// range over all streams | |
for _, stream := range streams.Stream { | |
// Wowza url escapes our stream url | |
if quUrl, err := url.QueryUnescape(stream.Url); err == nil { | |
if parsedUrl, err := url.Parse(quUrl); err == nil { | |
buffer.WriteString(fmt.Sprintf("listeners,host=%s,mount=%s value=%di %d\n", host, parsedUrl.Path, stream.Listeners, timestamp)) | |
total += stream.Listeners | |
} | |
} | |
} | |
// write total listener for host | |
buffer.WriteString(fmt.Sprintf("listenerstotal,host=%s value=%di %d\n", host, total, timestamp)) | |
} else { | |
check(err) | |
return | |
} | |
seriesChannel <- buffer | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment