diff --git a/homepage.html b/homepage.html
new file mode 100644
index 0000000..6d9aaf9
--- /dev/null
+++ b/homepage.html
@@ -0,0 +1,13 @@
+
+
+
+ Plex Data Exporter
+
+
+ Hello, you have reached the homepage of a prometheus/jsonapi plex exporter!
+
+ You most likely want to configure prometheus to hit this server at the /metrics route to pull data.
+
+ Or you can use grafana JSONAPI plugin to pull ephemeral text data from /plex-stats.
+
+
\ No newline at end of file
diff --git a/main.go b/main.go
index 5e60b4c..efc963c 100644
--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@ import (
"net/http"
"os"
"strconv"
+ "text/template"
"time"
"github.com/prometheus/client_golang/prometheus"
@@ -68,6 +69,12 @@ func setupJSONAPI(plexStatsChannel chan *plexStats, settings serverSettings) {
// Create a new router
mux := http.NewServeMux()
+ tmpl := template.Must(template.ParseFiles("homepage.html"))
+
+ mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ tmpl.Execute(w, nil)
+ })
+
// Create a handler for the `/plex-stats` endpoint
mux.HandleFunc("/plex-stats", func(w http.ResponseWriter, r *http.Request) {
// Create a new plexStats struct
@@ -89,10 +96,16 @@ func setupJSONAPI(plexStatsChannel chan *plexStats, settings serverSettings) {
// Start the server
fmt.Println("Starting JSON API Server.....")
- err := http.ListenAndServe(fmt.Sprintf(":%d", settings.jsonPort), mux)
- if err != nil {
- log.Fatal("Unable to start JSON Server on port: ", settings.jsonPort, " with err: ", err)
- }
+ http.ListenAndServe(fmt.Sprintf(":%d", settings.jsonPort), mux)
+
+}
+
+func setStats(stats *plexStats) {
+ plexActiveSessions.Set(stats.CurrentSessions)
+ plexNumMovies.Set(stats.NumMovies)
+ plexNumTVShows.Set(stats.NumTV)
+ plexTranscodeSessions.Set(stats.NumTranscodes)
+ plexAllSessions.Set(stats.NumAllSessions)
}
func main() {
@@ -108,15 +121,11 @@ func main() {
settings := setupServerSettings()
// Setup plex client
pc := setupClient(&settings)
-
- prometheus.MustRegister(plexActiveSessions)
- prometheus.MustRegister(plexNumMovies)
- prometheus.MustRegister(plexNumTVShows)
- prometheus.MustRegister(plexTranscodeSessions)
- prometheus.MustRegister(plexAllSessions)
-
+ // Our initial gather to ensure we don't send zeros to prometheus
+ stats := pc.gatherAllStats()
+ setStats(stats)
+ // Creating a data sharing channel to send same data to our json api server
plexStatsChannel := make(chan *plexStats)
-
go setupJSONAPI(plexStatsChannel, settings)
go func() {
@@ -124,17 +133,29 @@ func main() {
time.Sleep(time.Second * 5)
fmt.Println("Collecting info...")
stats := pc.gatherAllStats()
- plexStatsChannel <- stats
- plexActiveSessions.Set(stats.CurrentSessions)
- plexNumMovies.Set(stats.NumMovies)
- plexNumTVShows.Set(stats.NumTV)
- plexTranscodeSessions.Set(stats.NumTranscodes)
- plexAllSessions.Set(stats.NumAllSessions)
+ setStats(stats)
+ // Kind of an ugly workaround?
+ //Will cause some travel times to be up to 5 seconds as the channel in the mux waits for data to be populated?
+ select {
+ case plexStatsChannel <- stats:
+ default:
+ }
+
}
}()
+ prometheus.MustRegister(plexActiveSessions)
+ prometheus.MustRegister(plexNumMovies)
+ prometheus.MustRegister(plexNumTVShows)
+ prometheus.MustRegister(plexTranscodeSessions)
+ prometheus.MustRegister(plexAllSessions)
+
// m := NewMetrics(reg)
+ fmt.Println("Startup of promethus handler complete...")
http.Handle("/metrics", promhttp.Handler())
- http.ListenAndServe(fmt.Sprintf(":%d", settings.port), nil)
+ err := http.ListenAndServe(fmt.Sprintf(":%d", settings.port), nil)
+ if err != nil {
+ log.Fatal("Unable to start the prometheus handler....")
+ }
}
diff --git a/plex_client.go b/plex_client.go
index 6d6affd..9b10592 100644
--- a/plex_client.go
+++ b/plex_client.go
@@ -115,7 +115,7 @@ func (pc *PlexClient) getNumTranscodes() *RawPlexModel {
// getServerHistory gets the entire history of plays on the plex server
func (pc *PlexClient) getServerHistory() *RawPlexModel {
- result := pc.sendRequest("/status/sessions/history/all", nil) //Hard code library ID for now TODO: Grab from library/sections call
+ result := pc.sendRequest("/status/sessions/history/all", nil)
sessionsAll := RawPlexModel{}
err := json.Unmarshal(result, &sessionsAll)
if err != nil {