2014-07-31 17:21:38 -04:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
2015-03-25 03:44:12 -04:00
|
|
|
"fmt"
|
2014-07-31 17:21:38 -04:00
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2015-04-09 18:13:01 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
2014-07-31 17:21:38 -04:00
|
|
|
)
|
|
|
|
|
2015-04-09 18:13:01 -04:00
|
|
|
func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
|
|
|
if psArgs == "" {
|
2014-07-31 17:21:38 -04:00
|
|
|
psArgs = "-ef"
|
|
|
|
}
|
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
container, err := daemon.Get(name)
|
|
|
|
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-04-09 18:13:01 -04:00
|
|
|
return nil, fmt.Errorf("Container %s is not running", name)
|
2014-12-16 18:06:35 -05:00
|
|
|
}
|
2015-04-09 18:13:01 -04:00
|
|
|
|
2014-12-16 18:06:35 -05:00
|
|
|
pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID)
|
|
|
|
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-04-09 18:13:01 -04:00
|
|
|
return nil, fmt.Errorf("Error running ps: %s", 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-04-09 18:13:01 -04:00
|
|
|
return nil, fmt.Errorf("Couldn't find PID field in ps output")
|
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 _, line := range lines[1:] {
|
|
|
|
if len(line) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
fields := strings.Fields(line)
|
|
|
|
p, err := strconv.Atoi(fields[pidIndex])
|
|
|
|
if err != nil {
|
2015-04-09 18:13:01 -04:00
|
|
|
return nil, fmt.Errorf("Unexpected pid '%s': %s", 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-04-09 18:13:01 -04:00
|
|
|
return procList, nil
|
2014-07-31 17:21:38 -04:00
|
|
|
}
|