Skip to content

Commit

Permalink
added tasklist for windows
Browse files Browse the repository at this point in the history
  • Loading branch information
deven96 committed Oct 27, 2021
1 parent 4810ebf commit 09912ab
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 3 deletions.
11 changes: 8 additions & 3 deletions inspector/inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ const (
File
)

// exclude windows specific inspectors
var inspectorMap = map[string]Inspector{
`disk`: NewDF(),
`meminfo`: NewMemInfo(),
`dockerstats`: NewDockerStats(),
`disk`: NewDF(),
`meminfo`: NewMemInfo(),
`dockerstats`: NewDockerStats(),
`uptime`: NewUptime(),
`loadavg`: NewLoadAvg(),
`responsetime`: NewResponseTime(),
`custom`: NewCustom(``),
}

type fields struct {
Expand Down
80 changes: 80 additions & 0 deletions inspector/tasklist_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package inspector

import (
log "github.com/sirupsen/logrus"
"strconv"
"strings"
)

// TasklistMetrics : Metrics used by Tasklist
type TasklistMetrics struct {
Command string
Session string
Pid int
// value of memory used
Memory float64
}

// Tasklist : Parsing the `tasklist` output for process monitoring on windows
type Tasklist struct {
fields
// Track this particular PID
TrackPID int
// We want do display memory values in KB
DisplayByteSize string
// Values of metrics being read
Values []TasklistMetrics
}

// Parse : run custom parsing on output of the command
func (i *Tasklist) Parse(output string) {
var values []TasklistMetrics
lines := strings.Split(output, "\r\n")
for index, line := range lines {
// skip title line
if index == 0 || index == 1 {
continue
}
columns := strings.Fields(line)
lenCol := len(columns)
if lenCol >= 6 {
pid, err := strconv.Atoi(columns[lenCol-5])
if err != nil {
log.Fatal("Could not parse pid in Tasklist")
}
// If we are tracking only a particular ID then break loop
if i.TrackPID != 0 && i.TrackPID == pid {
value := i.createMetric(columns, pid)
values = append(values, value)
break
} else if i.TrackPID == 0 {
value := i.createMetric(columns, pid)
values = append(values, value)
}
}
}
i.Values = values
}

func (i Tasklist) createMetric(columns []string, pid int) TasklistMetrics {
lenCol := len(columns)

return TasklistMetrics{
Command: strings.Join(columns[:lenCol-5], " "),
Memory: NewByteSize(columns[lenCol-2], `KB`).format(i.DisplayByteSize),
Session: columns[lenCol-4],
Pid: pid,
}
}

// NewTasklist : Initialize a new Tasklist instance
func NewTasklist() *Tasklist {
return &Tasklist{
fields: fields{
Type: Command,
Command: `tasklist`,
},
DisplayByteSize: `KB`,
}

}
23 changes: 23 additions & 0 deletions integration/integration_windows_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package integration

import (
"fmt"
"testing"

"github.com/bisoncorps/saido/driver"
"github.com/bisoncorps/saido/inspector"
)

func TestTasklistonLocal(t *testing.T) {
d := driver.Local{}
i := inspector.NewTasklist()
output, err := d.RunCommand(i.String())
if err != nil {
t.Error(err)
}
i.Parse(output)
if len(i.Values) <= 1 {
t.Error("showing 1 or less tasks/processes")
}
fmt.Printf(`%#v`, i.Values)
}

0 comments on commit 09912ab

Please sign in to comment.