Skip to content

Instantly share code, notes, and snippets.

@vadv
Last active October 7, 2015 21:18
Show Gist options
  • Save vadv/3226836 to your computer and use it in GitHub Desktop.
Save vadv/3226836 to your computer and use it in GitHub Desktop.
Лаг по времени для встроенной репликации psql для nagios
#!/usr/bin/env ruby
require 'rubygems'
require 'pg'
require 'timeout'
require 'nagios-probe'
def master_connection_string
{:user => 'nagios', :dbname => 'nagios', :host => 'master', :password => 'nagios'}
end
def slave_connection_string
{:user => 'nagios', :dbname => 'nagios', :host => 'slave', :password => 'nagios'}
end
$stamp = Random.rand(100)
class ReplicationProbe< Nagios::Probe
def check_crit
conn = PG.connect( master_connection_string )
conn.transaction do |conn|
conn.exec("drop table if exists lag_table; create table lag_table (stamp integer); insert into lag_table VALUES( '#{$stamp}' );")
end
status = Timeout::timeout(60) {
slave_stamp = -1
sleep 1 #ERROR: could not open relation with OID
while (slave_stamp != $stamp)
conn = PG.connect( slave_connection_string )
conn.transaction do |conn|
slave_stamp = conn.exec("select stamp from lag_table limit 1;").first["stamp"].to_i
end
sleep 1 #Timeout probe
end
}
end
def check_warn
false
end
def crit_message
"Lag is critical!"
end
def warn_message
"nothing to message"
end
def ok_message
"nothing to see here"
end
end
begin
options = { }
probe = ReplicationProbe.new(options)
probe.run
rescue Exception => e
puts "unknown: #{e}"
exit Nagios::UNKNOWN
end
puts probe.message
exit probe.retval
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment