stat.go 5.19 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5
package commands

import (
	"fmt"
	"io"
Jan Winkelmann's avatar
Jan Winkelmann committed
6
	"os"
Jeromy's avatar
Jeromy committed
7 8
	"time"

9 10
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"

Jakub Sztandera's avatar
Jakub Sztandera committed
11
	humanize "gx/ipfs/QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K/go-humanize"
12 13
	peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
	cmdkit "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
keks's avatar
keks committed
14
	cmds "gx/ipfs/QmSwR1QndLsdnw2mpW2rrywoCUPeY7o8UxtK1dLpPCNFGD/go-ipfs-cmds"
15
	protocol "gx/ipfs/QmZNkThpqfVXs9GNbexPrfBbXSLNYeKrE7jwFM2oqHbyqN/go-libp2p-protocol"
16
	metrics "gx/ipfs/QmdhwKw53CTV8EJSAsR1bpmMT5kXiWBgeAyv1EXeeDiXqR/go-libp2p-metrics"
Jeromy's avatar
Jeromy committed
17 18 19
)

var StatsCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
20
	Helptext: cmdkit.HelpText{
21
		Tagline: "Query IPFS statistics.",
22
		ShortDescription: `'ipfs stats' is a set of commands to help look at statistics
23
for your IPFS node.
24
`,
Jeromy's avatar
Jeromy committed
25
		LongDescription: `'ipfs stats' is a set of commands to help look at statistics
26
for your IPFS node.`,
Jeromy's avatar
Jeromy committed
27 28 29
	},

	Subcommands: map[string]*cmds.Command{
30 31 32
		"bw":      statBwCmd,
		"repo":    repoStatCmd,
		"bitswap": bitswapStatCmd,
Jeromy's avatar
Jeromy committed
33 34 35 36
	},
}

var statBwCmd = &cmds.Command{
Jan Winkelmann's avatar
Jan Winkelmann committed
37
	Helptext: cmdkit.HelpText{
Jeromy's avatar
Jeromy committed
38
		Tagline: "Print ipfs bandwidth information.",
39 40 41 42 43 44
		ShortDescription: `'ipfs stats bw' prints bandwidth information for the ipfs daemon.
It displays: TotalIn, TotalOut, RateIn, RateOut.
		`,
		LongDescription: `'ipfs stats bw' prints bandwidth information for the ipfs daemon.
It displays: TotalIn, TotalOut, RateIn, RateOut.

45 46 47 48 49
By default, overall bandwidth and all protocols are shown. To limit bandwidth
to a particular peer, use the 'peer' option along with that peer's multihash
id. To specify a specific protocol, use the 'proto' option. The 'peer' and
'proto' options cannot be specified simultaneously. The protocols that are
queried using this method are outlined in the specification:
50
https://github.com/libp2p/specs/blob/master/7-properties.md#757-protocol-multicodecs
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

Example protocol options:
  - /ipfs/id/1.0.0
  - /ipfs/bitswap
  - /ipfs/dht

Example:

    > ipfs stats bw -t /ipfs/bitswap
    Bandwidth
    TotalIn: 5.0MB
    TotalOut: 0B
    RateIn: 343B/s
    RateOut: 0B/s
    > ipfs stats bw -p QmepgFW7BHEtU4pZJdxaNiv75mKLLRQnPi1KaaXmQN4V1a
    Bandwidth
    TotalIn: 4.9MB
    TotalOut: 12MB
    RateIn: 0B/s
    RateOut: 0B/s
`,
Jeromy's avatar
Jeromy committed
72
	},
Jan Winkelmann's avatar
Jan Winkelmann committed
73 74 75
	Options: []cmdkit.Option{
		cmdkit.StringOption("peer", "p", "Specify a peer to print bandwidth for."),
		cmdkit.StringOption("proto", "t", "Specify a protocol to print bandwidth for."),
76
		cmdkit.BoolOption("poll", "Print bandwidth at an interval."),
Jan Winkelmann's avatar
Jan Winkelmann committed
77
		cmdkit.StringOption("interval", "i", `Time interval to wait between updating output, if 'poll' is true.
78 79

    This accepts durations such as "300s", "1.5h" or "2h45m". Valid time units are:
80
    "ns", "us" (or "µs"), "ms", "s", "m", "h".`).WithDefault("1s"),
Jeromy's avatar
Jeromy committed
81 82
	},

keks's avatar
keks committed
83
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
84
		nd, err := cmdenv.GetNode(env)
Jeromy's avatar
Jeromy committed
85
		if err != nil {
keks's avatar
keks committed
86
			return err
Jeromy's avatar
Jeromy committed
87 88 89 90
		}

		// Must be online!
		if !nd.OnlineMode() {
keks's avatar
keks committed
91
			return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
Jeromy's avatar
Jeromy committed
92 93
		}

94
		if nd.Reporter == nil {
keks's avatar
keks committed
95
			return fmt.Errorf("bandwidth reporter disabled in config")
96 97
		}

98 99
		pstr, pfound := req.Options["peer"].(string)
		tstr, tfound := req.Options["proto"].(string)
Jeromy's avatar
Jeromy committed
100
		if pfound && tfound {
keks's avatar
keks committed
101
			return cmdkit.Errorf(cmdkit.ErrClient, "please only specify peer OR protocol")
Jeromy's avatar
Jeromy committed
102 103 104 105 106 107
		}

		var pid peer.ID
		if pfound {
			checkpid, err := peer.IDB58Decode(pstr)
			if err != nil {
keks's avatar
keks committed
108
				return err
Jeromy's avatar
Jeromy committed
109 110 111 112
			}
			pid = checkpid
		}

113
		timeS, _ := req.Options["interval"].(string)
114 115
		interval, err := time.ParseDuration(timeS)
		if err != nil {
keks's avatar
keks committed
116
			return err
Jeromy's avatar
Jeromy committed
117 118
		}

119
		doPoll, _ := req.Options["poll"].(bool)
Jan Winkelmann's avatar
Jan Winkelmann committed
120 121 122 123 124 125 126 127 128 129 130
		for {
			if pfound {
				stats := nd.Reporter.GetBandwidthForPeer(pid)
				res.Emit(&stats)
			} else if tfound {
				protoId := protocol.ID(tstr)
				stats := nd.Reporter.GetBandwidthForProtocol(protoId)
				res.Emit(&stats)
			} else {
				totals := nd.Reporter.GetBandwidthTotals()
				res.Emit(&totals)
Jeromy's avatar
Jeromy committed
131
			}
Jan Winkelmann's avatar
Jan Winkelmann committed
132
			if !doPoll {
keks's avatar
keks committed
133
				return nil
Jan Winkelmann's avatar
Jan Winkelmann committed
134 135 136
			}
			select {
			case <-time.After(interval):
137
			case <-req.Context.Done():
keks's avatar
keks committed
138
				break
Jan Winkelmann's avatar
Jan Winkelmann committed
139 140
			}
		}
Jeromy's avatar
Jeromy committed
141 142
	},
	Type: metrics.Stats{},
