Correctly send stats & proc information back for servers
This commit is contained in:
@@ -47,19 +47,27 @@ func (e *Environment) Attach() error {
|
||||
e.SetStream(&st)
|
||||
}
|
||||
|
||||
console := new(Console)
|
||||
c := new(Console)
|
||||
go func(console *Console) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
|
||||
// TODO: resource polling should be handled by the server itself and just call a function
|
||||
// on the environment that can return the data. Same for disabling polling.
|
||||
go func() {
|
||||
defer cancel()
|
||||
defer e.stream.Close()
|
||||
defer func() {
|
||||
e.setState(system.ProcessOfflineState)
|
||||
e.SetStream(nil)
|
||||
}()
|
||||
|
||||
// Poll resources in a seperate thread since this will block the copy call below
|
||||
// from being reached until it is completed if not run in a seperate process. However,
|
||||
// we still want it to be stopped when the copy operation below is finished running which
|
||||
// indicates that the container is no longer running.
|
||||
go e.pollResources(ctx)
|
||||
|
||||
// Stream the reader output to the console which will then fire off events and handle console
|
||||
// throttling and sending the output to the user.
|
||||
_, _ = io.Copy(console, e.stream.Reader)
|
||||
}()
|
||||
}(c)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
120
environment/docker/stats.go
Normal file
120
environment/docker/stats.go
Normal file
@@ -0,0 +1,120 @@
|
||||
package docker
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"github.com/apex/log"
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/pterodactyl/wings/environment"
|
||||
"github.com/pterodactyl/wings/system"
|
||||
"io"
|
||||
"math"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
// Attach to the instance and then automatically emit an event whenever the resource usage for the
|
||||
// server process changes.
|
||||
func (e *Environment) pollResources(ctx context.Context) error {
|
||||
if e.State() == system.ProcessOfflineState {
|
||||
return errors.New("attempting to enable resource polling on a stopped server instance")
|
||||
}
|
||||
|
||||
stats, err := e.client.ContainerStats(context.Background(), e.Id, true)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
dec := json.NewDecoder(stats.Body)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
var v *types.StatsJSON
|
||||
|
||||
if err := dec.Decode(&v); err != nil {
|
||||
if err != io.EOF {
|
||||
log.WithField("container_id", e.Id).Warn("encountered error processing docker stats output, stopping collection")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Disable collection if the server is in an offline state and this process is still running.
|
||||
if e.State() == system.ProcessOfflineState {
|
||||
return nil
|
||||
}
|
||||
|
||||
var rx uint64
|
||||
var tx uint64
|
||||
for _, nw := range v.Networks {
|
||||
atomic.AddUint64(&rx, nw.RxBytes)
|
||||
atomic.AddUint64(&tx, nw.RxBytes)
|
||||
}
|
||||
|
||||
st := &environment.Stats{
|
||||
Memory: calculateDockerMemory(v.MemoryStats),
|
||||
MemoryLimit: v.MemoryStats.Limit,
|
||||
CpuAbsolute: calculateDockerAbsoluteCpu(&v.PreCPUStats, &v.CPUStats),
|
||||
Network: struct {
|
||||
RxBytes uint64 `json:"rx_bytes"`
|
||||
TxBytes uint64 `json:"tx_bytes"`
|
||||
}{
|
||||
RxBytes: rx,
|
||||
TxBytes: tx,
|
||||
},
|
||||
}
|
||||
|
||||
b, _ := json.Marshal(st)
|
||||
e.Events().Publish(environment.ResourceEvent, string(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The "docker stats" CLI call does not return the same value as the types.MemoryStats.Usage
|
||||
// value which can be rather confusing to people trying to compare panel usage to
|
||||
// their stats output.
|
||||
//
|
||||
// This math is straight up lifted from their CLI repository in order to show the same
|
||||
// values to avoid people bothering me about it. It should also reflect a slightly more
|
||||
// correct memory value anyways.
|
||||
//
|
||||
// @see https://github.com/docker/cli/blob/96e1d1d6/cli/command/container/stats_helpers.go#L227-L249
|
||||
func calculateDockerMemory(stats types.MemoryStats) uint64 {
|
||||
if v, ok := stats.Stats["total_inactive_file"]; ok && v < stats.Usage {
|
||||
return stats.Usage - v
|
||||
}
|
||||
|
||||
if v := stats.Stats["inactive_file"]; v < stats.Usage {
|
||||
return stats.Usage - v
|
||||
}
|
||||
|
||||
return stats.Usage
|
||||
}
|
||||
|
||||
// Calculates the absolute CPU usage used by the server process on the system, not constrained
|
||||
// by the defined CPU limits on the container.
|
||||
//
|
||||
// @see https://github.com/docker/cli/blob/aa097cf1aa19099da70930460250797c8920b709/cli/command/container/stats_helpers.go#L166
|
||||
func calculateDockerAbsoluteCpu(pStats *types.CPUStats, stats *types.CPUStats) float64 {
|
||||
// Calculate the change in CPU usage between the current and previous reading.
|
||||
cpuDelta := float64(stats.CPUUsage.TotalUsage) - float64(pStats.CPUUsage.TotalUsage)
|
||||
|
||||
// Calculate the change for the entire system's CPU usage between current and previous reading.
|
||||
systemDelta := float64(stats.SystemUsage) - float64(pStats.SystemUsage)
|
||||
|
||||
// Calculate the total number of CPU cores being used.
|
||||
cpus := float64(stats.OnlineCPUs)
|
||||
if cpus == 0.0 {
|
||||
cpus = float64(len(stats.CPUUsage.PercpuUsage))
|
||||
}
|
||||
|
||||
percent := 0.0
|
||||
if systemDelta > 0.0 && cpuDelta > 0.0 {
|
||||
percent = (cpuDelta / systemDelta) * cpus * 100.0
|
||||
}
|
||||
|
||||
return math.Round(percent*1000) / 1000
|
||||
}
|
||||
Reference in New Issue
Block a user