diff options
author | kaustubh-d <kaustubh@clogeny.com> | 2013-07-29 06:06:39 -0500 |
---|---|---|
committer | adamedx <adamed@opscode.com> | 2013-08-19 11:52:45 -0700 |
commit | 25ebdd161cae3eb756c25e2a93e9253d8a1e9422 (patch) | |
tree | 68e6f04e4acaadc8b397ae613cdcba5a942e64b4 | |
parent | 9649a1b5f6126b93a34ae7fd3e0d575ae065eb93 (diff) | |
download | ohai-25ebdd161cae3eb756c25e2a93e9253d8a1e9422.tar.gz |
implement network plugin for aix.
-rw-r--r-- | lib/ohai/plugins/aix/network.rb | 141 |
1 files changed, 138 insertions, 3 deletions
diff --git a/lib/ohai/plugins/aix/network.rb b/lib/ohai/plugins/aix/network.rb index 7071abef..d7668d2b 100644 --- a/lib/ohai/plugins/aix/network.rb +++ b/lib/ohai/plugins/aix/network.rb @@ -1,6 +1,6 @@ # -# Author:: Doug MacEachern <dougm@vmware.com> -# Copyright:: Copyright (c) 2010 VMware, Inc. +# Author:: Kaustubh Deorukhkar (<kaustubh@clogeny.com>) +# Copyright:: Copyright (c) 2013 Opscode, Inc. # License:: Apache License, Version 2.0 # # Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,4 +16,139 @@ # limitations under the License. # -require_plugin "sigar::network" +require 'ipaddr' +provides "network", "counters/network" + +# Loads following information. +# :default_interface, :default_gateway - route -n get 0 +# :interfaces +# => routes(netstat -nr | grep en0) +# => addresses (ifconfig en0 or lsattr -El en0), macaddress (entstat -d en0 = Hardware Address: be:42:80:00:b0:05) +# => flags (ifconfig en0) +# => state up/down (ifconfig/lsattr) +# => arp (arp -an) + +iface = Mash.new + + +# :default_interface, :default_gateway - route -n get 0 +popen4("route -n get 0") do |pid, stdin, stdout, stderr| + stdin.close + stdout.each do |line| + case line + when /gateway: (\S+)/ + network[:default_gateway] = $1 + when /interface: (\S+)/ + network[:default_interface] = $1 + end + end +end + +# Helpers +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 + +# List the interfaces in system. +popen4("lsdev -Cc if") do |pid, stdin, stdout, stderr| + stdin.close + stdout.each do |line| + if line =~ /(\S+) (\S+)(\s+)(.+)/ + interface = $1 + iface[interface] = Mash.new unless iface[interface] + iface[interface][:state] = ($2 == 'Available' ? 'up' : 'down') + iface[interface][:description] = $3 + + # Query the interface information + popen4("ifconfig #{interface}") do |if_pid, if_stdin, if_stdout, if_stderr| + if_stdin.close + if_stdout.each do |line| + case line + when /^#{interface}:\sflags=\S+<(\S+)>/ + iface[interface][:flags] = $1.split(',') + iface[interface][:metric] = $1 if line =~ /metric\s(\S+)/ + else + # We have key value pairs. + if line =~ /inet (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})(\/(\d{1,2}))?/ + tmp_addr, tmp_prefix = $1, $3 + if tmp_prefix.nil? + netmask = hex_to_dec_netmask($1) if line =~ /netmask\s(\S+)\s/ + unless netmask + tmp_prefix ||= "32" + IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s + end + else + netmask = IPAddr.new("255.255.255.255").mask(tmp_prefix.to_i).to_s + end + + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses][tmp_addr] = { "family" => "inet", "prefixlen" => tmp_prefix } + iface[interface][:addresses][tmp_addr][:netmask] = netmask + + if line =~ /broadcast\s(\S+)\s/ + iface[interface][:addresses][tmp_addr][:broadcast] = $1 + end + elsif line =~ /inet6 ([a-f0-9\:]+)\/(\d+)/ + # TODO do we have more properties on inet6 in aix? broadcast + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + iface[interface][:addresses][$1] = { "family" => "inet6", "prefixlen" => $2 } + else + # load all key-values, example "tcp_sendspace 131072 tcp_recvspace 131072 rfc1323 1" + properties = line.split + n = properties.length/2 - 1 + for i in 0..n + iface[interface][properties[i]] = properties[(i == 0 ? 1 : i*2)] + end + end + end + end + end #ifconfig stdout + + # Query macaddress + popen4("entstat -d #{interface} | grep \"Hardware Address\"") do |e_pid, e_stdin, e_stdout, e_stderr| + e_stdin.close + iface[interface][:addresses] = Mash.new unless iface[interface][:addresses] + e_stdout.each do |line| + iface[interface][:addresses][$1.upcase] = { "family" => "lladdr" } if line =~ /Hardware Address: (\S+)/ + end + end + end + end #lsdev stdout +end + +# Query routes information +['inet', 'inet6'].each do |family| + popen4("netstat -nrf #{family}") do |n_pid, n_stdin, n_stdout, n_stderr| + n_stdin.close + n_stdout.each do |line| + if line =~ /(\S+)\s+(\S+)\s+(\S+)\s+(\d+)\s+(\d+)\s+(\S+)/ + interface = $6 + iface[interface][:routes] = Array.new unless iface[interface][:routes] + iface[interface][:routes] << Mash.new( :destination => $1, :family => family, + :via => $2, :flags => $3) + end + end + end +end + +# List the arp entries in system. +popen4("arp -an") do |pid, stdin, stdout, stderr| + stdin.close + count = 0 + stdout.each do |line| + network[:arp] = Mash.new unless network[:arp] + if line =~ /\s*(\S+) \((\S+)\) at ([a-fA-F0-9\:]+) \[(\w+)\] stored in bucket/ + network[:arp][count] = Mash.new unless network[:arp][count] + network[:arp][count][:remote_host] = $1 + network[:arp][count][:remote_ip] = $2 + network[:arp][count][:remote_mac] = $3.downcase + count += 1 + end + end +end + +network["interfaces"] = iface + |