module Cloud66 module Utils class VitalSigns def self.system_info # system info return parse_data(`facter`) end def self.address_info # AWS special case if $config.is_aws result = {} reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4` result[:ext_ipv4] = reported_ip if reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/local-ipv4` result[:int_ipv4] = reported_ip if reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ return result # GC special case elsif $config.is_gc result = {} external_ip = `/usr/bin/curl -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip` result[:ext_ipv4] = external_ip if external_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ internal_ip = `/usr/bin/curl -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/ip` result[:int_ipv4] = internal_ip if internal_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ return result else interfaces_raw = `facter interfaces` interfaces = interfaces_raw.split(',').select { |interface| interface =~ /^eth/ } # don't have any ip address info raise 'no address information' if interfaces.empty? # address information facter_command = "facter #{interfaces.map { |interface| "ipaddress_#{interface} ipaddress6_#{interface}" }.join(' ')}" hash = parse_data(`#{facter_command}`) # don't have any ip address info raise 'no address information' if hash.empty? # return all interface information return hash end end def self.is_aws? # 6 seconds to find out if this is a AWS image or not! instance = `/usr/bin/curl --connect-timeout 6 -s http://169.254.169.254/latest/meta-data/instance-id` rescue nil return false if instance.nil? || instance.empty? # now check the external ip to make sure we're not just being routed reported_ip = `/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-ipv4` rescue nil return false if reported_ip.nil? || reported_ip.empty? return false unless reported_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ # it is aws return true end def self.is_gc? # 6 seconds to find out if this is a AWS image or not! external_ip = `/usr/bin/curl --connect-timeout 6 -s -H "Metadata-Flavor: Google" http://metadata/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip` rescue nil return false if external_ip.nil? || external_ip.empty? return false unless external_ip =~ /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/ # it is google cloud return true end private # parse the data, not using YAML due to YAML parsing issues and JSON output not always working def self.parse_data(data) hash = {} data.lines.each do |line| split = line.split('=>') if split.size == 2 key = split[0] value = split[1] unless value.nil? value = value.strip # exclude empty or long results (like ssh keys) if !value.empty? && value.size < 100 key = key.strip hash[key] = value end end end end return hash end end end end