-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathload.go
45 lines (36 loc) · 1.42 KB
/
load.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package main
import (
"fmt"
"github.com/shirou/gopsutil/cpu"
"github.com/shirou/gopsutil/load"
)
// LoadWarningThreshold is the warning threshold
const LoadWarningThreshold = 1.5
// LoadThreshold is the problem threshold
const LoadThreshold = 2.0
func checkLoad() (bool, string, string, int, error) {
// get the system load
systemLoad, err := load.Avg()
if err != nil {
return false, fmt.Sprintf("Error system load: %s", err.Error()), "", StatusProblem, err
}
// count the cpus
numCPU, err := cpu.Counts(true)
if err != nil {
return false, fmt.Sprintf("Error checking CPU: %s", err.Error()), "", StatusProblem, err
}
okay := true
newStatusID := StatusHealthy
msg := fmt.Sprintf("System load okay: %0.4f, %0.4f, %0.4f for %d cpus", systemLoad.Load1, systemLoad.Load5, systemLoad.Load15, numCPU)
// check the threshold * number of cpus to determine if there is a warning/problem
if systemLoad.Load1 > LoadWarningThreshold*float64(numCPU) {
newStatusID = StatusWarning
msg = fmt.Sprintf("Warning: Moderate system load: %0.4f, %0.4f, %0.4f for %d cpus", systemLoad.Load1, systemLoad.Load5, systemLoad.Load15, numCPU)
okay = false
} else if systemLoad.Load1 > LoadThreshold*float64(numCPU) {
okay = false
msg = fmt.Sprintf("Problem: High system load: %0.4f, %0.4f, %0.4f for %d cpus", systemLoad.Load1, systemLoad.Load5, systemLoad.Load15, numCPU)
newStatusID = StatusProblem
}
return okay, msg, "", newStatusID, nil
}