Adding "stats" and "spec" option to nsinit binary which will print the stats and spec respectively.

Docker-DCO-1.1-Signed-off-by: Vishnu Kannan <vishnuk@google.com> (github: vishh)
This commit is contained in:
Vishnu Kannan 2014-05-30 00:23:18 +00:00
parent 1a38e6ff10
commit 3589181812
2 changed files with 48 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"syscall"
"github.com/dotcloud/docker/pkg/libcontainer/cgroups" "github.com/dotcloud/docker/pkg/libcontainer/cgroups"
) )
@ -49,6 +50,9 @@ func (s *cpuGroup) GetStats(d *data, stats *cgroups.Stats) error {
f, err := os.Open(filepath.Join(path, "cpu.stat")) f, err := os.Open(filepath.Join(path, "cpu.stat"))
if err != nil { if err != nil {
if pathErr, ok := err.(*os.PathError); ok && pathErr.Err == syscall.ENOENT {
return nil
}
return err return err
} }
defer f.Close() defer f.Close()

View file

@ -2,6 +2,7 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -11,6 +12,7 @@ import (
"strconv" "strconv"
"github.com/dotcloud/docker/pkg/libcontainer" "github.com/dotcloud/docker/pkg/libcontainer"
"github.com/dotcloud/docker/pkg/libcontainer/cgroups/fs"
"github.com/dotcloud/docker/pkg/libcontainer/nsinit" "github.com/dotcloud/docker/pkg/libcontainer/nsinit"
) )
@ -67,6 +69,26 @@ func main() {
if err := nsinit.Init(container, rootfs, console, syncPipe, os.Args[2:]); err != nil { if err := nsinit.Init(container, rootfs, console, syncPipe, os.Args[2:]); err != nil {
log.Fatalf("unable to initialize for container: %s", err) log.Fatalf("unable to initialize for container: %s", err)
} }
case "stats":
// returns the stats of the current container.
stats, err := getContainerStats(container)
if err != nil {
log.Printf("Failed to get stats - %v\n", err)
os.Exit(1)
}
fmt.Printf("Stats:\n%v\n", stats)
os.Exit(0)
case "spec":
// returns the spec of the current container.
spec, err := getContainerSpec(container)
if err != nil {
log.Printf("Failed to get spec - %v\n", err)
os.Exit(1)
}
fmt.Printf("Spec:\n%v\n", spec)
os.Exit(0)
default: default:
log.Fatalf("command not supported for nsinit %s", os.Args[0]) log.Fatalf("command not supported for nsinit %s", os.Args[0])
} }
@ -125,3 +147,25 @@ func startContainer(container *libcontainer.Container, term nsinit.Terminal, dat
return nsinit.Exec(container, term, "", dataPath, args, createCommand, startCallback) return nsinit.Exec(container, term, "", dataPath, args, createCommand, startCallback)
} }
// returns the container stats in json format.
func getContainerStats(container *libcontainer.Container) (string, error) {
stats, err := fs.GetStats(container.Cgroups)
if err != nil {
return "", err
}
out, err := json.MarshalIndent(stats, "", "\t")
if err != nil {
return "", err
}
return string(out), nil
}
// returns the container spec in json format.
func getContainerSpec(container *libcontainer.Container) (string, error) {
spec, err := json.MarshalIndent(container, "", "\t")
if err != nil {
return "", err
}
return string(spec), nil
}