2020-03-12 08:09:25 +00:00
|
|
|
package notifiers
|
2018-11-07 07:53:39 +00:00
|
|
|
|
|
|
|
import (
|
2020-07-02 18:41:46 +00:00
|
|
|
"github.com/statping/statping/types/errors"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/failures"
|
2020-03-14 03:13:20 +00:00
|
|
|
"github.com/statping/statping/types/notifications"
|
|
|
|
"github.com/statping/statping/types/notifier"
|
2020-07-22 19:07:42 +00:00
|
|
|
"github.com/statping/statping/types/null"
|
2020-03-09 18:17:55 +00:00
|
|
|
"github.com/statping/statping/types/services"
|
|
|
|
"github.com/statping/statping/utils"
|
2020-03-02 07:53:46 +00:00
|
|
|
"strings"
|
2018-11-07 07:53:39 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
var _ notifier.Notifier = (*commandLine)(nil)
|
2020-03-09 15:15:15 +00:00
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
type commandLine struct {
|
2020-03-14 03:13:20 +00:00
|
|
|
*notifications.Notification
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
func (c *commandLine) Select() *notifications.Notification {
|
|
|
|
return c.Notification
|
|
|
|
}
|
|
|
|
|
2020-08-03 05:48:35 +00:00
|
|
|
func (c *commandLine) Valid(values notifications.Values) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-03-14 03:13:20 +00:00
|
|
|
var Command = &commandLine{¬ifications.Notification{
|
2020-03-09 15:15:15 +00:00
|
|
|
Method: "command",
|
2020-04-11 05:59:51 +00:00
|
|
|
Title: "Command",
|
2019-10-25 15:13:32 +00:00
|
|
|
Description: "Shell Command allows you to run a customized shell/bash Command on the local machine it's running on.",
|
2018-11-07 07:53:39 +00:00
|
|
|
Author: "Hunter Long",
|
|
|
|
AuthorUrl: "https://github.com/hunterlong",
|
|
|
|
Delay: time.Duration(1 * time.Second),
|
2018-11-07 09:33:51 +00:00
|
|
|
Icon: "fas fa-terminal",
|
2020-07-22 19:07:42 +00:00
|
|
|
SuccessData: null.NewNullString("/usr/bin/curl -L http://localhost:8080"),
|
|
|
|
FailureData: null.NewNullString("/usr/bin/curl -L http://localhost:8080"),
|
2020-06-15 04:46:28 +00:00
|
|
|
DataType: "text",
|
2020-03-17 03:13:07 +00:00
|
|
|
Limits: 60,
|
2020-07-02 18:41:46 +00:00
|
|
|
}}
|
2018-11-07 07:53:39 +00:00
|
|
|
|
2020-07-02 18:41:46 +00:00
|
|
|
func runCommand(cmd string) (string, string, error) {
|
|
|
|
utils.Log.Infof("Command notifier sending: %s", cmd)
|
|
|
|
cmdApp := strings.Split(cmd, " ")
|
|
|
|
if len(cmd) == 0 {
|
|
|
|
return "", "", errors.New("you need at least 1 command")
|
|
|
|
}
|
|
|
|
var cmdArgs []string
|
|
|
|
if len(cmd) > 1 {
|
|
|
|
cmdArgs = append(cmdArgs, cmd[1:])
|
|
|
|
}
|
|
|
|
outStr, errStr, err := utils.Command(cmdApp[0], cmdArgs...)
|
2018-11-07 07:53:39 +00:00
|
|
|
return outStr, errStr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnSuccess for commandLine will trigger successful service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (c *commandLine) OnSuccess(s services.Service) (string, error) {
|
2020-07-22 19:07:42 +00:00
|
|
|
tmpl := ReplaceVars(c.SuccessData.String, s, failures.Failure{})
|
2020-07-02 18:41:46 +00:00
|
|
|
out, _, err := runCommand(tmpl)
|
2020-06-15 07:46:27 +00:00
|
|
|
return out, err
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
|
|
|
|
2020-04-11 05:59:51 +00:00
|
|
|
// OnFailure for commandLine will trigger failing service
|
2020-06-26 04:08:12 +00:00
|
|
|
func (c *commandLine) OnFailure(s services.Service, f failures.Failure) (string, error) {
|
2020-07-22 19:07:42 +00:00
|
|
|
tmpl := ReplaceVars(c.FailureData.String, s, f)
|
2020-07-02 18:41:46 +00:00
|
|
|
out, _, err := runCommand(tmpl)
|
|
|
|
return out, err
|
2020-04-11 05:59:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
// OnTest for commandLine triggers when this notifier has been saved
|
2020-04-11 05:59:51 +00:00
|
|
|
func (c *commandLine) OnTest() (string, error) {
|
2020-07-22 19:07:42 +00:00
|
|
|
tmpl := ReplaceVars(c.Var1.String, services.Example(true), failures.Example())
|
2020-07-02 18:41:46 +00:00
|
|
|
in, out, err := runCommand(tmpl)
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Infoln(in)
|
|
|
|
utils.Log.Infoln(out)
|
2020-04-11 05:59:51 +00:00
|
|
|
return out, err
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
2020-06-21 06:52:07 +00:00
|
|
|
|
|
|
|
// OnSave will trigger when this notifier is saved
|
|
|
|
func (c *commandLine) OnSave() (string, error) {
|
|
|
|
return "", nil
|
|
|
|
}
|