Skip to content

Commit

Permalink
feat: enhance rules to UNKNOWN status
Browse files Browse the repository at this point in the history
  • Loading branch information
macrat committed Apr 11, 2021
1 parent f65ce88 commit 70694bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
5 changes: 5 additions & 0 deletions probe/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package probe

import (
"context"
"errors"
"fmt"
"net/url"
"os"
Expand Down Expand Up @@ -64,6 +65,10 @@ func (p ExecuteProbe) Check() store.Record {

if err != nil {
status = store.STATUS_FAIL
if e := errors.Unwrap(err); e != nil && e.Error() == "no such file or directory" || e.Error() == "executable file not found in $PATH" {
status = store.STATUS_UNKNOWN
}

if message == "" {
message = err.Error()
}
Expand Down
5 changes: 4 additions & 1 deletion probe/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package probe

import (
"errors"
"net"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -82,7 +83,9 @@ func (p HTTPProbe) Check() store.Record {
message := ""
if err != nil {
message = err.Error()
status = store.STATUS_UNKNOWN
if e, ok := errors.Unwrap(errors.Unwrap(err)).(*net.DNSError); ok && e.IsNotFound {
status = store.STATUS_UNKNOWN
}
} else {
message = resp.Status
if 200 <= resp.StatusCode && resp.StatusCode <= 299 {
Expand Down
14 changes: 9 additions & 5 deletions probe/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package probe

import (
"fmt"
"net"
"net/url"
"time"

Expand All @@ -28,10 +29,16 @@ func (p PingProbe) Target() *url.URL {
func (p PingProbe) Check() store.Record {
pinger, err := ping.NewPinger(p.target.Opaque)
if err != nil {
status := store.STATUS_FAIL

if e, ok := err.(*net.DNSError); ok && e.IsNotFound {
status = store.STATUS_UNKNOWN
}

return store.Record{
CheckedAt: time.Now(),
Target: p.target,
Status: store.STATUS_FAIL,
Status: status,
Message: err.Error(),
}
}
Expand All @@ -43,10 +50,7 @@ func (p PingProbe) Check() store.Record {

startTime := time.Now()

err = pinger.Run()
if err != nil {
fmt.Println(err)
}
pinger.Run()

stat := pinger.Statistics()

Expand Down
7 changes: 7 additions & 0 deletions probe/tcp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package probe

import (
"errors"
"fmt"
"net"
"net/url"
Expand Down Expand Up @@ -39,6 +40,12 @@ func (p TCPProbe) Check() store.Record {
if err != nil {
r.Status = store.STATUS_FAIL
r.Message = err.Error()
if _, ok := errors.Unwrap(err).(*net.AddrError); ok {
r.Status = store.STATUS_UNKNOWN
}
if e, ok := errors.Unwrap(err).(*net.DNSError); ok && e.IsNotFound {
r.Status = store.STATUS_UNKNOWN
}
} else {
r.Status = store.STATUS_OK
r.Message = fmt.Sprintf("%s -> %s", conn.LocalAddr(), conn.RemoteAddr())
Expand Down

0 comments on commit 70694bd

Please sign in to comment.