// Run gathers network interfaces data from gopsutil. func (n *NetInterfaces) Run() error { dataSlice, err := gopsutil_net.NetInterfaces() if err != nil { return err } for _, data := range dataSlice { n.Data[data.Name] = data } return nil }
// 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 main() { header, _ := json.Marshal(Header{Version: 1}) fmt.Println(string(header)) fmt.Println("[") go notifyd() for { if mode == "standard" { line := make([]Block, 0) // TIME block t := time.Now() const t_format = "2006-01-02 15:04:05" time_block := Block{ Name: "time", FullText: t.Format(t_format), Color: GRAY, } // LOAD block load, _ := load.LoadAvg() load_block := Block{ Name: "load", FullText: strconv.FormatFloat(load.Load1, 'f', 2, 64), } if load.Load1 > 2 { load_block.Color = BAD } else if load.Load1 > 1 { load_block.Color = WARN } else { load_block.Color = OKAY } // NET block net_blocks := make([]Block, 0) interfaces, _ := net.NetInterfaces() for _, iface := range interfaces { if iface.Name == "lo" { continue } text := iface.Addrs[0].Addr net_blocks = append(net_blocks, Block{ Name: "iface", FullText: text, Instance: iface.Name, Color: GRAY, }) } // HDD block root_stat, _ := disk.DiskUsage("/") home_stat, _ := disk.DiskUsage("/home") data_stat, _ := disk.DiskUsage("/media/me/data") root_block := Block{ Name: "hdd", Instance: "root", FullText: FormatGigabytes(root_stat.Free) + "GB", } if root_stat.Free > 5*1024*1024*1024 { root_block.Color = OKAY } else if root_stat.Free > 2.5*1024*1024*1024 { root_block.Color = WARN } else { root_block.Color = BAD } home_block := Block{ Name: "hdd", Instance: "root", FullText: FormatGigabytes(home_stat.Free) + "GB", } if home_stat.Free > 20*1024*1024*1024 { home_block.Color = OKAY } else if home_stat.Free > 10*1024*1024*1024 { home_block.Color = WARN } else { home_block.Color = BAD } data_block := Block{ Name: "hdd", Instance: "data", FullText: FormatGigabytes(data_stat.Free) + "GB", } if data_stat.Free > 30*1024*1024*1024 { data_block.Color = OKAY } else if data_stat.Free > 15*1024*1024*1024 { data_block.Color = WARN } else { data_block.Color = BAD } /* // Headphones block headphones := Block { Name: "headphones", FullText: "Headphones ", } if exec.Command("sh", "-c", "pacmd list-sinks | grep DR-BTN200").Run() == nil { headphones.FullText += "connected"; headphones.Color = OKAY; } else { headphones.FullText += "disconnected"; headphones.Color = BAD; } */ nowplaying := Block{ Name: "nowplaying", } nowplaying_text, _ := exec.Command("nowplaying").Output() nowplaying.FullText = string(nowplaying_text) line = append(line, nowplaying) //line = append(line, headphones); line = append(line, root_block) line = append(line, home_block) line = append(line, data_block) /*for _, block := range net_blocks { line = append(line, block) }*/ line = append(line, load_block) line = append(line, time_block) PrintLine(line) time.Sleep(time.Second * 1) } else if mode == "animation" { text := anim_text[0:anim_frame] anim_frame++ PrintLine([]Block{Block{Name: "anim", FullText: text, Instance: strconv.Itoa(anim_frame)}}) if anim_frame > len(anim_text) { mode = "standard" anim_frame = 0 time.Sleep(time.Second * 3) } else { time.Sleep(time.Millisecond * time.Duration(math.Max(35, float64(40-len(anim_text))))) } } } }