Skip to content

Instantly share code, notes, and snippets.

@aeppert
Last active July 5, 2017 17:22
Show Gist options
  • Select an option

  • Save aeppert/b0c5364059f1ed5ad6e843bf526278f2 to your computer and use it in GitHub Desktop.

Select an option

Save aeppert/b0c5364059f1ed5ad6e843bf526278f2 to your computer and use it in GitHub Desktop.
Periodic polling of load average and check for an increase
package main
import (
"fmt"
"time"
"github.com/shirou/gopsutil/load"
)
var currentLoad *load.AvgStat
func hasLoadIncreased(old, new *load.AvgStat) bool {
if &old == &new {
return false
}
if new.Load1 > old.Load1 ||
new.Load5 > old.Load5 ||
new.Load15 > old.Load15 {
return true
}
return false
}
func main() {
tick := time.Tick(2 * time.Second)
boom := time.After(1 * time.Second)
currentLoad, _ = load.Avg()
for {
select {
case <-tick:
fmt.Printf("currentLoad = %v\n", currentLoad)
newLoad, _ := load.Avg()
fmt.Printf("newLoad = %v\n", newLoad)
fmt.Printf("Increase - %v\n", hasLoadIncreased(currentLoad, newLoad))
currentLoad = newLoad
time.Sleep(10 * time.Second)
case <-boom:
fmt.Printf("BOOM - currentLoad = %v\n", currentLoad)
return
default:
fmt.Printf("DONE - currentLoad = %v\n", currentLoad)
time.Sleep(30 * time.Second)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment