func NetIOCountersStat_f() ([]NetIOCountersStat_j, error) { network, _ := net.NetIOCounters(true) time.Sleep(time.Second * 1) networknew, _ := net.NetIOCounters(true) timestamp := time.Now().Unix() var ret []NetIOCountersStat_j // fmt.Println(mem) var info NetIOCountersStat_j for i, v := range networknew { info.Name = v.Name info.BytesSent = v.BytesSent info.BytesRecv = v.BytesRecv info.SpeedSent = v.BytesSent - network[i].BytesSent info.SpeedRecv = v.BytesRecv - network[i].BytesRecv info.PacketsSent = v.PacketsSent info.PacketsRecv = v.PacketsRecv info.Errin = v.Errin info.Errout = v.Errout info.Dropin = v.Dropin info.Dropout = v.Dropout info.Timestamp = timestamp ret = append(ret, info) } return ret, nil }
func (node mgmNode) collectHostStatistics(out chan mgm.HostStat) { for { //start calculating network sent fInet, err := psnet.NetIOCounters(false) if err != nil { node.logger.Error("Error reading networking", err) } s := mgm.HostStat{} s.Running = true c, err := pscpu.CPUPercent(time.Second, true) if err != nil { node.logger.Error("Error readin CPU: ", err) } s.CPUPercent = c v, err := psmem.VirtualMemory() if err != nil { node.logger.Error("Error reading Memory", err) } s.MEMTotal = v.Total / 1000 s.MEMUsed = (v.Total - v.Available) / 1000 s.MEMPercent = v.UsedPercent lInet, err := psnet.NetIOCounters(false) if err != nil { node.logger.Error("Error reading networking", err) } s.NetSent = (lInet[0].BytesSent - fInet[0].BytesSent) s.NetRecv = (lInet[0].BytesRecv - fInet[0].BytesRecv) out <- s } }
/** read node resource usage **/ func GetNodeResource(w http.ResponseWriter, r *http.Request) { // get this node memory memory, _ := mem.VirtualMemory() // get this node cpu percent usage cpu_percent, _ := cpu.CPUPercent(time.Duration(1)*time.Second, false) // Disk mount Point disk_partitions, _ := disk.DiskPartitions(true) // Disk usage var disk_usages []*disk.DiskUsageStat for _, disk_partition := range disk_partitions { if disk_partition.Mountpoint == "/" || disk_partition.Mountpoint == "/home" { disk_stat, _ := disk.DiskUsage(disk_partition.Device) disk_usages = append(disk_usages, disk_stat) } } // Network network, _ := net.NetIOCounters(false) // create new node obj with resource usage information node_metric := thoth.NodeMetric{ Cpu: cpu_percent, Memory: memory, DiskUsage: disk_usages, Network: network, } node_json, err := json.MarshalIndent(node_metric, "", "\t") if err != nil { fmt.Println("error:", err) } fmt.Fprint(w, string(node_json)) }
//NewMonitoringData creates and fill a monitoringData func NewMonitoringData(updateTime time.Duration) (monitorData *monitoringData, err error) { returnMonitoringData := monitoringData{} //Time info returnMonitoringData.Time = time.Now().UnixNano() returnMonitoringData.UpdatePeriod = updateTime //Host info err = initHostInfo(&returnMonitoringData) //Memory info err = initMemoryInfo(&returnMonitoringData) //CPU info err = initCPUInfo(&returnMonitoringData) //Network info netIOCounter, err := net.NetIOCounters(true) if netIOCounter != nil { returnMonitoringData.NetworkIOCounter = netIOCounter } //Disk info err = initDiskInfo(&returnMonitoringData) return &returnMonitoringData, nil }
func main() { netIOCounterSt, err := net.NetIOCounters(true) if err != nil { log.Panicln(err) } for _, netIOSt := range netIOCounterSt { log.Println(netIOSt) } netProtoCounters, err := net.NetProtoCounters(nil) if err != nil { log.Panicln(err) } for _, netProtoSt := range netProtoCounters { log.Println(netProtoSt) } netConnectionStat, err := net.NetConnections("all") if err != nil { log.Panicln(err) } for _, netConnSt := range netConnectionStat { log.Println(netConnSt) } }
// NetworkUsage - list func NetworkUsage() (NetworkUsageList, error) { netio, _ := net.NetIOCounters(true) time.Sleep(1000 * time.Millisecond) // Sleep 1 second to get kb/s netioSecondRun, _ := net.NetIOCounters(true) ifaces, _ := net.NetInterfaces() var usage NetworkUsageList var validInterfaces []string for _, iface := range ifaces { if !stringInSlice("loopback", iface.Flags) { validInterfaces = append(validInterfaces, iface.Name) } } for _, io := range netio { if stringInSlice(io.Name, validInterfaces) { for _, lastio := range netioSecondRun { if lastio.Name == io.Name { Inbound := lastio.BytesRecv - io.BytesRecv InboundKB, _ := util.ConvertBytesTo(Inbound, "kb", 0) Outbound := lastio.BytesSent - io.BytesSent OutboundKB, _ := util.ConvertBytesTo(Outbound, "kb", 0) n := NetworkStruct{ Name: io.Name, Inbound: InboundKB, Outbound: OutboundKB, } usage = append(usage, n) } } } } return usage, nil }
func network() string { a, err := _net.NetIOCounters(true) if err != nil { log.Println(err) } up := a[0].BytesSent - bytesSent down := a[0].BytesRecv - bytesRecv bytesSent = a[0].BytesSent bytesRecv = a[0].BytesRecv return fmt.Sprintf("\x0b\uF280 %6s/s \uF53E %6s/s ", humanize.Bytes(down), humanize.Bytes(up)) }
// Run gathers network IO data from gopsutil. func (n *NetIO) Run() error { dataSlice, err := gopsutil_net.NetIOCounters(true) if err != nil { return err } for _, data := range dataSlice { n.Data[data.Name] = data } return nil }
func netIOCounters(ns []string) (*plugin.PluginMetricType, error) { nets, err := net.NetIOCounters(true) if err != nil { return nil, err } for _, net := range nets { switch { case regexp.MustCompile(`^/intel/psutil/net/.*/bytes_sent$`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.BytesSent, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/bytes_recv`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.BytesRecv, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/packets_sent`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.BytesSent, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/packets_recv`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.BytesRecv, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/errin`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.Errin, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/errout`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.Errout, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/dropin`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.Dropin, }, nil case regexp.MustCompile(`^/intel/psutil/net/.*/dropout`).MatchString(joinNamespace(ns)): return &plugin.PluginMetricType{ Namespace_: ns, Data_: net.Dropout, }, nil } } return nil, fmt.Errorf("Unknown error processing %v", ns) }
func getNetIOCounterMetricTypes() ([]plugin.PluginMetricType, error) { mts := make([]plugin.PluginMetricType, 0) nets, err := net.NetIOCounters(false) if err != nil { return nil, err } //total for all nics for _, name := range netIOCounterLabels { mts = append(mts, plugin.PluginMetricType{Namespace_: []string{"intel", "psutil", "net", nets[0].Name, name}}) } //per nic nets, err = net.NetIOCounters(true) if err != nil { return nil, err } for _, net := range nets { for _, name := range netIOCounterLabels { mts = append(mts, plugin.PluginMetricType{Namespace_: []string{"intel", "psutil", "net", net.Name, name}}) } } return mts, nil }
func (s *systemPS) NetIO() ([]net.NetIOCountersStat, error) { return net.NetIOCounters(true) }