-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #280 from mackerelio/check-ping
Add check-ping
- Loading branch information
Showing
7 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"memcached", | ||
"mysql", | ||
"ntpoffset", | ||
"ping", | ||
"postgresql", | ||
"procs", | ||
"redis", | ||
|