Skip to content

Commit

Permalink
Support to Check the HTTP Response Text (megaease#80)
Browse files Browse the repository at this point in the history
* Support to Check the HTTP Response Text

* Update README.md

Co-authored-by: Pantelis Roditis <[email protected]>

* Update README.md

Co-authored-by: Pantelis Roditis <[email protected]>

* Support the customized name and icon

* forget commit a file

* add the license

* slightly adjust the code

Co-authored-by: Pantelis Roditis <[email protected]>
  • Loading branch information
haoel and proditis committed May 14, 2022
1 parent edf3a07 commit 108daeb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,9 @@ http:
success_code:
- [200,206] # the code >=200 and <= 206
- [300,308] # the code >=300 and <= 308
# Response Checking
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.
# configuration
timeout: 10s # default is 30 seconds

Expand Down
4 changes: 1 addition & 3 deletions probe/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ func CommandLine(cmd string, args []string) string {
// CheckOutput checks the output text,
// - if it contains a configured string then return nil
// - if it does not contain a configured string then return nil
func CheckOutput(Contain, NotContain string, Output string) error {
func CheckOutput(Contain, NotContain, Output string) error {

if len(Contain) > 0 && !strings.Contains(Output, Contain) {

return fmt.Errorf("the output does not contain [%s]", Contain)
}

if len(NotContain) > 0 && strings.Contains(Output, NotContain) {
return fmt.Errorf("the output contains [%s]", NotContain)

}
return nil
}
Expand Down
12 changes: 11 additions & 1 deletion probe/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"strings"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
"github.com/megaease/easeprobe/probe/base"
log "github.com/sirupsen/logrus"
)
Expand All @@ -38,6 +39,8 @@ type HTTP struct {
Method string `yaml:"method,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
Body string `yaml:"body,omitempty"`
Contain string `yaml:"contain,omitempty"`
NotContain string `yaml:"not_contain,omitempty"`

// Option - HTTP Basic Auth Credentials
User string `yaml:"username,omitempty"`
Expand Down Expand Up @@ -152,5 +155,12 @@ func (h *HTTP) DoProbe() (bool, string) {
return false, fmt.Sprintf("HTTP Status Code is %d. It missed in %v", resp.StatusCode, h.SuccessCode)
}

return true, fmt.Sprintf("HTTP Status Code is %d", resp.StatusCode)
message := fmt.Sprintf("HTTP Status Code is %d", resp.StatusCode)
if err := probe.CheckOutput(h.Contain, h.NotContain, string(response)); err != nil {
log.Errorf("[%s / %s] - %v", h.ProbeKind, h.ProbeName, err)
message += fmt.Sprintf(". Error: %v", err)
return false, message
}

return true, message
}

0 comments on commit 108daeb

Please sign in to comment.