Hi there, using this program you will be able to find out if Zookeeper server is alive. If it does next it checks for Znode in provided path. Hope this helps you. I searched lot of google pages and other search engine but could not find exact fit so i created on my own. if you have any questions of comments please do contact me via ContactMe.
For below program to run you will basically needs these:
Linux server with ruby 1.8.7 or later (run ruby -v to find out version) and rubygem 1.8.10 or later (run gem -v to find out your version.)
Copy and paste below code in any file ( i did zkNodeCheck.rb) and then give execute permission on that with chomd +x command.
You will also needs few Ruby modules, you can install via gem. (You would be running gem install zookeeper and so on)
#!/usr/bin/env ruby
require ‘rubygems’
require ‘zookeeper’
require ‘choice’
require ‘logger’Choice.options do
header ‘Application options:’separator ‘Required:’
option :_zkHost, :required => true do
short ‘-z’
long ‘–_zkHost=ZKHOST’
desc ‘Zookeeper host.’
endoption :_envId, :required => true do
short ‘-e’
long ‘–_envId=ENVID’
desc ‘EnvironmentId.’
endseparator ‘Common:’
option :help do
short ‘-h’
long ‘–help’
desc ‘Show this message.’
endoption :version do
short ‘-v’
long ‘–version’
desc ‘Show version.’
action do
puts ‘ChoiceExample version 1.0′
exit
end
end
end
def log(msg)
puts “[#{Process.pid}] #{msg}”
enddef debug(obj)
log(obj.inspect)
endzk = Zookeeper.new(“#{Choice.choices._zkHost}:2181″)
if zk.state != Zookeeper::ZOO_CONNECTED_STATE
puts “\tZookeeper server ‘ERROR’ \t\t#{Choice.choices._zkHost}”
exit(1)
else
puts ”
sleep(7)
puts “\tZookeeper server ‘OK’ \t\t#{Choice.choices._zkHost}”
endgetNode = zk.get_children(:path => “/#{Choice.choices._envId}”)
date = `date “+%s”`.chomp
f = File.open(“/tmp/#{date}.txt”, “w”)
f.puts “My Znode is: #{getNode}”
f.closeout = `cat /tmp/#{date}.txt | tail -c 2`.chomp
if “#{out}” == “0″
puts(“\tZnode \t’OK’ \t\t/#{Choice.choices._envId}”)
system “rm -f /tmp/#{date}.txt”
else
puts(“\tZnode \t’ERROR’ \t/#{Choice.choices._envId}”)
system “rm -f /tmp/#{date}.txt”
exit(1)
end


Recent Comments