123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- 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 {
- cpuLoad float64
- cpuUsage float64
- memoryUsage float64
- }
- func (g *GuptaReport) GetCPULoad() string {
- return fmt.Sprintf("%.2f", g.cpuLoad)
- }
- // 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
- }
|