Fix podman stats based on QE feedback

QE found issues with formatting the go template and
the man page was lacking information.
Changed the format of the output to match latest docker.
Add shortID function that returns the truncated ID

Signed-off-by: umohnani8 <umohnani@redhat.com>

Closes: #258
Approved by: rhatdan
This commit is contained in:
umohnani8 2018-01-23 16:50:20 -05:00 committed by Atomic Bot
parent 4c7bab9812
commit 12e3d9d8a2
7 changed files with 75 additions and 51 deletions

View File

@ -22,7 +22,10 @@ var (
}
)
const crioConfigPath = "/etc/crio/crio.conf"
const (
crioConfigPath = "/etc/crio/crio.conf"
idTruncLength = 12
)
func getRuntime(c *cli.Context) (*libpod.Runtime, error) {
@ -88,6 +91,13 @@ func splitCamelCase(src string) string {
return strings.Join(entries, " ")
}
func shortID(id string) string {
if len(id) > idTruncLength {
return id[:idTruncLength]
}
return id
}
// validateFlags searches for StringFlags or StringSlice flags that never had
// a value set. This commonly occurs when the CLI mistakenly takes the next
// option and uses it as a value.

View File

@ -14,10 +14,7 @@ import (
"github.com/urfave/cli"
)
const (
createdByTruncLength = 45
idTruncLength = 12
)
const createdByTruncLength = 45
// historyTemplateParams stores info about each layer
type historyTemplateParams struct {
@ -169,7 +166,7 @@ func getHistoryTemplateOutput(history []v1.History, layers []types.BlobInfo, ima
imageID = "<missing>"
}
if !opts.noTrunc && i == len(history)-1 {
imageID = imageID[:idTruncLength]
imageID = shortID(imageID)
}
var size int64

View File

@ -194,7 +194,7 @@ func getImagesTemplateOutput(runtime *libpod.Runtime, images []*storage.Image, o
imageID := "sha256:" + img.ID
if !opts.noTrunc {
imageID = img.ID[:idTruncLength]
imageID = shortID(img.ID)
}
repository := "<none>"

View File

@ -482,7 +482,7 @@ func getTemplateOutput(containers []*libpod.Container, opts psOptions) ([]psTemp
}
if !opts.noTrunc {
ctrID = ctr.ID()[:idTruncLength]
ctrID = shortID(ctr.ID())
imageName = conConfig.RootfsImageName
}

View File

@ -15,14 +15,14 @@ import (
)
type statsOutputParams struct {
Container string `json:"name"`
ID string `json:"id"`
CPUPerc string `json:"cpu_percent"`
MemUsage string `json:"mem_usage"`
MemPerc string `json:"mem_percent"`
NetIO string `json:"netio"`
BlockIO string `json:"blocki"`
PIDS string `json:"pids"`
ID string `json:"id"`
Name string `json:"name"`
CPUPerc string `json:"cpu_percent"`
MemUsage string `json:"mem_usage"`
MemPerc string `json:"mem_percent"`
NetIO string `json:"netio"`
BlockIO string `json:"blocki"`
PIDS string `json:"pids"`
}
var (
@ -37,7 +37,7 @@ var (
},
cli.StringFlag{
Name: "format",
Usage: "pretty-print container statistics using a Go template",
Usage: "pretty-print container statistics to JSON or using a Go template",
},
cli.BoolFlag{
Name: "no-reset",
@ -184,7 +184,12 @@ func outputStats(stats []*libpod.ContainerStats, format string) error {
}
func genStatsFormat() (format string) {
return "table {{.Container}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}"
if format != "" {
// "\t" from the command line is not being recognized as a tab
// replacing the string "\t" to a tab character if the user passes in "\t"
return strings.Replace(format, `\t`, "\t", -1)
}
return "table {{.ID}}\t{{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}\t{{.NetIO}}\t{{.BlockIO}}\t{{.PIDS}}"
}
// imagesToGeneric creates an empty array of interfaces for output
@ -248,13 +253,13 @@ func pidsToString(pid uint64) string {
func getStatsOutputParams(stats *libpod.ContainerStats) statsOutputParams {
return statsOutputParams{
Container: stats.ContainerID[:12],
ID: stats.ContainerID,
CPUPerc: floatToPercentString(stats.CPU),
MemUsage: combineHumanValues(stats.MemUsage, stats.MemLimit),
MemPerc: floatToPercentString(stats.MemPerc),
NetIO: combineHumanValues(stats.NetInput, stats.NetOutput),
BlockIO: combineHumanValues(stats.BlockInput, stats.BlockOutput),
PIDS: pidsToString(stats.PIDs),
Name: stats.Name,
ID: shortID(stats.ContainerID),
CPUPerc: floatToPercentString(stats.CPU),
MemUsage: combineHumanValues(stats.MemUsage, stats.MemLimit),
MemPerc: floatToPercentString(stats.MemPerc),
NetIO: combineHumanValues(stats.NetInput, stats.NetOutput),
BlockIO: combineHumanValues(stats.BlockInput, stats.BlockOutput),
PIDS: pidsToString(stats.PIDs),
}
}

View File

@ -31,48 +31,58 @@ Disable streaming stats and only pull the first result, default setting is false
**--format="TEMPLATE"**
Pretty-print images using a Go template
Pretty-print container statistics to JSON or using a Go template
Valid placeholders for the Go template are listed below:
| **Placeholder** | **Description** |
| --------------- | --------------- |
| .ID | Container ID |
| .Name | Container Name |
| .CPUPerc | CPU percentage |
| .MemUsage | Memory usage |
| .MemPerc | Memory percentage |
| .NetIO | Network IO |
| .BlockIO | Block IO |
| .PIDS | Number of PIDs |
## EXAMPLE
```
# podman stats -a --no-stream
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS
132ade621b5d 0.00% 1.618MB / 33.08GB 0.00% 0B / 0B 0B / 0B 0
940e00a40a77 0.00% 1.544MB / 33.08GB 0.00% 0B / 0B 0B / 0B 0
72a1dfb44ca7 0.00% 1.528MB / 33.08GB 0.00% 0B / 0B 0B / 0B 0
f5a62a71b07b 0.00% 5.669MB / 33.08GB 0.02% 0B / 0B 0B / 0B 3
31eab2cf93f4 0.00% 16.42MB / 33.08GB 0.05% 0B / 0B 22.43MB / 0B 0
#
ID NAME CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS
a9f807ffaacd frosty_hodgkin -- 3.092MB / 16.7GB 0.02% -- / -- -- / -- 2
3b33001239ee sleepy_stallman -- -- / -- -- -- / -- -- / -- --
```
```
# podman stats --no-stream 31eab2cf93f4
CONTAINER CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS
31eab2cf93f4 0.00% 16.42MB / 33.08GB 0.05% 0B / 0B 22.43MB / 0B 0
# podman stats --no-stream a9f80
ID NAME CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS
a9f807ffaacd frosty_hodgkin -- 3.092MB / 16.7GB 0.02% -- / -- -- / -- 2
```
#
```
```
# podman stats --no-stream --format=json 31eab2cf93f4
# podman stats --no-stream --format=json a9f80
[
{
"name": "31eab2cf93f4",
"id": "31eab2cf93f413af64a3f13d8d78393238658465d75e527333a8577f251162ec",
"cpu_percent": "0.00%",
"mem_usage": "16.42MB / 33.08GB",
"mem_percent": "0.05%",
"netio": "0B / 0B",
"blocki": "22.43MB / 0B",
"pids": 0
"id": "a9f807ffaacd",
"name": "frosty_hodgkin",
"cpu_percent": "--",
"mem_usage": "3.092MB / 16.7GB",
"mem_percent": "0.02%",
"netio": "-- / --",
"blocki": "-- / --",
"pids": "2"
}
]
#
```
```
# podman stats --no-stream --format "table {{.ID}} {{.Name}} {{.MemUsage}}" 6eae
ID NAME MEM USAGE / LIMIT
6eae9e25a564 clever_bassi 3.031MB / 16.7GB
```
## SEE ALSO
podman(1)

View File

@ -13,6 +13,7 @@ import (
// ContainerStats contains the statistics information for a running container
type ContainerStats struct {
ContainerID string
Name string
CPU float64
CPUNano uint64
SystemNano uint64
@ -30,6 +31,7 @@ type ContainerStats struct {
func (c *Container) GetContainerStats(previousStats *ContainerStats) (*ContainerStats, error) {
stats := new(ContainerStats)
stats.ContainerID = c.ID()
stats.Name = c.Name()
c.lock.Lock()
defer c.lock.Unlock()
if err := c.syncContainer(); err != nil {