From 5c230986d0579a2580e8c2014e4f85f37756aa45 Mon Sep 17 00:00:00 2001 From: martin Date: Mon, 13 Feb 2023 00:59:12 +0100 Subject: [PATCH] wip Signed-off-by: martin --- src/immich/data.go | 18 ++++++------ src/immich/sendbackmessage.go | 52 +++++++++++++++++++++++++++++------ src/main.go | 28 ++++++++----------- src/promtheus/tests.go | 26 ------------------ 4 files changed, 66 insertions(+), 58 deletions(-) delete mode 100644 src/promtheus/tests.go diff --git a/src/immich/data.go b/src/immich/data.go index 0a50f7b..2fba021 100644 --- a/src/immich/data.go +++ b/src/immich/data.go @@ -7,20 +7,23 @@ import ( "io/ioutil" "log" "net/http" + + "github.com/prometheus/client_golang/prometheus" ) -func Allrequests() string { +func Allrequests(r *prometheus.Registry) { - return serverversion() + Analyze() + serverversion(r) + Analyze(r) } -func Analyze() string { +func Analyze(r *prometheus.Registry) { allusers, err := GetAllUsers() users, err2 := users() if err != nil && err2 != nil { - return "" + } else { - return Sendbackmessagepreference(users, allusers) + Sendbackmessagepreference(users, allusers, r) } } @@ -57,7 +60,7 @@ func GetAllUsers() (*models.AllUsers, error) { return &models.AllUsers{}, err } -func serverversion() string { +func serverversion(r *prometheus.Registry) { resp, err := Apirequest("/api/server-info/version", "GET") if err != nil { if err.Error() == "403" { @@ -82,12 +85,11 @@ func serverversion() string { log.Println("Can not unmarshal JSON") } - return Sendbackmessageserverversion(&result) + Sendbackmessageserverversion(&result, r) } } - return "" } func users() (*models.Users, error) { diff --git a/src/immich/sendbackmessage.go b/src/immich/sendbackmessage.go index 2868829..44b6e13 100644 --- a/src/immich/sendbackmessage.go +++ b/src/immich/sendbackmessage.go @@ -1,15 +1,41 @@ package immich import ( + "fmt" "immich-exporter/src/models" + "strconv" + + "github.com/prometheus/client_golang/prometheus" ) -func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers) string { - total := "# HELP immich_app_number_users The number of immich users\n# TYPE immich_app_max_active_downloads gauge\nimmich_app_nb_users " + strconv.Itoa(len((*result).UsageByUser)) + "\n" - total = total + "# HELP immich_app_total_photos The total number of photos\n# TYPE immich_app_total_photos gauge\nimmich_app_total_photos " + strconv.Itoa((*result).Photos) + "\n" - total = total + "# HELP immich_app_total_videos The total number of videos\n# TYPE immich_app_total_videos gauge\nimmich_app_total_videos " + strconv.Itoa((*result).Videos) + "\n" - total = total + "# HELP immich_app_total_usage The usage of the user\n# TYPE immich_app_total_usage gauge\nimmich_app_total_usage " + strconv.Itoa(int((*result).UsageRaw)) + "\n" +func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers, r *prometheus.Registry) { + total_photos := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "immich_app_total_photos", + Help: "The total number of photos", + }) + total_videos := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "immich_app_total_videos", + Help: "The total number of videos", + }) + total_usage := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "immich_app_total_usage", + Help: "The total usage of disk", + }) + total_users := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "immich_app_number_users", + Help: "The total number of users", + }) + + r.MustRegister(total_usage) + r.MustRegister(total_videos) + r.MustRegister(total_photos) + r.MustRegister(total_users) + total_photos.Add(float64((*result).Photos)) + total_videos.Add(float64((*result).Videos)) + total_usage.Add(float64((*result).UsageRaw)) + total_users.Add(float64(len((*result).UsageByUser))) + immich_user_videos := "# HELP immich_app_user_videos The number of videos of the user\n# TYPE immich_app_user_videos gauge\n" immich_user_photos := "# HELP immich_app_user_photos The number of photo of the user\n# TYPE immich_app_user_photos gauge\n" immich_user_usageRaw := "# HELP immich_app_user_usage The usage of the user\n# TYPE immich_app_user_usage gauge\n" @@ -21,12 +47,22 @@ func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers) s immich_user_photos = immich_user_photos + `immich_user_photos{uid="` + (*result).UsageByUser[i].UserID + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `",} ` + strconv.Itoa((*result).UsageByUser[i].Photos) + "\n" immich_user_videos = immich_user_videos + `immich_user_videos{uid="` + (*result).UsageByUser[i].UserID + `",firstname="` + myuser.FirstName + `",lastname="` + myuser.LastName + `",} ` + strconv.Itoa((*result).UsageByUser[i].Videos) + "\n" } - return total + immich_user_info + immich_user_videos + immich_user_photos + immich_user_usageRaw + } -func Sendbackmessageserverversion(result *models.ServerVersion) string { +func Sendbackmessageserverversion(result *models.ServerVersion, r *prometheus.Registry) { + fmt.Println("test") + + version := prometheus.NewGauge(prometheus.GaugeOpts{ + Name: "version", + Help: "Immich version", + ConstLabels: map[string]string{ + "version": strconv.Itoa((*result).Major) + "." + strconv.Itoa((*result).Minor) + "." + strconv.Itoa((*result).Patch), + }, + }) + version.Set(1) + r.MustRegister(version) - return "# HELP immich_app_version The current immich version\n# TYPE immich_app_version gauge\nimmich_app_version" + `{version="` + strconv.Itoa((*result).Major) + "." + strconv.Itoa((*result).Minor) + "." + strconv.Itoa((*result).Patch) + `",} 1.0` + "\n" } func GetName(result string, result2 *models.AllUsers) models.CustomUser { diff --git a/src/main.go b/src/main.go index 8fd9313..675228e 100644 --- a/src/main.go +++ b/src/main.go @@ -1,10 +1,9 @@ package main import ( - "fmt" "immich-exporter/src/immich" "immich-exporter/src/models" - myprom "immich-exporter/src/promtheus" + "log" "net/http" @@ -20,22 +19,19 @@ func main() { log.Println("Started") r := prometheus.NewRegistry() - r.MustRegister(myprom.HttpRequestDuration) - r.MustRegister(myprom.Version) + immich.Allrequests(r) - mux := http.NewServeMux() - - mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{})) - var srv *http.Server - srv = &http.Server{Addr: ":8090", Handler: mux} - log.Fatal(srv.ListenAndServe()) + http.HandleFunc("/metrics", test) + http.ListenAndServe(":8090", nil) } -func metrics(w http.ResponseWriter, req *http.Request) { - value := immich.Allrequests() - if value == "" { - value = immich.Allrequests() - } +func test(w http.ResponseWriter, r *http.Request) { + registry := prometheus.NewRegistry() + + immich.Allrequests(registry) + + // Delegate http serving to Promethues client library, which will call collector.Collect. + h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{}) + h.ServeHTTP(w, r) - fmt.Fprintf(w, value) } diff --git a/src/promtheus/tests.go b/src/promtheus/tests.go deleted file mode 100644 index 530e827..0000000 --- a/src/promtheus/tests.go +++ /dev/null @@ -1,26 +0,0 @@ -package myprom - -import "github.com/prometheus/client_golang/prometheus" - -var r = prometheus.NewRegistry() - -var ( - AppVersion string - Version = prometheus.NewGauge(prometheus.GaugeOpts{ - Name: "version", - Help: "Version information about this binary", - ConstLabels: map[string]string{ - "version": AppVersion, - }, - }) - - UbhttpRequestsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{ - Name: "http_requests_total", - Help: "Count of all HTTP requests", - }, []string{"code", "method"}) - - HttpRequestDuration = prometheus.NewHistogramVec(prometheus.HistogramOpts{ - Name: "http_request_duration_seconds", - Help: "Duration of all HTTP requests", - }, []string{"code", "handler", "method"}) -)