main.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package main
  2. import (
  3. "bytes"
  4. "flag"
  5. "fmt"
  6. "github.com/oz123/gupta"
  7. "os"
  8. "time"
  9. )
  10. func readProcStatLine(r *os.File) string {
  11. buf := make([]byte, 80)
  12. r.ReadAt(buf, 0)
  13. idx := bytes.Index(buf, []byte("cpu0"))
  14. line := string(buf[:idx])
  15. r.Seek(0, 0)
  16. return line
  17. }
  18. func main() {
  19. var freq int
  20. var partition, netInterface string
  21. var load, memory, cpu bool
  22. flag.IntVar(&freq, "frequency", 5, "Frequency of polling in secods")
  23. flag.IntVar(&freq, "f", 5, "Frequency of polling in secods")
  24. flag.BoolVar(&memory, "m", false, "Poll memory")
  25. flag.BoolVar(&load, "l", false, "Poll load")
  26. flag.BoolVar(&cpu, "c", false, "poll CPU usage")
  27. flag.StringVar(&partition, "p", "", "Poll disk usage (partition)")
  28. flag.StringVar(&netInterface, "n", "", "Network usage (interface)")
  29. flag.Parse()
  30. fmt.Println(gupta.Hello())
  31. r, _ := os.Open("/proc/stat")
  32. for true {
  33. line := readProcStatLine(r)
  34. time.Sleep(time.Second)
  35. lineNew := readProcStatLine(r)
  36. cpustat := gupta.NewCPUStat(line, lineNew)
  37. fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
  38. }
  39. }