net_freebsd.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // +build freebsd
  2. package net
  3. import (
  4. "context"
  5. "os/exec"
  6. "strconv"
  7. "strings"
  8. "github.com/shirou/gopsutil/internal/common"
  9. )
  10. func IOCounters(pernic bool) ([]IOCountersStat, error) {
  11. return IOCountersWithContext(context.Background(), pernic)
  12. }
  13. func IOCountersWithContext(ctx context.Context, pernic bool) ([]IOCountersStat, error) {
  14. netstat, err := exec.LookPath("netstat")
  15. if err != nil {
  16. return nil, err
  17. }
  18. out, err := invoke.CommandWithContext(ctx, netstat, "-ibdnW")
  19. if err != nil {
  20. return nil, err
  21. }
  22. lines := strings.Split(string(out), "\n")
  23. ret := make([]IOCountersStat, 0, len(lines)-1)
  24. exists := make([]string, 0, len(ret))
  25. for _, line := range lines {
  26. values := strings.Fields(line)
  27. if len(values) < 1 || values[0] == "Name" {
  28. continue
  29. }
  30. if common.StringsHas(exists, values[0]) {
  31. // skip if already get
  32. continue
  33. }
  34. exists = append(exists, values[0])
  35. if len(values) < 12 {
  36. continue
  37. }
  38. base := 1
  39. // sometimes Address is omitted
  40. if len(values) < 13 {
  41. base = 0
  42. }
  43. parsed := make([]uint64, 0, 8)
  44. vv := []string{
  45. values[base+3], // PacketsRecv
  46. values[base+4], // Errin
  47. values[base+5], // Dropin
  48. values[base+6], // BytesRecvn
  49. values[base+7], // PacketSent
  50. values[base+8], // Errout
  51. values[base+9], // BytesSent
  52. values[base+11], // Dropout
  53. }
  54. for _, target := range vv {
  55. if target == "-" {
  56. parsed = append(parsed, 0)
  57. continue
  58. }
  59. t, err := strconv.ParseUint(target, 10, 64)
  60. if err != nil {
  61. return nil, err
  62. }
  63. parsed = append(parsed, t)
  64. }
  65. n := IOCountersStat{
  66. Name: values[0],
  67. PacketsRecv: parsed[0],
  68. Errin: parsed[1],
  69. Dropin: parsed[2],
  70. BytesRecv: parsed[3],
  71. PacketsSent: parsed[4],
  72. Errout: parsed[5],
  73. BytesSent: parsed[6],
  74. Dropout: parsed[7],
  75. }
  76. ret = append(ret, n)
  77. }
  78. if pernic == false {
  79. return getIOCountersAll(ret)
  80. }
  81. return ret, nil
  82. }
  83. // NetIOCountersByFile is an method which is added just a compatibility for linux.
  84. func IOCountersByFile(pernic bool, filename string) ([]IOCountersStat, error) {
  85. return IOCountersByFileWithContext(context.Background(), pernic, filename)
  86. }
  87. func IOCountersByFileWithContext(ctx context.Context, pernic bool, filename string) ([]IOCountersStat, error) {
  88. return IOCounters(pernic)
  89. }
  90. func FilterCounters() ([]FilterStat, error) {
  91. return FilterCountersWithContext(context.Background())
  92. }
  93. func FilterCountersWithContext(ctx context.Context) ([]FilterStat, error) {
  94. return nil, common.ErrNotImplementedError
  95. }
  96. func ConntrackStats(percpu bool) ([]ConntrackStat, error) {
  97. return ConntrackStatsWithContext(context.Background(), percpu)
  98. }
  99. func ConntrackStatsWithContext(ctx context.Context, percpu bool) ([]ConntrackStat, error) {
  100. return nil, common.ErrNotImplementedError
  101. }
  102. // NetProtoCounters returns network statistics for the entire system
  103. // If protocols is empty then all protocols are returned, otherwise
  104. // just the protocols in the list are returned.
  105. // Not Implemented for FreeBSD
  106. func ProtoCounters(protocols []string) ([]ProtoCountersStat, error) {
  107. return ProtoCountersWithContext(context.Background(), protocols)
  108. }
  109. func ProtoCountersWithContext(ctx context.Context, protocols []string) ([]ProtoCountersStat, error) {
  110. return nil, common.ErrNotImplementedError
  111. }