mirror of
https://github.com/bcicen/ctop.git
synced 2024-08-30 18:23:19 +00:00
refactor collectors into subpackage
This commit is contained in:
parent
671c944272
commit
1be452d7c0
@ -1,16 +1,17 @@
|
||||
package metrics
|
||||
package collector
|
||||
|
||||
import (
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
api "github.com/fsouza/go-dockerclient"
|
||||
)
|
||||
|
||||
// Docker collector
|
||||
type Docker struct {
|
||||
Metrics
|
||||
metrics.Metrics
|
||||
id string
|
||||
client *api.Client
|
||||
running bool
|
||||
stream chan Metrics
|
||||
stream chan metrics.Metrics
|
||||
done chan bool
|
||||
lastCpu float64
|
||||
lastSysCpu float64
|
||||
@ -18,7 +19,7 @@ type Docker struct {
|
||||
|
||||
func NewDocker(client *api.Client, id string) *Docker {
|
||||
return &Docker{
|
||||
Metrics: Metrics{},
|
||||
Metrics: metrics.Metrics{},
|
||||
id: id,
|
||||
client: client,
|
||||
}
|
||||
@ -26,7 +27,7 @@ func NewDocker(client *api.Client, id string) *Docker {
|
||||
|
||||
func (c *Docker) Start() {
|
||||
c.done = make(chan bool)
|
||||
c.stream = make(chan Metrics)
|
||||
c.stream = make(chan metrics.Metrics)
|
||||
stats := make(chan *api.Stats)
|
||||
|
||||
go func() {
|
||||
@ -60,7 +61,7 @@ func (c *Docker) Running() bool {
|
||||
return c.running
|
||||
}
|
||||
|
||||
func (c *Docker) Stream() chan Metrics {
|
||||
func (c *Docker) Stream() chan metrics.Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
21
connector/collector/main.go
Normal file
21
connector/collector/main.go
Normal file
@ -0,0 +1,21 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/bcicen/ctop/logging"
|
||||
)
|
||||
|
||||
var log = logging.Init()
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
// return rounded percentage
|
||||
func percent(val float64, total float64) int {
|
||||
if total <= 0 {
|
||||
return 0
|
||||
}
|
||||
return round((val / total) * 100)
|
||||
}
|
@ -1,16 +1,18 @@
|
||||
// +build !release
|
||||
|
||||
package metrics
|
||||
package collector
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
)
|
||||
|
||||
// Mock collector
|
||||
type Mock struct {
|
||||
Metrics
|
||||
stream chan Metrics
|
||||
metrics.Metrics
|
||||
stream chan metrics.Metrics
|
||||
done bool
|
||||
running bool
|
||||
aggression int64
|
||||
@ -18,7 +20,7 @@ type Mock struct {
|
||||
|
||||
func NewMock(a int64) *Mock {
|
||||
c := &Mock{
|
||||
Metrics: Metrics{},
|
||||
Metrics: metrics.Metrics{},
|
||||
aggression: a,
|
||||
}
|
||||
c.MemLimit = 2147483648
|
||||
@ -31,7 +33,7 @@ func (c *Mock) Running() bool {
|
||||
|
||||
func (c *Mock) Start() {
|
||||
c.done = false
|
||||
c.stream = make(chan Metrics)
|
||||
c.stream = make(chan metrics.Metrics)
|
||||
go c.run()
|
||||
}
|
||||
|
||||
@ -39,7 +41,7 @@ func (c *Mock) Stop() {
|
||||
c.done = true
|
||||
}
|
||||
|
||||
func (c *Mock) Stream() chan Metrics {
|
||||
func (c *Mock) Stream() chan metrics.Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
@ -1,4 +1,4 @@
|
||||
package metrics
|
||||
package collector
|
||||
|
||||
import (
|
||||
linuxproc "github.com/c9s/goprocinfo/linux"
|
@ -1,18 +1,19 @@
|
||||
package metrics
|
||||
package collector
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
"github.com/opencontainers/runc/libcontainer"
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups"
|
||||
)
|
||||
|
||||
// Runc collector
|
||||
type Runc struct {
|
||||
Metrics
|
||||
metrics.Metrics
|
||||
id string
|
||||
libc libcontainer.Container
|
||||
stream chan Metrics
|
||||
stream chan metrics.Metrics
|
||||
done bool
|
||||
running bool
|
||||
interval int // collection interval, in seconds
|
||||
@ -22,7 +23,7 @@ type Runc struct {
|
||||
|
||||
func NewRunc(libc libcontainer.Container) *Runc {
|
||||
c := &Runc{
|
||||
Metrics: Metrics{},
|
||||
Metrics: metrics.Metrics{},
|
||||
id: libc.ID(),
|
||||
libc: libc,
|
||||
interval: 1,
|
||||
@ -36,7 +37,7 @@ func (c *Runc) Running() bool {
|
||||
|
||||
func (c *Runc) Start() {
|
||||
c.done = false
|
||||
c.stream = make(chan Metrics)
|
||||
c.stream = make(chan metrics.Metrics)
|
||||
go c.run()
|
||||
}
|
||||
|
||||
@ -44,7 +45,7 @@ func (c *Runc) Stop() {
|
||||
c.done = true
|
||||
}
|
||||
|
||||
func (c *Runc) Stream() chan Metrics {
|
||||
func (c *Runc) Stream() chan metrics.Metrics {
|
||||
return c.stream
|
||||
}
|
||||
|
@ -6,8 +6,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"github.com/bcicen/ctop/connector/collector"
|
||||
"github.com/bcicen/ctop/container"
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
api "github.com/fsouza/go-dockerclient"
|
||||
)
|
||||
|
||||
@ -128,7 +128,7 @@ func (cm *Docker) MustGet(id string) *container.Container {
|
||||
// append container struct for new containers
|
||||
if !ok {
|
||||
// create collector
|
||||
collector := metrics.NewDocker(cm.client, id)
|
||||
collector := collector.NewDocker(cm.client, id)
|
||||
// create container
|
||||
c = container.New(id, collector)
|
||||
cm.lock.Lock()
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/bcicen/ctop/connector/collector"
|
||||
"github.com/bcicen/ctop/container"
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
"github.com/jgautheron/codename-generator"
|
||||
"github.com/nu7hatch/gouuid"
|
||||
)
|
||||
@ -39,7 +39,7 @@ func (cs *Mock) Init() {
|
||||
}
|
||||
|
||||
func (cs *Mock) makeContainer(aggression int64) {
|
||||
collector := metrics.NewMock(aggression)
|
||||
collector := collector.NewMock(aggression)
|
||||
c := container.New(makeID(), collector)
|
||||
c.SetMeta("name", makeName())
|
||||
c.SetState(makeState())
|
||||
|
@ -8,8 +8,8 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/bcicen/ctop/connector/collector"
|
||||
"github.com/bcicen/ctop/container"
|
||||
"github.com/bcicen/ctop/metrics"
|
||||
"github.com/opencontainers/runc/libcontainer"
|
||||
"github.com/opencontainers/runc/libcontainer/cgroups/systemd"
|
||||
)
|
||||
@ -170,7 +170,7 @@ func (cm *Runc) MustGet(id string) *container.Container {
|
||||
libc := cm.GetLibc(id)
|
||||
|
||||
// create collector
|
||||
collector := metrics.NewRunc(libc)
|
||||
collector := collector.NewRunc(libc)
|
||||
|
||||
// create container
|
||||
c = container.New(id, collector)
|
||||
|
@ -1,13 +1,5 @@
|
||||
package metrics
|
||||
|
||||
import (
|
||||
"math"
|
||||
|
||||
"github.com/bcicen/ctop/logging"
|
||||
)
|
||||
|
||||
var log = logging.Init()
|
||||
|
||||
type Metrics struct {
|
||||
CPUUtil int
|
||||
NetTx int64
|
||||
@ -39,15 +31,3 @@ type Collector interface {
|
||||
Start()
|
||||
Stop()
|
||||
}
|
||||
|
||||
func round(num float64) int {
|
||||
return int(num + math.Copysign(0.5, num))
|
||||
}
|
||||
|
||||
// return rounded percentage
|
||||
func percent(val float64, total float64) int {
|
||||
if total <= 0 {
|
||||
return 0
|
||||
}
|
||||
return round((val / total) * 100)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user