main.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. m, _ := os.Open("/proc/meminfo")
  33. for true {
  34. line := readProcStatLine(r)
  35. time.Sleep(time.Second)
  36. lineNew := readProcStatLine(r)
  37. cpustat := gupta.NewCPUStat(line, lineNew)
  38. fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
  39. mem := gupta.NewMemory(m)
  40. fmt.Printf("Memory free %d\n", mem.Total())
  41. m.Seek(0, 0)
  42. }
  43. }