Skip to content

Commit

Permalink
Merge pull request #280 from mackerelio/check-ping
Browse files Browse the repository at this point in the history
Add check-ping
  • Loading branch information
a-know authored Mar 26, 2019
2 parents ece83f2 + d094c2f commit ddd2737
Show file tree
Hide file tree
Showing 7 changed files with 181 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Documentation for each plugin is located in its respective sub directory.
* [check-mysql](./check-mysql/README.md)
* [check-ntpoffset](./check-ntpoffset/README.md)
* [check-ntservice](./check-ntservice/README.md)
* [check-ping](./check-ping/README.md)
* [check-postgresql](./check-postgresql/README.md)
* [check-procs](./check-procs/README.md)
* [check-redis](./check-redis/README.md)
Expand Down
51 changes: 51 additions & 0 deletions check-ping/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# check-ping

## Description
Check ICMP Ping connections with the specified host.

## Synopsis
```
check-ping -H 127.0.0.1 -n 5 -w 100
```

## Installation

First, build this program.

```
go get github.com/mackerelio/go-check-plugins
cd $(go env GOPATH)/src/github.com/mackerelio/go-check-plugins/check-ping
go install
```

Or you can use this program by installing the official Mackerel package. See [Using the official check plugin pack for check monitoring - Mackerel Docs](https://mackerel.io/docs/entry/howto/mackerel-check-plugins).


Next, you can execute this program :-)

```
check-ping -H 127.0.0.1 -n 5 -w 100
```


## Setting for mackerel-agent

If there are no problems in the execution result, add a setting in mackerel-agent.conf .

```
[plugin.checks.check-ping-sample]
command = ["check-ping", "-H", "127.0.0.1", "-n", "5", "-w", "100"]
```

## Usage
### Options

```
-H, --host= check target IP Address
-n, --count= sending (and receiving) count ping packets (default: 1)
-w, --wait-time= wait time, Max RTT(ms) (default: 1000)
```

## For more information

Please execute `check-ping -h` and you can get command line options.
74 changes: 74 additions & 0 deletions check-ping/lib/check-ping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package checkping

import (
"net"
"os"
"time"

flags "github.com/jessevdk/go-flags"
"github.com/mackerelio/checkers"
ping "github.com/tatsushid/go-fastping"
)

var opts struct {
Host string `long:"host" short:"H" description:"check target IP Address"`
Count int `long:"count" short:"n" default:"1" description:"sending (and receiving) count ping packets"`
WaitTime int `long:"wait-time" short:"w" default:"1000" description:"wait time, Max RTT(ms)"`
}

func run(args []string) *checkers.Checker {
var parser = flags.NewParser(&opts, flags.Default)
_, err := parser.ParseArgs(args)
if err != nil {
os.Exit(1)
}
if opts.Host == "" {
parser.WriteHelp(os.Stderr)
os.Exit(1)
}

p := ping.NewPinger()
netProto := "ip4:icmp"
if isIPv6(opts.Host) {
netProto = "ip6:ipv6-icmp"
}

ra, err := net.ResolveIPAddr(netProto, opts.Host)
if err != nil {
os.Exit(1)
}
p.AddIPAddr(ra)

status := checkers.CRITICAL
p.MaxRTT = time.Millisecond * time.Duration(opts.WaitTime)
p.OnRecv = func(_ *net.IPAddr, _ time.Duration) {
status = checkers.OK
}

for i := 0; i < opts.Count; i++ {
err := p.Run()
if err != nil {
return checkers.NewChecker(status, err.Error())
}
}

return checkers.NewChecker(status, "")
}

func isIPv6(host string) bool {
addr, err := net.ResolveIPAddr("ip", host)
if err != nil {
os.Exit(1)
}
if ip4 := addr.IP.To4(); len(ip4) != net.IPv4len {
return true
}
return false
}

// Do the plugin
func Do() {
ckr := run(os.Args[1:])
ckr.Name = "Ping"
ckr.Exit()
}
43 changes: 43 additions & 0 deletions check-ping/lib/check-ping_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package checkping

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestIsIPv6(t *testing.T) {
testCases := []struct {
casename string
host string
expectDetectation bool
}{
{
casename: "IPv4 IP address",
host: "127.0.0.1",
expectDetectation: false,
},
{
casename: "IPv6 IP address 01",
host: "fe80::1",
expectDetectation: true,
},
{
casename: "IPv6 IP address 02",
host: "2001:db8::1",
expectDetectation: true,
},
{
casename: "IPv6 IP address 03",
host: "::1",
expectDetectation: true,
},
}

for _, tc := range testCases {
t.Run(tc.casename, func(t *testing.T) {
result := isIPv6(tc.host)
assert.Equal(t, tc.expectDetectation, result, "something went wrong")
})
}
}
7 changes: 7 additions & 0 deletions check-ping/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "github.com/mackerelio/go-check-plugins/check-ping/lib"

func main() {
checkping.Do()
}
4 changes: 4 additions & 0 deletions mackerel-check_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packaging/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"memcached",
"mysql",
"ntpoffset",
"ping",
"postgresql",
"procs",
"redis",
Expand Down

0 comments on commit ddd2737

Please sign in to comment.