Browse Source

fix execution of notify script (#2769)

main
ulricqin GitHub 1 year ago
parent
commit
ca712f62a4
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
4 changed files with 90 additions and 13 deletions
  1. +3
    -13
      models/notify_channel.go
  2. +37
    -0
      pkg/cmdx/cmd_notwindows.go
  3. +35
    -0
      pkg/cmdx/cmd_windows.go
  4. +15
    -0
      pkg/cmdx/cmdx.go

+ 3
- 13
models/notify_channel.go View File

@@ -21,10 +21,10 @@ import (
"sort"
"strconv"
"strings"
"syscall"
"time"
"unicode/utf8"

"github.com/ccfos/nightingale/v6/pkg/cmdx"
"github.com/ccfos/nightingale/v6/pkg/ctx"
"github.com/ccfos/nightingale/v6/pkg/poster"
"github.com/ccfos/nightingale/v6/pkg/tplx"
@@ -33,7 +33,6 @@ import (
"github.com/pkg/errors"
"github.com/toolkits/pkg/file"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/sys"
"gopkg.in/gomail.v2"
)

@@ -196,10 +195,8 @@ func (ncc *NotifyChannelConfig) SendScript(events []*AlertCurEvent, tpl map[stri
cmd.Stdout = &buf
cmd.Stderr = &buf

err := startCmd(cmd)
if err != nil {
return "", "", fmt.Errorf("failed to start script: %v", err)
}
err, isTimeout := cmdx.RunTimeout(cmd, time.Duration(config.Timeout)*time.Millisecond)
logger.Infof("event_script_notify_result: exec %s output: %s isTimeout: %v err: %v", fpath, buf.String(), isTimeout, err)

res := buf.String()

@@ -218,8 +215,6 @@ func (ncc *NotifyChannelConfig) SendScript(events []*AlertCurEvent, tpl map[stri
res = res[:validLen] + "..."
}

err, isTimeout := sys.WrapTimeout(cmd, time.Duration(config.Timeout)*time.Second)
logger.Infof("event_script_notify_result: exec %s output: %s isTimeout: %v err: %v", fpath, buf.String(), isTimeout, err)
if isTimeout {
if err == nil {
return cmd.String(), res, errors.New("timeout and killed process")
@@ -257,11 +252,6 @@ func getStdinBytes(events []*AlertCurEvent, tpl map[string]interface{}, params m
return jsonBytes
}

func startCmd(c *exec.Cmd) error {
c.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return c.Start()
}

func NotifyChannelStatistics(ctx *ctx.Context) (*Statistics, error) {
if !ctx.IsCenter {
s, err := poster.GetByUrls[*Statistics](ctx, "/v1/n9e/statistic?name=notify_channel")


+ 37
- 0
pkg/cmdx/cmd_notwindows.go View File

@@ -0,0 +1,37 @@
//go:build !windows
// +build !windows

package cmdx

import (
"os/exec"
"syscall"
"time"
)

func CmdWait(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
var err error

done := make(chan error)
go func() {
done <- cmd.Wait()
}()

select {
case <-time.After(timeout):
go func() {
<-done // allow goroutine to exit
}()

// IMPORTANT: cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} is necessary before cmd.Start()
err = syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
return err, true
case err = <-done:
return err, false
}
}

func CmdStart(cmd *exec.Cmd) error {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
return cmd.Start()
}

+ 35
- 0
pkg/cmdx/cmd_windows.go View File

@@ -0,0 +1,35 @@
//go:build windows
// +build windows

package cmdx

import (
"os/exec"
"syscall"
"time"
)

func CmdWait(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
var err error

done := make(chan error)
go func() {
done <- cmd.Wait()
}()

select {
case <-time.After(timeout):
go func() {
<-done // allow goroutine to exit
}()

err = cmd.Process.Signal(syscall.SIGKILL)
return err, true
case err = <-done:
return err, false
}
}

func CmdStart(cmd *exec.Cmd) error {
return cmd.Start()
}

+ 15
- 0
pkg/cmdx/cmdx.go View File

@@ -0,0 +1,15 @@
package cmdx

import (
"os/exec"
"time"
)

func RunTimeout(cmd *exec.Cmd, timeout time.Duration) (error, bool) {
err := CmdStart(cmd)
if err != nil {
return err, false
}

return CmdWait(cmd, timeout)
}

Loading…
Cancel
Save