Skip to content

Commit

Permalink
Merge pull request #130 from vjsamuel/master
Browse files Browse the repository at this point in the history
Merge adding per core support
  • Loading branch information
monicasarbu committed Nov 23, 2015
2 parents e16e813 + 338da77 commit 53aead5
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Godeps/Godeps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,18 @@ System statistics:
"softirq": 0,
"steal": 0
},
"cpu0": {
"user": 2985331,
"user_p": 0,
"nice": 0,
"system": 1727403,
"system_p": 0,
"idle": 25915908,
"iowait": 0,
"irq": 0,
"softirq": 0,
"steal": 0
},
"load": {
"load1": 1.52392578125,
"load5": 1.79736328125,
Expand Down
8 changes: 4 additions & 4 deletions docs/overview.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ Topbeat helps you monitor your servers by collecting metrics like:
*System-wide statistics*

* System load: in the last minute, in the last 5 minutes, and in the last 15 minutes
* System-wide CPU usage: user (and percentage), system, idle, IOWait, and so on
* System-wide memory usage: total, used (and percentage), free, and so on
* System-wide swap usage: total, used (and percentage), free, and so on
* System wide CPU usage: user (and percentage), system, idle, IOWait, and so on at both per CPU and overall level
* System wide memory usage: total, used (and percentage), free, and so on
* System wide swap usage: total, used (and percentage), free, and so on

*Per-process statistics*

* Process name
* Process name
* Process parent pid
* Process state
* Process pid
Expand Down
1 change: 0 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,4 @@ func main() {
}

b.Run()

}
26 changes: 26 additions & 0 deletions sigar.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,32 @@ func GetCpuTimes() (*CpuTimes, error) {
}, nil
}

func GetCpuTimesList() ([]CpuTimes, error) {

cpuList := sigar.CpuList{}
err := cpuList.Get()
if err != nil {
return nil, err
}

cpuTimes := make([]CpuTimes, len(cpuList.List))

for i, cpu := range cpuList.List {
cpuTimes[i] = CpuTimes{
User: cpu.User,
Nice: cpu.Nice,
System: cpu.Sys,
Idle: cpu.Idle,
IOWait: cpu.Wait,
Irq: cpu.Irq,
SoftIrq: cpu.SoftIrq,
Steal: cpu.Stolen,
}
}

return cpuTimes, nil
}

func GetMemory() (*MemStat, error) {

mem := sigar.Mem{}
Expand Down
51 changes: 45 additions & 6 deletions topbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"math"
"regexp"
"strconv"
"time"

"github.com/elastic/gosigar"
Expand All @@ -18,12 +19,13 @@ import (
type ProcsMap map[int]*Process

type Topbeat struct {
period time.Duration
procs []string
procsMap ProcsMap
lastCpuTimes *CpuTimes
TbConfig ConfigSettings
events publisher.Client
period time.Duration
procs []string
procsMap ProcsMap
lastCpuTimes *CpuTimes
lastCpuTimesList []CpuTimes
TbConfig ConfigSettings
events publisher.Client

sysStats bool
procStats bool
Expand Down Expand Up @@ -227,6 +229,13 @@ func (t *Topbeat) exportSystemStats() error {

t.addCpuPercentage(cpu_stat)

cpu_core_stat, err := GetCpuTimesList()
if err != nil {
logp.Warn("Getting cpu core times: %v", err)
return err
}
t.addCpuPercentageList(cpu_core_stat)

mem_stat, err := GetMemory()
if err != nil {
logp.Warn("Getting memory details: %v", err)
Expand All @@ -250,6 +259,10 @@ func (t *Topbeat) exportSystemStats() error {
"swap": swap_stat,
}

for coreNumber, core := range cpu_core_stat {
event["cpu"+strconv.Itoa(coreNumber)] = core
}

t.events.PublishEvent(event)

return nil
Expand Down Expand Up @@ -342,6 +355,32 @@ func (t *Topbeat) addCpuPercentage(t2 *CpuTimes) {

}

func (t *Topbeat) addCpuPercentageList(t2 []CpuTimes) {

t1 := t.lastCpuTimesList

if t1 != nil && t2 != nil && len(t1) == len(t2) {

calculate := func(field2 uint64, field1 uint64, all_delta uint64) float64 {

perc := 0.0
delta := field2 - field1
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 2)
}

for i := 0; i < len(t1); i++ {
all_delta := t2[i].sum() - t1[i].sum()
t2[i].UserPercent = calculate(t2[i].User, t1[i].User, all_delta)
t2[i].SystemPercent = calculate(t2[i].System, t1[i].System, all_delta)
}

}

t.lastCpuTimesList = t2

}

func (t *Topbeat) addProcMemPercentage(proc *Process, total_phymem uint64) {

// in unit tests, total_phymem is set to a value greater than zero
Expand Down

0 comments on commit 53aead5

Please sign in to comment.