...

Source file src/github.com/shirou/gopsutil/net/net_freebsd.go

Documentation: github.com/shirou/gopsutil/net

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

View as plain text