diff --git a/xerxes.conf b/xerxes.conf new file mode 100644 index 0000000..60cf4f4 --- /dev/null +++ b/xerxes.conf @@ -0,0 +1,3 @@ +#tcp:127.0.0.1:13338 tcp:localhost:3306 quiet debug +tcp:127.0.0.1:13338 tcp:localhost:3306 quiet +unix:testfile tcp:localhost:3306 quiet diff --git a/xerxes_runner.rb b/xerxes_runner.rb new file mode 100755 index 0000000..12d59e3 --- /dev/null +++ b/xerxes_runner.rb @@ -0,0 +1,78 @@ +#!/usr/bin/ruby + +require 'syslog' + +@config_file="xerxes.conf" +@bin_name="./xerxes" + +@log = Syslog.open('xerxes') + +@threads = Array.new ; + +def run_process(cfg, num) + m = Thread.current + t = Thread.start{ + + pid = Process.fork{ + exec(@bin_name, "--src", cfg[:src], "--dst", cfg[:dst], (cfg[:debug] ? "--debug" : ""), (cfg[:quiet] ? "--quiet" : "")) + } + @threads[num][:pid] = pid + @log.notice("Xerxes for #{cfg[:src]}<->#{cfg[:dst]} started") + Process.wait(pid) + @log.warning("Xerxes for #{cfg[:src]}<->#{cfg[:dst]} died") + sleep 0.2 + m.wakeup + } + t +end + +def check_threads + @threads.each_index{|idx| + if (!@threads[idx][:thread].alive?) + @log.notice("try to restart Xerxes for #{@threads[idx][:config][:src]}<->#{@threads[idx][:config][:dst]}") + @threads[idx][:thread] = run_process(@threads[idx][:config], idx) + end + } +end + +i = 0; +File.foreach(@config_file){|line| + if (line =~ /^((tcp|unix):([\d\w_.\/-]+)(:(\d+))?) ((tcp|unix):([\d\w_.\/-]+)(:(\d+))?)( quiet)?( debug)?$/) + + match = Regexp.last_match[1..12] + + @threads[i] = Hash.new + @threads[i][:config] = Hash.new + + @threads[i][:config][:src] = match[0] + @threads[i][:config][:dst] = match[5] + @threads[i][:config][:quiet] = ! match[10].nil? + @threads[i][:config][:debug] = ! match[11].nil? + + i = i +1 + end +} + +@threads.each_index{|idx| + @log.notice("try to start Xerxes for #{@threads[idx][:config][:src]}<->#{@threads[idx][:config][:dst]}") + @threads[idx][:thread] = run_process(@threads[idx][:config], idx) +} + +m = Thread.current +Thread.start{ + Signal.trap("TERM"){ + @log.warning("SIGTERM recived, exiting") + @threads.each{|thread| + @log.info("quit Xerxes for #{thread[:config][:src]}<->#{thread[:config][:dst]}") + thread[:thread].kill + Process.kill("TERM", thread[:pid]) + } + Process.exit! + } +} + +while(true) do + sleep + sleep 0.5 + check_threads +end