forked from hswaw/hscloud
go/statusz: factor out load avg to separate file
This commit is contained in:
parent
3a2a693e0c
commit
1affad42e7
3 changed files with 19 additions and 9 deletions
|
@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
|||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["statusz.go"],
|
||||
srcs = [
|
||||
"load.go",
|
||||
"statusz.go",
|
||||
],
|
||||
importpath = "code.hackerspace.pl/hscloud/go/statusz",
|
||||
visibility = ["//visibility:public"],
|
||||
deps = [
|
||||
|
|
14
go/statusz/load.go
Normal file
14
go/statusz/load.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package statusz
|
||||
|
||||
import "github.com/shirou/gopsutil/load"
|
||||
|
||||
import "fmt"
|
||||
|
||||
func loadAverage() string {
|
||||
loadstr := "unknown"
|
||||
l, err := load.Avg()
|
||||
if err == nil {
|
||||
loadstr = fmt.Sprintf("%.2f %.2f %.2f", l.Load1, l.Load5, l.Load15)
|
||||
}
|
||||
return loadstr
|
||||
}
|
|
@ -46,7 +46,6 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/shirou/gopsutil/load"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -134,12 +133,6 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||
lock.Lock()
|
||||
defer lock.Unlock()
|
||||
|
||||
loadavg := "unknown"
|
||||
l, err := load.AvgWithContext(r.Context())
|
||||
if err == nil {
|
||||
loadavg = fmt.Sprintf("%.2f %.2f %.2f", l.Load1, l.Load5, l.Load15)
|
||||
}
|
||||
|
||||
data := struct {
|
||||
Sections []section
|
||||
BinaryName string
|
||||
|
@ -157,7 +150,7 @@ func StatusHandler(w http.ResponseWriter, r *http.Request) {
|
|||
Username: username,
|
||||
StartTime: serverStart.Format(time.RFC1123),
|
||||
CurrentTime: time.Now().Format(time.RFC1123),
|
||||
LoadAvg: loadavg,
|
||||
LoadAvg: loadAverage(),
|
||||
}
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "status", data); err != nil {
|
||||
|
|
Loading…
Add table
Reference in a new issue