PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/ohai/plugins/solaris2/network.rb

https://github.com/joyent/ohai
Ruby | 159 lines | 94 code | 12 blank | 53 comment | 17 complexity | 1aaca5738a5617473b36172f00142478 MD5 | raw file
Possible License(s): Apache-2.0
  1. #
  2. # Author:: Benjamin Black (<nostromo@gmail.com>)
  3. # Copyright:: Copyright (c) 2008 Opscode, Inc.
  4. # License:: Apache License, Version 2.0
  5. #
  6. # Licensed under the Apache License, Version 2.0 (the "License");
  7. # you may not use this file except in compliance with the License.
  8. # You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing, software
  13. # distributed under the License is distributed on an "AS IS" BASIS,
  14. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. # See the License for the specific language governing permissions and
  16. # limitations under the License.
  17. #
  18. # EXAMPLE SOLARIS IFCONFIG OUTPUT; CURRENTLY, ONLY SIMPLE STUFF IS SUPPORTED (E.G., NO TUNNELS)
  19. # DEAR SUN: YOU GET AN F FOR YOUR IFCONFIG
  20. #lo0:3: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
  21. # inet 127.0.0.1 netmask ff000000
  22. #e1000g0:3: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 3
  23. # inet 72.2.115.28 netmask ffffff80 broadcast 72.2.115.127
  24. #e1000g2:1: flags=201000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4,CoS> mtu 1500 index 4
  25. # inet 10.2.115.28 netmask ffffff80 broadcast 10.2.115.127
  26. # inet6 2001:0db8:3c4d:55:a00:20ff:fe8e:f3ad/64
  27. #ip.tun0: flags=2200851<UP,POINTOPOINT,RUNNING,MULTICAST,NONUD,IPv6> mtu 1480 index 3
  28. # inet tunnel src 109.146.85.57 tunnel dst 109.146.85.212
  29. # tunnel security settings --> use 'ipsecconf -ln -i ip.tun1'
  30. # tunnel hop limit 60
  31. # inet6 fe80::6d92:5539/10 --> fe80::6d92:55d4
  32. #ip.tun0:1: flags=2200851<UP,POINTOPOINT,RUNNING,MULTICAST,NONUD,IPv6> mtu 1480 index 3
  33. # inet6 2::45/128 --> 2::46
  34. #lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index 1
  35. # inet 127.0.0.1 netmask ff000000
  36. #eri0: flags=1004843<UP,BROADCAST,RUNNING,MULTICAST,DHCP,IPv4> mtu 1500 \
  37. #index 2
  38. # inet 172.17.128.208 netmask ffffff00 broadcast 172.17.128.255
  39. #ip6.tun0: flags=10008d1<UP,POINTOPOINT,RUNNING,NOARP,MULTICAST,IPv4> \
  40. #mtu 1460
  41. # index 3
  42. # inet6 tunnel src fe80::1 tunnel dst fe80::2
  43. # tunnel security settings --> use 'ipsecconf -ln -i ip.tun1'
  44. # tunnel hop limit 60 tunnel encapsulation limit 4
  45. # inet 10.0.0.208 --> 10.0.0.210 netmask ff000000
  46. #qfe1: flags=2000841<UP,RUNNING,MULTICAST,IPv6> mtu 1500 index 3
  47. # usesrc vni0
  48. # inet6 fe80::203:baff:fe17:4be0/10
  49. # ether 0:3:ba:17:4b:e0
  50. #vni0: flags=2002210041<UP,RUNNING,NOXMIT,NONUD,IPv6,VIRTUAL> mtu 0
  51. # index 5
  52. # srcof qfe1
  53. # inet6 fe80::203:baff:fe17:4444/128
  54. provides "network"
  55. require 'scanf'
  56. def encaps_lookup(ifname)
  57. return "Ethernet" if ifname.eql?("e1000g")
  58. return "Ethernet" if ifname.eql?("eri")
  59. return "Loopback" if ifname.eql?("lo")
  60. "Unknown"
  61. end
  62. def arpname_to_ifname(iface, arpname)
  63. iface.keys.each do |ifn|
  64. return ifn if ifn.split(':')[0].eql?(arpname)
  65. end
  66. nil
  67. end
  68. iface = Mash.new
  69. popen4("ifconfig -a") do |pid, stdin, stdout, stderr|
  70. stdin.close
  71. cint = nil
  72. stdout.each do |line|
  73. if line =~ /^([0-9a-zA-Z\.\:\-]+): \S+ mtu (\d+) index (\d+)/
  74. cint = $1
  75. iface[cint] = Mash.new unless iface[cint]
  76. iface[cint][:mtu] = $2
  77. iface[cint][:index] = $3
  78. if line =~ / flags\=\d+\<((ADDRCONF|ANYCAST|BROADCAST|CoS|DEPRECATED|DHCP|DUPLICATE|FAILED|FIXEDMTU|INACTIVE|LOOPBACK|MIP|MULTI_BCAST|MULTICAST|NOARP|NOFAILOVER|NOLOCAL|NONUD|NORTEXCH|NOXMIT|OFFLINE|POINTOPOINT|PREFERRED|PRIVATE|ROUTER|RUNNING|STANDBY|TEMPORARY|UNNUMBERED|UP|VIRTUAL|XRESOLV|IPv4|IPv6|,)+)\>\s/
  79. flags = $1.split(',')
  80. else
  81. flags = Array.new
  82. end
  83. iface[cint][:flags] = flags.flatten
  84. if cint =~ /^(\w+)(\d+.*)/
  85. iface[cint][:type] = $1
  86. iface[cint][:number] = $2
  87. iface[cint][:encapsulation] = encaps_lookup($1)
  88. end
  89. end
  90. if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8})\s*$/
  91. iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
  92. iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*"."}
  93. end
  94. if line =~ /\s+inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) netmask (([0-9a-f]){1,8}) broadcast (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/
  95. iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
  96. iface[cint][:addresses][$1] = { "family" => "inet", "netmask" => $2.scanf('%2x'*4)*".", "broadcast" => $4 }
  97. end
  98. if line =~ /\s+inet6 ([a-f0-9\:]+)(\s*|(\%[a-z0-9]+)\s*)\/(\d+)\s*$/
  99. iface[cint][:addresses] = Mash.new unless iface[cint][:addresses]
  100. iface[cint][:addresses][$1] = { "family" => "inet6", "prefixlen" => $4 }
  101. end
  102. end
  103. end
  104. popen4("arp -an") do |pid, stdin, stdout, stderr|
  105. stdin.close
  106. stdout.each do |line|
  107. if line =~ /([0-9a-zA-Z]+)\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s+(\w+)\s+([a-zA-Z0-9\.\:\-]+)/
  108. next unless iface[arpname_to_ifname(iface, $1)] # this should never happen, except on solaris because sun hates you.
  109. iface[arpname_to_ifname(iface, $1)][:arp] = Mash.new unless iface[arpname_to_ifname(iface, $1)][:arp]
  110. iface[arpname_to_ifname(iface, $1)][:arp][$2] = $5
  111. end
  112. end
  113. end
  114. iface.keys.each do |ifn|
  115. iaddr = nil
  116. if iface[ifn][:encapsulation].eql?("Ethernet")
  117. iface[ifn][:addresses].keys.each do |addr|
  118. if iface[ifn][:addresses][addr]["family"].eql?("inet")
  119. iaddr = addr
  120. break
  121. end
  122. end
  123. if iface[ifn][:arp]
  124. iface[ifn][:arp].keys.each do |addr|
  125. if addr.eql?(iaddr)
  126. iface[ifn][:addresses][iface[ifn][:arp][iaddr]] = { "family" => "lladdr" }
  127. break
  128. end
  129. end
  130. end
  131. end
  132. end
  133. network[:interfaces] = iface
  134. popen4("route -n get default") do |pid, stdin, stdout, stderr|
  135. stdin.close
  136. route_get = stdout.read
  137. matches = /interface: (\S+)/.match(route_get)
  138. if matches
  139. Ohai::Log.debug("found gateway device: #{$1}")
  140. network[:default_interface] = matches[1]
  141. end
  142. matches = /gateway: (\S+)/.match(route_get)
  143. if matches
  144. Ohai::Log.debug("found gateway: #{$1}")
  145. network[:default_gateway] = matches[1]
  146. end
  147. end