func GetInfo() map[string]interface{} { path := "/" if runtime.GOOS == "windows" { file, _ := exec.LookPath(os.Args[0]) path = filepath.VolumeName(file) } diskinfo := disk.UsageStat{} if v, err := disk.Usage(path); err == nil { var i interface{} i = v switch inst := i.(type) { case disk.UsageStat: diskinfo = inst break case *disk.UsageStat: if inst != nil { diskinfo = *inst } break } } diskinfo.Total /= 1024 * 1024 diskinfo.Used /= 1024 * 1024 vm := mem.VirtualMemoryStat{} sm := mem.SwapMemoryStat{} if v, err := mem.VirtualMemory(); err == nil { vm = *v } vm.Total /= 1024 * 1024 vm.Used /= 1024 * 1024 if v, err := mem.SwapMemory(); err == nil { sm = *v } sm.Total /= 1024 * 1024 sm.Used /= 1024 * 1024 var cpupercent float64 ps, err := cpu.Percent(time.Millisecond, true) if err == nil && len(ps) > 0 { for _, v := range ps { cpupercent += v } cpupercent /= float64(len(ps)) } return map[string]interface{}{ "DiskInfo": diskinfo, "SwapMemory": sm, "VirtualMemory": vm, "CpuPercent": cpupercent * 100, } }
func (c *CpuPerc) report(kill <-chan struct{}) chan float64 { out := make(chan float64) go func() { defer close(out) out <- 0.0 for { select { case <-kill: return default: p, err := cpu.Percent(c.Interval, false) if err != nil { continue } out <- p[0] } } }() return out }
func main() { fmt.Println("Hello RGB-spear") ser, err := serial.OpenPort(&serial.Config{ Name: flagPort, Baud: 9600, }) if err != nil { panic(err) } defer ser.Close() defer ser.Flush() fmt.Println("Wating 10 seconds for booting...") time.Sleep(10 * time.Second) spr = new(Spear) // RED for cpu if flagCPU { fmt.Println("Enable CPU") go func() { for { c, err := cpu.Percent(100*time.Millisecond, false) if err != nil { panic(err) } t := byte((c[0] * 255) / 100) spr.Lock() spr.R = t // log.Println("R->", spr.R) spr.Unlock() time.Sleep(300 * time.Millisecond) } }() } // Green for memory if flagMEM { fmt.Println("Enable MEM") go func() { for { v, err := mem.VirtualMemory() if err != nil { panic(err) } t := byte((v.UsedPercent * 255) / 100) spr.Lock() spr.G = t // log.Println("G->", spr.R) spr.Unlock() time.Sleep(300 * time.Millisecond) } }() } // Blue for network if flagNET { fmt.Println("Enable Network") go func() { lastSent, lastRecv := uint64(0), uint64(0) for { c, err := net.IOCounters(false) if err != nil { panic(err) } currSent := c[0].BytesSent currRecv := c[0].BytesRecv sent := currSent - lastSent recv := currRecv - lastRecv t := byte(((sent + recv) * 255) / 1000000) spr.Lock() spr.B = t spr.Unlock() time.Sleep(1 * time.Second) lastSent = currSent lastRecv = currRecv } }() t := time.NewTicker(100 * time.Millisecond) for { select { case <-t.C: fmt.Fprint(ser, spr) // log.Println("spr:", spr) ser.Flush() } } } }
func (c *CPUOps) Percent(interval time.Duration, perCpu bool) ([]float64, error) { return CPUUtil.Percent(interval, perCpu) }