Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'rubygems'
- require 'active_support/core_ext/numeric/time'
- require 'getoptlong'
- require 'mysql2'
- require 'yaml'
- require 'time'
- # The name of the plugin. Make it meaningfull
- PLUGIN_NAME = 'hours_since_db_backup'
- BACKUP_LOG_DATABASE_CONFIG = "/opt/sage/phpmyadmin/database.yml"
- def usage
- puts("#{$0} -h <host_id> [-i <sampling_interval>]")
- exit
- end
- def main
- # Sync stdout so that it will flush to collectd properly
- $stdout.sync = true
- # parse command line options
- hostname = nil
- sampling_interval = 60 # Sec, default value
- opts = GetoptLong.new(
- [ '--hostid', '-h', GetoptLong::REQUIRED_ARGUMENT],
- [ '--sampling-interval', '-i', GetoptLong::OPTIONAL_ARGUMENT]
- )
- opts.each do |opt, arg|
- case opt
- when '--hostid'
- hostname = arg
- when '--sampling-interval'
- sampling_interval = arg.to_i
- end
- end
- usage if !hostname
- create_connection_to_local_database
- # Collection loop
- while true do
- begin
- start_run = Time.now.to_i
- next_run = start_run + sampling_interval
- result = nil
- hosts = []
- # get all hosts
- result = @database_backups.query("select host_dns_name from host_backup_schedule group by host_dns_name;")
- result.each do |row|
- hosts << "#{row["host_dns_name"]}"
- end
- # for each host
- hosts.each do |host|
- 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;"
- result = @database_backups.query(query)
- result.each do |row|
- # The sketchy data seems to reject graphnames (gauge-#{host_dns_name}) longer than ~ 60 characters
- host_dns_name = host.gsub('.','_')[0..55]
- last_backup_time = row["last_backup_time"]
- hours_since_last_backup = ((Time.now - last_backup_time) / 1.hour).round(2)
- puts "PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-#{host_dns_name} interval=#{sampling_interval} #{start_run}:#{hours_since_last_backup}"
- end
- end
- # sleep to make the interval
- while((time_left = (next_run - Time.now.to_i)) > 0) do
- sleep(time_left)
- end
- rescue Exception
- STDERR.puts "Failed to run health check: #{$!}"
- log("Failed to run health check: #{$!}")
- puts("PUTVAL #{hostname}/#{PLUGIN_NAME}/gauge-response_code #{start_run}:99999")
- # sleep to make the interval
- while((time_left = (next_run - Time.now.to_i)) > 0) do
- sleep(time_left)
- end
- end
- end
- end
- # Creates aconnection to access local database-backups database
- def create_connection_to_local_database
- app_config = YAML.load_file(BACKUP_LOG_DATABASE_CONFIG)
- host = app_config["host"]
- username = app_config["username"]
- password = app_config["password"] || ""
- @database_backups = Mysql2::Client.new(:host => "#{host}",
- :username => "#{username}",
- :password => "#{password}",
- :database => "database_backups")
- rescue => e
- puts e.message
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement