From c3cac72b92a5017423ce2358b8bfc3a56bb45fbb Mon Sep 17 00:00:00 2001 From: Ace-Tang Date: Mon, 22 Oct 2018 11:39:33 +0800 Subject: [PATCH] ctr: fix potential panic in metric Signed-off-by: Ace-Tang --- cmd/ctr/commands/tasks/metrics.go | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/cmd/ctr/commands/tasks/metrics.go b/cmd/ctr/commands/tasks/metrics.go index dc0973070..2330482b4 100644 --- a/cmd/ctr/commands/tasks/metrics.go +++ b/cmd/ctr/commands/tasks/metrics.go @@ -89,13 +89,19 @@ var metricsCommand = cli.Command{ fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp) fmt.Fprintf(w, "METRIC\tVALUE\t\n") - fmt.Fprintf(w, "memory.usage_in_bytes\t%d\t\n", data.Memory.Usage.Usage) - fmt.Fprintf(w, "memory.limit_in_bytes\t%d\t\n", data.Memory.Usage.Limit) - fmt.Fprintf(w, "memory.stat.cache\t%d\t\n", data.Memory.TotalCache) - fmt.Fprintf(w, "cpuacct.usage\t%d\t\n", data.CPU.Usage.Total) - fmt.Fprintf(w, "cpuacct.usage_percpu\t%v\t\n", data.CPU.Usage.PerCPU) - fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current) - fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit) + if data.Memory != nil { + fmt.Fprintf(w, "memory.usage_in_bytes\t%d\t\n", data.Memory.Usage.Usage) + fmt.Fprintf(w, "memory.limit_in_bytes\t%d\t\n", data.Memory.Usage.Limit) + fmt.Fprintf(w, "memory.stat.cache\t%d\t\n", data.Memory.TotalCache) + } + if data.CPU != nil { + fmt.Fprintf(w, "cpuacct.usage\t%d\t\n", data.CPU.Usage.Total) + fmt.Fprintf(w, "cpuacct.usage_percpu\t%v\t\n", data.CPU.Usage.PerCPU) + } + if data.Pids != nil { + fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current) + fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit) + } return w.Flush() case formatJSON: marshaledJSON, err := json.MarshalIndent(data, "", " ")