Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. require 'rubygems'
  2. require 'open-uri'
  3. require 'time'
  4.  
  5. $config = {
  6. "-ip" => "168.198.1.213"
  7. }
  8. $maxitem = 10
  9. $pollseries = []
  10.  
  11. #---------------------
  12. def poll( ip )
  13.  
  14. puts "#{ip}"
  15. url="http://#{ip}:9100/metrics"
  16. puts $url
  17. cpu_idle_val = {}
  18. open($url).read.split("\n").each {|line|
  19. if line[/^node_cpu_seconds_total/] && line[/idle/]
  20.  
  21. tokens = line.split(" ")
  22. txt = tokens[0]
  23. match = txt.match(/(?<cpu>cpu=\".\")/)
  24. cpu_id = match.captures[0].gsub("\"","").split("=")[1].to_i
  25. val = tokens[1].to_f
  26. cpu_idle_val[cpu_id] = val
  27.  
  28. end
  29. }
  30. pollitem = {}
  31. pollitem[:time] = Time.now.to_i
  32. pollitem[:idle] = cpu_idle_val
  33. $pollseries << pollitem
  34.  
  35. $pollseries.shift if $pollseries.count > $maxitem
  36.  
  37. end
  38.  
  39.  
  40. #-----------------------------
  41. def report
  42.  
  43. if $pollseries.count >= 2
  44.  
  45. n = $pollseries.count
  46. last_0 = $pollseries[n-1]
  47. last_1 = $pollseries[n-2]
  48. irates = {}
  49. last_0[:idle].keys.each { |cpu_id|
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement