Skip to content

Commit

Permalink
Backport of elastic#845 to 1.2 - Add username to Topbeat
Browse files Browse the repository at this point in the history
Backport of  elastic#1128 to 1.2 - Change Cpu.Get() on Windows to not use floating point arithmetic
  • Loading branch information
andrewkroh committed Mar 30, 2016
1 parent d9a4af4 commit ba3ed7b
Show file tree
Hide file tree
Showing 31 changed files with 794 additions and 581 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ https://github.com/elastic/beats/compare/v1.1.2...v1.2.0[View commits]
- Add the command line used to start processes {issue}533[533]
- Add username to processes {pull}845[845]
- Fix issue with cpu.system_p being greater than 1 on Windows {pull}1128[1128]
*Filebeat*
Expand Down
13 changes: 7 additions & 6 deletions topbeat/beat/sigar.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/elastic/gosigar"
sigar "github.com/elastic/gosigar"
)

type SystemLoad struct {
Expand Down Expand Up @@ -269,11 +269,12 @@ func GetProcess(pid int, cmdline string) (*Process, error) {
}

proc := Process{
Pid: pid,
Ppid: state.Ppid,
Name: state.Name,
State: getProcState(byte(state.State)),
CmdLine: cmdline,
Pid: pid,
Ppid: state.Ppid,
Name: state.Name,
State: getProcState(byte(state.State)),
Username: state.Username,
CmdLine: cmdline,
Mem: &ProcMemStat{
Size: mem.Size,
Rss: mem.Resident,
Expand Down
1 change: 1 addition & 0 deletions topbeat/beat/sigar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestGetProcess(t *testing.T) {
assert.True(t, (process.Pid > 0))
assert.True(t, (process.Ppid >= 0))
assert.True(t, (len(process.Name) > 0))
assert.True(t, (len(process.Username) > 0))
assert.NotEqual(t, "unknown", process.State)

// Memory Checks
Expand Down
21 changes: 11 additions & 10 deletions topbeat/beat/topbeat.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strconv"
"time"

"github.com/elastic/gosigar"
sigar "github.com/elastic/gosigar"

"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/cfgfile"
Expand Down Expand Up @@ -213,12 +213,13 @@ func (t *Topbeat) exportProcStats() error {
newProcs[process.Pid] = process

proc := common.MapStr{
"pid": process.Pid,
"ppid": process.Ppid,
"name": process.Name,
"state": process.State,
"mem": process.Mem,
"cpu": process.Cpu,
"pid": process.Pid,
"ppid": process.Ppid,
"name": process.Name,
"state": process.State,
"username": process.Username,
"mem": process.Mem,
"cpu": process.Cpu,
}

if process.CmdLine != "" {
Expand Down Expand Up @@ -383,9 +384,9 @@ func (t *Topbeat) addCpuPercentage(t2 *CpuTimes) {
calculate := func(field2 uint64, field1 uint64) float64 {

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

t2.UserPercent = calculate(t2.User, t1.User)
Expand All @@ -407,7 +408,7 @@ func (t *Topbeat) addCpuPercentageList(t2 []CpuTimes) {
perc := 0.0
delta := field2 - field1
perc = float64(delta) / float64(all_delta)
return Round(perc, .5, 2)
return Round(perc, .5, 4)
}

for i := 0; i < len(t1); i++ {
Expand Down
4 changes: 2 additions & 2 deletions topbeat/beat/topbeat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ func TestCpuPercentage(t *testing.T) {

beat.addCpuPercentage(&cpu2)

assert.Equal(t, cpu2.UserPercent, 0.95)
assert.Equal(t, cpu2.SystemPercent, 0.04)
assert.Equal(t, cpu2.UserPercent, 0.9502)
assert.Equal(t, cpu2.SystemPercent, 0.0448)
}

func TestProcMemPercentage(t *testing.T) {
Expand Down
14 changes: 14 additions & 0 deletions topbeat/tests/system/test_procs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import re
import os
import getpass
from topbeat import TestCase


Expand Down Expand Up @@ -30,6 +32,7 @@ def test_procs(self):
assert re.match("(?i).*topbeat.test(.exe)? -e -c", output["proc.cmdline"])
assert isinstance(output["proc.state"], basestring)
assert isinstance(output["proc.cpu.start_time"], basestring)
self.check_username(output["proc.username"])

for key in [
"proc.pid",
Expand All @@ -48,3 +51,14 @@ def test_procs(self):
"proc.mem.rss_p",
]:
assert type(output[key]) in [int, float]

def check_username(self, observed, expected = None):
if expected == None:
expected = getpass.getuser()

if os.name == 'nt':
parts = observed.split("\\", 2)
assert len(parts) == 2, "Expected proc.username to be of form DOMAIN\username, but was %s" % observed
observed = parts[1]

assert expected == observed, "proc.username = %s, but expected %s" % (observed, expected)

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.

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.

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

Loading

0 comments on commit ba3ed7b

Please sign in to comment.