main.go 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, "i", 5, "polling inteval")
  25. flag.BoolVar(&memory, "m", false, "Poll memory")
  26. flag.BoolVar(&load, "l", false, "Poll load")
  27. flag.BoolVar(&cpu, "c", false, "poll CPU usage")
  28. flag.StringVar(&partition, "p", "", "Poll disk usage (partition)")
  29. flag.StringVar(&netInterface, "n", "", "Network usage (interface)")
  30. flag.Parse()
  31. fmt.Println(gupta.Hello())
  32. r, _ := os.Open("/proc/stat")
  33. m, _ := os.Open("/proc/meminfo")
  34. for true {
  35. line := readProcStatLine(r)
  36. time.Sleep(time.Second)
  37. lineNew := readProcStatLine(r)
  38. cpustat := gupta.NewCPUStat(line, lineNew)
  39. fmt.Printf("CPU usage %.2f%%\n", cpustat.CPUUsage())
  40. mem := gupta.NewMemory(m)
  41. //fmt.Printf("Memory free %d\n", mem.Total())
  42. report := gupta.GuptaReport{}
  43. report.MemoryUsage = mem
  44. writer := os.Stdout
  45. enc := json.NewEncoder(writer)
  46. if err := enc.Encode(&report); err != nil {
  47. log.Println(err)
  48. }
  49. m.Seek(0, 0)
  50. }
  51. }