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

Add implementation of sg_get_user_stats from C library libstatgrab latest version #18

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
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
27 changes: 27 additions & 0 deletions .github/workflows/ci.yaml
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 ./...
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
[![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)

![go report](https://goreportcard.com/badge/github.com/bieli/statgo)
Comment on lines +3 to +5
Copy link
Owner

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


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.


Expand All @@ -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:
Copy link
Owner

Choose a reason for hiding this comment

The 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()
Expand Down Expand Up @@ -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
Expand All @@ -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)
11 changes: 11 additions & 0 deletions go.mod
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
)
10 changes: 10 additions & 0 deletions go.sum
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=
76 changes: 76 additions & 0 deletions users_stats.go
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)
}