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