sample.go 1.7 KB

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