Skip to content

Commit

Permalink
Add proc.username to Topbeat
Browse files Browse the repository at this point in the history
Backport of elastic#845 to 1.2.1
  • Loading branch information
andrewkroh committed Apr 6, 2016
1 parent 4930b6a commit 570ffce
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ https://github.com/elastic/beats/compare/v1.1.2...v1.2.0[View commits]
*Topbeat*
- 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
11 changes: 6 additions & 5 deletions topbeat/beat/sigar.go
Original file line number Diff line number Diff line change
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
13 changes: 7 additions & 6 deletions topbeat/beat/topbeat.go
Original file line number Diff line number Diff line change
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
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)

0 comments on commit 570ffce

Please sign in to comment.