Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add one minute load avg for monitor.
Fix CI/CD errors. Fix Windows. Fix Windows. Fix golint error.
- Loading branch information
Showing
14 changed files
with
676 additions
and
3 deletions.
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
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,31 @@ | ||
package load | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
//var invoke common.Invoker = common.Invoke{} | ||
|
||
type AvgStat struct { | ||
Load1 float64 `json:"load1"` | ||
Load5 float64 `json:"load5"` | ||
Load15 float64 `json:"load15"` | ||
} | ||
|
||
func (l AvgStat) String() string { | ||
s, _ := json.Marshal(l) | ||
return string(s) | ||
} | ||
|
||
type MiscStat struct { | ||
ProcsTotal int64 `json:"procsTotal"` | ||
ProcsCreated int64 `json:"procsCreated"` | ||
ProcsRunning int64 `json:"procsRunning"` | ||
ProcsBlocked int64 `json:"procsBlocked"` | ||
Ctxt int64 `json:"ctxt"` | ||
} | ||
|
||
func (m MiscStat) String() string { | ||
s, _ := json.Marshal(m) | ||
return string(s) | ||
} |
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,80 @@ | ||
// +build freebsd openbsd | ||
|
||
package load | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"strings" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func Avg() (*AvgStat, error) { | ||
return AvgWithContext(context.Background()) | ||
} | ||
|
||
func AvgWithContext(ctx context.Context) (*AvgStat, error) { | ||
// This SysctlRaw method borrowed from | ||
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go | ||
type loadavg struct { | ||
load [3]uint32 | ||
scale int | ||
} | ||
b, err := unix.SysctlRaw("vm.loadavg") | ||
if err != nil { | ||
return nil, err | ||
} | ||
load := *(*loadavg)(unsafe.Pointer((&b[0]))) | ||
scale := float64(load.scale) | ||
ret := &AvgStat{ | ||
Load1: float64(load.load[0]) / scale, | ||
Load5: float64(load.load[1]) / scale, | ||
Load15: float64(load.load[2]) / scale, | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
type forkstat struct { | ||
forks int | ||
vforks int | ||
__tforks int | ||
} | ||
|
||
// Misc returns miscellaneous host-wide statistics. | ||
// darwin use ps command to get process running/blocked count. | ||
// Almost same as Darwin implementation, but state is different. | ||
func Misc() (*MiscStat, error) { | ||
return MiscWithContext(context.Background()) | ||
} | ||
|
||
func MiscWithContext(ctx context.Context) (*MiscStat, error) { | ||
bin, err := exec.LookPath("ps") | ||
if err != nil { | ||
return nil, err | ||
} | ||
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state") | ||
if err != nil { | ||
return nil, err | ||
} | ||
lines := strings.Split(string(out), "\n") | ||
|
||
ret := MiscStat{} | ||
for _, l := range lines { | ||
if strings.Contains(l, "R") { | ||
ret.ProcsRunning++ | ||
} else if strings.Contains(l, "D") { | ||
ret.ProcsBlocked++ | ||
} | ||
} | ||
|
||
f, err := getForkStat() | ||
if err != nil { | ||
return nil, err | ||
} | ||
ret.ProcsCreated = f.forks | ||
|
||
return &ret, nil | ||
} |
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,71 @@ | ||
// +build darwin | ||
|
||
package load | ||
|
||
import ( | ||
"context" | ||
"os/exec" | ||
"strings" | ||
"unsafe" | ||
|
||
"golang.org/x/sys/unix" | ||
) | ||
|
||
func Avg() (*AvgStat, error) { | ||
return AvgWithContext(context.Background()) | ||
} | ||
|
||
func AvgWithContext(ctx context.Context) (*AvgStat, error) { | ||
// This SysctlRaw method borrowed from | ||
// https://github.com/prometheus/node_exporter/blob/master/collector/loadavg_freebsd.go | ||
// this implementation is common with BSDs | ||
type loadavg struct { | ||
load [3]uint32 | ||
scale int | ||
} | ||
b, err := unix.SysctlRaw("vm.loadavg") | ||
if err != nil { | ||
return nil, err | ||
} | ||
load := *(*loadavg)(unsafe.Pointer((&b[0]))) | ||
scale := float64(load.scale) | ||
ret := &AvgStat{ | ||
Load1: float64(load.load[0]) / scale, | ||
Load5: float64(load.load[1]) / scale, | ||
Load15: float64(load.load[2]) / scale, | ||
} | ||
|
||
return ret, nil | ||
} | ||
|
||
// Misc returnes miscellaneous host-wide statistics. | ||
// darwin use ps command to get process running/blocked count. | ||
// Almost same as FreeBSD implementation, but state is different. | ||
// U means 'Uninterruptible Sleep'. | ||
func Misc() (*MiscStat, error) { | ||
return MiscWithContext(context.Background()) | ||
} | ||
|
||
func MiscWithContext(ctx context.Context) (*MiscStat, error) { | ||
bin, err := exec.LookPath("ps") | ||
if err != nil { | ||
return nil, err | ||
} | ||
out, err := invoke.CommandWithContext(ctx, bin, "axo", "state") | ||
if err != nil { | ||
return nil, err | ||
} | ||
lines := strings.Split(string(out), "\n") | ||
|
||
ret := MiscStat{} | ||
for _, l := range lines { | ||
if strings.Contains(l, "R") { | ||
ret.ProcsRunning++ | ||
} else if strings.Contains(l, "U") { | ||
// uninterruptible sleep == blocked | ||
ret.ProcsBlocked++ | ||
} | ||
} | ||
|
||
return &ret, nil | ||
} |
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,25 @@ | ||
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris | ||
|
||
package load | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/shirou/gopsutil/internal/common" | ||
) | ||
|
||
func Avg() (*AvgStat, error) { | ||
return AvgWithContext(context.Background()) | ||
} | ||
|
||
func AvgWithContext(ctx context.Context) (*AvgStat, error) { | ||
return nil, common.ErrNotImplementedError | ||
} | ||
|
||
func Misc() (*MiscStat, error) { | ||
return MiscWithContext(context.Background()) | ||
} | ||
|
||
func MiscWithContext(ctx context.Context) (*MiscStat, error) { | ||
return nil, common.ErrNotImplementedError | ||
} |
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,7 @@ | ||
// +build freebsd | ||
|
||
package load | ||
|
||
func getForkStat() (forkstat, error) { | ||
return forkstat{}, nil | ||
} |
Oops, something went wrong.