From ab1ca015bd5a23fe47ac499a3e84ac04ef5c8ff1 Mon Sep 17 00:00:00 2001 From: Kris Zentner Date: Wed, 7 Mar 2018 10:03:27 -0800 Subject: Separating network mash In Azure, receiving the network section of the JSON output is not guaranteed, and will crash this plugin in these cases. Signed-off-by: Kris Zentner --- lib/ohai/plugins/azure.rb | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/lib/ohai/plugins/azure.rb b/lib/ohai/plugins/azure.rb index cf173a59..7f74299d 100644 --- a/lib/ohai/plugins/azure.rb +++ b/lib/ohai/plugins/azure.rb @@ -68,16 +68,20 @@ Ohai.plugin(:Azure) do end # create the basic structure we'll store our data in - def initialize_metadata_mash + def initialize_metadata_mash_compute metadata = Mash.new metadata["compute"] = Mash.new + metadata + end + def initialize_metadata_mash_network + metadata = Mash.new metadata["network"] = Mash.new metadata["network"]["interfaces"] = Mash.new %w{public_ipv4 local_ipv4 public_ipv6 local_ipv6}.each do |type| metadata["network"][type] = [] end metadata - end + end def fetch_ip_data(data, type, field) ips = [] @@ -93,27 +97,31 @@ Ohai.plugin(:Azure) do endpoint_data = fetch_metadata return nil if endpoint_data.nil? - metadata = initialize_metadata_mash + metadata = initialize_metadata_mash_compute # blindly add everything in compute to our data structure endpoint_data["compute"].each do |k, v| metadata["compute"][k] = v end + + # receiving network output is not guaranteed + unless endpoint_data["network"].nil? + metadata = initialize_metadata_mash_network + # parse out per interface interface IP data + endpoint_data["network"]["interface"].each do |int| + metadata["network"]["interfaces"][int["macAddress"]] = Mash.new + metadata["network"]["interfaces"][int["macAddress"]]["mac"] = int["macAddress"] + metadata["network"]["interfaces"][int["macAddress"]]["public_ipv6"] = fetch_ip_data(int, "ipv6", "publicIpAddress") + metadata["network"]["interfaces"][int["macAddress"]]["public_ipv4"] = fetch_ip_data(int, "ipv4", "publicIpAddress") + metadata["network"]["interfaces"][int["macAddress"]]["local_ipv6"] = fetch_ip_data(int, "ipv6", "privateIpAddress") + metadata["network"]["interfaces"][int["macAddress"]]["local_ipv4"] = fetch_ip_data(int, "ipv4", "privateIpAddress") + end - # parse out per interface interface IP data - endpoint_data["network"]["interface"].each do |int| - metadata["network"]["interfaces"][int["macAddress"]] = Mash.new - metadata["network"]["interfaces"][int["macAddress"]]["mac"] = int["macAddress"] - metadata["network"]["interfaces"][int["macAddress"]]["public_ipv6"] = fetch_ip_data(int, "ipv6", "publicIpAddress") - metadata["network"]["interfaces"][int["macAddress"]]["public_ipv4"] = fetch_ip_data(int, "ipv4", "publicIpAddress") - metadata["network"]["interfaces"][int["macAddress"]]["local_ipv6"] = fetch_ip_data(int, "ipv6", "privateIpAddress") - metadata["network"]["interfaces"][int["macAddress"]]["local_ipv4"] = fetch_ip_data(int, "ipv4", "privateIpAddress") - end - - # aggregate the total IP data - %w{public_ipv4 local_ipv4 public_ipv6 local_ipv6}.each do |type| - metadata["network"]["interfaces"].each_value do |val| - metadata["network"][type].concat val[type] unless val[type].empty? + # aggregate the total IP data + %w{public_ipv4 local_ipv4 public_ipv6 local_ipv6}.each do |type| + metadata["network"]["interfaces"].each_value do |val| + metadata["network"][type].concat val[type] unless val[type].empty? + end end end -- cgit v1.2.1