statusUpdater.go 880 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package client
  2. import (
  3. "log"
  4. "strconv"
  5. "time"
  6. "github.com/inhies/go-bytesize"
  7. "github.com/mackerelio/go-osstat/memory"
  8. "github.com/shirou/gopsutil/cpu"
  9. )
  10. // Look into using a ticker instead of a for-loop
  11. func (client *Client) statusUpdater() {
  12. for {
  13. // Fetch CPU usage in percentages
  14. cpustats, err := cpu.Percent(time.Second, false)
  15. if err != nil {
  16. log.Println("ERROR: fetching CPU usage infor - ", err)
  17. }
  18. // Fetch memory stats in bytes
  19. mem, err := memory.Get()
  20. if err != nil {
  21. log.Println("ERROR: fetching memory info - ", err)
  22. }
  23. // Use the ByteSize package to allow for memory calculations
  24. b := bytesize.New(float64(mem.Used))
  25. // Prepare the message.
  26. update := "update;" + strconv.Itoa(int(cpustats[0])) + ";" + b.String() + ";" + strconv.Itoa(client.taskId)
  27. // Write the message to the server
  28. client.writeToServer(update)
  29. }
  30. }