2015-07-31 00:25:15 +03:00
|
|
|
//+build !windows
|
|
|
|
|
2014-08-01 01:21:38 +04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-09-16 21:56:26 +03:00
|
|
|
derr "github.com/docker/docker/api/errors"
|
2015-04-10 01:13:01 +03:00
|
|
|
"github.com/docker/docker/api/types"
|
2014-08-01 01:21:38 +04:00
|
|
|
)
|
|
|
|
|
2015-07-31 00:01:53 +03: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-04-10 01:13:01 +03:00
|
|
|
func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
|
|
|
if psArgs == "" {
|
2014-08-01 01:21:38 +04:00
|
|
|
psArgs = "-ef"
|
|
|
|
}
|
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
if err != nil {
|
2015-04-10 01:13:01 +03:00
|
|
|
return nil, err
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2015-04-10 01:13:01 +03:00
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
if !container.IsRunning() {
|
2015-09-16 21:56:26 +03:00
|
|
|
return nil, derr.ErrorCodeNotRunning.WithArgs(name)
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2015-04-10 01:13:01 +03:00
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID)
|
|
|
|
if err != nil {
|
2015-04-10 01:13:01 +03:00
|
|
|
return nil, err
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2015-04-10 01:13:01 +03:00
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
|
|
|
|
if err != nil {
|
2015-09-16 21:56:26 +03:00
|
|
|
return nil, derr.ErrorCodePSError.WithArgs(err)
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2014-08-01 01:21:38 +04:00
|
|
|
|
2015-04-10 01:13:01 +03:00
|
|
|
procList := &types.ContainerProcessList{}
|
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
lines := strings.Split(string(output), "\n")
|
2015-04-10 01:13:01 +03:00
|
|
|
procList.Titles = strings.Fields(lines[0])
|
2014-08-01 01:21:38 +04:00
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
pidIndex := -1
|
2015-04-10 01:13:01 +03:00
|
|
|
for i, name := range procList.Titles {
|
2014-12-17 02:06:35 +03:00
|
|
|
if name == "PID" {
|
|
|
|
pidIndex = i
|
2014-08-01 01:21:38 +04:00
|
|
|
}
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
|
|
|
if pidIndex == -1 {
|
2015-09-16 21:56:26 +03:00
|
|
|
return nil, derr.ErrorCodeNoPID
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2014-08-01 01:21:38 +04:00
|
|
|
|
2015-07-31 00:01:53 +03:00
|
|
|
// loop through the output and extract the PID from each line
|
2014-12-17 02:06:35 +03: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 21:56:26 +03:00
|
|
|
return nil, derr.ErrorCodeBadPID.WithArgs(fields[pidIndex], err)
|
2014-12-17 02:06:35 +03:00
|
|
|
}
|
2014-08-01 01:21:38 +04:00
|
|
|
|
2014-12-17 02:06:35 +03:00
|
|
|
for _, pid := range pids {
|
|
|
|
if pid == p {
|
|
|
|
// Make sure number of fields equals number of header titles
|
|
|
|
// merging "overhanging" fields
|
2015-04-10 01:13:01 +03:00
|
|
|
process := fields[:len(procList.Titles)-1]
|
|
|
|
process = append(process, strings.Join(fields[len(procList.Titles)-1:], " "))
|
|
|
|
procList.Processes = append(procList.Processes, process)
|
2014-08-01 01:21:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-31 00:01:53 +03:00
|
|
|
container.logEvent("top")
|
2015-04-10 01:13:01 +03:00
|
|
|
return procList, nil
|
2014-08-01 01:21:38 +04:00
|
|
|
}
|