From 84403788d2e76977689d1fec36414608ec18f201 Mon Sep 17 00:00:00 2001 From: Laurent-PANEK Date: Mon, 24 Jun 2019 15:15:51 +0200 Subject: [PATCH 1/3] :sparkles: add sessions info --- internal/pkg/agent/action.go | 8 ++++++-- internal/pkg/info/host.go | 4 ++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/internal/pkg/agent/action.go b/internal/pkg/agent/action.go index cf086c9..f9a6026 100644 --- a/internal/pkg/agent/action.go +++ b/internal/pkg/agent/action.go @@ -1,11 +1,12 @@ package agent import ( - "github.com/boxmetrics/boxmetrics-agent/internal/pkg/errors" - "github.com/boxmetrics/boxmetrics-agent/internal/pkg/info" "os" "os/exec" "strconv" + + "github.com/boxmetrics/boxmetrics-agent/internal/pkg/errors" + "github.com/boxmetrics/boxmetrics-agent/internal/pkg/info" ) func dispatchEvent(e event) (interface{}, error) { @@ -47,6 +48,9 @@ func dispatchInfo(e event) (interface{}, error) { case "users": Log.Debug("users") return info.Users() + case "sessions": + Log.Debug("sessions") + return info.Sessions() case "network": Log.Debug("network") return info.Network(e.Format) diff --git a/internal/pkg/info/host.go b/internal/pkg/info/host.go index 91ea203..df7ad5b 100644 --- a/internal/pkg/info/host.go +++ b/internal/pkg/info/host.go @@ -168,8 +168,8 @@ func Host(format bool) (HostStatFormat, error) { return hostS.Text(), nil } -// Users of host -func Users() ([]host.UserStat, error) { +// Sessions of active users on system +func Sessions() ([]host.UserStat, error) { usr, err := host.Users() return usr, errors.Convert(err) From 0da91e57ea148af5ffd1fe51aa8bdd050e7578f8 Mon Sep 17 00:00:00 2001 From: Laurent-PANEK Date: Mon, 24 Jun 2019 15:16:30 +0200 Subject: [PATCH 2/3] :rocket: update users info --- internal/pkg/info/host.go | 61 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/internal/pkg/info/host.go b/internal/pkg/info/host.go index df7ad5b..e180187 100644 --- a/internal/pkg/info/host.go +++ b/internal/pkg/info/host.go @@ -1,7 +1,13 @@ package info import ( + "bufio" "encoding/json" + "io" + "os" + "os/user" + "strings" + "github.com/boxmetrics/boxmetrics-agent/internal/pkg/errors" "github.com/shirou/gopsutil/host" ) @@ -173,4 +179,57 @@ func Sessions() ([]host.UserStat, error) { usr, err := host.Users() return usr, errors.Convert(err) -} \ No newline at end of file +} + +// Users list all system user +func Users() ([]*user.User, error) { + var users []string + file, err := os.Open("/etc/passwd") + + if err != nil { + return nil, errors.Convert(err) + } + + defer file.Close() + + reader := bufio.NewReader(file) + + for { + line, err := reader.ReadString('\n') + + // skip all line starting with # + if equal := strings.Index(line, "#"); equal < 0 { + // get the username and description + lineSlice := strings.FieldsFunc(line, func(divide rune) bool { + return divide == ':' + }) + + if len(lineSlice) > 0 { + users = append(users, lineSlice[0]) + } + + } + + if err == io.EOF { + break + } + if err != nil { + return nil, errors.Convert(err) + } + + } + + var usersStat []*user.User + + for _, name := range users { + + usr, err := user.Lookup(name) + if err != nil { + panic(err) + } + + usersStat = append(usersStat, usr) + } + + return usersStat, nil +} From 8ce7703d34286a7dd2f885c888141e903efd2f5f Mon Sep 17 00:00:00 2001 From: Laurent-PANEK Date: Mon, 24 Jun 2019 15:17:07 +0200 Subject: [PATCH 3/3] :pencil: update host info doc --- README.md | 3 ++- docs/schema/host.md | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7c819ab..0b14d94 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,8 @@ Both request and response are JSON message | containers | [Schema](docs/schema/container.md#container) | Return containers full information | | containersid | Array of string | Return containers ID list | | host | [Schema](docs/schema/host.md#host) | Return host information | -| users | [Schema](docs/schema/host.md#users) | Return users list | +| users | [Schema](docs/schema/host.md#user) | Return users list | +| sessions | [Schema](docs/schema/host.md#session) | Return user sessions list | | network | [Schema](docs/schema/network.md#network) | Return network information | | connections | [Schema](docs/schema/network.md#connection) | Return opened connections list | | processes | [Schema](docs/schema/process.md#process-light) | Return processes information list | diff --git a/docs/schema/host.md b/docs/schema/host.md index 9a3e596..eaf8a2e 100644 --- a/docs/schema/host.md +++ b/docs/schema/host.md @@ -25,11 +25,21 @@ | sensorKey | string | Sensor identifier | | temperature | string | Sensor temperature | -## Users +## User + +| Key | Type | Description | +| -------- | ------ | --------------------------- | +| uid | string | User id | +| gid | string | Primary group id | +| username | string | Login name | +| name | string | User's real or display name | +| homeDir | string | User's home directory | + +## Session | Key | Type | Description | | -------- | ------ | ------------------------- | | user | string | User name | | terminal | string | User default terminal | -| host | string | User host | +| host | string | Session host | | started | int | Number of session started |