#!/usr/bin/ruby sleep 20 Dir.chdir "/home/fedora14/Dropbox/Msc/simulations" $stdout.reopen("logs_sim.txt", "w") $stdout.sync = true $stderr.reopen($stdout) $stderr.sync = true require 'rubygems' require 'mysql' require 'poisson' require 'timeout' require 'fileutils' require 'net/scp' def set_config begin Net::SCP.start(ENV['sql_server'], "root", :password => "fedora14") do |scp| scp.download! "/home/fedora14/unesim_config.txt", "./unesim_config.txt" end rescue Exception => e puts e end load "unesim_config.txt" $component_id = @configs[:component_id] $join_rate = @configs[:join_rate] $fail_rate = @configs[:fail_rate] $netbot_count_init = @configs[$host_ip][0] $networks = @configs[$host_ip][1] end def init_simulation ENV['dns_server'] = "192.168.0.1" ENV['sql_server'] = "192.168.0.1" ENV['platform_id'] = '2011' $host_ip = `InformationPolicy/get_local_ip.sh`.chomp! set_config $is_running = false $netbots = [] `echo nameserver #{ENV['dns_server']} > /etc/resolv.conf` $mysql = Mysql.real_connect(ENV['sql_server'] , "root", nil, "fta") $cpu = `more /proc/cpuinfo | grep model | cut -d: -f2 | tail -1` $nb_cores = `grep processor /proc/cpuinfo | wc -l` ['INT'].each do |signal| trap(signal) do `killall InformationPolicy/InformationPolicy` Process.waitall for netbot in $netbots `ifconfig #{netbot['interface']} down` #FileUtils.rm "InformationPolicy/#{netbot['node_id']}.sqlite" end `rm -f InformationPolicy/[1-9]*.sqlite` #`rm -f [1-9]*.log` Process.exit!(true) end end end def start_netbot(netbot_count, ip_addr, node_perf) node_id = ip_addr.delete(".").to_i node_name = ip_addr begin $mysql.query "INSERT INTO node VALUES (#{node_id}, 2011, \"#{node_name}\", \"#{ip_addr}\",\"GHT\", #{Time.now.gmt_offset.to_i}, \"#{$cpu}\", \"#{`uname -ro`}\", 1, #{node_perf[0]}, #{node_perf[1]*1024}, #{node_perf[4]*1024}, 0, 0, 0,\"\")" rescue Mysql::Error => e end pid = Process.fork do join_time = 60_000 * $poisson_join.probability { |x| x == netbot_count/10} FileUtils.cp "InformationPolicy/infos.sqlite", "InformationPolicy/#{node_id}.sqlite" ENV['database'] = "InformationPolicy/#{node_id}.sqlite" begin $mysql.query("INSERT INTO component VALUES (#{$component_id}, #{node_id}, 2011, #{node_id}, \"#{node_name}\", #{join_time}, #{Time.now.to_i}, 0,0)") $mysql.query("INSERT INTO creator VALUES (#{node_id}, #{$component_id}, #{node_id}, 2011, \"Ghislain Fouodji Tasse\", \"\", \"Ghislain Fouodji Tasse\")") rescue Mysql::Error => e puts e end sleep join_time Process.exec("InformationPolicy/InformationPolicy", "#{node_perf[0]}", "#{node_perf[1]}", "#{node_perf[2]}", "#{node_perf[3]}", "#{node_perf[4]}", "#{ip_addr}", "#{$component_id}" ) end sleep 3 puts "started #{ip_addr} component with pid #{pid}" pid end def stop_netbot(netbot) puts "stopping #{netbot['pid']} simutation" end_reason = 1 begin 1.times do begin $mysql.real_query("UPDATE component SET trace_end = #{Time.now.to_i}, resolution = #{1} WHERE node_id = #{netbot['node_id']} AND component_id = #{$component_id}") rescue Mysql::Error => e puts e $mysql = Mysql.real_connect(ENV['sql_server'] , "root", nil, "fta") redo end end Process.kill 'TERM', netbot['pid'] Process.waitpid netbot['pid'], 0 #collect return status rescue Exception => e puts e `kill -9 #{netbot['pid']}` ensure #`ifconfig #{netbot['interface']} down` #FileUtils.rm "InformationPolicy/#{netbot['node_id']}.sqlite" end end def run_simulation (join_rate, fail_rate) netbot_count = $netbot_count_init $networks.each do |network| ipAddr = network 1.upto(5) do |i| ipAddr.next! netbot_count += 1 interface = "eth1:#{netbot_count - $netbot_count_init}" `ifconfig #{interface} #{ipAddr} up` node_perf = [ %w[1 2 3 4].choice.to_i, %w[256 512 768 1024 1536 2048 3072].choice.to_i, 0, 128, 1000] pid = start_netbot( netbot_count, ipAddr, node_perf ) fail_time = 120 + 60000 * $poisson_fail.probability { |x| x == netbot_count/10} #100_000 $netbots[netbot_count - $netbot_count_init -1 ] = { 'pid' => pid, 'node_id' => ipAddr.delete(".").to_i, 'interface' => interface, 'fail_time' => fail_time } end end $is_running = true $netbots.sort! {|x,y| x['fail_time'] <=> y['fail_time']} $netbots.each_index do |i| if i == 0 then timeout = $netbots[i]['fail_time'] else timeout = $netbots[i]['fail_time'] - $netbots[i-1]['fail_time'] end begin puts "stop timeout = #{timeout}" unless timeout.zero? Timeout::timeout(timeout) do #stop after timeout. pid, status = Process.wait2 #Process.waitpid($req_pid, 0) end_reason = 2 puts "something failed in #{pid} with status #{status.exitstatus}" redo end end rescue Timeout::Error => te end_reason = 1 #normal rescue Exception => e puts e break end stop_netbot $netbots[i] unless $netbots[i].nil? end end def terminate_simulation puts 'terminating simulation' `killall InformationPolicy/InformationPolicy` Process.waitall $is_running = false for netbot in $netbots `ifconfig #{netbot['interface']} down` #FileUtils.rm "InformationPolicy/#{netbot['node_id']}.sqlite" end `rm -f InformationPolicy/[1-9]*.sqlite` #`rm -f [1-9]*.log` $netbots.clear end if __FILE__ == $0 `echo 0 > /selinux/enforce` `route add default dev eth1` init_simulation $poisson_join = Poisson.new($join_rate) $poisson_fail = Poisson.new($fail_rate) run_simulation $join_rate, $fail_rate terminate_simulation end