aboutsummaryrefslogtreecommitdiff
path: root/cmd/kpod/info.go
blob: 0b8ae75b49e8490c85d00d4581df43df37ec8f81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"runtime"

	"github.com/docker/docker/pkg/system"
	"github.com/pkg/errors"
	"github.com/projectatomic/libpod/cmd/kpod/formats"
	"github.com/urfave/cli"
)

var (
	infoDescription = "display system information"
	infoCommand     = cli.Command{
		Name:        "info",
		Usage:       infoDescription,
		Description: `Information display here pertain to the host, current storage stats, and build of kpod. Useful for the user and when reporting issues.`,
		Flags:       infoFlags,
		Action:      infoCmd,
		ArgsUsage:   "",
	}
	infoFlags = []cli.Flag{
		cli.BoolFlag{
			Name:  "debug, D",
			Usage: "display additional debug information",
		},
		cli.StringFlag{
			Name:  "format",
			Usage: "Change the output format to JSON or a Go template",
		},
	}
)

func infoCmd(c *cli.Context) error {
	if err := validateFlags(c, infoFlags); err != nil {
		return err
	}
	info := map[string]interface{}{}

	infoGivers := []infoGiverFunc{
		storeInfo,
		hostInfo,
	}

	if c.Bool("debug") {
		infoGivers = append(infoGivers, debugInfo)
	}

	for _, giver := range infoGivers {
		thisName, thisInfo, err := giver(c)
		if err != nil {
			info[thisName] = infoErr(err)
			continue
		}
		info[thisName] = thisInfo
	}

	var out formats.Writer
	infoOutputFormat := c.String("format")
	switch infoOutputFormat {
	case formats.JSONString:
		out = formats.JSONStruct{Output: info}
	case "":
		out = formats.YAMLStruct{Output: info}
	default:
		out = formats.StdoutTemplate{Output: info, Template: infoOutputFormat}
	}

	formats.Writer(out).Out()

	return nil
}

func infoErr(err error) map[string]interface{} {
	return map[string]interface{}{
		"error": err.Error(),
	}
}

type infoGiverFunc func(c *cli.Context) (name string, info map[string]interface{}, err error)

// top-level "debug" info
func debugInfo(c *cli.Context) (string, map[string]interface{}, error) {
	info := map[string]interface{}{}
	info["compiler"] = runtime.Compiler
	info["go version"] = runtime.Version()
	info["kpod version"] = c.App.Version
	info["git commit"] = gitCommit
	return "debug", info, nil
}

// top-level "host" info
func hostInfo(c *cli.Context) (string, map[string]interface{}, error) {
	// lets say OS, arch, number of cpus, amount of memory, maybe os distribution/version, hostname, kernel version, uptime
	info := map[string]interface{}{}
	info["os"] = runtime.GOOS
	info["arch"] = runtime.GOARCH
	info["cpus"] = runtime.NumCPU()
	mi, err := system.ReadMemInfo()
	if err != nil {
		info["meminfo"] = infoErr(err)
	} else {
		// TODO this might be a place for github.com/dustin/go-humanize
		info["MemTotal"] = mi.MemTotal
		info["MemFree"] = mi.MemFree
		info["SwapTotal"] = mi.SwapTotal
		info["SwapFree"] = mi.SwapFree
	}
	if kv, err := readKernelVersion(); err != nil {
		info["kernel"] = infoErr(err)
	} else {
		info["kernel"] = kv
	}

	if up, err := readUptime(); err != nil {
		info["uptime"] = infoErr(err)
	} else {
		info["uptime"] = up
	}
	if host, err := os.Hostname(); err != nil {
		info["hostname"] = infoErr(err)
	} else {
		info["hostname"] = host
	}
	return "host", info, nil
}

// top-level "store" info
func storeInfo(c *cli.Context) (string, map[string]interface{}, error) {
	storeStr := "store"
	config, err := getConfig(c)
	if err != nil {
		return storeStr, nil, errors.Wrapf(err, "Could not get config")
	}
	store, err := getStore(config)
	if err != nil {
		return storeStr, nil, err
	}

	// lets say storage driver in use, number of images, number of containers
	info := map[string]interface{}{}
	info["GraphRoot"] = store.GraphRoot()
	info["RunRoot"] = store.RunRoot()
	info["GraphDriverName"] = store.GraphDriverName()
	info["GraphOptions"] = store.GraphOptions()
	statusPairs, err := store.Status()
	if err != nil {
		return storeStr, nil, err
	}
	status := map[string]string{}
	for _, pair := range statusPairs {
		status[pair[0]] = pair[1]
	}
	info["GraphStatus"] = status
	images, err := store.Images()
	if err != nil {
		info["ImageStore"] = infoErr(err)
	} else {
		info["ImageStore"] = map[string]interface{}{
			"number": len(images),
		}
	}
	containers, err := store.Containers()
	if err != nil {
		info["ContainerStore"] = infoErr(err)
	} else {
		info["ContainerStore"] = map[string]interface{}{
			"number": len(containers),
		}
	}
	return storeStr, info, nil
}

func readKernelVersion() (string, error) {
	buf, err := ioutil.ReadFile("/proc/version")
	if err != nil {
		return "", err
	}
	f := bytes.Fields(buf)
	if len(f) < 2 {
		return string(bytes.TrimSpace(buf)), nil
	}
	return string(f[2]), nil
}

func readUptime() (string, error) {
	buf, err := ioutil.ReadFile("/proc/uptime")
	if err != nil {
		return "", err
	}
	f := bytes.Fields(buf)
	if len(f) < 1 {
		return "", fmt.Errorf("invalid uptime")
	}
	return string(f[0]), nil
}