2019-08-17 20:19:56 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2020-08-19 04:38:42 +00:00
|
|
|
"github.com/pterodactyl/wings/environment"
|
2020-07-17 02:56:53 +00:00
|
|
|
"sync"
|
2019-08-17 20:19:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Defines the current resource usage for a given server instance. If a server is offline you
|
|
|
|
// should obviously expect memory and CPU usage to be 0. However, disk will always be returned
|
|
|
|
// since that is not dependent on the server being running to collect that data.
|
|
|
|
type ResourceUsage struct {
|
2020-07-19 23:27:55 +00:00
|
|
|
mu sync.RWMutex
|
|
|
|
|
2020-08-19 04:38:42 +00:00
|
|
|
// Embed the current environment stats into this server specific resource usage struct.
|
|
|
|
environment.Stats
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// The current server status.
|
|
|
|
State string `json:"state" default:"offline"`
|
2020-07-17 02:56:53 +00:00
|
|
|
|
2019-08-17 20:19:56 +00:00
|
|
|
// The current disk space being used by the server. This is cached to prevent slow lookup
|
|
|
|
// issues on frequent refreshes.
|
2019-08-17 23:10:48 +00:00
|
|
|
Disk int64 `json:"disk_bytes"`
|
2019-08-17 20:19:56 +00:00
|
|
|
}
|
|
|
|
|
2020-07-19 23:27:55 +00:00
|
|
|
// Returns the resource usage stats for the server instance. If the server is not running, only the
|
|
|
|
// disk space currently used will be returned. When the server is running all of the other stats will
|
|
|
|
// be returned.
|
|
|
|
//
|
|
|
|
// When a process is stopped all of the stats are zeroed out except for the disk.
|
|
|
|
func (s *Server) Proc() *ResourceUsage {
|
|
|
|
s.resources.mu.RLock()
|
|
|
|
defer s.resources.mu.RUnlock()
|
|
|
|
|
|
|
|
return &s.resources
|
|
|
|
}
|
|
|
|
|
2020-08-19 04:42:57 +00:00
|
|
|
func (s *Server) emitProcUsage() {
|
|
|
|
s.resources.mu.RLock()
|
2020-09-13 03:13:48 +00:00
|
|
|
if err := s.Events().PublishJson(StatsEvent, s.resources); err != nil {
|
|
|
|
s.Log().WithField("error", err).Warn("error while emitting server resource usage to listeners")
|
2020-09-05 19:08:40 +00:00
|
|
|
}
|
2020-09-13 03:13:48 +00:00
|
|
|
s.resources.mu.RUnlock()
|
2020-08-19 04:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-20 00:46:39 +00:00
|
|
|
// Returns the servers current state.
|
|
|
|
func (ru *ResourceUsage) getInternalState() string {
|
|
|
|
ru.mu.RLock()
|
|
|
|
defer ru.mu.RUnlock()
|
|
|
|
|
|
|
|
return ru.State
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the new state for the server.
|
2020-07-19 23:27:55 +00:00
|
|
|
func (ru *ResourceUsage) setInternalState(state string) {
|
|
|
|
ru.mu.Lock()
|
|
|
|
ru.State = state
|
|
|
|
ru.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ru *ResourceUsage) SetDisk(i int64) {
|
2020-08-19 04:42:57 +00:00
|
|
|
ru.mu.Lock()
|
|
|
|
ru.Disk = i
|
|
|
|
ru.mu.Unlock()
|
2020-05-09 05:06:26 +00:00
|
|
|
}
|