// NetIOStats get interface ios related stats // Go equivalent to sg_get_network_io_stats func (s *Stat) NetIOStats() []*NetIOStats { s.Lock() defer s.Unlock() var num_network_stats C.size_t var cArray *C.sg_network_io_stats = C.sg_get_network_io_stats_diff(&num_network_stats) length := int(num_network_stats) slice := (*[1 << 16]C.sg_network_io_stats)(unsafe.Pointer(cArray))[:length:length] var res []*NetIOStats for _, v := range slice { n := &NetIOStats{ IntName: C.GoString(v.interface_name), TX: int(v.tx), RX: int(v.rx), IPackets: int(v.ipackets), OPackets: int(v.opackets), IErrors: int(v.ierrors), OErrors: int(v.oerrors), Collisions: int(v.collisions), } res = append(res, n) } return res }
// GetNetworkIoStatsDiff returns information about I/O transfers the last call for all networks, see sg_get_network_io_stats_diff(3). func GetNetworkIoStatsDiff() ([]*NetworkIoStats, error) { var ct C.int c := C.sg_get_network_io_stats_diff(&ct) if c == nil { return nil, getError() } return asNetworkIoStatsArray(c, int(ct)), nil }
// NewStat return a new Stat handle func NewStat() *Stat { s := &Stat{} runtime.SetFinalizer(s, (*Stat).free) initDone := make(chan bool) s.exitMessage = make(chan bool) C.sg_init(1) go func() { // We need some function calls to be performed on the same thread // Those for which statgrab is using a thread local runtime.LockOSThread() defer runtime.UnlockOSThread() // Throw away the first reading as thats averaged over the machines uptime C.sg_get_cpu_stats_diff(nil) C.sg_get_network_io_stats_diff(nil) C.sg_get_page_stats_diff(nil) C.sg_get_disk_io_stats_diff(nil) initDone <- true for { select { case <-s.exitMessage: break case f := <-mainfunc: f() } } }() <-initDone return s }