Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. package gpu
  2.  
  3. import (
  4. "github.com/elastic/beats/libbeat/common"
  5. "github.com/elastic/beats/libbeat/common/cfgwarn"
  6. "github.com/elastic/beats/metricbeat/mb"
  7. "encoding/csv"
  8. "errors"
  9. "io"
  10. "os/exec"
  11. "strconv"
  12. "strings"
  13. )
  14.  
  15. // init registers the MetricSet with the central registry as soon as the program
  16. // starts. The New function will be called later to instantiate an instance of
  17. // the MetricSet for each host defined in the module's configuration. After the
  18. // MetricSet has been created then Fetch will begin to be called periodically.
  19. func init() {
  20. mb.Registry.MustAddMetricSet("nvidia", "gpu", New)
  21. }
  22.  
  23. // MetricSet holds any configuration or state information. It must implement
  24. // the mb.MetricSet interface. And this is best achieved by embedding
  25. // mb.BaseMetricSet because it implements all of the required mb.MetricSet
  26. // interface methods except for Fetch.
  27. type MetricSet struct {
  28. mb.BaseMetricSet
  29. gpucount int
  30. }
  31.  
  32. // New creates a new instance of the MetricSet. New is responsible for unpacking
  33. // any MetricSet specific configuration options if there are any.
  34. func New(base mb.BaseMetricSet) (mb.MetricSet, error) {
  35. cfgwarn.Beta("The nvidia gpu metricset is alpha.")
  36.  
  37. config := struct{}{}
  38. if err := base.Module().UnpackConfig(&config); err != nil {
  39. return nil, err
  40. }
  41. cmd := "echo (nvidia-smi --query-gpu=gpu_name --format=csv | MEasure-Object -line).Lines"
  42. mycount := exec.Command("powershell.exe", "-Command", cmd)
  43. mycount := mycount - 1
  44.  
  45. return &MetricSet{
  46. BaseMetricSet: base,
  47. gpucount: mycount,
  48. }, nil
  49. }
  50.  
  51.  
  52. func (g Utilization) command() *exec.Cmd {
  53. return exec.Command("nvidia-smi", "--query-gpu=utilization.gpu,utilization.memory,memory.total,memory.free,memory.used,temperature.gpu,pstate",format=csv")
  54. }
  55.  
  56. // Fetch methods implements the data gathering and data conversion to the right
  57. // format. It publishes the event which is then forwarded to the output. In case
  58. // of an error set the Error field of mb.Event or simply call report.Error().
  59. func (m *MetricSet) Fetch(report mb.ReporterV2) error {
  60. command() *exec.Cmd
  61. reader := action.start(*exec.Cmd)
  62. gpuIndex := 0
  63.  
  64. for {
  65. line, err := reader.ReadString('\n')
  66. if err == io.EOF {
  67. break
  68. }
  69. // Ignore header
  70. if strings.Contains(line, "utilization") {
  71. continue
  72. }
  73. if len(line) == 0 {
  74. return nil, errors.New("Unable to fetch any events from nvidia-smi: Error " + err.Error())
  75. }
  76.  
  77. // Remove units put by nvidia-smi
  78. line = strings.Replace(line, " %", "", -1)
  79. line = strings.Replace(line, " MiB", "", -1)
  80. line = strings.Replace(line, " P", "", -1)
  81. line = strings.Replace(line, " ", "", -1)
  82.  
  83. r := csv.NewReader(strings.NewReader(line))
  84. record, err := r.Read()
  85. if err == io.EOF {
  86. break
  87. }
  88. headers := strings.Split(query, ",")
  89. event := common.MapStr{
  90. "gpuIndex": m.gpuIndex,
  91. "type": "azBombDiggity",
  92. }
  93. for i := 0; i < len(record); i++ {
  94. value, _ := strconv.Atoi(record[i])
  95. event.Put(headers[i], value)
  96. }
  97. report.Event(mb.Event{
  98. MetricSetFields: event,
  99. })
  100. gpuIndex++
  101. }
  102.  
  103. return nil
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement