Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update users info #29

Merged
merged 3 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
Expand Down
14 changes: 12 additions & 2 deletions docs/schema/host.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
8 changes: 6 additions & 2 deletions internal/pkg/agent/action.go
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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)
Expand Down
65 changes: 62 additions & 3 deletions internal/pkg/info/host.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -168,9 +174,62 @@ 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)
}
}

// 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
}