|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "text/tabwriter" |
| 9 | + |
| 10 | + "github.com/gepis/ps" |
| 11 | + "github.com/sirupsen/logrus" |
| 12 | +) |
| 13 | + |
| 14 | +func main() { |
| 15 | + var ( |
| 16 | + descriptors []string |
| 17 | + pidsList []string |
| 18 | + data [][]string |
| 19 | + err error |
| 20 | + |
| 21 | + pids = flag.String("pids", "", "comma separated list of process IDs to retrieve") |
| 22 | + format = flag.String("format", "", "ps(1) AIX format comma-separated string") |
| 23 | + list = flag.Bool("list", false, "list all supported descriptors") |
| 24 | + join = flag.Bool("join", false, "join namespace of provided pids (containers)") |
| 25 | + fillMappings = flag.Bool("fill-mappings", false, "fill the UID and GID mappings with the current user namespace") |
| 26 | + ) |
| 27 | + |
| 28 | + flag.Parse() |
| 29 | + |
| 30 | + if *fillMappings && !*join { |
| 31 | + fmt.Fprintln(os.Stderr, "-fill-mappings requires -join") |
| 32 | + os.Exit(1) |
| 33 | + } |
| 34 | + |
| 35 | + if *list { |
| 36 | + fmt.Println(strings.Join(ps.ListDescriptors(), ", ")) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + if *format != "" { |
| 41 | + descriptors = strings.Split(*format, ",") |
| 42 | + } |
| 43 | + |
| 44 | + if *pids != "" { |
| 45 | + pidsList = strings.Split(*pids, ",") |
| 46 | + } |
| 47 | + |
| 48 | + if len(pidsList) > 0 { |
| 49 | + opts := ps.JoinNamespaceOpts{FillMappings: *fillMappings} |
| 50 | + |
| 51 | + if *join { |
| 52 | + data, err = ps.JoinNamespaceAndProcessInfoByPidsWithOptions(pidsList, descriptors, &opts) |
| 53 | + } else { |
| 54 | + data, err = ps.ProcessInfoByPids(pidsList, descriptors) |
| 55 | + } |
| 56 | + |
| 57 | + if err != nil { |
| 58 | + logrus.Panic(err) |
| 59 | + } |
| 60 | + } else { |
| 61 | + data, err = ps.ProcessInfo(descriptors) |
| 62 | + if err != nil { |
| 63 | + logrus.Panic(err) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + tw := tabwriter.NewWriter(os.Stdout, 5, 1, 3, ' ', 0) |
| 68 | + for _, d := range data { |
| 69 | + fmt.Fprintln(tw, strings.Join(d, "\t")) |
| 70 | + } |
| 71 | + |
| 72 | + tw.Flush() |
| 73 | +} |
0 commit comments