lib/ovsimager/ovsimager.rb in ovsimager-0.0.3 vs lib/ovsimager/ovsimager.rb in ovsimager-0.0.4

- old
+ new

@@ -5,20 +5,21 @@ require_relative 'tcpdump' require_relative 'dotwriter' module OVSImager class OVSImager - DOT_FILENAME = 'interfaces.dot' - PNG_FILENAME = 'interfaces.png' + DEFAULT_DOT_FILENAME = 'interfaces.dot' + DEFAULT_PNG_FILENAME = 'interfaces.png' def initialize(dump_mode=false, ping_from=nil, ping_to=nil) @netns = IPNetNS.new @ifaces = @netns.ifaces_hash - @linbr = LinuxBridge.new + @linbr = LinuxBridge.new(@netns.ns) @ovsvs = OVSVS.new - @dotwriter = DotWriter.new(DOT_FILENAME) + @dot_filename = DEFAULT_DOT_FILENAME + @png_filename = DEFAULT_PNG_FILENAME @dump_mode = dump_mode @mark = {} @dump_result = {} @ping_from = ping_from @@ -39,16 +40,21 @@ end opts.on("-t ADDRESS", "--to ADDRESS", "send ping to specified address") do |v| @ping_to = v end + opts.on("-o FILENAME.png", "--out FILENAME.png", + "output PNG filename (default: interfaces.png)") do |v| + @png_filename = v + @dot_filename = v.sub(/\.png$/i, '') + '.dot' + end end.parse! end def execute_dump return unless @dump_mode - tcpdump = TcpDump.new(@ping_from && @ping_to, @ping_from, @ping_to) + tcpdump = TcpDump.new(!!@ping_to, @ping_from, @ping_to) @dump_result = tcpdump.test(@ifaces) @dump_result.each do |(iface, result)| if result[0] && result[2] @mark[iface] = '*' elsif result[0] @@ -58,21 +64,22 @@ end end end def show_all + @dotwriter = DotWriter.new(@dot_filename) show_ovsvs puts '-' * 80 show_linbr puts '-' * 80 show_netns - @dotwriter.finish(PNG_FILENAME) if @dotwriter + @dotwriter.finish(@png_filename) @dotwriter = nil end private - def show_iface_common(name, inet, patch, tag='', ns='') + def show_iface_common(name, inet, patch='', tag='', ns='') puts " [#{@mark[name]||' '}] #{name}#{tag}#{patch}\t" + "#{inet.join(',')}\t#{ns}" end def show_iface(iface) @@ -81,65 +88,80 @@ end def show_ovsvs @ovsvs.to_hash[:bridges].each do |br| puts "OVS Bridge #{br[:name]}:" - @dotwriter.br_begin(br[:name], 'OVS ') + @dotwriter.bridge(br[:name], 'OVS ') do |dot_br| - br[:ports].each do |port| - name = port[:name] - iface = @ifaces[name] || {} - inet = iface[:inet] || [] - tag = port[:tag] ? ' (tag=' + port[:tag] + ')' : '' - peer = port[:peer] || iface[:peer] - remote = port[:remote_ip] ? - " #{port[:local_ip] || ''} => #{port[:remote_ip]}" : '' - patch = peer ? ' <-> ' + peer : remote - ns = iface[:ns] == :root ? '' : iface[:ns] + br[:ports].each do |port| + name = port[:name] + iface = @ifaces[name] || {} + inet = iface[:inet] || [] + tag = port[:tag] ? ' (tag=' + port[:tag] + ')' : '' + peer = port[:peer] || iface[:peer] + remote = port[:remote_ip] ? + " #{port[:local_ip] || ''} => #{port[:remote_ip]}" : '' + patch = peer ? ' <-> ' + peer : remote + ns = iface[:ns] == :root ? '' : iface[:ns] - show_iface_common(name, inet, patch, tag, ns) - @dotwriter.br_iface(name, @mark[name], @dump_result[name], + show_iface_common(name, inet, patch, tag, ns) + dot_br.add_iface(name, @mark[name], @dump_result[name], inet, tag, peer, remote) - @done[name] = true + @done[name] = true + + port[:interfaces].each do |port_if| + port_name = port_if[:name] + if port_name != name + print " " + port_inet = @ifaces[port_name] && @ifaces[port_name][:inet] + show_iface_common(port_name, inet) + dot_br.add_iface(port_name, @mark[port_name], + @dump_result[port_name], port_inet, + '', ' '+name) + ### + @done[port_name] = true + end + end + end + end - @dotwriter.br_end end end def show_linbr @linbr.to_hash.each do |name, br| puts "Bridge #{name}" - @dotwriter.br_begin(name, '') - br[:interfaces].each do |ifname| - iface = @ifaces[ifname] - next unless iface - @dotwriter.br_iface(ifname, @mark[ifname], @dump_result[ifname], - iface[:inet], iface[:tag], iface[:peer]) - show_iface iface - @done[ifname] = true + @dotwriter.bridge(name, '') do |dot_br| + br[:interfaces].each do |ifname| + iface = @ifaces[ifname] + next unless iface + dot_br.add_iface(ifname, @mark[ifname], @dump_result[ifname], + iface[:inet], iface[:tag], iface[:peer]) + show_iface iface + @done[ifname] = true + end end - @dotwriter.br_end end end def show_netns @netns.to_hash.each do |name, ifaces| puts "Namespace #{name}" - @dotwriter.ns_begin(name) - ifaces.each do |iface| - ifname = iface[:name] - if ifname != 'lo' and !iface[:inet].empty? - if @done[ifname] - @dotwriter.ns_br_iface(ifname) - else - @dotwriter.ns_iface(ifname, @mark[ifname], @dump_result[ifname], - iface[:inet], iface[:tag], iface[:peer]) + @dotwriter.namespace(name) do |dot_ns| + ifaces.each do |iface| + ifname = iface[:name] + if ifname != 'lo' and !iface[:inet].empty? + if @done[ifname] + dot_ns.add_br_iface(ifname) + else + dot_ns.add_iface(ifname, @mark[ifname], @dump_result[ifname], + iface[:inet], iface[:tag], iface[:peer]) + end end + show_iface iface unless @done[iface[:name]] + @done[iface[:name]] = true end - show_iface iface unless @done[iface[:name]] - @done[iface[:name]] = true end - @dotwriter.ns_end end end end end