Skip to content

Commit

Permalink
feat: support plugin mode output check for probe (#172)
Browse files Browse the repository at this point in the history
  • Loading branch information
samanhappy committed Jul 21, 2022
1 parent e7f7521 commit 281b61d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ all: ${TARGET}
${TARGET}: ${SOURCE}
mkdir -p ${RELEASE_DIR}
go mod tidy
CGO_ENABLED=0 go build -a -ldflags '-s -w -extldflags "-static"' -gcflags=-G=3 -o ${TARGET} ${MKFILE_DIR}cmd/easeprobe
CGO_ENABLED=1 go build -a -ldflags '-s -w' -gcflags=-G=3 -o ${TARGET} ${MKFILE_DIR}cmd/easeprobe

build: all

Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,9 @@ http:
contain: "success" # response body must contain this string, if not the probe is considered failed.
not_contain: "failure" # response body must NOT contain this string, if it does the probe is considered failed.
regex: false # if true, the contain and not_contain will be treated as regular expression. default: false
plugin: false # if true, the plugin_path and plugin_func will be used to do arbitrary probe. default: false
plugin_path: "plugin.so" # the plugin file used for plugin mode
plugin_func: "Check" # the plugin func used for plugin mode
# configuration
timeout: 10s # default is 30 seconds
Expand All @@ -584,6 +587,24 @@ http:
>
> The Regular Expression supported refer to https://github.com/google/re2/wiki/Syntax

> **Note**:
>
> The Plugin Mode supported refer to https://pkg.go.dev/plugin
>
> A similar check plugin implement maybe like this
> ```CODE
> package main
>
> // the Input Name is defined, cannot change!
> var Input string
>
> func Check() (err error) {
>
> // TODO check logic
>
> return nil
> }
> ```

### 3.2 TCP Probe Configuration

Expand Down
39 changes: 38 additions & 1 deletion probe/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package probe

import (
"fmt"
"plugin"
"regexp"
"strings"
)
Expand All @@ -28,14 +29,17 @@ type TextChecker struct {
Contain string `yaml:"contain,omitempty"`
NotContain string `yaml:"not_contain,omitempty"`
RegExp bool `yaml:"regex,omitempty"`
Plugin bool `yaml:"plugin,omitempty"`
PluginPath string `yaml:"plugin_path,omitempty"`
PluginFunc string `yaml:"plugin_func,omitempty"`

containReg *regexp.Regexp `yaml:"-"`
notContainReg *regexp.Regexp `yaml:"-"`
}

// Config the text checker initialize the regexp
func (tc *TextChecker) Config() (err error) {
if !tc.RegExp {
if !tc.RegExp && !tc.Plugin {
return nil
}

Expand All @@ -61,13 +65,19 @@ func (tc *TextChecker) Check(Text string) error {
if tc.RegExp {
return tc.CheckRegExp(Text)
}
if tc.Plugin {
return tc.CheckPlugin(Text)
}
return tc.CheckText(Text)
}

func (tc *TextChecker) String() string {
if tc.RegExp {
return fmt.Sprintf("RegExp Mode - Contain:[%s], NotContain:[%s]", tc.Contain, tc.NotContain)
}
if tc.Plugin {
return fmt.Sprintf("Plugin Mode - PluginPath:[%s], PluginFunc:[%s]", tc.PluginPath, tc.PluginFunc)
}
return fmt.Sprintf("Text Mode - Contain:[%s], NotContain:[%s]", tc.Contain, tc.NotContain)
}

Expand Down Expand Up @@ -101,6 +111,33 @@ func (tc *TextChecker) CheckRegExp(Output string) error {
return nil
}

// CheckPlugin checks the output text using plugin defined by user
func (tc *TextChecker) CheckPlugin(Output string) error {

p, err := plugin.Open(tc.PluginPath)
if err != nil {
return fmt.Errorf("failed to open plugin: %v", err)
}

input, err := p.Lookup("Input")
if err != nil {
return fmt.Errorf("failed to lookup value: %v", err)
}

f, err := p.Lookup(tc.PluginFunc)
if err != nil {
return fmt.Errorf("failed to lookup func: %v", err)
}

*input.(*string) = Output
err = f.(func() error)()
if err != nil {
return fmt.Errorf("the output does not match the plugin func [%s]", err)
}

return nil
}

// CheckEmpty return "empty" if the string is empty
func CheckEmpty(s string) string {
if len(strings.TrimSpace(s)) <= 0 {
Expand Down
26 changes: 26 additions & 0 deletions probe/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,29 @@ func TestCheckEmpty(t *testing.T) {
assert.Equal(t, "empty", CheckEmpty("\n\r\t"))
assert.Equal(t, "empty", CheckEmpty(" \n\r\t "))
}

func TestCheckPlugin(t *testing.T) {
checker := TextChecker{
Plugin: true,
PluginPath: "xxx.so",
PluginFunc: "Check",
}
checker.Config()
assert.NotNil(t, checker.Check("hello world"))

checker = TextChecker{
Plugin: true,
PluginPath: "plugin.so",
PluginFunc: "xxx",
}
checker.Config()
assert.NotNil(t, checker.Check("hello world"))

checker = TextChecker{
Plugin: true,
PluginPath: "easeprobe-plugin.so",
PluginFunc: "Check",
}
checker.Config()
assert.NotNil(t, checker.Check("hello world"))
}

0 comments on commit 281b61d

Please sign in to comment.