-
Notifications
You must be signed in to change notification settings - Fork 96
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add check-ping #280
Merged
Merged
Add check-ping #280
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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", | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like it!