net_freebsd.go 3.2 KB

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