2021-03-21 23:17:15 +00:00
|
|
|
package check
|
|
|
|
|
2021-03-22 02:39:42 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"go/slack-bot/pkg/notify"
|
|
|
|
"os/exec"
|
|
|
|
)
|
2021-03-21 23:17:15 +00:00
|
|
|
|
2021-03-22 02:39:42 +00:00
|
|
|
//Checker representation of check set
|
2021-03-21 23:17:15 +00:00
|
|
|
type Checker interface {
|
2021-03-22 21:19:50 +00:00
|
|
|
Check(f func(stdout string) []notify.Message) []notify.Message
|
2021-03-22 02:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//AdvancedChecker like Checker except contain Parsing step
|
|
|
|
type AdvancedChecker interface {
|
|
|
|
Checker
|
2021-03-22 21:19:50 +00:00
|
|
|
Parse(stdout string) interface{}
|
2021-03-21 23:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//CliChecker implement checker with cli command
|
|
|
|
type CliChecker struct {
|
2021-03-22 02:39:42 +00:00
|
|
|
alertingMessage string
|
|
|
|
command []string
|
2021-03-21 23:17:15 +00:00
|
|
|
}
|
|
|
|
|
2021-03-22 02:39:42 +00:00
|
|
|
//NewCliChecker build new cli checker
|
|
|
|
func NewCliChecker(alertingMessage string, command ...string) Checker {
|
|
|
|
return &CliChecker{
|
|
|
|
alertingMessage: alertingMessage,
|
|
|
|
command: command,
|
|
|
|
}
|
2021-03-21 23:17:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Check function use to run cli call to check some parameter
|
2021-03-22 21:19:50 +00:00
|
|
|
func (cc *CliChecker) Check(f func(stdout string) []notify.Message) []notify.Message {
|
|
|
|
message := []notify.Message{}
|
2021-03-22 02:39:42 +00:00
|
|
|
|
|
|
|
out, err := exec.Command(cc.command[0], cc.command[1:]...).CombinedOutput()
|
|
|
|
if err != nil {
|
|
|
|
// command fail notify with error
|
2021-03-22 21:19:50 +00:00
|
|
|
message = append(message,
|
|
|
|
notify.Message{
|
|
|
|
Content: fmt.Sprint(cc.alertingMessage, " : ", string(out), err),
|
|
|
|
ShouldNotify: true,
|
|
|
|
},
|
|
|
|
)
|
2021-03-22 02:39:42 +00:00
|
|
|
} else {
|
|
|
|
// command success check if condition required notify
|
|
|
|
message = f(string(out))
|
2021-03-21 23:17:15 +00:00
|
|
|
}
|
2021-03-22 02:39:42 +00:00
|
|
|
|
|
|
|
return message
|
2021-03-21 23:17:15 +00:00
|
|
|
}
|