Signed-off-by: martin <martin.labat92@gmail.com>
This commit is contained in:
martin 2023-02-13 00:59:12 +01:00
parent a3f089ed86
commit 5c230986d0
No known key found for this signature in database
GPG key ID: 4544CC55835A3B9E
4 changed files with 66 additions and 58 deletions

View file

@ -7,20 +7,23 @@ import (
"io/ioutil" "io/ioutil"
"log" "log"
"net/http" "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() allusers, err := GetAllUsers()
users, err2 := users() users, err2 := users()
if err != nil && err2 != nil { if err != nil && err2 != nil {
return ""
} else { } else {
return Sendbackmessagepreference(users, allusers) Sendbackmessagepreference(users, allusers, r)
} }
} }
@ -57,7 +60,7 @@ func GetAllUsers() (*models.AllUsers, error) {
return &models.AllUsers{}, err return &models.AllUsers{}, err
} }
func serverversion() string { func serverversion(r *prometheus.Registry) {
resp, err := Apirequest("/api/server-info/version", "GET") resp, err := Apirequest("/api/server-info/version", "GET")
if err != nil { if err != nil {
if err.Error() == "403" { if err.Error() == "403" {
@ -82,12 +85,11 @@ func serverversion() string {
log.Println("Can not unmarshal JSON") log.Println("Can not unmarshal JSON")
} }
return Sendbackmessageserverversion(&result) Sendbackmessageserverversion(&result, r)
} }
} }
return ""
} }
func users() (*models.Users, error) { func users() (*models.Users, error) {

View file

@ -1,15 +1,41 @@
package immich package immich
import ( import (
"fmt"
"immich-exporter/src/models" "immich-exporter/src/models"
"strconv" "strconv"
"github.com/prometheus/client_golang/prometheus"
) )
func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers) string { func Sendbackmessagepreference(result *models.Users, result2 *models.AllUsers, r *prometheus.Registry) {
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_photos := prometheus.NewGauge(prometheus.GaugeOpts{
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" Name: "immich_app_total_photos",
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" Help: "The total number of photos",
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" })
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_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_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" 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_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" 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 { func GetName(result string, result2 *models.AllUsers) models.CustomUser {

View file

@ -1,10 +1,9 @@
package main package main
import ( import (
"fmt"
"immich-exporter/src/immich" "immich-exporter/src/immich"
"immich-exporter/src/models" "immich-exporter/src/models"
myprom "immich-exporter/src/promtheus"
"log" "log"
"net/http" "net/http"
@ -20,22 +19,19 @@ func main() {
log.Println("Started") log.Println("Started")
r := prometheus.NewRegistry() r := prometheus.NewRegistry()
r.MustRegister(myprom.HttpRequestDuration) immich.Allrequests(r)
r.MustRegister(myprom.Version)
mux := http.NewServeMux() http.HandleFunc("/metrics", test)
http.ListenAndServe(":8090", nil)
mux.Handle("/metrics", promhttp.HandlerFor(r, promhttp.HandlerOpts{}))
var srv *http.Server
srv = &http.Server{Addr: ":8090", Handler: mux}
log.Fatal(srv.ListenAndServe())
} }
func metrics(w http.ResponseWriter, req *http.Request) { func test(w http.ResponseWriter, r *http.Request) {
value := immich.Allrequests() registry := prometheus.NewRegistry()
if value == "" {
value = immich.Allrequests() 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)
} }

View file

@ -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"})
)