Debian Codegraph @

cmd/ping/ping.go

raw symbols SCIP
1 // Copyright The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package main
15
16 import (
17 "flag"
18 "fmt"
19 "os"
20 "os/signal"
21 "time"
22
23 probing "github.com/prometheus-community/pro-bing"
24 )
25
26 var examples = `
27 Examples:
28
29 # ping google continuously
30 ping www.google.com
31
32 # ping google 5 times
33 ping -c 5 www.google.com
34
35 # ping google 5 times at 500ms intervals
36 ping -c 5 -i 500ms www.google.com
37
38 # ping google for 10 seconds
39 ping -t 10s www.google.com
40
41 # ping google specified interface
42 ping -I eth1 www.goole.com
43
44 # Send a privileged raw ICMP ping
45 sudo ping --privileged www.google.com
46
47 # Send ICMP messages with a 100-byte payload
48 ping -s 100 1.1.1.1
49
50 # Send ICMP messages with DSCP CS4 and ECN bits set to 0
51 ping -Q 128 8.8.8.8
52 `
53
54 func main() {
55 timeout := flag.Duration("t", time.Second*100000, "")
56 interval := flag.Duration("i", time.Second, "")
57 count := flag.Int("c", -1, "")
58 size := flag.Int("s", 24, "")
59 ttl := flag.Int("l", 64, "TTL")
60 iface := flag.String("I", "", "interface name")
61 tclass := flag.Int("Q", 192, "Set Quality of Service related bits in ICMP datagrams (DSCP + ECN bits). Only decimal number supported")
62 privileged := flag.Bool("privileged", false, "")
63 flag.Usage = func() {
64 out := flag.CommandLine.Output()
65 fmt.Fprintf(out, "Usage of %s:\n", os.Args[0])
66 flag.PrintDefaults()
67 fmt.Fprint(out, examples)
68 }
69 flag.Parse()
70
71 if flag.NArg() == 0 {
72 flag.Usage()
73 return
74 }
75
76 host := flag.Arg(0)
77 pinger, err := probing.NewPinger(host)
78 if err != nil {
79 fmt.Println("ERROR:", err)
80 return
81 }
82
83 // listen for ctrl-C signal
84 c := make(chan os.Signal, 1)
85 signal.Notify(c, os.Interrupt)
86 go func() {
87 for range c {
88 pinger.Stop()
89 }
90 }()
91
92 pinger.OnRecv = func(pkt *probing.Packet) {
93 fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v\n",
94 pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL)
95 }
96 pinger.OnDuplicateRecv = func(pkt *probing.Packet) {
97 fmt.Printf("%d bytes from %s: icmp_seq=%d time=%v ttl=%v (DUP!)\n",
98 pkt.Nbytes, pkt.IPAddr, pkt.Seq, pkt.Rtt, pkt.TTL)
99 }
100 pinger.OnFinish = func(stats *probing.Statistics) {
101 fmt.Printf("\n--- %s ping statistics ---\n", stats.Addr)
102 fmt.Printf("%d packets transmitted, %d packets received, %d duplicates, %v%% packet loss\n",
103 stats.PacketsSent, stats.PacketsRecv, stats.PacketsRecvDuplicates, stats.PacketLoss)
104 fmt.Printf("round-trip min/avg/max/stddev = %v/%v/%v/%v\n",
105 stats.MinRtt, stats.AvgRtt, stats.MaxRtt, stats.StdDevRtt)
106 }
107
108 pinger.Count = *count
109 pinger.Size = *size
110 pinger.Interval = *interval
111 pinger.Timeout = *timeout
112 pinger.TTL = *ttl
113 pinger.InterfaceName = *iface
114 pinger.SetPrivileged(*privileged)
115 pinger.SetTrafficClass(uint8(*tclass))
116
117 fmt.Printf("PING %s (%s):\n", pinger.Addr(), pinger.IPAddr())
118 err = pinger.Run()
119 if err != nil {
120 fmt.Println("Failed to ping target host:", err)
121 }
122 }