diff --git a/app/api/api.go b/app/api/api.go index 90ad6ea5..da5aeeac 100644 --- a/app/api/api.go +++ b/app/api/api.go @@ -1500,6 +1500,7 @@ func (a *api) start(ctx context.Context) error { return false }, + Resources: a.resources, } mainserverhandler, err := http.NewServer(serverConfig) diff --git a/http/api/about.go b/http/api/about.go index 0203ae1d..5887423a 100644 --- a/http/api/about.go +++ b/http/api/about.go @@ -2,17 +2,18 @@ package api // About is some general information about the API type About struct { - App string `json:"app"` - Auths []string `json:"auths"` - Name string `json:"name"` - ID string `json:"id"` - CreatedAt string `json:"created_at"` // RFC3339 - Uptime uint64 `json:"uptime_seconds"` - Version Version `json:"version"` + App string `json:"app"` + Auths []string `json:"auths"` + Name string `json:"name"` + ID string `json:"id"` + CreatedAt string `json:"created_at"` // RFC3339 + Uptime uint64 `json:"uptime_seconds"` + Version AboutVersion `json:"version"` + Resources AboutResources `json:"resources"` } -// Version is some information about the binary -type Version struct { +// AboutVersion is some information about the binary +type AboutVersion struct { Number string `json:"number"` Commit string `json:"repository_commit"` Branch string `json:"repository_branch"` @@ -21,13 +22,26 @@ type Version struct { Compiler string `json:"compiler"` } -// MinimalAbout is the minimal information about the API -type MinimalAbout struct { - App string `json:"app"` - Auths []string `json:"auths"` - Version VersionMinimal `json:"version"` +// AboutResources holds information about the current resource usage +type AboutResources struct { + IsThrottling bool // Whether this core is currently throttling + NCPU float64 // Number of CPU on this node + CPU float64 // Current CPU load, 0-100*ncpu + CPULimit float64 // Defined CPU load limit, 0-100*ncpu + CPUCore float64 // Current CPU load of the core itself, 0-100*ncpu + Mem uint64 // Currently used memory in bytes + MemLimit uint64 // Defined memory limit in bytes + MemTotal uint64 // Total available memory in bytes + MemCore uint64 // Current used memory of the core itself in bytes } -type VersionMinimal struct { +// MinimalAbout is the minimal information about the API +type MinimalAbout struct { + App string `json:"app"` + Auths []string `json:"auths"` + Version AboutVersionMinimal `json:"version"` +} + +type AboutVersionMinimal struct { Number string `json:"number"` } diff --git a/http/handler/api/about.go b/http/handler/api/about.go index a9add8ca..cba674dd 100644 --- a/http/handler/api/about.go +++ b/http/handler/api/about.go @@ -6,6 +6,7 @@ import ( "github.com/datarhei/core/v16/app" "github.com/datarhei/core/v16/http/api" + "github.com/datarhei/core/v16/resources" "github.com/datarhei/core/v16/restream" "github.com/labstack/echo/v4" @@ -14,15 +15,17 @@ import ( // The AboutHandler type provides handler functions for retrieving details // about the API version and build infos. type AboutHandler struct { - restream restream.Restreamer - auths func() []string + restream restream.Restreamer + resources resources.Resources + auths func() []string } // NewAbout returns a new About type -func NewAbout(restream restream.Restreamer, auths func() []string) *AboutHandler { +func NewAbout(restream restream.Restreamer, resources resources.Resources, auths func() []string) *AboutHandler { return &AboutHandler{ - restream: restream, - auths: auths, + restream: restream, + resources: resources, + auths: auths, } } @@ -41,7 +44,7 @@ func (p *AboutHandler) About(c echo.Context) error { return c.JSON(http.StatusOK, api.MinimalAbout{ App: app.Name, Auths: p.auths(), - Version: api.VersionMinimal{ + Version: api.AboutVersionMinimal{ Number: app.Version.MajorString(), }, }) @@ -56,7 +59,7 @@ func (p *AboutHandler) About(c echo.Context) error { ID: p.restream.ID(), CreatedAt: createdAt.Format(time.RFC3339), Uptime: uint64(time.Since(createdAt).Seconds()), - Version: api.Version{ + Version: api.AboutVersion{ Number: app.Version.String(), Commit: app.Commit, Branch: app.Branch, @@ -66,5 +69,18 @@ func (p *AboutHandler) About(c echo.Context) error { }, } + if p.resources != nil { + res := p.resources.Info() + about.Resources.IsThrottling = res.CPU.Throttling + about.Resources.NCPU = res.CPU.NCPU + about.Resources.CPU = (100 - res.CPU.Idle) * res.CPU.NCPU + about.Resources.CPULimit = res.CPU.Limit * res.CPU.NCPU + about.Resources.CPUCore = res.CPU.Core * res.CPU.NCPU + about.Resources.Mem = res.Mem.Total - res.Mem.Available + about.Resources.MemLimit = res.Mem.Limit + about.Resources.MemTotal = res.Mem.Total + about.Resources.MemCore = res.Mem.Core + } + return c.JSON(http.StatusOK, about) } diff --git a/http/handler/api/about_test.go b/http/handler/api/about_test.go index a2f039b5..192b5e98 100644 --- a/http/handler/api/about_test.go +++ b/http/handler/api/about_test.go @@ -19,7 +19,7 @@ func getDummyAboutRouter() (*echo.Echo, error) { return nil, err } - handler := NewAbout(rs, func() []string { return []string{} }) + handler := NewAbout(rs, nil, func() []string { return []string{} }) router.Add("GET", "/", handler.About) diff --git a/http/handler/api/cluster_node.go b/http/handler/api/cluster_node.go index c3bfee05..51f4c463 100644 --- a/http/handler/api/cluster_node.go +++ b/http/handler/api/cluster_node.go @@ -100,7 +100,7 @@ func (h *ClusterHandler) NodeGetVersion(c echo.Context) error { v := peer.CoreAbout() - version := api.Version{ + version := api.AboutVersion{ Number: v.Version.Number, Commit: v.Version.Commit, Branch: v.Version.Branch, diff --git a/http/server.go b/http/server.go index adde251a..959165bd 100644 --- a/http/server.go +++ b/http/server.go @@ -51,6 +51,7 @@ import ( "github.com/datarhei/core/v16/monitor" "github.com/datarhei/core/v16/net" "github.com/datarhei/core/v16/prometheus" + "github.com/datarhei/core/v16/resources" "github.com/datarhei/core/v16/restream" "github.com/datarhei/core/v16/rtmp" "github.com/datarhei/core/v16/session" @@ -100,6 +101,7 @@ type Config struct { Cluster cluster.Cluster IAM iam.IAM IAMSkipper func(ip string) bool + Resources resources.Resources } type CorsConfig struct { @@ -251,6 +253,7 @@ func NewServer(config Config) (serverhandler.Server, error) { s.handler.about = api.NewAbout( config.Restream, + config.Resources, func() []string { return config.IAM.Validators() }, )