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"
|
|
|
|
"github.com/docker/libcontainer/cgroups/fs"
|
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-05 07:46:30 +08:00
|
|
|
stats, err := getContainerStats(container)
|
|
|
|
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-24 07:54:35 +08:00
|
|
|
func getContainerStats(container *libcontainer.Config) (string, error) {
|
2014-06-05 07:46:30 +08:00
|
|
|
stats, err := fs.GetStats(container.Cgroups)
|
|
|
|
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
|
|
|
|
}
|