collectors.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package main
  2. import (
  3. "log"
  4. "time"
  5. "github.com/cloudfoundry/gosigar"
  6. "github.com/prometheus/client_golang/prometheus"
  7. )
  8. func CollectRam(sleepLen time.Duration, name string) {
  9. var mem sigar.Mem
  10. go func() {
  11. for {
  12. checkFatal(mem.Get())
  13. log.Printf("Mem: total[%12d] used[%12d] free[%12d]\n", mem.Total, mem.Used, mem.Free)
  14. time.Sleep(sleepLen)
  15. }
  16. }()
  17. prometheus.MustRegister(prometheus.NewGaugeFunc(
  18. prometheus.GaugeOpts{
  19. Subsystem: "ram",
  20. Name: "actualUsed",
  21. Help: "ram that is used",
  22. ConstLabels: prometheus.Labels{"host": name},
  23. },
  24. func() float64 {
  25. return float64(mem.ActualUsed)
  26. },
  27. ))
  28. }
  29. func CollectSwap(sleepLen time.Duration, name string) {
  30. var swap sigar.Swap
  31. go func() {
  32. for {
  33. // swap
  34. checkFatal(swap.Get())
  35. log.Printf("Swap: total[%12d] used[%12d] free[%12d]\n", swap.Total, swap.Used, swap.Free)
  36. time.Sleep(sleepLen)
  37. }
  38. }()
  39. prometheus.MustRegister(prometheus.NewGaugeFunc(
  40. prometheus.GaugeOpts{
  41. Subsystem: "swap",
  42. Name: "used",
  43. Help: "swap space that is used",
  44. ConstLabels: prometheus.Labels{"host": name},
  45. },
  46. func() float64 {
  47. return float64(swap.Used)
  48. },
  49. ))
  50. }
  51. func CollectUsagePercent(name, path string) {
  52. var diskspace sigar.FileSystemUsage
  53. log.Printf("collecting DiskSpace[%s]", path)
  54. usage := func() float64 {
  55. checkFatal(diskspace.Get(path))
  56. log.Printf("df[%-10s]: %4s %4s %4s %4s\n", path,
  57. formatSize(diskspace.Total),
  58. formatSize(diskspace.Used),
  59. formatSize(diskspace.Avail),
  60. sigar.FormatPercent(diskspace.UsePercent()),
  61. )
  62. return diskspace.UsePercent()
  63. }
  64. prometheus.MustRegister(prometheus.NewGaugeFunc(
  65. prometheus.GaugeOpts{
  66. Subsystem: "diskspace",
  67. Name: "percentage",
  68. Help: "disk space used in percent",
  69. ConstLabels: prometheus.Labels{
  70. "host": name,
  71. "path": path,
  72. },
  73. },
  74. usage,
  75. ))
  76. }
  77. func CollectUsed(sleepLen time.Duration, name, path string) {
  78. var diskspace sigar.FileSystemUsage
  79. g := prometheus.NewGauge(prometheus.GaugeOpts{
  80. Subsystem: "diskspace",
  81. Name: "bytesUsed",
  82. Help: "disk space used in percent",
  83. ConstLabels: prometheus.Labels{
  84. "host": name,
  85. "path": path,
  86. },
  87. })
  88. prometheus.MustRegister(g)
  89. for {
  90. checkFatal(diskspace.Get(path))
  91. g.Set(float64(diskspace.Used * 1024))
  92. time.Sleep(sleepLen)
  93. }
  94. }
  95. func CollectCPULoad(sleepLen time.Duration, name string) {
  96. var avg sigar.LoadAverage
  97. go func() {
  98. for {
  99. checkFatal(avg.Get())
  100. log.Printf("Load: 1m[%.2f] 5m[%.2f] 15m[%.2f]\n", avg.One, avg.Five, avg.Fifteen)
  101. time.Sleep(sleepLen)
  102. }
  103. }()
  104. prometheus.MustRegister(prometheus.NewGaugeFunc(
  105. prometheus.GaugeOpts{
  106. Subsystem: "usage",
  107. Name: "loadavg",
  108. Help: "system load",
  109. ConstLabels: prometheus.Labels{
  110. "host": name,
  111. "time": "1m",
  112. },
  113. },
  114. func() float64 { return avg.One },
  115. ))
  116. prometheus.MustRegister(prometheus.NewGaugeFunc(
  117. prometheus.GaugeOpts{
  118. Subsystem: "usage",
  119. Name: "loadavg",
  120. Help: "system load",
  121. ConstLabels: prometheus.Labels{
  122. "host": name,
  123. "time": "5m",
  124. },
  125. },
  126. func() float64 { return avg.Five },
  127. ))
  128. prometheus.MustRegister(prometheus.NewGaugeFunc(
  129. prometheus.GaugeOpts{
  130. Subsystem: "usage",
  131. Name: "loadavg",
  132. Help: "system load",
  133. ConstLabels: prometheus.Labels{
  134. "host": name,
  135. "time": "15m",
  136. },
  137. },
  138. func() float64 { return avg.Fifteen },
  139. ))
  140. }