Signed-off-by: John Howard <jhoward@microsoft.com>
| 1 | 1 |
deleted file mode 100644 |
| ... | ... |
@@ -1,73 +0,0 @@ |
| 1 |
-package daemon |
|
| 2 |
- |
|
| 3 |
-import ( |
|
| 4 |
- "fmt" |
|
| 5 |
- "os/exec" |
|
| 6 |
- "strconv" |
|
| 7 |
- "strings" |
|
| 8 |
- |
|
| 9 |
- "github.com/docker/docker/api/types" |
|
| 10 |
-) |
|
| 11 |
- |
|
| 12 |
-func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
|
| 13 |
- if psArgs == "" {
|
|
| 14 |
- psArgs = "-ef" |
|
| 15 |
- } |
|
| 16 |
- |
|
| 17 |
- container, err := daemon.Get(name) |
|
| 18 |
- if err != nil {
|
|
| 19 |
- return nil, err |
|
| 20 |
- } |
|
| 21 |
- |
|
| 22 |
- if !container.IsRunning() {
|
|
| 23 |
- return nil, fmt.Errorf("Container %s is not running", name)
|
|
| 24 |
- } |
|
| 25 |
- |
|
| 26 |
- pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID) |
|
| 27 |
- if err != nil {
|
|
| 28 |
- return nil, err |
|
| 29 |
- } |
|
| 30 |
- |
|
| 31 |
- output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
|
|
| 32 |
- if err != nil {
|
|
| 33 |
- return nil, fmt.Errorf("Error running ps: %s", err)
|
|
| 34 |
- } |
|
| 35 |
- |
|
| 36 |
- procList := &types.ContainerProcessList{}
|
|
| 37 |
- |
|
| 38 |
- lines := strings.Split(string(output), "\n") |
|
| 39 |
- procList.Titles = strings.Fields(lines[0]) |
|
| 40 |
- |
|
| 41 |
- pidIndex := -1 |
|
| 42 |
- for i, name := range procList.Titles {
|
|
| 43 |
- if name == "PID" {
|
|
| 44 |
- pidIndex = i |
|
| 45 |
- } |
|
| 46 |
- } |
|
| 47 |
- if pidIndex == -1 {
|
|
| 48 |
- return nil, fmt.Errorf("Couldn't find PID field in ps output")
|
|
| 49 |
- } |
|
| 50 |
- |
|
| 51 |
- for _, line := range lines[1:] {
|
|
| 52 |
- if len(line) == 0 {
|
|
| 53 |
- continue |
|
| 54 |
- } |
|
| 55 |
- fields := strings.Fields(line) |
|
| 56 |
- p, err := strconv.Atoi(fields[pidIndex]) |
|
| 57 |
- if err != nil {
|
|
| 58 |
- return nil, fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err)
|
|
| 59 |
- } |
|
| 60 |
- |
|
| 61 |
- for _, pid := range pids {
|
|
| 62 |
- if pid == p {
|
|
| 63 |
- // Make sure number of fields equals number of header titles |
|
| 64 |
- // merging "overhanging" fields |
|
| 65 |
- process := fields[:len(procList.Titles)-1] |
|
| 66 |
- process = append(process, strings.Join(fields[len(procList.Titles)-1:], " ")) |
|
| 67 |
- procList.Processes = append(procList.Processes, process) |
|
| 68 |
- } |
|
| 69 |
- } |
|
| 70 |
- } |
|
| 71 |
- container.LogEvent("top")
|
|
| 72 |
- return procList, nil |
|
| 73 |
-} |
| 74 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,75 @@ |
| 0 |
+//+build !windows |
|
| 1 |
+ |
|
| 2 |
+package daemon |
|
| 3 |
+ |
|
| 4 |
+import ( |
|
| 5 |
+ "fmt" |
|
| 6 |
+ "os/exec" |
|
| 7 |
+ "strconv" |
|
| 8 |
+ "strings" |
|
| 9 |
+ |
|
| 10 |
+ "github.com/docker/docker/api/types" |
|
| 11 |
+) |
|
| 12 |
+ |
|
| 13 |
+func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
|
| 14 |
+ if psArgs == "" {
|
|
| 15 |
+ psArgs = "-ef" |
|
| 16 |
+ } |
|
| 17 |
+ |
|
| 18 |
+ container, err := daemon.Get(name) |
|
| 19 |
+ if err != nil {
|
|
| 20 |
+ return nil, err |
|
| 21 |
+ } |
|
| 22 |
+ |
|
| 23 |
+ if !container.IsRunning() {
|
|
| 24 |
+ return nil, fmt.Errorf("Container %s is not running", name)
|
|
| 25 |
+ } |
|
| 26 |
+ |
|
| 27 |
+ pids, err := daemon.ExecutionDriver().GetPidsForContainer(container.ID) |
|
| 28 |
+ if err != nil {
|
|
| 29 |
+ return nil, err |
|
| 30 |
+ } |
|
| 31 |
+ |
|
| 32 |
+ output, err := exec.Command("ps", strings.Split(psArgs, " ")...).Output()
|
|
| 33 |
+ if err != nil {
|
|
| 34 |
+ return nil, fmt.Errorf("Error running ps: %s", err)
|
|
| 35 |
+ } |
|
| 36 |
+ |
|
| 37 |
+ procList := &types.ContainerProcessList{}
|
|
| 38 |
+ |
|
| 39 |
+ lines := strings.Split(string(output), "\n") |
|
| 40 |
+ procList.Titles = strings.Fields(lines[0]) |
|
| 41 |
+ |
|
| 42 |
+ pidIndex := -1 |
|
| 43 |
+ for i, name := range procList.Titles {
|
|
| 44 |
+ if name == "PID" {
|
|
| 45 |
+ pidIndex = i |
|
| 46 |
+ } |
|
| 47 |
+ } |
|
| 48 |
+ if pidIndex == -1 {
|
|
| 49 |
+ return nil, fmt.Errorf("Couldn't find PID field in ps output")
|
|
| 50 |
+ } |
|
| 51 |
+ |
|
| 52 |
+ for _, line := range lines[1:] {
|
|
| 53 |
+ if len(line) == 0 {
|
|
| 54 |
+ continue |
|
| 55 |
+ } |
|
| 56 |
+ fields := strings.Fields(line) |
|
| 57 |
+ p, err := strconv.Atoi(fields[pidIndex]) |
|
| 58 |
+ if err != nil {
|
|
| 59 |
+ return nil, fmt.Errorf("Unexpected pid '%s': %s", fields[pidIndex], err)
|
|
| 60 |
+ } |
|
| 61 |
+ |
|
| 62 |
+ for _, pid := range pids {
|
|
| 63 |
+ if pid == p {
|
|
| 64 |
+ // Make sure number of fields equals number of header titles |
|
| 65 |
+ // merging "overhanging" fields |
|
| 66 |
+ process := fields[:len(procList.Titles)-1] |
|
| 67 |
+ process = append(process, strings.Join(fields[len(procList.Titles)-1:], " ")) |
|
| 68 |
+ procList.Processes = append(procList.Processes, process) |
|
| 69 |
+ } |
|
| 70 |
+ } |
|
| 71 |
+ } |
|
| 72 |
+ container.LogEvent("top")
|
|
| 73 |
+ return procList, nil |
|
| 74 |
+} |
| 0 | 75 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,11 @@ |
| 0 |
+package daemon |
|
| 1 |
+ |
|
| 2 |
+import ( |
|
| 3 |
+ "fmt" |
|
| 4 |
+ |
|
| 5 |
+ "github.com/docker/docker/api/types" |
|
| 6 |
+) |
|
| 7 |
+ |
|
| 8 |
+func (daemon *Daemon) ContainerTop(name string, psArgs string) (*types.ContainerProcessList, error) {
|
|
| 9 |
+ return nil, fmt.Errorf("Top is not supported on Windows")
|
|
| 10 |
+} |