Guest User

Untitled

a guest
Aug 10th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. # MySQL Health plugin
  2. #
  3. # Plugin is intended to monitor basic MySQL parametrs
  4. # plugin was inspired by mysqltuner
  5. #
  6. # Currently plugin monitors
  7. # * InnoDB data size / buffer pool
  8. # * Highest usage of available connections
  9. # * Active processes
  10.  
  11. class MySQLHealthPlugin < Scout::Plugin
  12. needs 'mysql'
  13.  
  14. OPTIONS=<<-EOS
  15. user:
  16. name: MySQL username
  17. notes: Specify the username to connect with
  18. default: root
  19. password:
  20. name: MySQL password
  21. notes: Specify the password to connect with
  22. attributes: password
  23. host:
  24. name: MySQL host
  25. notes: Specify something other than 'localhost' to connect via TCP
  26. default: localhost
  27. port:
  28. name: MySQL port
  29. notes: Specify the port to connect to MySQL with (if nonstandard)
  30. socket:
  31. name: MySQL socket
  32. notes: Specify the location of the MySQL socket
  33. buffer_pool_notify_treshhold:
  34. name: Buffer pool notify treshhold
  35. notes: When you want to be notify, that data size is approaching to buffer pool (Default - 1024M)
  36. EOS
  37.  
  38. def build_report
  39. user = option(:user) || 'root'
  40. password = option(:password)
  41. host = option(:host)
  42. port = option(:port)
  43. socket = option(:socket)
  44.  
  45. # Buffer pool stuff
  46.  
  47. data_size = nil
  48. innodb_buffer_pool = nil
  49.  
  50. mysql = Mysql.connect(host, user, password, nil, (port.nil? ? nil : port.to_i), socket)
  51. result = mysql.query("SELECT SUM(data_length)/1024/1024 AS data_mb FROM information_schema.tables WHERE engine = 'InnoDB'")
  52.  
  53. result.each do |row|
  54. data_size = row.first.to_i
  55. end
  56.  
  57. result = mysql.query("SHOW variables")
  58. result.each do |row|
  59. if row.first == 'innodb_buffer_pool_size'
  60. innodb_buffer_pool = row.last.to_i / 1024 / 1024
  61. end
  62. end
  63.  
  64. buffer_pool_treshhold = option(:buffer_pool_notify_treshhold) || 1024
  65.  
  66. if ( (data_size + buffer_pool_treshhold) > innodb_buffer_pool)
  67. alert('You should increase innodb_buffer_pool_size') unless memory(:notified)
  68. remember(:notified => true)
  69. else
  70. remember(:notified => false)
  71. end
  72.  
  73. # Processlist count
  74. active_processes_count = nil
  75.  
  76. result = mysql.query("SELECT COUNT(*) FROM information_schema.processlist WHERE command != 'Sleep'")
  77. result.each do |row|
  78. active_processes_count = row.first.to_i
  79. end
  80.  
  81.  
  82. # Connections
  83. max_connections = nil
  84. max_used_connections = nil
  85.  
  86. result = mysql.query("show variables like 'max_connections'")
  87. result.each do |row|
  88. max_connections = row.last.to_f
  89. end
  90.  
  91. result = mysql.query("show status like 'Max_used_connections'")
  92. result.each do |row|
  93. max_used_connections = row.last.to_f
  94. end
  95.  
  96. connection_utilization = (max_used_connections / max_connections) * 100
  97.  
  98. if (connection_utilization > 90.0)
  99. alert("Connection utilization exceed 90% and now is #{connection_utilization}. You should increase max_connection parametr")
  100. end
  101.  
  102. report :innodb_buffer_pool => innodb_buffer_pool, :data_size => data_size,
  103. :active_processes_count => active_processes_count,
  104. :connection_utilization => connection_utilization
  105. end
  106.  
  107. end
Add Comment
Please, Sign In to add comment