From d63e48f22722a51cf00d4accf409cebf7c288ad9 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:19:21 +0100 Subject: [PATCH 01/11] add go module --- go.mod | 11 +++++++++++ go.sum | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..5fc1358 --- /dev/null +++ b/go.mod @@ -0,0 +1,11 @@ +module github.com/bieli/statgo + +go 1.21.6 + +require github.com/stretchr/testify v1.8.4 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..fa4b6e6 --- /dev/null +++ b/go.sum @@ -0,0 +1,10 @@ +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 7159ce034f2db09e7240283c95d8cf58b7099d0b Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:23:35 +0100 Subject: [PATCH 02/11] add implementation of sg_get_user_stats from C library libstatgrab latest version --- users_stats.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 users_stats.go diff --git a/users_stats.go b/users_stats.go new file mode 100644 index 0000000..b0fc2c1 --- /dev/null +++ b/users_stats.go @@ -0,0 +1,76 @@ +package statgo + +// #cgo LDFLAGS: -lstatgrab +// #include +import "C" +import ( + "fmt" + "time" + "unsafe" +) + +// UserStats contains user stats +// expressed in bytes +type UserStats struct { + // The username which was used to log in + LoginName string + + // Record identifier of host database containing login information (not necessarily 0-terminated) + RecordId string + + // Size of the record identifier + recordIdSize int + + // Device name (eg. "pts/0") of the tty assigned to the login session + Device string + + // (remote) Hostname from where the user is logged on, eg. "infoterm7.some.kind.of.domain.local", "localhost", "10.42.17.4" or ":0.0" (in case it's a local logon via new xterm) + Hostname string + + // Process identifier of the process which made the entry to the logged on users database + pid int + + // Timestamp (time in seconds since epoch) when the user logged on + LoginTime time.Duration + + // The timestamp when the above stats where collected in seconds since epoch + SysTime time.Time +} + +// UserStats return an UserStats list +func (s *Stat) UserStats() []*UserStats { + s.Lock() + defer s.Unlock() + var userSize C.size_t + var cArray *C.sg_user_stats = C.sg_get_user_stats(&userSize) + length := int(userSize) + slice := (*[1 << 16]C.sg_user_stats)(unsafe.Pointer(cArray))[:length:length] + + var res []*UserStats + + for _, v := range slice { + f := &UserStats{ + LoginName: C.GoString(v.login_name), + RecordId: C.GoString(v.record_id), + Device: C.GoString(v.device), + Hostname: C.GoString(v.hostname), + LoginTime: time.Duration(int(v.login_time)) * time.Second, + } + res = append(res, f) + } + return res +} + +func (m *UserStats) String() string { + return fmt.Sprintf( + "LoginName:\t%s\n"+ + "RecordId:\t\t%d\n"+ + "Device:\t\t%d\n"+ + "Hostname:\t\t%d\n"+ + "LoginTime:\t%d\n", + m.LoginName, + m.RecordId, + m.Device, + m.Hostname, + m.LoginTime) +} From c56441c64458171bdb01af46b1819354d7699878 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:28:07 +0100 Subject: [PATCH 03/11] fix Sprintf formants --- users_stats.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/users_stats.go b/users_stats.go index b0fc2c1..7d2fcbb 100644 --- a/users_stats.go +++ b/users_stats.go @@ -64,10 +64,10 @@ func (s *Stat) UserStats() []*UserStats { func (m *UserStats) String() string { return fmt.Sprintf( "LoginName:\t%s\n"+ - "RecordId:\t\t%d\n"+ - "Device:\t\t%d\n"+ - "Hostname:\t\t%d\n"+ - "LoginTime:\t%d\n", + "RecordId:\t\t%s\n"+ + "Device:\t\t%s\n"+ + "Hostname:\t\t%s\n"+ + "LoginTime:\t%v\n", m.LoginName, m.RecordId, m.Device, From 6f770b39efc293a394559a410a23c584b44b89f2 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:29:43 +0100 Subject: [PATCH 04/11] add CI in github actions --- .github/workspace/ci.yaml | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .github/workspace/ci.yaml diff --git a/.github/workspace/ci.yaml b/.github/workspace/ci.yaml new file mode 100644 index 0000000..48d714e --- /dev/null +++ b/.github/workspace/ci.yaml @@ -0,0 +1,25 @@ +name: Go + +on: [push] + +jobs: + build: + + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [ '1.19', '1.20', '1.21.x' ] + + steps: + - uses: actions/checkout@v4 + - name: Setup Go ${{ matrix.go-version }} + uses: actions/setup-go@v4 + with: + go-version: ${{ matrix.go-version }} + - name: Install dependencies + run: | + go get . + - name: Display Go version + run: go version + - name: Run tests + run: go test \ No newline at end of file From 8865e5da1cc699b646509afe3a5b3d6e4a35d94a Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:31:10 +0100 Subject: [PATCH 05/11] add CI in github actions --- .github/{workspace => workflows}/ci.yaml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/{workspace => workflows}/ci.yaml (100%) diff --git a/.github/workspace/ci.yaml b/.github/workflows/ci.yaml similarity index 100% rename from .github/workspace/ci.yaml rename to .github/workflows/ci.yaml From ab47716ac7b57718584301c7c744d6b06606bd16 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:34:09 +0100 Subject: [PATCH 06/11] add CI in github actions --- .github/workflows/ci.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 48d714e..01cc1c6 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -1,6 +1,6 @@ -name: Go +name: statgo CI -on: [push] +on: [push, pull_request] jobs: build: From 1f5b2cf6319664b4702d85648ffaa96cad5cf4ed Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:37:55 +0100 Subject: [PATCH 07/11] add CI in github actions --- .github/workflows/ci.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 01cc1c6..ac1bca8 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.19', '1.20', '1.21.x' ] + go-version: [ '1.17', '1.18', '1.19', '1.20', '1.21.x' ] steps: - uses: actions/checkout@v4 @@ -16,6 +16,8 @@ jobs: uses: actions/setup-go@v4 with: go-version: ${{ matrix.go-version }} + - name: Install libstatgrab-dev + run: sudo apt-get install -y libstatgrab-dev - name: Install dependencies run: | go get . From 6828267f3d5ea7f8a8571eafc64fa417598770af Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 21:39:50 +0100 Subject: [PATCH 08/11] add CI in github actions --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index ac1bca8..1874f80 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.17', '1.18', '1.19', '1.20', '1.21.x' ] + go-version: [ '1.21.x' ] steps: - uses: actions/checkout@v4 From de0f19b4c9985c3a78060f57589ea423b15f08b8 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 22:03:20 +0100 Subject: [PATCH 09/11] update README --- README.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fd3589a..c8ed906 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,12 @@ [![wercker status](https://app.wercker.com/status/c56e26bf18be587114d26764a7a0ce7a/s/master "wercker status")](https://app.wercker.com/project/byKey/c56e26bf18be587114d26764a7a0ce7a) [![](https://godoc.org/github.com/akhenakh/statgo?status.png)](http://godoc.org/github.com/akhenakh/statgo) +![CI status](https://github.com/bieli/statgo/actions/workflows/ci.yaml/badge.svg) + + StatGo ====== StatGo give you access to OS metrics like network interface bandwith, cpus usage ... -It supports FreeBSD, Linux, OSX & more, it's in fact a [libstatgrab](http://www.i-scream.org/libstatgrab/) binding for Golang. +It supports FreeBSD, Linux, OSX & more, it's in fact a [libstatgrab](https://libstatgrab.org/) binding for Golang. Tested on FreeBSD, OSX, Linux amd64, Linux arm. @@ -27,6 +30,11 @@ Note: On OSX you need to install gcc to access cgo. go get github.com/akhenakh/statgo +Note: On OSX you need to install brew and install library in OS with this cmd: + + brew install libstatgrab + + ### Usage ``` s := NewStat() @@ -100,6 +108,14 @@ Zombie: 8 s.PagesStats() PageIn: 90173695 PageOut: 90173695 + +s.UserStats()[0] +LoginName: penguin, +RecordId: , +Device: :0, +Hostname: :0, +LoginTime: 170515858, +SysTime: 2023-01-29T21:38:53Z" ``` ### Status @@ -114,6 +130,8 @@ PageOut: 90173695 - [x] net io stats - [x] process count - [x] page stats +- [x] users stats ### Contributors * [HeinOldewage](https://github.com/HeinOldewage) +* [Marcin Bielak -> bieli](https://github.com/bieli) From 02d0547a26a09d907fa7fcce227b0f03d6bb7440 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 22:43:13 +0100 Subject: [PATCH 10/11] update README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c8ed906..7418365 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,7 @@ ![CI status](https://github.com/bieli/statgo/actions/workflows/ci.yaml/badge.svg) +![go report](https://goreportcard.com/badge/github.com/bieli/statgo) StatGo ====== From 02cb0b4451f309deca21658f5fc257fc17311cd6 Mon Sep 17 00:00:00 2001 From: Marcin Bielak Date: Mon, 29 Jan 2024 22:58:44 +0100 Subject: [PATCH 11/11] update test run in CI --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 1874f80..edff7a0 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -24,4 +24,4 @@ jobs: - name: Display Go version run: go version - name: Run tests - run: go test \ No newline at end of file + run: go test -v ./... \ No newline at end of file