Add container status and container runtime logs

pull/4960/head
tstromberg 2019-08-02 14:36:46 -07:00
parent f872767afe
commit 2dd3445176
6 changed files with 26 additions and 2 deletions

View File

@ -97,5 +97,5 @@ var logsCmd = &cobra.Command{
func init() {
logsCmd.Flags().BoolVarP(&followLogs, "follow", "f", false, "Show only the most recent journal entries, and continuously print new entries as they are appended to the journal.")
logsCmd.Flags().BoolVar(&showProblems, "problems", false, "Show only log entries which point to known problems")
logsCmd.Flags().IntVarP(&numberOfLines, "length", "n", 50, "Number of lines back to go within the log")
logsCmd.Flags().IntVarP(&numberOfLines, "length", "n", 30, "Number of lines back to go within the log")
}

View File

@ -136,3 +136,8 @@ func (r *Containerd) StopContainers(ids []string) error {
func (r *Containerd) ContainerLogCmd(id string, len int, follow bool) string {
return criContainerLogCmd(id, len, follow)
}
// SystemLogCmd returns the command to retrieve system logs
func (r *Containerd) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u containerd -n %d", len)
}

View File

@ -133,3 +133,8 @@ func (r *CRIO) StopContainers(ids []string) error {
func (r *CRIO) ContainerLogCmd(id string, len int, follow bool) string {
return criContainerLogCmd(id, len, follow)
}
// SystemLogCmd returns the command to retrieve system logs
func (r *CRIO) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u crio -n %d", len)
}

View File

@ -66,6 +66,8 @@ type Manager interface {
StopContainers([]string) error
// ContainerLogCmd returns the command to retrieve the log for a container based on ID
ContainerLogCmd(string, int, bool) string
// SystemLogCmd returns the command to return the system logs
SystemLogCmd(int) string
}
// Config is runtime configuration

View File

@ -153,3 +153,8 @@ func (r *Docker) ContainerLogCmd(id string, len int, follow bool) string {
cmd.WriteString(id)
return cmd.String()
}
// SystemLogCmd returns the command to retrieve system logs
func (r *Docker) SystemLogCmd(len int) string {
return fmt.Sprintf("sudo journalctl -u docker -n %d", len)
}

View File

@ -117,12 +117,14 @@ func Output(r cruntime.Manager, bs bootstrapper.Bootstrapper, runner command.Run
// These are not technically logs, but are useful to have in bug reports.
cmds["kernel"] = "uptime && uname -a"
cmds := logCommands(r, bs, lines, false)
cmds["kernel"] = "uptime && uname -a && grep PRETTY /etc/os-release"
names := []string{}
for k := range cmds {
names = append(names, k)
}
sort.Strings(names)
sort.Strings(names)
failed := []string{}
for i, name := range names {
if i > 0 {
@ -130,6 +132,7 @@ func Output(r cruntime.Manager, bs bootstrapper.Bootstrapper, runner command.Run
}
out.T(out.Empty, "==> {{.name}} <==", out.V{"name": name})
var b bytes.Buffer
err := runner.CombinedOutputTo(cmds[name], &b)
if err != nil {
glog.Errorf("failed: %v", err)
@ -141,6 +144,7 @@ func Output(r cruntime.Manager, bs bootstrapper.Bootstrapper, runner command.Run
out.T(out.Empty, scanner.Text())
}
}
if len(failed) > 0 {
return fmt.Errorf("unable to fetch logs for: %s", strings.Join(failed, ", "))
}
@ -163,5 +167,8 @@ func logCommands(r cruntime.Manager, bs bootstrapper.Bootstrapper, length int, f
}
cmds[pod] = r.ContainerLogCmd(ids[0], length, follow)
}
cmds[r.Name()] = r.SystemLogCmd(length)
// Works across container runtimes with good formatting
cmds["container status"] = "sudo crictl ps -a"
return cmds
}