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