2015-07-30 17:25:15 -04:00
|
|
|
//+build !windows
|
|
|
|
|
2014-07-31 17:21:38 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-09-17 14:54:14 -04:00
|
|
|
derr "github.com/docker/docker/errors"
|
2016-01-04 19:05:26 -05:00
|
|
|
"github.com/docker/engine-api/types"
|
2014-07-31 17:21:38 -04:00
|
|
|
)
|
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// ContainerTop lists the processes running inside of the given
|
|
|
|
// container by calling ps with the given args, or with the flags
|
|
|
|
// "-ef" if no args are given. An error is returned if the container
|
|
|
|
// is not found, or is not running, or if there are any problems
|
|
|
|
// running ps, or parsing the output.
|
2015-09-29 13:51:40 -04:00
|
|
|
func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
2015-04-09 18:13:01 -04:00
|
|
|
if psArgs == "" {
|
2014-07-31 17:21:38 -04:00
|
|
|
psArgs = "-ef"
|
|
|
|
}
|
|
|
|
|
2015-12-11 12:39:28 -05:00
|
|
|
container, err := daemon.GetContainer(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-04-09 18:13:01 -04:00
|
|
|
return nil, err
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
if !container.IsRunning() {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeNotRunning.WithArgs(name)
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2015-09-29 13:51:40 -04:00
|
|
|
pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID)
|
2014-12-16 18:06:35 -05:00
|
|
|
if err != nil {
|
2015-04-09 18:13:01 -04:00
|
|
|
return nil, err
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
|
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodePSError.WithArgs(err)
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2014-07-31 17:21:38 -04:00
|
|
|
|
2015-04-09 18:13:01 -04:00
|
|
|
procList := &types.ContainerProcessList{}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
lines := strings.Split(string(output), "\n")
|
2015-04-09 18:13:01 -04:00
|
|
|
procList.Titles = strings.Fields(lines[0])
|
2014-07-31 17:21:38 -04:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
pidIndex := -1
|
2015-04-09 18:13:01 -04:00
|
|
|
for i, name := range procList.Titles {
|
2014-12-16 18:06:35 -05:00
|
|
|
if name == "PID" {
|
|
|
|
pidIndex = i
|
2014-07-31 17:21:38 -04:00
|
|
|
}
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
|
|
|
if pidIndex == -1 {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeNoPID
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2014-07-31 17:21:38 -04:00
|
|
|
|
2015-07-30 17:01:53 -04:00
|
|
|
// loop through the output and extract the PID from each line
|
2014-12-16 18:06:35 -05:00
|
|
|
for _, line := range lines[1:] {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
p, err := strconv.Atoi(fields[pidIndex])
|
|
|
|
if err != nil {
|
2015-09-16 14:56:26 -04:00
|
|
|
return nil, derr.ErrorCodeBadPID.WithArgs(fields[pidIndex], err)
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2014-07-31 17:21:38 -04:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
for _, pid := range pids {
|
|
|
|
if pid == p {
|
|
|
|
// Make sure number of fields equals number of header titles
|
|
|
|
// merging "overhanging" fields
|
2015-04-09 18:13:01 -04:00
|
|
|
process := fields[:len(procList.Titles)-1]
|
|
|
|
process = append(process, strings.Join(fields[len(procList.Titles)-1:], " "))
|
|
|
|
procList.Processes = append(procList.Processes, process)
|
2014-07-31 17:21:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-03 12:33:13 -05:00
|
|
|
daemon.LogContainerEvent(container, "top")
|
2015-04-09 18:13:01 -04:00
|
|
|
return procList, nil
|
2014-07-31 17:21:38 -04:00
|
|
|
}
|