Sha256: a37f72113a13a5853c4e0c70f98e0cc5cd39d0d895ba69f21bddb91e1b985f23

Contents?: true

Size: 1.98 KB

Versions: 7

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

class Netdisco
  class Output
    class Dot
      INDENT        = " " * 2
      DEFAULT_COLOR = "black"
      def self.output(output)
        new(output).to_s
      end

      def to_s
        str = "graph Netdisco {\n"
        @output.to_a.each do |host|
          host_label = label(host)
          # 调整图形修饰符
          str += INDENT + id(host) + "[label=\"#{host_label}\" color=\"#{color(host_label)}\"]\n"

          if @peers.has_key? host
            @peers[host].each do |peer|
              peer_name = @resolve ? peer.name : peer.ip
              next if (not CFG.dot.bothlinks) && @connections.include?([peer_name, host].sort)
              @connections << [peer_name, host].sort
              labels = ""
              labels = "[headlabel=\"#{peer.dst}\" taillabel=\"#{peer.src}\"]" if CFG.dot.linklabel
              str << INDENT + INDENT + id(host) + " -- " + id(peer_name) + labels + "\n"
            end
          end
        end
        str += "}\n"
        str
      end

      private
        def initialize(output)
          @output      = output
          @connections = []
          @peers       = @output.peers
          @resolve     = @output.resolve
        end

        def id(host)
          host = host.gsub(/[-.]/, "_")
          "_" + host
        end

        def label(want_host)
          label = nil
          return want_host if CFG.ipname == true
          @peers.each do |host, peers|
            peers.each do |peer|
              got_host = @resolve ? peer.name : peer.ip
              if want_host == got_host
                label = peer.raw_name
                break
              end
            end
            break if label
          end
          label or want_host
        end

        def color(host)
          color = nil
          CFG.dot.color.each do |re, clr|
            if host.match re
              color = clr
              break
            end
          end
          color or DEFAULT_COLOR
        end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
netdisco-0.0.8 lib/netdisco/output/dot.rb
netdisco-0.0.7 lib/netdisco/output/dot.rb
netdisco-0.0.6 lib/netdisco/output/dot.rb
netdisco-0.0.5 lib/netdisco/output/dot.rb
netdisco-0.0.4 lib/netdisco/output/dot.rb
netdisco-0.0.3 lib/netdisco/output/dot.rb
netdisco-0.0.2 lib/netdisco/output/dot.rb