Skip to content

Commit

Permalink
Cleanup system module (elastic#1961)
Browse files Browse the repository at this point in the history
During migration of Topbeat, the existing topbeat files and tests were put into a temporary common folder. The files in these folders were no moved to the according metricsets. No code changes or cleanups were done in the process.
  • Loading branch information
ruflin authored and tsg committed Jul 11, 2016
1 parent 058a483 commit 54e25b8
Show file tree
Hide file tree
Showing 27 changed files with 151 additions and 1,242 deletions.
2 changes: 1 addition & 1 deletion glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import:
subpackages:
- /difflib
- package: github.com/elastic/gosigar
version: 61dfed4a9eb484b8248fe22e5ff5346dc18e4332
version: f720d6786aff82d4dae3d0af8c0ea5a34cd04029
- package: github.com/samuel/go-parser
version: ca8abbf65d0e61dedf061f98bd3850f250e27539
- package: github.com/samuel/go-thrift
Expand Down
8 changes: 4 additions & 4 deletions metricbeat/module/system/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package core
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
system "github.com/elastic/beats/metricbeat/module/system/common"
"github.com/elastic/beats/metricbeat/module/system/cpu"

"github.com/pkg/errors"
)
Expand All @@ -19,7 +19,7 @@ func init() {
// MetricSet for fetching system core metrics.
type MetricSet struct {
mb.BaseMetricSet
cpu *system.CPU
cpu *cpu.CPU
}

// New is a mb.MetricSetFactory that returns a cores.MetricSet.
Expand All @@ -37,7 +37,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

return &MetricSet{
BaseMetricSet: base,
cpu: &system.CPU{
cpu: &cpu.CPU{
CpuPerCore: true,
CpuTicks: config.CpuTicks,
},
Expand All @@ -47,7 +47,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch fetches CPU core metrics from the OS.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {

cpuCoreStat, err := system.GetCpuTimesList()
cpuCoreStat, err := cpu.GetCpuTimesList()
if err != nil {
return nil, errors.Wrap(err, "cpu core times")
}
Expand Down
9 changes: 4 additions & 5 deletions metricbeat/module/system/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package cpu
import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/metricbeat/mb"
system "github.com/elastic/beats/metricbeat/module/system/common"

"github.com/pkg/errors"
)
Expand All @@ -19,7 +18,7 @@ func init() {
// MetricSet for fetching system CPU metrics.
type MetricSet struct {
mb.BaseMetricSet
cpu *system.CPU
cpu *CPU
}

// New is a mb.MetricSetFactory that returns a cpu.MetricSet.
Expand All @@ -37,7 +36,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {

return &MetricSet{
BaseMetricSet: base,
cpu: &system.CPU{
cpu: &CPU{
CpuTicks: config.CpuTicks,
},
}, nil
Expand All @@ -46,13 +45,13 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch fetches CPU metrics from the OS.
func (m *MetricSet) Fetch() (common.MapStr, error) {

stat, err := system.GetCpuTimes()
stat, err := GetCpuTimes()
if err != nil {
return nil, errors.Wrap(err, "cpu times")
}
m.cpu.AddCpuPercentage(stat)

loadStat, err := system.GetSystemLoad()
loadStat, err := GetSystemLoad()
if err != nil {
return nil, errors.Wrap(err, "load statistics")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package common
// +build darwin freebsd linux openbsd windows

package cpu

import (
"time"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/module/system"
"github.com/elastic/beats/metricbeat/module/system/memory"
sigar "github.com/elastic/gosigar"
)

Expand Down Expand Up @@ -70,7 +74,7 @@ func GetCpuPercentage(last *CpuTimes, current *CpuTimes) *CpuTimes {
perc := 0.0
delta := int64(field2 - field1)
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 4)
return system.Round(perc, .5, 4)
}

current.UserPercent = calculate(current.Cpu.User, last.Cpu.User)
Expand All @@ -95,7 +99,7 @@ func GetCpuPercentageList(last, current []CpuTimes) []CpuTimes {
perc := 0.0
delta := int64(field2 - field1)
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 4)
return system.Round(perc, .5, 4)
}

for i := 0; i < len(last); i++ {
Expand Down Expand Up @@ -167,27 +171,27 @@ func (cpu *CPU) GetSystemStats() (common.MapStr, error) {

cpu.AddCpuPercentage(cpuStat)

memStat, err := GetMemory()
memStat, err := memory.GetMemory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
return nil, err
}
AddMemPercentage(memStat)
memory.AddMemPercentage(memStat)

swapStat, err := GetSwap()
swapStat, err := memory.GetSwap()
if err != nil {
logp.Warn("Getting swap details: %v", err)
return nil, err
}
AddSwapPercentage(swapStat)
memory.AddSwapPercentage(swapStat)

event := common.MapStr{
"@timestamp": common.Time(time.Now()),
"type": "system",
"load": loadStat,
"cpu": cpu.GetCpuStatEvent(cpuStat),
"mem": GetMemoryEvent(memStat),
"swap": GetSwapEvent(swapStat),
"mem": memory.GetMemoryEvent(memStat),
"swap": memory.GetSwapEvent(swapStat),
}

return event, nil
Expand Down Expand Up @@ -218,3 +222,24 @@ func (cpu *CPU) GetCoreStats() ([]common.MapStr, error) {

return events, nil
}

type SystemLoad struct {
Load1 float64 `json:"load1"`
Load5 float64 `json:"load5"`
Load15 float64 `json:"load15"`
}

func GetSystemLoad() (*SystemLoad, error) {

concreteSigar := sigar.ConcreteSigar{}
avg, err := concreteSigar.GetLoadAverage()
if err != nil {
return nil, err
}

return &SystemLoad{
Load1: avg.One,
Load5: avg.Five,
Load15: avg.Fifteen,
}, nil
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// +build !integration
// +build darwin freebsd linux openbsd windows

package common
package cpu

import (
"runtime"
"testing"

"github.com/elastic/gosigar"
Expand Down Expand Up @@ -61,3 +63,19 @@ func TestCpuPercentage(t *testing.T) {
assert.Equal(t, cpu2.UserPercent, 0.9502)
assert.Equal(t, cpu2.SystemPercent, 0.0448)
}

func TestGetSystemLoad(t *testing.T) {

if runtime.GOOS == "windows" {
return //no load data on windows
}

load, err := GetSystemLoad()

assert.NotNil(t, load)
assert.Nil(t, err)

assert.True(t, (load.Load1 > 0))
assert.True(t, (load.Load5 > 0))
assert.True(t, (load.Load15 > 0))
}
9 changes: 4 additions & 5 deletions metricbeat/module/system/filesystem/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
system "github.com/elastic/beats/metricbeat/module/system/common"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -34,20 +33,20 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch fetches filesystem metrics for all mounted filesystems and returns
// an event for each mount point.
func (m *MetricSet) Fetch() ([]common.MapStr, error) {
fss, err := system.GetFileSystemList()
fss, err := GetFileSystemList()
if err != nil {
return nil, errors.Wrap(err, "filesystem list")
}

filesSystems := make([]common.MapStr, 0, len(fss))
for _, fs := range fss {
fsStat, err := system.GetFileSystemStat(fs)
fsStat, err := GetFileSystemStat(fs)
if err != nil {
debugf("error getting filesystem stats for '%s': %v", fs.DirName, err)
continue
}
system.AddFileSystemUsedPercentage(fsStat)
filesSystems = append(filesSystems, system.GetFilesystemEvent(fsStat))
AddFileSystemUsedPercentage(fsStat)
filesSystems = append(filesSystems, GetFilesystemEvent(fsStat))
}

return filesSystems, nil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package common
// +build darwin freebsd linux openbsd windows

package filesystem

import (
"time"

"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/module/system"
sigar "github.com/elastic/gosigar"
)

Expand Down Expand Up @@ -50,7 +53,7 @@ func AddFileSystemUsedPercentage(f *FileSystemStat) {
}

perc := float64(f.Used) / float64(f.Total)
f.UsedPercent = Round(perc, .5, 4)
f.UsedPercent = system.Round(perc, .5, 4)
}

func CollectFileSystemStats(fss []sigar.FileSystem) []common.MapStr {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// +build !integration
// +build darwin freebsd linux openbsd windows

package common
package filesystem

import (
"os"
Expand Down
6 changes: 3 additions & 3 deletions metricbeat/module/system/fsstat/fsstat.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/logp"
"github.com/elastic/beats/metricbeat/mb"
system "github.com/elastic/beats/metricbeat/module/system/common"
"github.com/elastic/beats/metricbeat/module/system/filesystem"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -34,7 +34,7 @@ func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
// Fetch fetches filesystem metrics for all mounted filesystems and returns
// a single event containing aggregated data.
func (m *MetricSet) Fetch() (common.MapStr, error) {
fss, err := system.GetFileSystemList()
fss, err := filesystem.GetFileSystemList()
if err != nil {
return nil, errors.Wrap(err, "filesystem list")
}
Expand All @@ -43,7 +43,7 @@ func (m *MetricSet) Fetch() (common.MapStr, error) {
var totalFiles, totalSize, totalSizeFree, totalSizeUsed uint64

for _, fs := range fss {
fsStat, err := system.GetFileSystemStat(fs)
fsStat, err := filesystem.GetFileSystemStat(fs)
if err != nil {
debugf("error fetching filesystem stats for '%s': %v", fs.DirName, err)
continue
Expand Down
Loading

0 comments on commit 54e25b8

Please sign in to comment.