process.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. package linux
  2. import (
  3. "os"
  4. "path/filepath"
  5. "strconv"
  6. )
  7. type Process struct {
  8. Status ProcessStatus `json:"status"`
  9. Statm ProcessStatm `json:"statm"`
  10. Stat ProcessStat `json:"stat"`
  11. IO ProcessIO `json:"io"`
  12. Cmdline string `json:"cmdline"`
  13. }
  14. func ReadProcess(pid uint64, path string) (*Process, error) {
  15. var err error
  16. p := filepath.Join(path, strconv.FormatUint(pid, 10))
  17. if _, err = os.Stat(p); err != nil {
  18. return nil, err
  19. }
  20. process := Process{}
  21. var io *ProcessIO
  22. var stat *ProcessStat
  23. var statm *ProcessStatm
  24. var status *ProcessStatus
  25. var cmdline string
  26. if io, err = ReadProcessIO(filepath.Join(p, "io")); err != nil {
  27. return nil, err
  28. }
  29. if stat, err = ReadProcessStat(filepath.Join(p, "stat")); err != nil {
  30. return nil, err
  31. }
  32. if statm, err = ReadProcessStatm(filepath.Join(p, "statm")); err != nil {
  33. return nil, err
  34. }
  35. if status, err = ReadProcessStatus(filepath.Join(p, "status")); err != nil {
  36. return nil, err
  37. }
  38. if cmdline, err = ReadProcessCmdline(filepath.Join(p, "cmdline")); err != nil {
  39. return nil, err
  40. }
  41. process.IO = *io
  42. process.Stat = *stat
  43. process.Statm = *statm
  44. process.Status = *status
  45. process.Cmdline = cmdline
  46. return &process, nil
  47. }