Sha256: 6bf04203cfe2483619fc32f7d2593007c9b8a5c63eff117bad65f6dd7bd4e7da

Contents?: true

Size: 1.36 KB

Versions: 2

Compression:

Stored size: 1.36 KB

Contents

# -*- coding: utf-8 -*-

module Dcmgr
  module Helpers
    module NicHelper
      def find_nic(ifindex = 2)
        ifindex_map = {}
        Dir.glob("/sys/class/net/*/ifindex").each do |ifindex_path|
          device_name = File.split(File.split(ifindex_path).first)[1]
          ifindex_num = File.readlines(ifindex_path).first.strip
          ifindex_map[ifindex_num] = device_name
        end
        #p ifindex_map
        ifindex_map[ifindex.to_s]
      end

      def nic_state(if_name = 'eth0')
        operstate_path = "/sys/class/net/#{if_name}/operstate"
        if File.exists?(operstate_path)
          File.readlines(operstate_path).first.strip
        end
      end
      
      # This method cleans up ugly mac addressed stored in the dcmgr database.
      # Mac addresses in the database are stored as alphanumeric strings without
      # the : inbetween them. This method properly puts those in there.
      def clean_mac(mac,delim = ':')
        mac.unpack('A2'*6).join(delim)
      end

      def is_natted?(vnic_map)
        not vnic_map[:ipv4][:nat_address].nil?
      end

      def valid_nic?(nic)
        ifindex_path = "/sys/class/net/#{nic}/ifindex"
        if FileTest.exist?(ifindex_path)
          true
        else
          logger.warn("#{nic}: error fetching interface information: Device not found")
          false
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
wakame-vdc-agents-11.12.0 lib/dcmgr/helpers/nic_helper.rb
wakame-vdc-dcmgr-11.12.0 lib/dcmgr/helpers/nic_helper.rb