initial prototype complete, starting to package for docker

This commit is contained in:
2023-09-25 19:45:36 -04:00
parent e1e64cdad9
commit 28df368156
6 changed files with 442 additions and 0 deletions

91
main.go Normal file
View File

@@ -0,0 +1,91 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type serverSettings struct {
port int
plexAddr string
plexToken string
plexTimeout int
metricsPrefix string
metricsMediaCollectingIntervalSeconds int
}
func envGetter(key, fallback string) string {
if value, ok := os.LookupEnv(key); ok {
return value
}
return fallback
}
func setupServerSettings() serverSettings {
port, err := strconv.Atoi(envGetter("PORT", "9545"))
if err != nil {
log.Fatal("Unable to parse port string to int, failing....")
}
plexToken, value := os.LookupEnv("PLEX_TOKEN")
if !value {
log.Fatal("No plex token provided, failing....")
}
plexTimeout, err := strconv.Atoi(envGetter("PLEX_TIMEOUT", "10"))
if err != nil {
log.Fatal("Unable to parse plextimeout string to int, failing....")
}
metricsMediaCollectingIntervalSeconds, err := strconv.Atoi(envGetter("METRICS_MEDIA_COLLECTING_INTERVAL_SECONDS", "300"))
if err != nil {
log.Fatal("Unable to parse METRICS_MEDIA_COLLECTING_INTERVAL_SECONDS string to int, failing....")
}
return serverSettings{
port: port,
plexAddr: envGetter("PLEX_ADDR", "http://localhost:32400"),
plexToken: plexToken,
plexTimeout: plexTimeout,
metricsPrefix: envGetter("METRICS_PREFIX", "PLEX"),
metricsMediaCollectingIntervalSeconds: metricsMediaCollectingIntervalSeconds,
}
}
func main() {
os.Setenv("PORT", "9545")
os.Setenv("PLEX_ADDR", "https://plex.derajnet.duckdns.org:32400")
os.Setenv("PLEX_TOKEN", "5ezJu5cjnhoAbPJRKngs")
os.Setenv("PLEX_TIMEOUT", "10")
os.Setenv("METRICS_PREFIX", "PLEX")
os.Setenv("METRICS_MEDIA_COLLECTING_INTERVAL_SECONDS", "300")
settings := setupServerSettings()
// Setup plex client
pc := setupClient(&settings)
prometheus.MustRegister(plexActiveSessions)
prometheus.MustRegister(plexNumMovies)
prometheus.MustRegister(plexNumTVShows)
go func() {
for {
time.Sleep(time.Second * 5)
fmt.Println("Collecting info...")
stats := pc.gatherAllStats()
plexActiveSessions.Set(stats.currentSessions)
plexNumMovies.Set(stats.numMovies)
plexNumTVShows.Set(stats.numTV)
}
}()
// m := NewMetrics(reg)
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(fmt.Sprintf(":%d", settings.port), nil)
}