public
Last active

A Keepalived script

  • Download Gist
keepalived-notifier.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#!/usr/bin/ruby
 
# This is in my keepalived.conf
# ...
# notify_master "/usr/tools/keepalived/notify.sh primary"
# notify_backup "/usr/tools/keepalived/notify.sh backup"
# notify_fault "/usr/tools/keepalived/notify.sh fault"
# ...
 
require 'socket'
require 'logger'
require 'thread'
include Socket::Constants
 
pid = fork do
SCRIPTS = %w[radvd.sh conntrack.sh dhcp.sh routes.sh]
$workdir = '/usr/tools/keepalived'
 
LOG = Logger.new("/var/log/keepalived-notifier.log", 2, 1024*1024) # rotate after 1MB and keep 2 copies
LOG.level = Logger::INFO
LOG.datetime_format = '%Y-%m-%d %H:%M:%S'
LOG.info "Application started!"
mutex = Mutex.new
action = 0
act = 0
state_back = 0
state_prim = 0
time_start = Time.now.to_i
time_now = Time.now.to_i
Thread.start do
loop do
sleep 1800
mutex.synchronize do
LOG.info "stats: primary: #{state_prim} backup: #{state_back}"
end
end
end
socket = UDPSocket.new
socket.bind(nil, 503)
loop do
text, sender = socket.recvfrom(16384)
len = text.length-1
char = len == 0 ? text : text[len,len]
if char == "1"
LOG.info "Going primary"
mutex.synchronize { state_prim += 1 }
Dir.chdir $workdir do
SCRIPTS.each do |s|
next unless File.exist? s
cmd = "./#{s} primary"
LOG.info " Executing '#{cmd}'"
`#{cmd}`
LOG.info " Done executing '#{cmd}'"
end
end
LOG.info "Done"
elsif char == "2"
LOG.info "Going backup"
mutex.synchronize { state_back += 1 }
Dir.chdir $workdir do
SCRIPTS.each do |s|
next unless File.exist? s
cmd = "./#{s} backup"
LOG.info " Executing '#{cmd}'"
`#{cmd}`
LOG.info " Done executing '#{cmd}'"
end
end
LOG.info "Done"
elsif char == "3"
LOG.info "Going fault"
mutex.synchronize { state_back += 1 }
Dir.chdir $workdir do
SCRIPTS.each do |s|
next unless File.exist? s
cmd = "./#{s} fault"
LOG.info " Executing '#{cmd}'"
`#{cmd}`
LOG.info " Done executing '#{cmd}'"
end
end
LOG.info "Done"
else
LOG.warn "Unknown action: #{char.gsub("\n",'\n')}"
end
end
end
 
# write pid
File.open("/var/run/keepalived-notifier.pid",'w') { |fh| fh.write pid }
 
# and detach
Process.detach pid
notify.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#!/bin/bash
 
if [ ! -e /var/run/keepalived-notifier.pid ]
then
(cd /usr/tools/keepalived && ./keepalived-notifier.rb)
sleep 2
fi
 
 
if [ -e /var/run/keepalived-notifier.pid ]
then
case "$1" in
primary)
echo -n 1 | nc -u 127.0.0.1 503
;;
backup)
echo -n 2 | nc -u 127.0.0.1 503
;;
fault)
echo -n 3 | nc -u 127.0.0.1 503
;;
*)
logger "ERROR: unknown state transition '$1'"
exit 1
;;
esac
else
logger "ERROR: file /var/run/keepalived-notifier.pid does not exist. Cannot notify..."
exit 1
fi
 
exit 0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.