From e05c2ce9f1e9cb6b84248a5dd9a21a2e5e16f133 Mon Sep 17 00:00:00 2001 From: Josh Wilsdon Date: Mon, 11 Jul 2011 19:19:48 -0700 Subject: [PATCH] [HVM-476] merge hvm sysinfo changes. --- src/sysinfo | 279 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 242 insertions(+), 37 deletions(-) diff --git a/src/sysinfo b/src/sysinfo index cbba463f7..7a3a2cb47 100755 --- a/src/sysinfo +++ b/src/sysinfo @@ -16,9 +16,12 @@ #set -o xtrace +PATH=/bin:/usr/bin:/sbin:/usr/sbin:/smartdc/bin:/opt/local/bin:/opt/local/sbin + CACHE_FILE_PARSABLE="/tmp/.sysinfo.parsable" CACHE_FILE_JSON="/tmp/.sysinfo.json" CACHE="true" +UNAME_S=$(uname -s) DETECT_NICS= FORCE= PARSABLE= @@ -54,7 +57,7 @@ do esac done -if [[ $(zonename) != "global" ]]; then +if [[ ${UNAME_S} == "SunOS" && $(zonename) != "global" ]]; then echo "This program is for use in the global zone only." exit 1 fi @@ -75,9 +78,13 @@ if [[ -z ${FORCE} ]]; then fi fi -configfile="$(svcprop -p 'joyentfs/usb_copy_path' svc:/system/filesystem/smartdc:default 2>/dev/null)/config" -if [[ ! -f ${configfile} ]]; then - configfile="/mnt/$(svcprop -p 'joyentfs/usb_mountpoint' svc:/system/filesystem/smartdc:default 2>/dev/null)/config" +if [[ ${UNAME_S} == "SunOS" ]]; then + configfile="$(svcprop -p 'joyentfs/usb_copy_path' svc:/system/filesystem/smartdc:default 2>/dev/null)/config" + if [[ ! -f ${configfile} ]]; then + configfile="/mnt/$(svcprop -p 'joyentfs/usb_mountpoint' svc:/system/filesystem/smartdc:default 2>/dev/null)/config" + fi +else + configfile="/opt/smartdc/config/node.config" fi # helper to set global "normalized" to the expanded version of MAC ($1) @@ -122,17 +129,62 @@ function get_smbios_system_info() #echo "${Manufacturer}" } +function get_dmidecode_system_info() +{ + # This puts the variables we're pulling out into the local environment + eval $(dmidecode -t 1 \ + | egrep "Manufacturer: |Product Name: |Serial Number: |UUID: " \ + | sed -e 's/^ *//' \ + | sed -e 's/: /="/' \ + | sed -e 's/ *$/"/' \ + | sed -e 's/Product Name/Product/' \ + | sed -e 's/Serial Number/Serial_Number/') + + if [[ -z ${Manufacturer} && -z ${Product} && -z ${Serial_Number} ]]; then + # If we didn't get Manufacturer and stuff, try from Motherboard + eval $(dmidecode -t 2 \ + | egrep "Manufacturer: |Product Name: |Serial Number: " \ + | sed -e 's/^ *//' \ + | sed -e 's/: /="/' \ + | sed -e 's/ *$/"/' \ + | sed -e 's/Product Name/Product/' \ + | sed -e 's/Serial Number/Serial_Number/') + fi + + # overwrite UUID if config dictates otherwise + tmp_uuid=$(/usr/bin/bootparams | grep "^override_uuid=" | cut -f2 -d'=') + if [[ -n ${tmp_uuid} ]]; then + UUID=${tmp_uuid} + fi + + # On Solaris the UUID comes back lower case, so MAPI expects that. + UUID=$(echo ${UUID} | tr [:upper:] [:lower:]) + + #echo "${UUID}" + #echo "${Product}" + #echo "${Serial_Number}" + #echo "${Manufacturer}" +} + function get_memory_mib() { - # Get (misnamed in prtconf) memory size in Mebibytes - Memory_in_MiB=`prtconf \ - | grep "Memory size: [0-9]* Megabytes" \ - | cut -d' ' -f3` + if [[ ${UNAME_S} == "SunOS" ]]; then + # Get (misnamed in prtconf) memory size in Mebibytes + Memory_in_MiB=`prtconf \ + | grep "Memory size: [0-9]* Megabytes" \ + | cut -d' ' -f3` + else + Memory_in_MiB=$(($(cat /proc/meminfo \ + | grep "^MemTotal:" \ + | tr -s ' ' \ + | cut -d ' ' -f2) \ + / 1024)) + fi #echo "${Memory_in_MiB}" } -function get_cpu_info() +function get_smartos_cpu_info() { CPU_Version=`smbios -t SMB_TYPE_PROCESSOR | grep -v "Version: 0000000000" \ | grep "Version: " \ @@ -162,14 +214,52 @@ function get_cpu_info() #echo "${CPU_Cores}" } +function get_linux_cpu_info() +{ + CPU_Version=$(grep "^model name" /proc/cpuinfo \ + | cut -d ':' -f2 \ + | tr -s ' ' \ + | sed -e "s/^ //" \ + | head -n1) + + CPU_Cores=$(grep "^processor " /proc/cpuinfo \ + | wc -l \ + | tr -d ' ') + + CPU_Count=$(grep "^core id .*: 0$" /proc/cpuinfo \ + | wc -l \ + | tr -d ' ') + + if [[ ${CPU_Count} -eq 0 ]]; then + CPU_Count=${CPU_Cores} + fi + + CPU_Virtualization=$(egrep -m 1 -o "(vmx|svm)" /proc/cpuinfo) + if [[ -z ${CPU_Virtualization} ]]; then + CPU_Virtualization="none" + fi + + #echo "${CPU_Version}" + #echo "${CPU_Count}" + #echo "${CPU_Cores}" +} + function get_live_image_buildstamp() { # Add joyent buildstamp to SYSTEM_INFO - Live_Image=$(uname -v | sed -e "s/.*_//") - + if [[ ${UNAME_S} == "SunOS" ]]; then + Live_Image=$(uname -v | sed -e "s/.*_//") + else + Live_Image=HVM-$(cat /etc/sdc.buildstamp) + fi #echo "${Live_Image}" } +function get_system_type() +{ + Uname_System=${UNAME_S} +} + function get_hostname() { Hostname=$(hostname) @@ -177,19 +267,30 @@ function get_hostname() function get_disks() { - ORIGIFS=$IFS + if [[ ${UNAME_S} == "SunOS" ]]; then + ORIGIFS=$IFS - # set $IFS to end-of-line - IFS=`echo -en "\n\b"` + # set $IFS to end-of-line + IFS=`echo -en "\n\b"` - count=1 - for line in $(/usr/bin/disklist -s 2>/dev/null); do - Disks[${count}]=${line} - ((count++)) - done + count=1 + for line in $(/usr/bin/disklist -s 2>/dev/null); do + Disks[${count}]=${line} + ((count++)) + done - # set $IFS back - IFS=$ORIGIFS + # set $IFS back + IFS=$ORIGIFS + else + count=1 + for line in $(sfdisk -s | grep "^\/dev\/" | tr -d ' ' | sed -e "s/\/dev\///"); do + dev=$(echo ${line} | cut -d ':' -f1 | tr -- '-' '_') + size=$(echo ${line} | cut -d ':' -f2) + size=$((${size} * 1024)) + Disks[${count}]="${dev}=${size}" + ((count++)) + done + fi #for entry in "${Disks[@]}" #do @@ -204,7 +305,7 @@ function get_nic_mappings() { admin_nic=`/usr/bin/bootparams 2>/dev/null | grep "admin_nic" | cut -d '=' -f2-` if [[ -z ${admin_nic} ]] && [[ -f ${configfile} ]]; then - admin_nic=`grep "^admin_nic=" ${configfile} | cut -f2 -d'='` + admin_nic=`grep "^admin_nic=" ${configfile} | cut -f2- -d'='` fi if [[ -n ${admin_nic} ]]; then normalize_mac ${admin_nic} @@ -212,7 +313,7 @@ function get_nic_mappings() fi external_nic=`/usr/bin/bootparams 2>/dev/null | grep "external_nic" | cut -d '=' -f2-` if [[ -z ${external_nic} ]] && [[ -f ${configfile} ]]; then - external_nic=`grep "^external_nic=" ${configfile} | cut -f2 -d'='` + external_nic=`grep "^external_nic=" ${configfile} | cut -f2- -d'='` fi if [[ -n ${external_nic} ]]; then normalize_mac ${external_nic} @@ -220,7 +321,7 @@ function get_nic_mappings() fi internal_nic=`/usr/bin/bootparams 2>/dev/null | grep "internal_nic" | cut -d '=' -f2-` if [[ -z ${internal_nic} ]] && [[ -f ${configfile} ]]; then - internal_nic=`grep "^internal_nic=" ${configfile} | cut -f2 -d'='` + internal_nic=`grep "^internal_nic=" ${configfile} | cut -f2- -d'='` fi if [[ -n ${internal_nic} ]]; then normalize_mac ${internal_nic} @@ -232,8 +333,7 @@ function get_nic_mappings() #echo "${INTERNAL_NIC}" } - -function get_network_interfaces() +function get_smartos_network_interfaces() { count=1 nic_tag_count=1 @@ -318,6 +418,90 @@ function get_network_interfaces() #done } +function get_linux_network_interfaces() +{ + count=1 + nic_tag_count=1 + NicTagList="admin" + seen_nic_tag_admin="true" + + for iface in $(ifconfig -a | grep "^eth" | grep -v "\.[0-9]" | cut -d ' ' -f1); do + nicnames= + iface_info=$(ifconfig ${iface}) + mac=$(echo ${iface_info} | grep -o "HWaddr \([0-9a-z\:]*\)" | cut -d' ' -f2) + normalize_mac ${mac} + mac=${normalized} + ip4addr=$(echo ${iface_info} | grep -o "inet addr:\([0-9\.]*\)" | cut -d':' -f2) + + if [[ -n "${ADMIN_NIC}" ]] && [[ "${ADMIN_NIC}" == "${mac}" ]]; then + nicnames="admin" + NIC_admin=${iface} + fi + if [[ -n "${EXTERNAL_NIC}" ]] && [[ "${EXTERNAL_NIC}" == "${mac}" ]]; then + [[ -n ${nicnames} ]] && nicnames="${nicnames}," + nicnames="${nicnames}external" + NicTagList="${NicTagList},external" + NIC_external=${iface} + fi + if [[ -n "${INTERNAL_NIC}" ]] && [[ "${INTERNAL_NIC}" == "${mac}" ]]; then + [[ -n ${nicnames} ]] && nicnames="${nicnames}," + nicnames="${nicnames}internal" + NicTagList="${NicTagList},internal" + NIC_internal=${iface} + fi + + for tag in "${NicTagsMacs[@]}"; do + tag_fields=(${tag//=/ }) + tag_name=${tag_fields[0]} + normalize_mac ${tag_fields[1]} + tag_mac=${normalized} + if [[ "${tag_mac}" == "${mac}" ]]; then + [[ -n ${nicnames} ]] && nicnames="${nicnames}," + nicnames="${nicnames}${tag_name}" + NicTags[${nic_tag_count}]="${tag_name}=${iface}" + ((nic_tag_count++)) + fi + + # Build up the list of tags + eval "seen_nic_tag=\${seen_nic_tag_${tag_name}}" + if [[ -z "${seen_nic_tag}" ]]; then + NicTagList="${NicTagList},${tag_name}" + eval "seen_nic_tag_${tag_name}=true" + fi + done + + NetworkInterfaces[${count}]=${iface} + eval "Network_Interface_${iface}_MAC_Address=${mac}" + eval "Network_Interface_${iface}_IPv4_Address=${ip4addr}" + if [[ -n ${nicnames} ]]; then + eval "Network_Interface_${iface}_NIC_Names=${nicnames}" + fi + + link_status=$(ethtool ${iface} | grep -o "Link detected: .*" | cut -d' ' -f3-) + case ${link_status} in + yes) + link_status="up" + ;; + *) + link_status="unknown" + ;; + esac + eval "Network_Interface_${iface}_Link_Status=${link_status}" + ((count++)) + done + + #echo "${NIC_admin}" + #echo "${NIC_external}" + #echo "${NIC_internal}" + #for iface in "${NetworkInterfaces[@]}" + #do + #eval "mac=\${Network_Interface_${iface}_MAC_Address}" + #eval "ipv4=\${Network_Interface_${iface}_IPv4_Address}" + #eval "nicnames=\${Network_Interface_${iface}_NIC_Names}" + #echo "mac: ${mac} ---- ${ipv4} ---- ${nicnames}" + #done +} + function get_nic_tags() { count=1 @@ -358,11 +542,17 @@ function detect_nic_tags() { function get_bootparams() { + WHICH_GREP="/usr/xpg4/bin/grep" + + if [[ ${UNAME_S} == "Linux" ]]; then + WHICH_GREP=grep + fi + count=1 for line in $(/usr/bin/bootparams); do fields=(${line//=/ }) key=$(echo ${fields[0]} | sed -e "s/\-/\_/g") - if ! (echo "${key}" | /usr/xpg4/bin/grep \ + if ! (echo "${key}" | ${WHICH_GREP} \ -e "^tty" \ -e "^atapi" \ -e "^ata_dma" \ @@ -386,6 +576,7 @@ function output_parsable() { cat <> ${CACHE_FILE_PARSABLE}.new \ - && chmod 600 ${CACHE_FILE_PARSABLE}.new \ - && mv ${CACHE_FILE_PARSABLE}.new ${CACHE_FILE_PARSABLE} - output_json >> ${CACHE_FILE_JSON}.new \ - && chmod 600 ${CACHE_FILE_JSON}.new \ - && mv ${CACHE_FILE_JSON}.new ${CACHE_FILE_JSON} + output_parsable >> ${CACHE_FILE_PARSABLE}.new.$$ \ + && chmod 600 ${CACHE_FILE_PARSABLE}.new.$$ \ + && mv ${CACHE_FILE_PARSABLE}.new.$$ ${CACHE_FILE_PARSABLE} + output_json >> ${CACHE_FILE_JSON}.new.$$ \ + && chmod 600 ${CACHE_FILE_JSON}.new.$$ \ + && mv ${CACHE_FILE_JSON}.new.$$ ${CACHE_FILE_JSON} fi # if we also want output, give it now