2020-11-12 14:26:46 +01:00
|
|
|
package analyse
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/hamburghammer/gmon/stats"
|
|
|
|
)
|
|
|
|
|
|
|
|
// RAMRule represents a rule to analyse the ram usage.
|
|
|
|
type RAMRule struct {
|
|
|
|
Rule
|
|
|
|
Warning int
|
|
|
|
Alert int
|
|
|
|
}
|
|
|
|
|
|
|
|
// Analyse analyses the given data based on the rules.
|
|
|
|
func (rr RAMRule) Analyse(data stats.Data) (Result, error) {
|
|
|
|
notification := Result{Title: rr.Name, Description: rr.Description}
|
|
|
|
var cf compareIntFunc
|
|
|
|
|
|
|
|
switch rr.Compare {
|
|
|
|
case ">":
|
|
|
|
cf = func(want int) bool {
|
|
|
|
return data.Mem.Used > want
|
|
|
|
}
|
|
|
|
case "<":
|
|
|
|
cf = func(want int) bool {
|
|
|
|
return data.Mem.Used < want
|
|
|
|
}
|
|
|
|
case "=":
|
|
|
|
cf = func(want int) bool {
|
|
|
|
return data.Mem.Used == want
|
|
|
|
}
|
|
|
|
case "!=":
|
|
|
|
cf = func(want int) bool {
|
|
|
|
return data.Mem.Used != want
|
|
|
|
}
|
|
|
|
default:
|
2020-11-14 19:21:37 +01:00
|
|
|
return Result{}, fmt.Errorf("RAM rule '%s': %w", rr.Name, ErrCompareMatching)
|
2020-11-12 14:26:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
notification.Status = rr.compare(cf, rr.Compare)
|
|
|
|
return notification, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rr RAMRule) compare(cf compareIntFunc, compareChar string) Status {
|
|
|
|
if rr.Alert != 0 && cf(rr.Alert) {
|
2020-11-14 19:21:37 +01:00
|
|
|
return Status{AlertStatus: StatusAlert, StatusMessage: fmt.Sprintf("RAM usage %s as %d", compareChar, rr.Alert)}
|
2020-11-12 14:26:46 +01:00
|
|
|
} else if rr.Warning != 0 && cf(rr.Warning) {
|
2020-11-14 19:21:37 +01:00
|
|
|
return Status{AlertStatus: StatusWarning, StatusMessage: fmt.Sprintf("RAM usage %s as %d", compareChar, rr.Warning)}
|
2020-11-12 14:26:46 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 19:21:37 +01:00
|
|
|
return Status{AlertStatus: StatusOK, StatusMessage: "RAM usage is OK"}
|
2020-11-12 14:26:46 +01:00
|
|
|
}
|