// Copyright The Prometheus Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package main import ( "flag" "fmt" "os" "os/signal" "time" probing "github.com/prometheus-community/pro-bing" ) var examples = ` Examples: # ping google continuously ping www.google.com # ping google 5 times ping -c 5 www.google.com # ping google 5 times at 500ms intervals ping -c 5 -i 500ms www.google.com # ping google for 10 seconds ping -t 10s www.google.com # ping google specified interface ping -I eth1 www.goole.com # Send a privileged raw ICMP ping sudo ping --privileged www.google.com # Send ICMP messages with a 100-byte payload ping -s 100 1.1.1.1 # Send ICMP messages with DSCP CS4 and ECN bits set to 0 ping -Q 128 8.8.8.8 ` func main() { timeout := flag.Duration("t", time.Second*100000, "") interval := flag.Duration("i", time.Second, "") count := flag.Int("c", -1, "") size := flag.Int("s", 24, "") ttl := flag.Int("l", 64, "TTL") iface := flag.String("I", "", "interface name") tclass := flag.Int("Q", 192, "Set Quality of Service related bits in ICMP datagrams (DSCP + ECN bits). Only decimal number supported") privileged := flag.Bool("privileged", false, "") flag.Usage = func() { out := flag.CommandLine.Output() fmt.Fprintf(out, "Usage of %s:\n", os.Args[0]) flag.PrintDefaults() fmt.Fprint(out, examples) } flag.Parse() if flag.NArg() == 0 { flag.Usage() return } host := flag.Arg(0) pinger, err := probing.NewPinger(host) if err != nil { fmt.Println("ERROR:", err) return } // listen for ctrl-C signal c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) go func() { for range c { pinger.Stop() } }() pinger.OnRecv = func(pkt *probing.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL) } pinger.OnDuplicateRecv = func(pkt *probing.Packet) { fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n", pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL) } pinger.OnFinish = func(stats *probing.Statistics) { fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr) fmt.Printf("%d packets transmitted, %d packets received, %d duplicates, %v%% packet loss\n", stats.PacketsSent, stats.PacketsRecv, stats.PacketsRecvDuplicates, stats.PacketLoss) fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n", stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt) } pinger.Count = *count pinger.Size = *size pinger.Interval = *interval pinger.Timeout = *timeout pinger.TTL = *ttl pinger.InterfaceName = *iface pinger.SetPrivileged(*privileged) pinger.SetTrafficClass(uint8(*tclass)) fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr()) err = pinger.Run() if err != nil { fmt.Println("Failed to ping target host:", err) } }