main.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "github.com/oz123/gupta"
  8. "log"
  9. "os"
  10. "time"
  11. )
  12. func readProcStatLine(r *os.File) string {
  13. buf := make([]byte, 80)
  14. r.ReadAt(buf, 0)
  15. idx := bytes.Index(buf, []byte("cpu0"))
  16. line := string(buf[:idx])
  17. r.Seek(0, 0)
  18. return line
  19. }
  20. func main() {
  21. var freq int
  22. var partition, netInterface string
  23. var load, memory, cpu bool
  24. flag.IntVar(&freq, "frequency", 5, "Frequency of polling in secods")
  25. flag.IntVar(&freq, "f", 5, "Frequency of polling in secods")
  26. flag.BoolVar(&memory, "m", false, "Poll memory")
  27. flag.BoolVar(&load, "l", false, "Poll load")
  28. flag.BoolVar(&cpu, "c", false, "poll CPU usage")
  29. flag.StringVar(&partition, "p", "", "Poll disk usage (partition)")
  30. flag.StringVar(&netInterface, "n", "", "Network usage (interface)")
  31. flag.Parse()
  32. fmt.Println(gupta.Hello())
  33. r, _ := os.Open("/proc/stat")
  34. m, _ := os.Open("/proc/meminfo")
  35. for true {
  36. line := readProcStatLine(r)
  37. time.Sleep(time.Second)
  38. lineNew := readProcStatLine(r)
  39. cpustat := gupta.NewCPUStat(line, lineNew)
  40. fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
  41. mem := gupta.NewMemory(m)
  42. //fmt.Printf("Memory free %d\n", mem.Total())
  43. report := gupta.GuptaReport{}
  44. report.MemoryUsage = mem
  45. writer := os.Stdout
  46. enc := json.NewEncoder(writer)
  47. if err := enc.Encode(&report); err != nil {
  48. log.Println(err)
  49. }
  50. m.Seek(0, 0)
  51. }
  52. }