// Report resource. func (m *Memory) Report() { for { select { case <-time.Tick(m.Interval): log.Info("memory: reporting") stat, err := linux.ReadMemInfo(m.Path) if err != nil { log.Error("memory: %s", err) continue } m.client.Gauge("percent", percent(stat)) m.client.Gauge("swap.percent", swapPercent(stat)) if m.Extended { m.client.Gauge("total", bytes(stat["MemTotal"])) m.client.Gauge("used", bytes(used(stat))) m.client.Gauge("free", bytes(stat["MemFree"])) m.client.Gauge("active", bytes(stat["Active"])) m.client.Gauge("swap.total", bytes(stat["SwapTotal"])) m.client.Gauge("swap.free", bytes(stat["SwapFree"])) } case <-m.exit: log.Info("mem: exiting") return } } }
func (m *MemoryUsage) Usage() float64 { memInfo, _ := linuxproc.ReadMemInfo("/proc/meminfo") m.free = memInfo.MemFree + memInfo.Buffers + memInfo.Cached m.total = memInfo.MemTotal return float64(1 - float64(m.free)/float64(m.total)) }
func (mc memoryChecker) avMemoryCheck() (string, error) { meminfo, err := linuxproc.ReadMemInfo(*hostPath + "/proc/meminfo") if err != nil { return "", err } available := meminfo.MemAvailable total := meminfo.MemTotal availablePercent := float64(available) / float64(total) * 100 if availablePercent < mc.thresholdPercent { return fmt.Sprintf("%2.1f%%", availablePercent), fmt.Errorf("Low available memory: %2.1f%%", availablePercent) } return fmt.Sprintf("%2.1f%%", availablePercent), nil }
//Initalisiert Dynamischen Pc func (y *PCDynamic) PCInitDynamic(s *SSH) error { /* Id int `id` MemFree string `memfree` MemAvailable string `memavail` MemUsed uint64 `memused` MemUsedProzent float64 `memusedprozent` LoadAvg float64 `loadavg` NumProcess uint64 `numprocess` */ loadavg := &linuxproc.LoadAvg{} meminfo := &linuxproc.MemInfo{} err := errors.New("") if s.Client == nil { err := errors.New("Could not find a ssh Client ") revel.ERROR.Print(err) return err } else { s.Download("/proc/loadavg", tempDir+"/loadavg"+strconv.Itoa(y.Id)) loadavg, err = linuxproc.ReadLoadAvg("/proc/loadavg") if err != nil { revel.ERROR.Print(err) return err } y.LoadAvg = loadavg.Last1Min y.NumProcess = loadavg.ProcessRunning s.Download("/proc/meminfo", tempDir+"/meminfo"+strconv.Itoa(y.Id)) meminfo, err = linuxproc.ReadMemInfo("/home/pr0gramming/Dokumente/meminfo" + strconv.Itoa(y.Id)) if err != nil { revel.ERROR.Print(err) return err } } y.MemUsed = bytefmt.ByteSize((meminfo.MemTotal - meminfo.MemFree) * 1024) y.MemFree = bytefmt.ByteSize(meminfo.MemFree * 1024) y.MemAvailable = bytefmt.ByteSize(meminfo.MemAvailable * 1024) a := float64(meminfo.MemTotal) b := float64(meminfo.MemFree) y.MemUsedProzent = strconv.FormatFloat(float64(((a-b)/a)*100), 'f', -1, 64) //println("test: " + (y.MemUsedProzent)) return nil }
func main() { uaid := os.Getenv("UAID") if uaid == "" { panic("UAID environment required. Example: UAID=UA-XXXXXX-Y ./server-analytics") } client, err := ga.NewClient(uaid) if err != nil { panic(err) } ticker := time.NewTicker(15 * time.Second) quit := make(chan struct{}) stop := false for !stop { select { case <-ticker.C: diskInfo, err := linuxproc.ReadDisk("/") _ = err client.Send(ga.NewEvent("Disk", "All").Label("Total Disk Space").Value(int64(diskInfo.All))) client.Send(ga.NewEvent("Disk", "Used").Label("Used Disk Space").Value(int64(diskInfo.Used))) client.Send(ga.NewEvent("Disk", "Free").Label("Free Disk Space").Value(int64(diskInfo.Free))) loadAvg, err := linuxproc.ReadLoadAvg("/proc/loadavg") _ = err client.Send(ga.NewEvent("Load Average", "1m").Label("1m").Value(int64(loadAvg.Last1Min))) client.Send(ga.NewEvent("Load Average", "5m").Label("5m").Value(int64(loadAvg.Last5Min))) client.Send(ga.NewEvent("Load Average", "15m").Label("15m").Value(int64(loadAvg.Last15Min))) client.Send(ga.NewEvent("Load Average", "Running").Label("Process Running").Value(int64(loadAvg.ProcessRunning))) client.Send(ga.NewEvent("Load Average", "Total").Label("Process Total").Value(int64(loadAvg.ProcessTotal))) meminfo, err := linuxproc.ReadMemInfo("/proc/meminfo") _ = err client.Send(ga.NewEvent("Memory", "Total").Label("Memory Total").Value(int64(meminfo.MemTotal))) client.Send(ga.NewEvent("Memory", "Free").Label("Memory Free").Value(int64(meminfo.MemFree))) case <-quit: ticker.Stop() return } } }
//Initalisiert Pc func (x *PCAll) Pc_Init_All() error { cpuinfo, err := linuxproc.ReadCPUInfo("/proc/cpuinfo") if err != nil { revel.ERROR.Print(err) return err } meminfo, err := linuxproc.ReadMemInfo("/proc/meminfo") if err != nil { revel.ERROR.Print(err) return err } diskinfo, err := linuxproc.ReadDiskStats("/proc/diskstats") if err != nil { revel.ERROR.Print(err) return err } netstat, err := linuxproc.ReadNetStat("/proc/net/netstat") if err != nil { revel.ERROR.Print(err) return err } netdev, err := linuxproc.ReadNetworkStat("/proc/net/dev") if err != nil { revel.ERROR.Print(err) return err } x.Cpuinfo = cpuinfo x.Meminfo = meminfo x.Diskstat = diskinfo x.Netstat = netstat x.Netdev = netdev return nil }
// From http://stackoverflow.com/questions/23367857/accurate-calculation-of-cpu-usage-given-in-percentage-in-linux var ( currentStat = stat.CPUStatAll prevIdle = previousStat.Idle + previousStat.IOWait idle = currentStat.Idle + currentStat.IOWait prevNonIdle = (previousStat.User + previousStat.Nice + previousStat.System + previousStat.IRQ + previousStat.SoftIRQ + previousStat.Steal) nonIdle = (currentStat.User + currentStat.Nice + currentStat.System + currentStat.IRQ + currentStat.SoftIRQ + currentStat.Steal) prevTotal = prevIdle + prevNonIdle total = idle + nonIdle // differentiate: actual value minus the previous one totald = total - prevTotal idled = idle - prevIdle ) previousStat = currentStat return float64(totald-idled) * 100. / float64(totald), float64(len(stat.CPUStats)) * 100. } // GetMemoryUsageBytes returns the bytes memory usage and max var GetMemoryUsageBytes = func() (float64, float64) { meminfo, err := linuxproc.ReadMemInfo(ProcMemInfo) if err != nil { return 0.0, 0.0 } used := meminfo.MemTotal - meminfo.MemFree - meminfo.Buffers - meminfo.Cached return float64(used * kb), float64(meminfo.MemTotal * kb) }
//Initalisiert Statischen Pc func (y *PCStatic) PCInitStatic(s *SSH) error { /* NumCPUS int NumVCORES int NumVCORESUsed int NumPhysicalCores int NumVCORESUsedProzent float64 MemTotal string MemUsed string NetDevices string */ cpuinfo := &linuxproc.CPUInfo{} meminfo := &linuxproc.MemInfo{} netdev := []linuxproc.NetworkStat{} err := errors.New("") if s.Client == nil { err := errors.New("Could not find a ssh Client ") revel.ERROR.Print(err) return err } else { s.Download("/proc/cpuinfo", tempDir+"/cpuinfo"+strconv.Itoa(y.Id)) cpuinfo, err = linuxproc.ReadCPUInfo(tempDir + "/cpuinfo" + strconv.Itoa(y.Id)) if err != nil { revel.ERROR.Print(err) return err } s.Download("/proc/meminfo", tempDir+"/meminfo"+strconv.Itoa(y.Id)) meminfo, err = linuxproc.ReadMemInfo(tempDir + "/meminfo" + strconv.Itoa(y.Id)) if err != nil { revel.ERROR.Print(err) return err } s.Download("/proc/net/dev", tempDir+"/net"+strconv.Itoa(y.Id)) netdev, err = linuxproc.ReadNetworkStat(tempDir + "/net" + strconv.Itoa(y.Id)) if err != nil { revel.ERROR.Print(err) return err } } y.NumCPUS = cpuinfo.NumCPU() y.NumPhysicalCores = cpuinfo.NumPhysicalCPU() y.NumVCORES = (cpuinfo.NumCore() + 1) * 2 y.NumVCORESUsed = y.NumVCORES / 2 //%TODO fake und so a := float64(y.NumVCORESUsed) b := float64(y.NumVCORES) y.NumVCORESUsedProzent = strconv.FormatFloat(float64(a/b)*100, 'f', -1, 64) y.NetDevices = netdev[0].Iface y.MemTotal = bytefmt.ByteSize(meminfo.MemTotal * 1024) y.MemUsed = bytefmt.ByteSize((meminfo.MemTotal - meminfo.MemFree) * 1024) return nil }