Skip to content

Commit

Permalink
feat(Probe): Probe Abstraction
Browse files Browse the repository at this point in the history
  • Loading branch information
haoel committed Apr 9, 2022
1 parent d73bc7d commit e3826d5
Show file tree
Hide file tree
Showing 19 changed files with 273 additions and 203 deletions.
66 changes: 57 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ EaseProbe is a simple, standalone, and lightWeight tool that can do health/statu
- [3.1 HTTP Probe Configuration](#31-http-probe-configuration)
- [3.2 TCP Probe Configuration](#32-tcp-probe-configuration)
- [3.3 Shell Command Probe Configuration](#33-shell-command-probe-configuration)
- [3.4 Native Client Probe](#34-native-client-probe)
- [3.5 Notification Configuration](#35-notification-configuration)
- [3.6 Global Setting Configuration](#36-global-setting-configuration)
- [3.4 SSH Command Probe Configuration](#34-ssh-command-probe-configuration)
- [3.5 Native Client Probe](#35-native-client-probe)
- [3.6 Notification Configuration](#36-notification-configuration)
- [3.7 Global Setting Configuration](#37-global-setting-configuration)
- [4. Community](#4-community)
- [5. License](#5-license)

Expand All @@ -39,7 +40,7 @@ Ease Probe supports the following probing methods:
url: http:https://prometheus:9090/graph
```

- **TCP**. Just simply check the TCP connection can be established or not. ( [TCP Probe Configuration](#32-tcp-probe-configuration) )
- **TCP**. Just simply check whether the TCP connection can be established or not. ( [TCP Probe Configuration](#32-tcp-probe-configuration) )

```YAML
tcp:
Expand All @@ -65,7 +66,21 @@ Ease Probe supports the following probing methods:
contain : "PONG"
```

- **Client**. Currently, support the following native client. Support the mTLS. ( [Native Client Probe](#34-native-client-probe) )
- **SSH**. Run a remote command via SSH and check the result. ([SSH Command Probe Configuration](#34-ssh-command-probe-configuration))

```YAML
ssh:
- name : CubieBoard
host: 192.168.10.10:22
username: root
password: xxxxxxx
key: /Users/user/.ssh/id_rsa
cmd: "ps auxwe | grep easeprobe | grep -v grep"
contain: easeprobe
```


- **Client**. Currently, support the following native client. Support the mTLS. ( [Native Client Probe](#35-native-client-probe) )
- **MySQL**. Connect to the MySQL server and run the `SHOW STATUS` SQL.
- **Redis**. Connect to the Redis server and run the `PING` command.
- **MongoDB**. Connect to MongoDB server and just ping server.
Expand Down Expand Up @@ -137,7 +152,7 @@ notify:
webhook: "https://oapi.dingtalk.com/robot/send?access_token=xxxx"
```

Check the [Notification Configuration](#35-notification-configuration) to see how to configure it.
Check the [Notification Configuration](#36-notification-configuration) to see how to configure it.

### 1.3 Report

Expand All @@ -153,6 +168,7 @@ settings:
time: "23:59"
```

for more information, please check the [3.7 Global Setting Configuration](#37-global-setting-configuration)

## 2. Getting Start

Expand Down Expand Up @@ -246,6 +262,10 @@ tcp:

### 3.3 Shell Command Probe Configuration

The shell command probe is used to execute a shell command and check the output.

The following example shows how to configure the shell command probe.

```YAML
# Shell Probe Configuration
shell:
Expand Down Expand Up @@ -278,7 +298,35 @@ shell:
contain: "Mode:"
```

### 3.4 Native Client Probe
### 3.4 SSH Command Probe Configuration

SSH probe is similar with Shell probe, and it supports password and private key authentication.

The following are example of SSH probe configuration.

```YAML
# SSH Probe Configuration
ssh:
# run redis-cli ping and check the "PONG"
- name: Redis (Remote)
username: ubuntu # SSH Login username
password: xxxxx # SSH Login password
key: /path/to/private.key # SSH login private file
cmd: "redis-cli"
args:
- "-h"
- "127.0.0.1"
- "ping"
env:
# set the `REDISCLI_AUTH` environment variable for redis password
- "REDISCLI_AUTH=abc123"
# check the command output, if does not contain the PONG, mark the status down
contain : "PONG"
```



### 3.5 Native Client Probe

```YAML
# Native Client Probe
Expand Down Expand Up @@ -330,7 +378,7 @@ client:
```


### 3.5 Notification Configuration
### 3.6 Notification Configuration

```YAML
# Notification Configuration
Expand Down Expand Up @@ -401,7 +449,7 @@ notify:
```


### 3.6 Global Setting Configuration
### 3.7 Global Setting Configuration

```YAML
# Global settings for all probes and notifiers.
Expand Down
2 changes: 2 additions & 0 deletions conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/megaease/easeprobe/probe/client"
"github.com/megaease/easeprobe/probe/http"
"github.com/megaease/easeprobe/probe/shell"
"github.com/megaease/easeprobe/probe/ssh"
"github.com/megaease/easeprobe/probe/tcp"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -139,6 +140,7 @@ type Conf struct {
HTTP []http.HTTP `yaml:"http"`
TCP []tcp.TCP `yaml:"tcp"`
Shell []shell.Shell `yaml:"shell"`
SSH []ssh.SSH `yaml:"ssh"`
Client []client.Client `yaml:"client"`
Notify notify.Config `yaml:"notify"`
Settings Settings `yaml:"settings"`
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/sirupsen/logrus v1.8.1
github.com/uptrace/bun/driver/pgdriver v1.1.2
go.mongodb.org/mongo-driver v1.8.4
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb
gopkg.in/yaml.v2 v2.4.0
)
Expand All @@ -38,7 +39,6 @@ require (
github.com/xdg-go/scram v1.0.2 // indirect
github.com/xdg-go/stringprep v1.0.2 // indirect
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 // indirect
golang.org/x/text v0.3.7 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8 h1:OH54vjqzRWmbJ62fjuhxy7AxFFgoHN0/DPc/UrL8cAs=
golang.org/x/sys v0.0.0-20220319134239-a9b59b0215f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 h1:JGgROgKl9N8DuW20oFS5gxc+lE67/N3FcwmBPMe7ArY=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
99 changes: 85 additions & 14 deletions probe/base/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@
package base

import (
"fmt"
"time"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"

log "github.com/sirupsen/logrus"
)

// ProbeFuncType is the probe function type
type ProbeFuncType func() (bool, string)

// DefaultOptions is the default options for all probe
type DefaultOptions struct {
ProbeKind string `yaml:"kind"`
ProbeKind string `yaml:"-"`
ProbeTag string `yaml:"-"`
ProbeName string `yaml:"name"`
ProbeTimeout time.Duration `yaml:"timeout,omitempty"`
ProbeTimeInterval time.Duration `yaml:"interval,omitempty"`
ProbeFunc ProbeFuncType `yaml:"-"`
ProbeResult *probe.Result `yaml:"-"`
}

// Config default config
func (d *DefaultOptions) Config(gConf global.ProbeSettings, name, endpoint string) error {
d.ProbeTimeout = gConf.NormalizeTimeOut(d.ProbeTimeout)
d.ProbeTimeInterval = gConf.NormalizeInterval(d.ProbeTimeInterval)

d.ProbeResult = probe.NewResult()
d.ProbeResult.Name = name
d.ProbeResult.Endpoint = endpoint
d.ProbeResult.PreStatus = probe.StatusInit
d.ProbeResult.TimeFormat = gConf.TimeFormat
return nil
}

// Kind return the probe kind
func (d *DefaultOptions) Kind() string {
return d.ProbeKind
}

// Name return the probe name
func (d *DefaultOptions) Name() string {
return d.ProbeName
}

// Timeout get the probe timeout
func (d *DefaultOptions) Timeout() time.Duration {
return d.ProbeTimeout
Expand All @@ -64,3 +65,73 @@ func (d *DefaultOptions) Interval() time.Duration {
func (d *DefaultOptions) Result() *probe.Result {
return d.ProbeResult
}

// Config default config
func (d *DefaultOptions) Config(gConf global.ProbeSettings,
kind, tag, name, endpoint string, fn ProbeFuncType) error {

d.ProbeKind = kind
d.ProbeName = name
d.ProbeTag = tag
d.ProbeFunc = fn

d.ProbeTimeout = gConf.NormalizeTimeOut(d.ProbeTimeout)
d.ProbeTimeInterval = gConf.NormalizeInterval(d.ProbeTimeInterval)

d.ProbeResult = probe.NewResult()
d.ProbeResult.Name = name
d.ProbeResult.Endpoint = endpoint
d.ProbeResult.PreStatus = probe.StatusInit
d.ProbeResult.TimeFormat = gConf.TimeFormat

if len(d.ProbeTag) > 0 {
log.Infof("Probe [%s / %s] - [%s] is configured!", d.ProbeKind, d.ProbeTag, d.ProbeName)
} else {
log.Infof("Probe [%s] - [%s] is configured!", d.ProbeKind, d.ProbeName)
}
return nil
}

// Probe return the checking result
func (d *DefaultOptions) Probe() probe.Result {
if d.ProbeFunc == nil {
return *d.ProbeResult
}

now := time.Now()
d.ProbeResult.StartTime = now
d.ProbeResult.StartTimestamp = now.UnixMilli()

stat, msg := d.ProbeFunc()

d.ProbeResult.RoundTripTime.Duration = time.Since(now)

status := probe.StatusUp
if len(d.ProbeTag) > 0 {
d.ProbeResult.Message = fmt.Sprintf("%s / %s checked up successfully!", d.ProbeKind, d.ProbeTag)
} else {
d.ProbeResult.Message = fmt.Sprintf("%s checked up successfully!", d.ProbeKind)
}

if stat != true {
d.ProbeResult.Message = fmt.Sprintf("Error (%s): %s", d.ProbeKind, msg)
if len(d.ProbeTag) > 0 {
log.Errorf("[%s / %s / %s] - %s", d.ProbeKind, d.ProbeTag, d.ProbeName, msg)
} else {
log.Errorf("[%s / %s] - %s", d.ProbeKind, d.ProbeName, msg)
}
status = probe.StatusDown
} else {
if len(d.ProbeTag) > 0 {
log.Debugf("[%s / %s / %s] - %s", d.ProbeKind, d.ProbeTag, d.ProbeName, msg)
} else {
log.Debugf("[%s / %s] - %s", d.ProbeKind, d.ProbeName, msg)
}
}

d.ProbeResult.PreStatus = d.ProbeResult.Status
d.ProbeResult.Status = status

d.ProbeResult.DoStat(d.Interval())
return *d.ProbeResult
}
43 changes: 9 additions & 34 deletions probe/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@
package client

import (
"fmt"
"time"

"github.com/megaease/easeprobe/global"
"github.com/megaease/easeprobe/probe"
"github.com/megaease/easeprobe/probe/client/conf"
Expand All @@ -43,11 +40,13 @@ type Client struct {

// Config Client Config Object
func (c *Client) Config(gConf global.ProbeSettings) error {
c.ProbeKind = "client"
c.DefaultOptions.Config(gConf, c.Name, c.Host)
kind := "client"
tag := c.DriverType.String()
name := c.ProbeName
c.DefaultOptions.Config(gConf, kind, tag, name, c.Host, c.DoProbe)
c.configClientDriver()

log.Debugf("[%s] configuration: %+v, %+v", c.Kind(), c, c.Result())
log.Debugf("[%s] configuration: %+v, %+v", c.ProbeKind, c, c.Result())
return nil
}

Expand All @@ -71,36 +70,12 @@ func (c *Client) configClientDriver() {

}

// Probe return the checking result
func (c *Client) Probe() probe.Result {
// DoProbe return the checking result
func (c *Client) DoProbe() (bool, string) {
if c.DriverType == conf.Unknown {
c.ProbeResult.PreStatus = probe.StatusUnknown
c.ProbeResult.Status = probe.StatusUnknown
return *c.ProbeResult
return false, "Wrong Driver Type"
}

now := time.Now()
c.ProbeResult.StartTime = now
c.ProbeResult.StartTimestamp = now.UnixMilli()

stat, msg := c.client.Probe()

c.ProbeResult.RoundTripTime.Duration = time.Since(now)

status := probe.StatusUp
c.ProbeResult.Message = fmt.Sprintf("%s client checked up successfully!", c.DriverType.String())

if stat != true {
c.ProbeResult.Message = fmt.Sprintf("Error (%s): %s", c.DriverType.String(), msg)
log.Errorf("[%s / %s / %s] - %s", c.Kind(), c.client.Kind(), c.Name, msg)
status = probe.StatusDown
} else {
log.Debugf("[%s / %s / %s] - %s", c.Kind(), c.client.Kind(), c.Name, msg)
}

c.ProbeResult.PreStatus = c.ProbeResult.Status
c.ProbeResult.Status = status

c.ProbeResult.DoStat(c.Interval())
return *c.ProbeResult
return c.client.Probe()
}
5 changes: 2 additions & 3 deletions probe/client/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,15 @@ var DriverMap = map[DriverType]string{

// Options implements the configuration for native client
type Options struct {
Name string `yaml:"name"`
base.DefaultOptions `yaml:",inline"`

Host string `yaml:"host"`
DriverType DriverType `yaml:"driver"`
Username string `yaml:"username"`
Password string `yaml:"password"`

//TLS
global.TLS `yaml:",inline"`

base.DefaultOptions `yaml:",inline"`
}

// DriverTypeMap is the map of driver [name, driver]
Expand Down
Loading

0 comments on commit e3826d5

Please sign in to comment.