Jan Winkelmann's avatar
Jan Winkelmann committed
143
	PostRun: cmds.PostRunMap{
keks's avatar
keks committed
144 145 146 147 148 149 150 151 152 153 154
		cmds.CLI: func(res cmds.Response, re cmds.ResponseEmitter) error {
			polling, _ := res.Request().Options["poll"].(bool)
			log.Debug("postrun polling:", polling)
			if polling {
				fmt.Fprintln(os.Stdout, "Total Up    Total Down  Rate Up     Rate Down")
			}
			for {
				v, err := res.Next()
				if err != nil {
					if err == io.EOF {
						return nil
Jeromy's avatar
Jeromy committed
155
					}
keks's avatar
keks committed
156 157
					return err
				}
Jeromy's avatar
Jeromy committed
158

keks's avatar
keks committed
159
				bs := v.(*metrics.Stats)
Jan Winkelmann's avatar
Jan Winkelmann committed
160

keks's avatar
keks committed
161 162 163
				if !polling {
					printStats(os.Stdout, bs)
					return nil
Jan Winkelmann's avatar
Jan Winkelmann committed
164
				}
Jeromy's avatar
Jeromy committed
165

keks's avatar
keks committed
166 167 168 169 170
				fmt.Fprintf(os.Stdout, "%8s    ", humanize.Bytes(uint64(bs.TotalOut)))
				fmt.Fprintf(os.Stdout, "%8s    ", humanize.Bytes(uint64(bs.TotalIn)))
				fmt.Fprintf(os.Stdout, "%8s/s  ", humanize.Bytes(uint64(bs.RateOut)))
				fmt.Fprintf(os.Stdout, "%8s/s      \r", humanize.Bytes(uint64(bs.RateIn)))
			}
Jeromy's avatar
Jeromy committed
171 172 173 174 175 176 177 178 179 180 181
		},
	},
}

func printStats(out io.Writer, bs *metrics.Stats) {
	fmt.Fprintln(out, "Bandwidth")
	fmt.Fprintf(out, "TotalIn: %s\n", humanize.Bytes(uint64(bs.TotalIn)))
	fmt.Fprintf(out, "TotalOut: %s\n", humanize.Bytes(uint64(bs.TotalOut)))
	fmt.Fprintf(out, "RateIn: %s/s\n", humanize.Bytes(uint64(bs.RateIn)))
	fmt.Fprintf(out, "RateOut: %s/s\n", humanize.Bytes(uint64(bs.RateOut)))
}