Advertisement
Guest User

Untitled

a guest
May 27th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.67 KB | None | 0 0
  1. require 'time'
  2. require 'date'
  3. class MysqlReplicationMonitor < Scout::Plugin
  4. needs 'mysql'
  5.  
  6. OPTIONS=<<-EOS
  7. host:
  8. name: Host
  9. notes: The slave host to monitor
  10. default: 127.0.0.1
  11. port:
  12. name: Port
  13. notes: The port number on the slave host
  14. default: 3306
  15. username:
  16. name: Username
  17. notes: The MySQL username to use
  18. default: root
  19. password:
  20. name: Password
  21. notes: The password for the mysql user
  22. default:
  23. attributes: password
  24. ignore_window_start:
  25. name: Ignore Window Start
  26. notes: Time to start ignoring replication failures. Useful for disabling replication for backups. For Example, 7:00pm
  27. default:
  28. ignore_window_end:
  29. name: Ignore Window End
  30. notes: Time to resume alerting on replication failure. For Example, 2:00am
  31. default:
  32. EOS
  33.  
  34. attr_accessor :connection
  35.  
  36. def build_report
  37. begin
  38. self.connection = Mysql.new(option(:host), option(:username), option(:password), nil, option(:port).to_i)
  39. h = connection.query("show slave status").fetch_hash
  40. down_at = memory(:down_at)
  41. if h.nil?
  42. error("Replication not configured")
  43. elsif h["Seconds_Behind_Master"].nil? and !down_at
  44. unless in_ignore_window?
  45. alert("Replication not running", alert_body(h))
  46. down_at = Time.now
  47. end
  48. elsif h["Slave_IO_Running"] == "Yes" and h["Slave_SQL_Running"] == "Yes"
  49. if down_at
  50. alert("Replication running again","Replication was not running for #{(Time.now - down_at).to_i} seconds")
  51. down_at = nil
  52. end
  53. elsif !down_at
  54. unless in_ignore_window?
  55. alert("Replication not running", alert_body(h))
  56. down_at = Time.now
  57. end
  58. end
  59. report("Seconds Behind Master" => h["Seconds_Behind_Master"]) if h && h["Seconds_Behind_Master"]
  60. remember(:down_at, down_at)
  61. rescue Mysql::Error => e
  62. error("Unable to connect to MySQL", e.to_s)
  63. end
  64. end
  65.  
  66. def in_ignore_window?
  67. if (s = option(:ignore_window_start)) && (e = option(:ignore_window_end))
  68. start_time = Time.parse("#{Date.today} #{s}")
  69. end_time = Time.parse("#{Date.today} #{e}")
  70.  
  71. if start_time < end_time
  72. return(Time.now > start_time and Time.now < end_time)
  73. else
  74. return(Time.now > start_time or Time.now < end_time)
  75. end
  76. else
  77. false
  78. end
  79. end
  80.  
  81. def alert_body(h)
  82. """
  83. IO Slave Running: #{h["Slave_IO_Running"]}
  84. SQL Slave Running: #{h["Slave_SQL_Running"]}
  85.  
  86. Last Errno: #{h["Last_Errno"]}
  87. Last Error: #{h["Last_Error"]}
  88.  
  89. Last IO Errno: #{h["Last_IO_Errno"]}
  90. Last IO Error: #{h["Last_IO_Error"]}
  91.  
  92. Last_SQL_Errno: #{h["Last_SQL_Errno"]}
  93. Last SQL Error: #{h["Last_SQL_Error"]}
  94. """
  95. end
  96. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement