This commit is contained in:
Stefan Schwarz 2020-12-28 01:26:55 +01:00
commit d6cd5b5815
4 changed files with 99 additions and 0 deletions

14
Dockerfile Normal file
View file

@ -0,0 +1,14 @@
FROM golang:1.15 as build
ADD . /app
WORKDIR /app
ENV GOOS=linux
ENV CGO_ENABLED=0
RUN go build .
# ---
FROM busybox
COPY --from=build /app/viewercount /usr/local/bin/viewercount
CMD /usr/local/bin/viewercount

3
go.mod Normal file
View file

@ -0,0 +1,3 @@
module foosinn/viewercount
go 1.15

72
main.go Normal file
View file

@ -0,0 +1,72 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"regexp"
"strconv"
"net/http"
)
const COUNTER_SLOTS = 20
func main() {
_ = make([]int, 10)
_ = make([]int, 10)
counter := NewCounter()
go counter.ScanStdin()
http.HandleFunc("/", counter.MetricsHandler)
err := http.ListenAndServe(":8080", nil)
log.Fatalf("unable to listen: %s", err)
}
type Counter struct {
counters []int
names []string
current int
}
func NewCounter() *Counter {
return &Counter{
counters: make([]int, COUNTER_SLOTS),
names: make([]string, COUNTER_SLOTS),
}
}
func (c *Counter) MetricsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/plain")
for i, name := range c.names {
fmt.Fprintf(w, "rc3_stream_count[name=%q] %d\n", name, c.counters[i])
}
fmt.Fprintf(w, "rc3_stream_current[name=\"%d\"] 1\n", c.current)
}
func (c *Counter) ScanStdin() {
matcher := regexp.MustCompile(`/hls/stream-([0-9]*).ts`)
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
matches := matcher.FindStringSubmatch(scanner.Text())
if len(matches) == 2 {
c.countViewers(matches[1])
}
}
}
func (c *Counter) countViewers(part string) {
i, _ := strconv.Atoi(part)
mod := i % COUNTER_SLOTS
if c.names[mod] != part {
c.counters[mod] = 0
}
c.current = i
c.counters[mod] += 1
c.names[mod] = part
}

10
sample.txt Normal file
View file

@ -0,0 +1,10 @@
172.19.0.6 - - [27/Dec/2020:22:37:43 +0000] "GET /hls/stream-2817.ts HTTP/1.1" 200 479212 "https://live.franconian.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0" "5.100.54.73"
172.19.0.6 - - [27/Dec/2020:22:37:43 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 200 196 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0" "5.100.54.73"
172.19.0.6 - - [27/Dec/2020:22:37:43 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 200 196 "https://live.franconian.net/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:84.0) Gecko/20100101 Firefox/84.0" "217.61.144.191"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 200 196 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" "62.216.202.213"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 200 196 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36" "178.27.127.210"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream-2817.ts HTTP/1.1" 200 479212 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" "62.216.202.213"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream-2817.ts HTTP/1.1" 200 479212 "https://live.franconian.net/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:84.0) Gecko/20100101 Firefox/84.0" "188.193.113.175"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 200 196 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" "116.202.5.117"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream.m3u8 HTTP/1.1" 304 0 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:83.0) Gecko/20100101 Firefox/83.0" "185.53.42.143"
172.19.0.6 - - [27/Dec/2020:22:37:44 +0000] "GET /hls/stream-2817.ts HTTP/1.1" 200 479212 "https://live.franconian.net/" "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0" "116.202.5.117"