· 7 years ago · Nov 29, 2018, 09:40 AM
1#!/usr/bin/env ruby
2require 'rubygems'
3require 'pg'
4require 'timeout'
5require 'nagios-probe'
6
7def master_connection_string
8 {:user => 'nagios', :dbname => 'nagios', :host => 'master', :password => 'nagios'}
9end
10
11def slave_connection_string
12 {:user => 'nagios', :dbname => 'nagios', :host => 'slave', :password => 'nagios'}
13end
14
15$stamp = Random.rand(100)
16
17class ReplicationProbe< Nagios::Probe
18 def check_crit
19 conn = PG.connect( master_connection_string )
20 conn.transaction do |conn|
21 conn.exec("drop table if exists lag_table; create table lag_table (stamp integer); insert into lag_table VALUES( '#{$stamp}' );")
22 end
23 status = Timeout::timeout(60) {
24 slave_stamp = -1
25 sleep 1 #ERROR: could not open relation with OID
26 while (slave_stamp != $stamp)
27 conn = PG.connect( slave_connection_string )
28 conn.transaction do |conn|
29 slave_stamp = conn.exec("select stamp from lag_table limit 1;").first["stamp"].to_i
30 end
31 sleep 1 #Timeout probe
32 end
33 }
34 end
35
36 def check_warn
37 false
38 end
39
40 def crit_message
41 "Lag is critical!"
42 end
43
44 def warn_message
45 "nothing to message"
46 end
47
48 def ok_message
49 "nothing to see here"
50 end
51end
52
53begin
54 options = { }
55 probe = ReplicationProbe.new(options)
56 probe.run
57rescue Exception => e
58 puts "unknown: #{e}"
59 exit Nagios::UNKNOWN
60end
61
62puts probe.message
63exit probe.retval