-
Notifications
You must be signed in to change notification settings - Fork 30
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
Add implementation of sg_get_user_stats from C library libstatgrab latest version #18
Open
bieli
wants to merge
11
commits into
akhenakh:master
Choose a base branch
from
bieli:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d63e48f
add go module
bieli 7159ce0
add implementation of sg_get_user_stats from C library libstatgrab la…
bieli c56441c
fix Sprintf formants
bieli 6f770b3
add CI in github actions
bieli 8865e5d
add CI in github actions
bieli ab47716
add CI in github actions
bieli 1f5b2cf
add CI in github actions
bieli 6828267
add CI in github actions
bieli de0f19b
update README
bieli 02d0547
update README
bieli 02cb0b4
update test run in CI
bieli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: statgo CI | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
go-version: [ '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 libstatgrab-dev | ||
run: sudo apt-get install -y libstatgrab-dev | ||
- name: Install dependencies | ||
run: | | ||
go get . | ||
- name: Display Go version | ||
run: go version | ||
- name: Run tests | ||
run: go test -v ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
[](https://app.wercker.com/project/byKey/c56e26bf18be587114d26764a7a0ce7a) [](http://godoc.org/github.com/akhenakh/statgo) | ||
|
||
 | ||
|
||
 | ||
|
||
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 +31,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: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: On OSX, libstatgrab can be installed using brew: |
||
|
||
brew install libstatgrab | ||
|
||
|
||
### Usage | ||
``` | ||
s := NewStat() | ||
|
@@ -100,6 +109,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 +131,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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package statgo | ||
|
||
// #cgo LDFLAGS: -lstatgrab | ||
// #include <statgrab.h> | ||
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%s\n"+ | ||
"Device:\t\t%s\n"+ | ||
"Hostname:\t\t%s\n"+ | ||
"LoginTime:\t%v\n", | ||
m.LoginName, | ||
m.RecordId, | ||
m.Device, | ||
m.Hostname, | ||
m.LoginTime) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change the url for the status back to this repo