package gupta import ( "fmt" "strconv" "strings" ) const version = "0.0.1" func Hello() string { return fmt.Sprintf("Welcome to gupta version %s", version) } func Run(load, cpu, memory bool, partition, networkInterface string) (cpuLoad, cpuUsage, memoryUsage float64) { cpuLoad = 0.2 cpuUsage = 0.2 memoryUsage = 70.0 return cpuLoad, cpuUsage, memoryUsage } type GuptaReport struct { LoadAvarage float64 CPUUsage float64 MemoryUsage Memory `json:"memory,omitempty"` } func (g *GuptaReport) GetCPULoad() string { return fmt.Sprintf("%.2f", g.LoadAvarage) } // collect all CPU stat from /proc/cpustat type CPUStat struct { totalTime float64 idleTime float64 totalTimeNew float64 idleTimeNew float64 } func (c *CPUStat) ReadInfoNew(statline string) { rawInfo := strings.Fields(statline) idleNew, _ := strconv.ParseFloat(rawInfo[4], 10) iowaitNew, _ := strconv.ParseFloat(rawInfo[5], 10) c.idleTimeNew = idleNew + iowaitNew for _, s := range rawInfo { u, _ := strconv.ParseFloat(s, 10) c.totalTimeNew += u } } func (c *CPUStat) ReadInfo(statline string) { rawInfo := strings.Fields(statline) for _, s := range rawInfo[1:] { u, _ := strconv.ParseFloat(s, 10) c.totalTime += u } idle, _ := strconv.ParseFloat(rawInfo[4], 10) iowait, _ := strconv.ParseFloat(rawInfo[5], 10) c.idleTime = idle + iowait } func (c *CPUStat) CPUUsage() float64 { deltaIdleTime := c.idleTimeNew - c.idleTime deltaTotalTime := c.totalTimeNew - c.totalTime cpuUsage := (1.0 - float64(deltaIdleTime)/float64(deltaTotalTime)) * 100 return cpuUsage } func NewCPUStat(procstatline, procstatlineNew string) CPUStat { cpustat := CPUStat{} cpustat.ReadInfo(procstatline) cpustat.ReadInfoNew(procstatlineNew) return cpustat }