2019-07-23 22:35:19 +00:00
|
|
|
package notification
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2020-11-11 18:54:21 +00:00
|
|
|
// StatusRule includes parameters of status rules.
|
2019-07-23 22:35:19 +00:00
|
|
|
type StatusRule struct {
|
2019-08-30 00:46:00 +00:00
|
|
|
CurrentLevel CheckLevel `json:"currentLevel"`
|
|
|
|
PreviousLevel *CheckLevel `json:"previousLevel"`
|
2019-07-23 22:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CheckLevel is the enum value of status levels.
|
|
|
|
type CheckLevel int
|
|
|
|
|
|
|
|
// consts of CheckStatusLevel
|
|
|
|
const (
|
|
|
|
Unknown CheckLevel = iota
|
|
|
|
Ok
|
|
|
|
Info
|
|
|
|
Warn
|
2019-08-15 14:16:18 +00:00
|
|
|
Critical
|
2019-08-30 00:46:00 +00:00
|
|
|
Any
|
2019-07-23 22:35:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var checkLevels = []string{
|
|
|
|
"UNKNOWN",
|
|
|
|
"OK",
|
|
|
|
"INFO",
|
|
|
|
"WARN",
|
2019-08-15 14:16:18 +00:00
|
|
|
"CRIT",
|
2019-08-30 00:46:00 +00:00
|
|
|
"ANY",
|
2019-07-23 22:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var checkLevelMaps = map[string]CheckLevel{
|
|
|
|
"UNKNOWN": Unknown,
|
|
|
|
"OK": Ok,
|
|
|
|
"INFO": Info,
|
|
|
|
"WARN": Warn,
|
2019-08-15 14:16:18 +00:00
|
|
|
"CRIT": Critical,
|
2019-08-30 00:46:00 +00:00
|
|
|
"ANY": Any,
|
2019-07-23 22:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalJSON implements json.Marshaller.
|
|
|
|
func (cl CheckLevel) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(cl.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaller.
|
|
|
|
func (cl *CheckLevel) UnmarshalJSON(b []byte) error {
|
|
|
|
var ss string
|
|
|
|
if err := json.Unmarshal(b, &ss); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*cl = ParseCheckLevel(strings.ToUpper(ss))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns the string value, invalid CheckLevel will return Unknown.
|
|
|
|
func (cl CheckLevel) String() string {
|
2019-09-05 19:11:43 +00:00
|
|
|
if cl < Unknown || cl > Any {
|
2019-07-23 22:35:19 +00:00
|
|
|
cl = Unknown
|
|
|
|
}
|
|
|
|
return checkLevels[cl]
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseCheckLevel will parse the string to checkLevel
|
|
|
|
func ParseCheckLevel(s string) CheckLevel {
|
|
|
|
if cl, ok := checkLevelMaps[s]; ok {
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
return Unknown
|
|
|
|
}
|