2015-12-10 20:30:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"text/tabwriter"
|
2016-02-12 18:17:59 +00:00
|
|
|
"time"
|
2015-12-10 20:30:04 +00:00
|
|
|
|
|
|
|
"github.com/codegangsta/cli"
|
|
|
|
"github.com/docker/containerd/api/grpc/types"
|
2016-06-30 21:09:40 +00:00
|
|
|
"github.com/golang/protobuf/ptypes"
|
2015-12-10 20:30:04 +00:00
|
|
|
netcontext "golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-01-23 22:40:55 +00:00
|
|
|
var eventsCommand = cli.Command{
|
2015-12-10 20:30:04 +00:00
|
|
|
Name: "events",
|
|
|
|
Usage: "receive events from the containerd daemon",
|
2016-02-12 18:17:59 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
cli.StringFlag{
|
|
|
|
Name: "timestamp,t",
|
|
|
|
Usage: "get events from a specific time stamp in RFC3339Nano format",
|
|
|
|
},
|
|
|
|
},
|
2015-12-10 20:30:04 +00:00
|
|
|
Action: func(context *cli.Context) {
|
2016-02-12 18:17:59 +00:00
|
|
|
var (
|
2016-06-30 21:09:40 +00:00
|
|
|
t = time.Time{}
|
2016-02-12 18:17:59 +00:00
|
|
|
c = getClient(context)
|
|
|
|
)
|
|
|
|
if ts := context.String("timestamp"); ts != "" {
|
|
|
|
from, err := time.Parse(time.RFC3339Nano, ts)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-06-30 21:09:40 +00:00
|
|
|
t = from
|
|
|
|
}
|
|
|
|
tsp, err := ptypes.TimestampProto(t)
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
2016-02-12 18:17:59 +00:00
|
|
|
}
|
|
|
|
events, err := c.Events(netcontext.Background(), &types.EventsRequest{
|
2016-06-30 21:09:40 +00:00
|
|
|
Timestamp: tsp,
|
2016-02-12 18:17:59 +00:00
|
|
|
})
|
2015-12-10 20:30:04 +00:00
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-06-30 21:09:40 +00:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 31, 1, 1, ' ', 0)
|
2016-02-12 18:17:59 +00:00
|
|
|
fmt.Fprint(w, "TIME\tTYPE\tID\tPID\tSTATUS\n")
|
2015-12-10 20:30:04 +00:00
|
|
|
w.Flush()
|
|
|
|
for {
|
|
|
|
e, err := events.Recv()
|
|
|
|
if err != nil {
|
|
|
|
fatal(err.Error(), 1)
|
|
|
|
}
|
2016-06-30 21:09:40 +00:00
|
|
|
t, err := ptypes.Timestamp(e.Timestamp)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Unable to convert timestamp")
|
|
|
|
t = time.Time{}
|
|
|
|
}
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%d\n", t.Format(time.RFC3339Nano), e.Type, e.Id, e.Pid, e.Status)
|
2015-12-10 20:30:04 +00:00
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|