sysdiag.go 2.68 KB
Newer Older
Jeromy's avatar
Jeromy committed
1 2 3 4 5 6 7
package commands

import (
	"os"
	"path"
	"runtime"

8
	version "github.com/ipfs/go-ipfs"
9
	cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
Jeromy's avatar
Jeromy committed
10

Jakub Sztandera's avatar
Jakub Sztandera committed
11
	cmds "github.com/ipfs/go-ipfs-cmds"
12
	manet "github.com/multiformats/go-multiaddr/net"
Jakub Sztandera's avatar
Jakub Sztandera committed
13
	sysi "github.com/whyrusleeping/go-sysinfo"
Jeromy's avatar
Jeromy committed
14 15 16
)

var sysDiagCmd = &cmds.Command{
Steven Allen's avatar
Steven Allen committed
17
	Helptext: cmds.HelpText{
18
		Tagline: "Print system diagnostic information.",
Jeromy's avatar
Jeromy committed
19 20 21 22
		ShortDescription: `
Prints out information about your computer to aid in easier debugging.
`,
	},
23
	Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
Jeromy's avatar
Jeromy committed
24 25 26
		info := make(map[string]interface{})
		err := runtimeInfo(info)
		if err != nil {
27
			return err
Jeromy's avatar
Jeromy committed
28 29 30 31
		}

		err = envVarInfo(info)
		if err != nil {
32
			return err
Jeromy's avatar
Jeromy committed
33 34 35 36
		}

		err = diskSpaceInfo(info)
		if err != nil {
37
			return err
Jeromy's avatar
Jeromy committed
38 39 40 41
		}

		err = memInfo(info)
		if err != nil {
42
			return err
Jeromy's avatar
Jeromy committed
43
		}
44
		nd, err := cmdenv.GetNode(env)
45
		if err != nil {
46
			return err
47
		}
Jeromy's avatar
Jeromy committed
48

49
		err = netInfo(nd.IsOnline, info)
Jeromy's avatar
Jeromy committed
50
		if err != nil {
51
			return err
Jeromy's avatar
Jeromy committed
52 53
		}

54 55
		info["ipfs_version"] = version.CurrentVersionNumber
		info["ipfs_commit"] = version.CurrentCommit
56
		return cmds.EmitOnce(res, info)
Jeromy's avatar
Jeromy committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	},
}

func runtimeInfo(out map[string]interface{}) error {
	rt := make(map[string]interface{})
	rt["os"] = runtime.GOOS
	rt["arch"] = runtime.GOARCH
	rt["compiler"] = runtime.Compiler
	rt["version"] = runtime.Version()
	rt["numcpu"] = runtime.NumCPU()
	rt["gomaxprocs"] = runtime.GOMAXPROCS(0)
	rt["numgoroutines"] = runtime.NumGoroutine()

	out["runtime"] = rt
	return nil
}

func envVarInfo(out map[string]interface{}) error {
	ev := make(map[string]interface{})
	ev["GOPATH"] = os.Getenv("GOPATH")
	ev["IPFS_PATH"] = os.Getenv("IPFS_PATH")

	out["environment"] = ev
	return nil
}

func ipfsPath() string {
	p := os.Getenv("IPFS_PATH")
	if p == "" {
		p = path.Join(os.Getenv("HOME"), ".ipfs")
	}
	return p
}

func diskSpaceInfo(out map[string]interface{}) error {
	di := make(map[string]interface{})
Jeromy's avatar
Jeromy committed
93
	dinfo, err := sysi.DiskUsage(ipfsPath())
Jeromy's avatar
Jeromy committed
94 95 96 97
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
98
	di["fstype"] = dinfo.FsType
Jeromy's avatar
Jeromy committed
99 100 101 102 103 104 105 106 107 108
	di["total_space"] = dinfo.Total
	di["free_space"] = dinfo.Free

	out["diskinfo"] = di
	return nil
}

func memInfo(out map[string]interface{}) error {
	m := make(map[string]interface{})

Jeromy's avatar
Jeromy committed
109
	meminf, err := sysi.MemoryInfo()
Jeromy's avatar
Jeromy committed
110 111 112 113
	if err != nil {
		return err
	}

Jeromy's avatar
Jeromy committed
114 115
	m["swap"] = meminf.Swap
	m["virt"] = meminf.Used
Jeromy's avatar
Jeromy committed
116 117 118 119
	out["memory"] = m
	return nil
}

120
func netInfo(online bool, out map[string]interface{}) error {
Jeromy's avatar
Jeromy committed
121 122 123 124 125 126 127 128 129 130 131 132
	n := make(map[string]interface{})
	addrs, err := manet.InterfaceMultiaddrs()
	if err != nil {
		return err
	}

	var straddrs []string
	for _, a := range addrs {
		straddrs = append(straddrs, a.String())
	}

	n["interface_addresses"] = straddrs
133
	n["online"] = online
Jeromy's avatar
Jeromy committed
134 135 136
	out["net"] = n
	return nil
}