runc/nsinit/stats.go

51 lines
1002 B
Go
Raw Normal View History

package main
import (
"encoding/json"
"fmt"
"log"
"github.com/codegangsta/cli"
2014-06-10 23:14:16 +08:00
"github.com/docker/libcontainer"
)
var statsCommand = cli.Command{
Name: "stats",
Usage: "display statistics for the container",
Action: statsAction,
}
func statsAction(context *cli.Context) {
container, err := loadContainer()
if err != nil {
log.Fatal(err)
}
runtimeCkpt, err := libcontainer.GetRuntimeCkpt(dataPath)
if err != nil {
log.Fatal(err)
}
stats, err := getContainerStats(container, runtimeCkpt)
if err != nil {
log.Fatalf("Failed to get stats - %v\n", err)
}
fmt.Printf("Stats:\n%v\n", stats)
}
// returns the container stats in json format.
func getContainerStats(container *libcontainer.Config, runtimeCkpt *libcontainer.RuntimeCkpt) (string, error) {
stats, err := libcontainer.GetContainerStats(container, runtimeCkpt)
if err != nil {
return "", err
}
out, err := json.MarshalIndent(stats, "", "\t")
if err != nil {
return "", err
}
return string(out), nil
}