class Object
Constants
- IPROUTE_INT_REGEX
Match the lead line for an interface from iproute2 3: eth0 at eth0.11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP The '@eth0:' portion doesn't exist on primary interfaces and thus is optional in the regex
Public Instance Methods
# File lib/ohai/plugins/solaris2/network.rb, line 68 def arpname_to_ifname(iface, arpname) iface.keys.each do |ifn| return ifn if ifn.split(':')[0].eql?(arpname) end nil end
common *bsd code for collecting modules data
# File lib/ohai/plugins/kernel.rb, line 40 def bsd_modules(path) modules = Mash.new so = shell_out("#{Ohai.abs_path(path)}") so.stdout.lines do |line| # 1 7 0xc0400000 97f830 kernel if line =~ /(\d+)\s+(\d+)\s+([0-9a-fx]+)\s+([0-9a-fx]+)\s+([a-zA-Z0-9\_]+)/ modules[$5] = { :size => $4, :refcount => $2 } end end modules end
# File lib/ohai/plugins/hostname.rb, line 59 def collect_domain # Domain is everything after the first dot if fqdn fqdn =~ /.+?\.(.*)/ domain $1 end end
# File lib/ohai/plugins/hostname.rb, line 67 def collect_hostname # Hostname is everything before the first dot if machinename machinename =~ /(.+?)\./ hostname $1 elsif fqdn fqdn =~ /(.+?)\./ hostname $1 end end
# File lib/ohai/plugins/joyent.rb, line 39 def collect_pkgsrc if File.exist?('/opt/local/etc/pkg_install.conf') sm_pkgsrc = ::File.read("/opt/local/etc/pkg_install.conf").split("=") sm_pkgsrc[1].chomp else nil end end
# File lib/ohai/plugins/joyent.rb, line 27 def collect_product_file lines = [] if ::File.exists?("/etc/product") ::File.open("/etc/product") do |file| while line = file.gets lines << line end end end lines end
# File lib/ohai/plugins/solaris2/virtualization.rb, line 24 def collect_solaris_guestid command = '/usr/sbin/zoneadm list -p' so = shell_out(command) so.stdout.split(':').first end
# File lib/ohai/plugins/uptime.rb, line 33 def collect_uptime(path) # kern.boottime: { sec = 1232765114, usec = 823118 } Fri Jan 23 18:45:14 2009 so = shell_out("#{Ohai.abs_path(path)} kern.boottime") so.stdout.lines do |line| if line =~ /kern.boottime:\D+(\d+)/ usec = Time.new.to_i - $1.to_i return [usec, seconds_to_human(usec)] end end return [nil, nil] end
Make top-level cloud hashes
# File lib/ohai/plugins/cloud.rb, line 30 def create_objects cloud Mash.new cloud[:public_ips] = Array.new cloud[:private_ips] = Array.new end
# File lib/ohai/plugins/linux/mdadm.rb, line 22 def create_raid_device_mash(stdout) device_mash = Mash.new device_mash[:device_counts] = Mash.new stdout.lines.each do |line| case line when /Version\s+: ([0-9.]+)/ device_mash[:version] = Regexp.last_match[1].to_f when /Raid Level\s+: raid([0-9]+)/ device_mash[:level] = Regexp.last_match[1].to_i when /Array Size.*\(([0-9.]+)/ device_mash[:size] = Regexp.last_match[1].to_f when /State\s+: ([a-z]+)/ device_mash[:state] = Regexp.last_match[1] when /Total Devices\s+: ([0-9]+)/ device_mash[:device_counts][:total] = Regexp.last_match[1].to_i when /Raid Devices\s+: ([0-9]+)/ device_mash[:device_counts][:raid] = Regexp.last_match[1].to_i when /Working Devices\s+: ([0-9]+)/ device_mash[:device_counts][:working] = Regexp.last_match[1].to_i when /Failed Devices\s+: ([0-9]+)/ device_mash[:device_counts][:failed] = Regexp.last_match[1].to_i when /Active Devices\s+: ([0-9]+)/ device_mash[:device_counts][:active] = Regexp.last_match[1].to_i when /Spare Devices\s+: ([0-9]+)/ device_mash[:device_counts][:spare] = Regexp.last_match[1].to_i end end device_mash end
# File lib/ohai/plugins/darwin/network.rb, line 53 def darwin_encaps_lookup(ifname) return "Loopback" if ifname.eql?("lo") return "1394" if ifname.eql?("fw") return "IPIP" if ifname.eql?("gif") return "6to4" if ifname.eql?("stf") return "dot1q" if ifname.eql?("vlan") "Unknown" end
# File lib/ohai/plugins/darwin/network.rb, line 69 def excluded_setting?(setting) setting.match('_sw_cksum') end
# File lib/ohai/plugins/ssh_host_key.rb, line 23 def extract_keytype?(content) case content[0] when "ssh-dss" [ "dsa", nil ] when "ssh-rsa" [ "rsa", nil ] when /^ecdsa/ [ "ecdsa", content[0] ] else [ nil, nil ] end end
# File lib/ohai/plugins/sigar/network.rb, line 37 def fetch_interfaces(sigar) iface = Mash.new net_counters = Mash.new sigar.net_interface_list.each do |cint| iface[cint] = Mash.new if cint =~ /^(\w+)(\d+.*)/ iface[cint][:type] = $1 iface[cint][:number] = $2 end ifconfig = sigar.net_interface_config(cint) iface[cint][:encapsulation] = sigar_encaps_lookup(ifconfig.type) iface[cint][:addresses] = Mash.new # Backwards compat: loopback has no hwaddr if (ifconfig.flags & Sigar::IFF_LOOPBACK) == 0 iface[cint][:addresses][ifconfig.hwaddr] = { "family" => "lladdr" } end if ifconfig.address != "0.0.0.0" iface[cint][:addresses][ifconfig.address] = { "family" => "inet" } # Backwards compat: no broadcast on tunnel or loopback dev if (((ifconfig.flags & Sigar::IFF_POINTOPOINT) == 0) && ((ifconfig.flags & Sigar::IFF_LOOPBACK) == 0)) iface[cint][:addresses][ifconfig.address]["broadcast"] = ifconfig.broadcast end iface[cint][:addresses][ifconfig.address]["netmask"] = ifconfig.netmask end if ifconfig.prefix6_length != 0 iface[cint][:addresses][ifconfig.address6] = { "family" => "inet6" } iface[cint][:addresses][ifconfig.address6]["prefixlen"] = ifconfig.prefix6_length.to_s iface[cint][:addresses][ifconfig.address6]["scope"] = Sigar.net_scope_to_s(ifconfig.scope6) end iface[cint][:flags] = Sigar.net_interface_flags_to_s(ifconfig.flags).split(' ') iface[cint][:mtu] = ifconfig.mtu.to_s iface[cint][:queuelen] = ifconfig.tx_queue_len.to_s net_counters[cint] = Mash.new unless net_counters[cint] if (!cint.include?(":")) ifstat = sigar.net_interface_stat(cint) net_counters[cint][:rx] = { "packets" => ifstat.rx_packets.to_s, "errors" => ifstat.rx_errors.to_s, "drop" => ifstat.rx_dropped.to_s, "overrun" => ifstat.rx_overruns.to_s, "frame" => ifstat.rx_frame.to_s, "bytes" => ifstat.rx_bytes.to_s } net_counters[cint][:tx] = { "packets" => ifstat.tx_packets.to_s, "errors" => ifstat.tx_errors.to_s, "drop" => ifstat.tx_dropped.to_s, "overrun" => ifstat.tx_overruns.to_s, "carrier" => ifstat.tx_carrier.to_s, "collisions" => ifstat.tx_collisions.to_s, "bytes" => ifstat.tx_bytes.to_s } end end begin sigar.arp_list.each do |arp| next unless iface[arp.ifname] # this should never happen iface[arp.ifname][:arp] = Mash.new unless iface[arp.ifname][:arp] iface[arp.ifname][:arp][arp.address] = arp.hwaddr end rescue #64-bit AIX for example requires 64-bit caller end [iface, net_counters] end
# File lib/ohai/plugins/network.rb, line 59 def find_ip(family = "inet") ips=sorted_ips(family) # return if there isn't any #{family} address ! return [ nil, nil ] if ips.empty? # shortcuts to access default #{family} interface and gateway int_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] +"_interface" gw_attr = Ohai::Mixin::NetworkConstants::FAMILIES[family] + "_gateway" # If we have a default interface that has addresses, # populate the short-cut attributes ipaddress, ip6address and macaddress if network[int_attr] # working with the address(es) of the default network interface gw_if_ips = ips.select do |v| v[:iface] == network[int_attr] end if gw_if_ips.empty? Ohai::Log.warn("[#{family}] no ip address on #{network[int_attr]}") elsif network[gw_attr] and network["interfaces"][network[int_attr]] and network["interfaces"][network[int_attr]]["addresses"] if [ "0.0.0.0", "::", /^fe80:/ ].any? { |pat| pat === network[gw_attr] } # link level default route Ohai::Log.debug("link level default #{family} route, picking ip from #{network[gw_attr]}") r = gw_if_ips.first else # checking network masks r = gw_if_ips.select do |v| network_contains_address(network[gw_attr], v[:ipaddress], v[:iface]) end.first if r.nil? r = gw_if_ips.first Ohai::Log.debug("[#{family}] no ipaddress/mask on #{network[int_attr]} matching the gateway #{network[gw_attr]}, picking #{r[:ipaddress]}") else Ohai::Log.debug("[#{family}] Using default interface #{network[int_attr]} and default gateway #{network[gw_attr]} to set the default ip to #{r[:ipaddress]}") end end else # return the first ip address on network[int_attr] r = gw_if_ips.first end else r = ips.first Ohai::Log.debug("[#{family}] no default interface, picking the first ipaddress") end return [ nil, nil ] if r.nil? or r.empty? [ r[:ipaddress].to_s, r[:iface] ] end
# File lib/ohai/plugins/network.rb, line 112 def find_mac_from_iface(iface) r = network["interfaces"][iface]["addresses"].select{|k,v| v["family"]=="lladdr"} r.nil? or r.first.nil? ? nil : r.first.first end
# File lib/ohai/plugins/passwd.rb, line 7 def fix_encoding(str) str.force_encoding(Encoding.default_external) if str.respond_to?(:force_encoding) str end
sigar-only, from network_route plugin
# File lib/ohai/plugins/sigar/network.rb, line 104 def flags(flags) f = "" if (flags & Sigar::RTF_UP) != 0 f += "U" end if (flags & Sigar::RTF_GATEWAY) != 0 f += "G" end if (flags & Sigar::RTF_HOST) != 0 f += "H" end f end
hostname : short hostname machinename : output of hostname command (might be short on solaris) fqdn : result of canonicalizing hostname using DNS or /etc/hosts domain : domain part of FQDN
hostname and machinename should always exist fqdn and domain may be broken if DNS is broken on the host
# File lib/ohai/plugins/hostname.rb, line 41 def from_cmd(cmd) so = shell_out(cmd) so.stdout.split($/)[0] end
Fill cloud hash with azure values
# File lib/ohai/plugins/cloud.rb, line 207 def get_azure_values cloud[:vm_name] = azure["vm_name"] cloud[:public_ips] << azure['public_ip'] cloud[:public_ipv4] = azure['public_ip'] cloud[:public_fqdn] = azure['public_fqdn'] cloud[:public_hostname] = azure['public_fqdn'] cloud[:public_ssh_port] = azure['public_ssh_port'] if azure['public_ssh_port'] cloud[:public_winrm_port] = azure['public_winrm_port'] if azure['public_winrm_port'] cloud[:provider] = "azure" end
# File lib/ohai/plugins/linux/filesystem.rb, line 22 def get_blk_cmd(attr, have_lsblk) if have_lsblk attr = 'FSTYPE' if attr == 'TYPE' "lsblk -r -n -o NAME,#{attr}" else "blkid -s #{attr}" end end
# File lib/ohai/plugins/linux/filesystem.rb, line 31 def get_blk_regex(attr, have_lsblk) have_lsblk ? /^(\S+) (\S+)/ : /^(\S+): #{attr}="(\S+)"/ end
Fill cloud hash with ec2 values
# File lib/ohai/plugins/cloud.rb, line 86 def get_ec2_values cloud[:public_ips] << ec2['public_ipv4'] cloud[:private_ips] << ec2['local_ipv4'] cloud[:public_ipv4] = ec2['public_ipv4'] cloud[:public_hostname] = ec2['public_hostname'] cloud[:local_ipv4] = ec2['local_ipv4'] cloud[:local_hostname] = ec2['local_hostname'] cloud[:provider] = "ec2" end
# File lib/ohai/plugins/cloud.rb, line 159 def get_eucalyptus_values cloud[:public_ips] << eucalyptus['public_ipv4'] cloud[:private_ips] << eucalyptus['local_ipv4'] cloud[:public_ipv4] = eucalyptus['public_ipv4'] cloud[:public_hostname] = eucalyptus['public_hostname'] cloud[:local_ipv4] = eucalyptus['local_ipv4'] cloud[:local_hostname] = eucalyptus['local_hostname'] cloud[:provider] = "eucalyptus" end
# File lib/ohai/plugins/hostname.rb, line 78 def get_fqdn_from_sigar require 'sigar' sigar = Sigar.new sigar.fqdn end
Fill cloud hash with gce values
# File lib/ohai/plugins/cloud.rb, line 49 def get_gce_values cloud[:public_ipv4] = [] cloud[:local_ipv4] = [] public_ips = gce['instance']["networkInterfaces"].collect do |interface| if interface.has_key?('accessConfigs') interface['accessConfigs'].collect{|ac| ac['externalIp']} end end.flatten.compact private_ips = gce['instance']["networkInterfaces"].collect do |interface| interface['ip'] end.compact cloud[:public_ips] += public_ips cloud[:private_ips] += private_ips cloud[:public_ipv4] += public_ips cloud[:public_hostname] = nil cloud[:local_ipv4] += private_ips cloud[:local_hostname] = gce['instance']['hostname'] cloud[:provider] = "gce" end
Names rackspace ipv6 address for interface
Parameters¶ ↑
- name<Symbol>
-
Use :public_ip or :private_ip
- eth<Symbol>
-
Interface name of public or private ip
# File lib/ohai/plugins/rackspace.rb, line 73 def get_global_ipv6_address(name, eth) network[:interfaces][eth][:addresses].each do |key, info| # check if we got an ipv6 address and if its in global scope if info['family'] == 'inet6' && info['scope'] == 'Global' rackspace[name] = key break # break when we found an address end end end
Names linode ip address
name - symbol of ohai name (e.g. :public_ip) eth - Interface name (e.g. :eth0)
Alters linode mash with new interface based on name parameter
# File lib/ohai/plugins/linode.rb, line 45 def get_ip_address(name, eth) if eth_iface = network[:interfaces][eth] eth_iface[:addresses].each do |key, info| linode[name] = key if info['family'] == 'inet' end end end
# File lib/ohai/plugins/java.rb, line 23 def get_java_info java = Mash.new so = shell_out("java -mx64m -version") if so.exitstatus == 0 so.stderr.split(/\r?\n/).each do |line| case line when /java version \"([0-9\.\_]+)\"/ java[:version] = $1 when /^(.+Runtime Environment.*) \((build )?(.+)\)$/ java[:runtime] = { "name" => $1, "build" => $3 } when /^(.+ (Client|Server) VM) \(build (.+)\)$/ java[:hotspot] = { "name" => $1, "build" => $3 } end end languages[:java] = java if java[:version] end end
Fill cloud hash with linode values
# File lib/ohai/plugins/cloud.rb, line 136 def get_linode_values cloud[:public_ips] << linode['public_ip'] cloud[:private_ips] << linode['private_ip'] cloud[:public_ipv4] = linode['public_ipv4'] cloud[:public_hostname] = linode['public_hostname'] cloud[:local_ipv4] = linode['local_ipv4'] cloud[:local_hostname] = linode['local_hostname'] cloud[:provider] = "linode" end
# File lib/ohai/plugins/eucalyptus.rb, line 29 def get_mac_address(addresses) detected_addresses = addresses.detect { |address, keypair| keypair == {"family"=>"lladdr"} } if detected_addresses return detected_addresses.first else return "" end end
Fill cloud hash with openstack values
# File lib/ohai/plugins/cloud.rb, line 183 def get_openstack_values cloud[:public_ips] << openstack['public_ipv4'] cloud[:private_ips] << openstack['local_ipv4'] cloud[:public_ipv4] = openstack['public_ipv4'] cloud[:public_hostname] = openstack['public_hostname'] cloud[:local_ipv4] = openstack['local_ipv4'] cloud[:local_hostname] = openstack['local_hostname'] cloud[:provider] = openstack['provider'] end
Get the rackspace private networks
# File lib/ohai/plugins/rackspace.rb, line 99 def get_private_networks() so = shell_out('xenstore-ls vm-data/networking') if so.exitstatus == 0 networks = [] so.stdout.split("\n").map{|l| l.split('=').first.strip }.map do |item| _so = shell_out("xenstore-read vm-data/networking/#{item}") if _so.exitstatus == 0 networks.push(FFI_Yajl::Parser.new.parse(_so.stdout)) else Ohai::Log.debug('Unable to capture custom private networking information for Rackspace cloud') return false end end # these networks are already known to ohai, and are not 'private networks' networks.delete_if { |hash| hash['label'] == 'private' } networks.delete_if { |hash| hash['label'] == 'public' } end rescue Errno::ENOENT Ohai::Log.debug('Unable to capture custom private networking information for Rackspace cloud') nil end
Fill cloud hash with rackspace values
# File lib/ohai/plugins/cloud.rb, line 110 def get_rackspace_values cloud[:public_ips] << rackspace['public_ipv4'] if rackspace['public_ipv4'] cloud[:private_ips] << rackspace['local_ipv4'] if rackspace['local_ipv4'] cloud[:public_ipv4] = rackspace['public_ipv4'] cloud[:public_ipv6] = rackspace['public_ipv6'] cloud[:public_hostname] = rackspace['public_hostname'] cloud[:local_ipv4] = rackspace['local_ipv4'] cloud[:local_ipv6] = rackspace['local_ipv6'] cloud[:local_hostname] = rackspace['local_hostname'] cloud[:provider] = "rackspace" end
# File lib/ohai/plugins/linux/platform.rb, line 23 def get_redhatish_platform(contents) contents[/^Red Hat/i] ? "redhat" : contents[/(\w+)/i, 1].downcase end
# File lib/ohai/plugins/linux/platform.rb, line 27 def get_redhatish_version(contents) contents[/Rawhide/i] ? contents[/((\d+) \(Rawhide\))/i, 1].downcase : contents[/release ([\d\.]+)/, 1] end
Get the rackspace region
# File lib/ohai/plugins/rackspace.rb, line 85 def get_region() so = shell_out("xenstore-ls vm-data/provider_data") if so.exitstatus == 0 so.stdout.split("\n").each do |line| rackspace[:region] = line.split[2].delete('\"') if line =~ /^region/ end end rescue Errno::ENOENT Ohai::Log.debug("Unable to find xenstore-ls, cannot capture region information for Rackspace cloud") nil end
# File lib/ohai/plugins/ec2.rb, line 29 def has_ec2_mac? network[:interfaces].values.each do |iface| unless iface[:arp].nil? if iface[:arp].value?("fe:ff:ff:ff:ff:ff") Ohai::Log.debug("has_ec2_mac? == true") return true end end end Ohai::Log.debug("has_ec2_mac? == false") false end
# File lib/ohai/plugins/eucalyptus.rb, line 38 def has_euca_mac? network[:interfaces].values.each do |iface| has_mac = (get_mac_address(iface[:addresses]) =~ /^[dD]0:0[dD]:/) Ohai::Log.debug("has_euca_mac? == #{!!has_mac}") return true if has_mac end Ohai::Log.debug("has_euca_mac? == false") false end
Checks for matching linode kernel name
Returns true or false
# File lib/ohai/plugins/linode.rb, line 26 def has_linode_kernel? if kernel_data = kernel kernel_data[:release].split('-').last =~ /linode/ end end
Checks for rackspace provider attribute
Return¶ ↑
- true
-
If rackspace provider attribute found
- false
-
Otherwise
# File lib/ohai/plugins/rackspace.rb, line 36 def has_rackspace_metadata? so = shell_out("xenstore-read vm-data/provider_data/provider") if so.exitstatus == 0 so.stdout.strip.downcase == 'rackspace' end rescue Errno::ENOENT false end
On Mac OS X, the development tools include “stubs” for JVM executables that prompt the user to install the JVM if they desire. In our case we simply wish to detect if the JVM is there and do not want to trigger a popup window. As a workaround, we can run the java_home executable and check its exit status to determine if the `java` executable is the real one or the OS X stub. In the terminal, it looks like this:
$ /usr/libexec/java_home Unable to find any JVMs matching version "(null)". No Java runtime present, try --request to install. $ echo $? 1
This check always returns true when not on darwin because it is just a workaround for this particular annoyance.
# File lib/ohai/plugins/java.rb, line 58 def has_real_java? return true unless on_darwin? shell_out("/usr/libexec/java_home").status.success? end
Helpers
# File lib/ohai/plugins/aix/network.rb, line 26 def hex_to_dec_netmask(netmask) # example '0xffff0000' -> '255.255.0.0' dec = netmask[2..3].to_i(16).to_s(10) [4,6,8].each { |n| dec = dec + "." + netmask[n..n+1].to_i(16).to_s(10) } dec end
common initial kernel attribute values
# File lib/ohai/plugins/kernel.rb, line 29 def init_kernel kernel Mash.new [["uname -s", :name], ["uname -r", :release], ["uname -v", :version], ["uname -m", :machine]].each do |cmd, property| so = shell_out(cmd) kernel[property] = so.stdout.split($/)[0] end kernel end
# File lib/ohai/plugins/joyent.rb, line 48 def is_smartos? platform == 'smartos' end
# File lib/ohai/plugins/linux/network.rb, line 24 def linux_encaps_lookup(encap) return "Loopback" if encap.eql?("Local Loopback") || encap.eql?("loopback") return "PPP" if encap.eql?("Point-to-Point Protocol") return "SLIP" if encap.eql?("Serial Line IP") return "VJSLIP" if encap.eql?("VJ Serial Line IP") return "IPIP" if encap.eql?("IPIP Tunnel") return "6to4" if encap.eql?("IPv6-in-IPv4") return "Ethernet" if encap.eql?("ether") encap end
# File lib/ohai/plugins/darwin/network.rb, line 73 def locate_interface(ifaces, ifname, mac) return ifname unless ifaces[ifname].nil? # oh well, time to go hunting! return ifname.chop if ifname.match /\*$/ ifaces.keys.each do |ifc| ifaces[ifc][:addresses].keys.each do |addr| return ifc if addr.eql? mac end end nil end
# File lib/ohai/plugins/ec2.rb, line 42 def looks_like_ec2? # Try non-blocking connect so we don't "block" if # the Xen environment is *not* EC2 hint?('ec2') || has_ec2_mac? && can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR,80) end
# File lib/ohai/plugins/eucalyptus.rb, line 49 def looks_like_euca? # Try non-blocking connect so we don't "block" if # the Xen environment is *not* EC2 hint?('eucalyptus') || has_euca_mac? && can_metadata_connect?(Ohai::Mixin::Ec2Metadata::EC2_METADATA_ADDR,80) end
Identifies the linode cloud by preferring the hint, then
Returns true or false
# File lib/ohai/plugins/linode.rb, line 35 def looks_like_linode? hint?('linode') || has_linode_kernel? end
# File lib/ohai/plugins/linux/virtualization.rb, line 26 def lxc_version_exists? which('lxc-version') end
windows
# File lib/ohai/plugins/kernel.rb, line 53 def machine_lookup(sys_type) return "i386" if sys_type.eql?("X86-based PC") return "x86_64" if sys_type.eql?("x64-based PC") sys_type end
# File lib/ohai/plugins/network.rb, line 117 def network_contains_address(address_to_match, ipaddress, iface) # address_to_match: String # ipaddress: IPAddress # iface: String if peer = network["interfaces"][iface]["addresses"][ipaddress.to_s][:peer] IPAddress(peer) == IPAddress(address_to_match) else ipaddress.include? IPAddress(address_to_match) end end
# File lib/ohai/plugins/java.rb, line 63 def on_darwin? RUBY_PLATFORM.downcase.include?("darwin") end
windows
# File lib/ohai/plugins/kernel.rb, line 60 def os_lookup(sys_type) return "Unknown" if sys_type.to_s.eql?("0") return "Other" if sys_type.to_s.eql?("1") return "MSDOS" if sys_type.to_s.eql?("14") return "WIN3x" if sys_type.to_s.eql?("15") return "WIN95" if sys_type.to_s.eql?("16") return "WIN98" if sys_type.to_s.eql?("17") return "WINNT" if sys_type.to_s.eql?("18") return "WINCE" if sys_type.to_s.eql?("19") return nil end
# File lib/ohai/plugins/darwin/network.rb, line 23 def parse_media(media_string) media = Hash.new line_array = media_string.split(' ') 0.upto(line_array.length - 1) do |i| unless line_array[i].eql?("none") if line_array[i + 1] =~ /^\<([a-zA-Z\-\,]+)\>$/ media[line_array[i]] = Hash.new unless media.has_key?(line_array[i]) if media[line_array[i]].has_key?("options") $1.split(",").each do |opt| media[line_array[i]]["options"] << opt unless media[line_array[i]]["options"].include?(opt) end else media[line_array[i]]["options"] = $1.split(",") end else if line_array[i].eql?("autoselect") media["autoselect"] = Hash.new unless media.has_key?("autoselect") media["autoselect"]["options"] = [] end end else media["none"] = { "options" => [] } end end media end
# File lib/ohai/plugins/ip_scopes.rb, line 50 def private_addr?(address) address.to_ip.scope =~ /PRIVATE/ end
forward and reverse lookup to canonicalize FQDN (hostname -f equivalent) this is ipv6-safe, works on ruby 1.8.7+
# File lib/ohai/plugins/hostname.rb, line 48 def resolve_fqdn begin hostname = from_cmd("hostname") addrinfo = Socket.getaddrinfo(hostname, nil).first iaddr = IPAddr.new(addrinfo[3]) Socket.gethostbyaddr(iaddr.hton)[0] rescue nil end end
# File lib/ohai/plugins/ruby.rb, line 24 def run_ruby(command) cmd = "ruby -e \"require 'rbconfig'; #{command}\"" so = shell_out(cmd) so.stdout.strip end
# File lib/ohai/plugins/darwin/network.rb, line 62 def scope_lookup(scope) return "Node" if scope.eql?("::1") return "Link" if scope.match(/^fe80\:/) return "Site" if scope.match(/^fec0\:/) "Global" end
# File lib/ohai/plugins/sigar/network.rb, line 27 def sigar_encaps_lookup(encap) return "Loopback" if encap.eql?("Local Loopback") return "PPP" if encap.eql?("Point-to-Point Protocol") return "SLIP" if encap.eql?("Serial Line IP") return "VJSLIP" if encap.eql?("VJ Serial Line IP") return "IPIP" if encap.eql?("IPIP Tunnel") return "6to4" if encap.eql?("IPv6-in-IPv4") encap end
# File lib/ohai/plugins/hostname.rb, line 84 def sigar_is_available? begin require 'sigar' true rescue LoadError false end end
# File lib/ohai/plugins/solaris2/network.rb, line 60 def solaris_encaps_lookup(ifname) return "Ethernet" if ifname.eql?("e1000g") return "Ethernet" if ifname.eql?("eri") return "Ethernet" if ifname.eql?("net") return "Loopback" if ifname.eql?("lo") "Unknown" end
# File lib/ohai/plugins/network.rb, line 29 def sorted_ips(family = "inet") raise "bad family #{family}" unless [ "inet", "inet6" ].include? family # going to use that later to sort by scope scope_prio = [ "global", "site", "link", "host", "node", nil ] ipaddresses = [] # ipaddresses going to hold #{family} ipaddresses and their scope Mash[network['interfaces']].each do |iface, iface_v| next if iface_v.nil? or not iface_v.has_key? 'addresses' iface_v['addresses'].each do |addr, addr_v| next if addr_v.nil? or not addr_v.has_key? "family" or addr_v['family'] != family ipaddresses << { :ipaddress => addr_v["prefixlen"] ? IPAddress("#{addr}/#{addr_v["prefixlen"]}") : IPAddress("#{addr}/#{addr_v["netmask"]}"), :scope => addr_v["scope"].nil? ? nil : addr_v["scope"].downcase, :iface => iface } end end # sort ip addresses by scope, by prefixlen and then by ip address # 128 - prefixlen: longest prefixes first ipaddresses.sort_by do |v| [ ( scope_prio.index(v[:scope]) or 999999 ), 128 - v[:ipaddress].prefix.to_i, ( family == "inet" ? v[:ipaddress].to_u32 : v[:ipaddress].to_u128 ) ] end end
# File lib/ohai/plugins/ip_scopes.rb, line 54 def tunnel_iface?(interface) interface['type'] == 'ppp' end
# File lib/ohai/plugins/windows/network.rb, line 23 def windows_encaps_lookup(encap) return "Ethernet" if encap.eql?("Ethernet 802.3") encap end