2018-12-04 05:57:11 +00:00
|
|
|
// Statping
|
2018-11-07 07:53:39 +00:00
|
|
|
// Copyright (C) 2018. Hunter Long and the project contributors
|
|
|
|
// Written by Hunter Long <info@socialeck.com> and the project contributors
|
|
|
|
//
|
2018-12-04 04:17:29 +00:00
|
|
|
// https://github.com/hunterlong/statping
|
2018-11-07 07:53:39 +00:00
|
|
|
//
|
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package notifiers
|
|
|
|
|
|
|
|
import (
|
2019-03-04 17:18:50 +00:00
|
|
|
"fmt"
|
2020-03-04 10:29:00 +00:00
|
|
|
"github.com/hunterlong/statping/types/failures"
|
|
|
|
"github.com/hunterlong/statping/types/notifications"
|
|
|
|
"github.com/hunterlong/statping/types/services"
|
2018-12-04 04:17:29 +00:00
|
|
|
"github.com/hunterlong/statping/utils"
|
2020-03-02 07:53:46 +00:00
|
|
|
"strings"
|
2018-11-07 07:53:39 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-03-09 15:15:15 +00:00
|
|
|
var _ notifications.Notifier = (*commandLine)(nil)
|
|
|
|
|
2018-11-07 07:53:39 +00:00
|
|
|
type commandLine struct {
|
2020-03-04 10:29:00 +00:00
|
|
|
*notifications.Notification
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
var Command = &commandLine{¬ifications.Notification{
|
2020-03-09 15:15:15 +00:00
|
|
|
Method: "command",
|
2018-11-07 07:53:39 +00:00
|
|
|
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.",
|
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",
|
2018-12-10 05:43:15 +00:00
|
|
|
Host: "/bin/bash",
|
2020-03-04 10:29:00 +00:00
|
|
|
Form: []notifications.NotificationForm{{
|
2018-11-07 07:53:39 +00:00
|
|
|
Type: "text",
|
|
|
|
Title: "Shell or Bash",
|
2018-12-10 05:43:15 +00:00
|
|
|
Placeholder: "/bin/bash",
|
2018-11-07 07:53:39 +00:00
|
|
|
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.",
|
2018-11-07 07:53:39 +00:00
|
|
|
}, {
|
|
|
|
Type: "text",
|
|
|
|
Title: "Command to Run on OnSuccess",
|
2018-12-10 05:43:15 +00:00
|
|
|
Placeholder: "curl google.com",
|
2018-11-07 07:53:39 +00:00
|
|
|
DbField: "var1",
|
2019-10-25 15:13:32 +00:00
|
|
|
SmallText: "This Command will run every time a service is receiving a Successful event.",
|
2018-11-07 07:53:39 +00:00
|
|
|
}, {
|
|
|
|
Type: "text",
|
|
|
|
Title: "Command to Run on OnFailure",
|
2018-12-10 05:43:15 +00:00
|
|
|
Placeholder: "curl offline.com",
|
2018-11-07 07:53:39 +00:00
|
|
|
DbField: "var2",
|
2019-10-25 15:13:32 +00:00
|
|
|
SmallText: "This Command will run every time a service is receiving a Failing event.",
|
2018-11-07 07:53:39 +00:00
|
|
|
}}},
|
|
|
|
}
|
|
|
|
|
2020-03-02 07:53:46 +00:00
|
|
|
func runCommand(app string, cmd ...string) (string, string, error) {
|
|
|
|
outStr, errStr, err := utils.Command(app, cmd...)
|
2018-11-07 07:53:39 +00:00
|
|
|
return outStr, errStr, err
|
|
|
|
}
|
|
|
|
|
2020-03-04 10:29:00 +00:00
|
|
|
func (u *commandLine) Select() *notifications.Notification {
|
2018-11-07 07:53:39 +00:00
|
|
|
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)
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnSuccess for commandLine will trigger successful service
|
2020-03-04 10:29:00 +00:00
|
|
|
func (u *commandLine) OnSuccess(s *services.Service) {
|
2019-08-15 23:26:36 +00:00
|
|
|
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)
|
2018-11-07 07:53:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-11-07 07:53:39 +00:00
|
|
|
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...)
|
2019-12-28 09:01:07 +00:00
|
|
|
utils.Log.Infoln(in)
|
|
|
|
utils.Log.Infoln(out)
|
2018-11-07 07:53:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-25 15:13:32 +00:00
|
|
|
// Send for commandLine will send message to expo Command push notifications endpoint
|
2018-11-07 07:53:39 +00:00
|
|
|
func (u *commandLine) Send(msg interface{}) error {
|
|
|
|
cmd := msg.(string)
|
|
|
|
_, _, err := runCommand(u.Host, cmd)
|
|
|
|
return err
|
|
|
|
}
|