Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.09 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2. require 'rubygems'
  3. require 'active_support/core_ext/numeric/time'
  4. require 'getoptlong'
  5. require 'mysql2'
  6. require 'yaml'
  7. require 'time'
  8.  
  9. # The name of the plugin. Make it meaningfull
  10. PLUGIN_NAME = 'hours_since_db_backup'
  11. BACKUP_LOG_DATABASE_CONFIG = "/opt/sage/phpmyadmin/database.yml"
  12.  
  13. def usage
  14. puts("#{$0} -h <host_id> [-i <sampling_interval>]")
  15. exit
  16. end
  17.  
  18. def main
  19. # Sync stdout so that it will flush to collectd properly
  20. $stdout.sync = true
  21.  
  22. # parse command line options
  23. hostname = nil
  24. sampling_interval = 60 # Sec, default value
  25. opts = GetoptLong.new(
  26. [ '--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT],
  27. [ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT]
  28. )
  29.  
  30. opts.each do |opt, arg|
  31. case opt
  32. when '--hostid'
  33. hostname = arg
  34. when '--sampling-interval'
  35. sampling_interval = arg.to_i
  36. end
  37. end
  38. usage if !hostname
  39.  
  40. create_connection_to_local_database
  41.  
  42. # Collection loop
  43. while true do
  44. begin
  45. start_run = Time.now.to_i
  46. next_run = start_run + sampling_interval
  47.  
  48. result = nil
  49. hosts = []
  50.  
  51. # get all hosts
  52. result = @database_backups.query("select host_dns_name from host_backup_schedule group by host_dns_name;")
  53. result.each do |row|
  54. hosts << "#{row["host_dns_name"]}"
  55. end
  56.  
  57. # for each host
  58. hosts.each do |host|
  59. query = "SELECT host_dns_name, last_backup_time FROM host_backup_schedule WHERE host_dns_name = '#{host}' AND last_result = 'SUCCESS' ORDER BY last_backup_time Desc limit 1;"
  60. result = @database_backups.query(query)
  61.  
  62. result.each do |row|
  63. # The sketchy data seems to reject graphnames (gauge-#{host_dns_name}) longer than ~ 60 characters
  64. host_dns_name = host.gsub('.','_')[0..55]
  65. last_backup_time = row["last_backup_time"]
  66. hours_since_last_backup = ((Time.now - last_backup_time) / 1.hour).round(2)
  67. puts "PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-#{host_dns_name} interval=#{sampling_interval} #{start_run}:#{hours_since_last_backup}"
  68. end
  69. end
  70.  
  71. # sleep to make the interval
  72. while((time_left = (next_run - Time.now.to_i)) > 0) do
  73. sleep(time_left)
  74. end
  75.  
  76. rescue Exception
  77. STDERR.puts "Failed to run health check: #{$!}"
  78. log("Failed to run health check: #{$!}")
  79. puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-response_code #{start_run}:99999")
  80.  
  81. # sleep to make the interval
  82. while((time_left = (next_run - Time.now.to_i)) > 0) do
  83. sleep(time_left)
  84. end
  85. end
  86. end
  87. end
  88.  
  89. # Creates aconnection to access local database-backups database
  90. def create_connection_to_local_database
  91. app_config = YAML.load_file(BACKUP_LOG_DATABASE_CONFIG)
  92. host = app_config["host"]
  93. username = app_config["username"]
  94. password = app_config["password"] || ""
  95. @database_backups = Mysql2::Client.new(:host => "#{host}",
  96. :username => "#{username}",
  97. :password => "#{password}",
  98. :database => "database_backups")
  99. rescue => e
  100. puts e.message
  101. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement