turn process monitoring events into structured objects

This commit is contained in:
Dominic Breuker
2019-04-24 22:08:59 +02:00
parent 8a1838faee
commit d1b6518db5
6 changed files with 61 additions and 31 deletions

View File

@@ -25,7 +25,7 @@ var cmdLineReader = func(pid int) ([]byte, error) {
type procList map[int]string
func (pl procList) refresh(eventCh chan string) error {
func (pl procList) refresh(eventCh chan PSEvent) error {
pids, err := getPIDs()
if err != nil {
return err
@@ -74,16 +74,16 @@ func file2Pid(f os.FileInfo) (int, error) {
return pid, nil
}
func (pl procList) addPid(pid int, eventCh chan string) {
func (pl procList) addPid(pid int, eventCh chan PSEvent) {
cmd, err := getCmd(pid)
if err != nil {
cmd = "???" // process probably terminated
}
uid, err := getUID(pid)
if err != nil {
uid = "???"
uid = -1
}
eventCh <- fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, pid, cmd)
eventCh <- PSEvent{UID: uid, PID: pid, CMD: cmd}
pl[pid] = cmd
}
@@ -100,18 +100,26 @@ func getCmd(pid int) (string, error) {
return string(cmd), nil
}
func getUID(pid int) (string, error) {
func getUID(pid int) (int, error) {
stat, err := procStatusReader(pid)
if err != nil {
return "", err
return -1, err
}
lines := strings.Split(string(stat), "\n")
if len(lines) < 9 {
return "", fmt.Errorf("no uid information")
return -1, fmt.Errorf("no uid information")
}
uidL := strings.Split(lines[8], "\t")
if len(uidL) < 2 {
return "", fmt.Errorf("uid line read incomplete")
return -1, fmt.Errorf("uid line read incomplete")
}
return uidL[1], nil
uid, err := strconv.Atoi(uidL[1])
if err != nil {
return -1, fmt.Errorf("converting %s to int: %v", uidL[1], err)
}
return uid, nil
}

View File

@@ -136,14 +136,14 @@ func TestGetUID(t *testing.T) {
pid int
stat []byte
statErr error
uid string
uid int
err string
}{
{pid: 7, stat: completeStatus, statErr: nil, uid: "0", err: ""}, // can read normal stat
{pid: 7, stat: uidLineBroken, statErr: nil, uid: "", err: "uid line read incomplete"}, // errors on incomplete Uid line
{pid: 7, stat: notEnoughLines, statErr: nil, uid: "", err: "no uid information"}, // errors for insufficient lines
{pid: 7, stat: []byte(""), statErr: nil, uid: "", err: "no uid information"}, // errors for insufficient lines
{pid: 7, stat: []byte(""), statErr: errors.New("file-system-error"), uid: "", err: "file-system-error"}, // returns file system errors
{pid: 7, stat: completeStatus, statErr: nil, uid: 0, err: ""}, // can read normal stat
{pid: 7, stat: uidLineBroken, statErr: nil, uid: -1, err: "uid line read incomplete"}, // errors on incomplete Uid line
{pid: 7, stat: notEnoughLines, statErr: nil, uid: -1, err: "no uid information"}, // errors for insufficient lines
{pid: 7, stat: []byte(""), statErr: nil, uid: -1, err: "no uid information"}, // errors for insufficient lines
{pid: 7, stat: []byte(""), statErr: errors.New("file-system-error"), uid: -1, err: "file-system-error"}, // returns file system errors
}
for _, tt := range tests {
@@ -151,7 +151,7 @@ func TestGetUID(t *testing.T) {
uid, err := getUID(tt.pid)
if uid != tt.uid {
fmt.Printf("STAT: %s", tt.stat)
t.Errorf("Wrong uid returned: got %s but want %s", uid, tt.uid)
t.Errorf("Wrong uid returned: got %d but want %d", uid, tt.uid)
}
if (err != nil || tt.err != "") && fmt.Sprintf("%v", err) != tt.err {
t.Errorf("Wrong error returned: got %v but want %s", err, tt.err)
@@ -174,18 +174,18 @@ func mockProcStatusReader(stat []byte, err error) (restore func()) {
func TestRefresh(t *testing.T) {
tests := []struct {
eventCh chan string
eventCh chan PSEvent
pl procList
newPids []int
pidsAfter []int
events []string
}{
{eventCh: make(chan string), pl: procList{}, newPids: []int{1, 2, 3}, pidsAfter: []int{3, 2, 1}, events: []string{
{eventCh: make(chan PSEvent), pl: procList{}, newPids: []int{1, 2, 3}, pidsAfter: []int{3, 2, 1}, events: []string{
"UID=??? PID=3 | the-command",
"UID=??? PID=2 | the-command",
"UID=??? PID=1 | the-command",
}},
{eventCh: make(chan string), pl: procList{1: "pid-found-before"}, newPids: []int{1, 2, 3}, pidsAfter: []int{1, 3, 2}, events: []string{
{eventCh: make(chan PSEvent), pl: procList{1: "pid-found-before"}, newPids: []int{1, 2, 3}, pidsAfter: []int{1, 3, 2}, events: []string{
"UID=??? PID=3 | the-command",
"UID=??? PID=2 | the-command",
}}, // no events emitted for PIDs already known
@@ -200,7 +200,7 @@ func TestRefresh(t *testing.T) {
done := make(chan struct{})
go func() {
for e := range tt.eventCh {
events = append(events, e)
events = append(events, e.String())
}
done <- struct{}{}
}()

View File

@@ -1,13 +1,33 @@
package psscanner
import (
"fmt"
"strconv"
)
type PSScanner struct{}
type PSEvent struct {
UID int
PID int
CMD string
}
func (evt PSEvent) String() string {
uid := strconv.Itoa(evt.UID)
if evt.UID == -1 {
uid = "???"
}
return fmt.Sprintf("UID=%-4s PID=%-6d | %s", uid, evt.PID, evt.CMD)
}
func NewPSScanner() *PSScanner {
return &PSScanner{}
}
func (p *PSScanner) Run(triggerCh chan struct{}) (chan string, chan error) {
eventCh := make(chan string, 100)
func (p *PSScanner) Run(triggerCh chan struct{}) (chan PSEvent, chan error) {
eventCh := make(chan PSEvent, 100)
errCh := make(chan error)
pl := make(procList)

View File

@@ -48,7 +48,7 @@ func TestRun(t *testing.T) {
case <-time.After(timeout):
t.Errorf("did not receive event in time")
case e := <-eventCh:
if e != tt.events[i] {
if e.String() != tt.events[i] {
t.Errorf("Wrong event received: got '%s' but wanted '%s'", e, tt.events[i])
}
case err := <-errCh: