statping/notifiers/command.go

107 lines
3.3 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2020-03-09 18:17:55 +00:00
// https://github.com/statping/statping
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2020-03-12 08:09:25 +00:00
package notifiers
import (
2019-03-04 17:18:50 +00:00
"fmt"
2020-03-09 18:17:55 +00:00
"github.com/statping/statping/types/failures"
"github.com/statping/statping/types/services"
"github.com/statping/statping/utils"
2020-03-02 07:53:46 +00:00
"strings"
"time"
)
2020-03-12 08:09:25 +00:00
var _ Notifier = (*commandLine)(nil)
2020-03-09 15:15:15 +00:00
type commandLine struct {
2020-03-12 08:09:25 +00:00
*Notification
}
2020-03-12 08:09:25 +00:00
var Command = &commandLine{&Notification{
2020-03-09 15:15:15 +00:00
Method: "command",
Title: "Shell 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.",
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",
2018-12-10 05:43:15 +00:00
Host: "/bin/bash",
2020-03-12 08:09:25 +00:00
Form: []NotificationForm{{
Type: "text",
Title: "Shell or Bash",
2018-12-10 05:43:15 +00:00
Placeholder: "/bin/bash",
DbField: "host",
2019-01-17 20:56:09 +00:00
SmallText: "You can use '/bin/sh', '/bin/bash' or even an absolute path for an application.",
}, {
Type: "text",
Title: "Command to Run on OnSuccess",
2018-12-10 05:43:15 +00:00
Placeholder: "curl google.com",
DbField: "var1",
2019-10-25 15:13:32 +00:00
SmallText: "This Command will run every time a service is receiving a Successful event.",
}, {
Type: "text",
Title: "Command to Run on OnFailure",
2018-12-10 05:43:15 +00:00
Placeholder: "curl offline.com",
DbField: "var2",
2019-10-25 15:13:32 +00:00
SmallText: "This Command will run every time a service is receiving a Failing event.",
}}},
}
2020-03-02 07:53:46 +00:00
func runCommand(app string, cmd ...string) (string, string, error) {
outStr, errStr, err := utils.Command(app, cmd...)
return outStr, errStr, err
}
2020-03-12 08:09:25 +00:00
func (u *commandLine) Select() *Notification {
return u.Notification
}
// OnFailure for commandLine will trigger failing service
2020-03-04 10:29:00 +00:00
func (u *commandLine) OnFailure(s *services.Service, f *failures.Failure) {
2019-03-04 17:18:50 +00:00
u.AddQueue(fmt.Sprintf("service_%v", s.Id), u.Var2)
}
// OnSuccess for commandLine will trigger successful service
2020-03-04 10:29:00 +00:00
func (u *commandLine) OnSuccess(s *services.Service) {
if !s.Online {
2019-03-04 17:18:50 +00:00
u.ResetUniqueQueue(fmt.Sprintf("service_%v", s.Id))
u.AddQueue(fmt.Sprintf("service_%v", s.Id), u.Var1)
}
}
// OnSave for commandLine triggers when this notifier has been saved
func (u *commandLine) OnSave() error {
2019-03-04 17:18:50 +00:00
u.AddQueue("saved", u.Var1)
u.AddQueue("saved", u.Var2)
return nil
}
// OnTest for commandLine triggers when this notifier has been saved
func (u *commandLine) OnTest() error {
2020-03-02 07:53:46 +00:00
cmds := strings.Split(u.Var1, " ")
in, out, err := runCommand(u.Host, cmds...)
utils.Log.Infoln(in)
utils.Log.Infoln(out)
return err
}
2019-10-25 15:13:32 +00:00
// Send for commandLine will send message to expo Command push notifications endpoint
func (u *commandLine) Send(msg interface{}) error {
cmd := msg.(string)
_, _, err := runCommand(u.Host, cmd)
return err
}