From 3aaa914c9708ddf54fc2b9d45a5e530d1892fe67 Mon Sep 17 00:00:00 2001 From: Dustin Kirkland Date: Wed, 23 Nov 2011 12:50:58 -0600 Subject: [PATCH] * usr/lib/byobu/ec2_cost, usr/lib/byobu/hostname, usr/lib/byobu/include/shutil, usr/lib/byobu/ip_address: LP: #894038 - background all wgets, as these can be expensive (cough OpenStack cough) - sleep for 0.02 seconds after each wget, as most EC2 wgets take less than 0.01 seconds, but some might take longer - after backgrounded wget and sleep, attempt to read the results of the wget from a cache file; if this succeeds, proceed and use it - if not, fail through gracefully, and pick up the results from the cach file on the next run --- debian/changelog | 9 +++++++++ usr/lib/byobu/ec2_cost | 17 +++++++++-------- usr/lib/byobu/hostname | 8 ++++++-- usr/lib/byobu/include/shutil | 16 +++++++++++++--- usr/lib/byobu/ip_address | 26 +++++++++++++++++--------- 5 files changed, 54 insertions(+), 22 deletions(-) diff --git a/debian/changelog b/debian/changelog index 3baca69d..9b045eb0 100644 --- a/debian/changelog +++ b/debian/changelog @@ -10,6 +10,15 @@ byobu (4.50) unreleased; urgency=low * usr/share/byobu/profiles/tmux: - fix action bar color * usr/lib/byobu/include/mondrian: + * usr/lib/byobu/ec2_cost, usr/lib/byobu/hostname, + usr/lib/byobu/include/shutil, usr/lib/byobu/ip_address: LP: #894038 + - background all wgets, as these can be expensive (cough OpenStack cough) + - sleep for 0.02 seconds after each wget, as most EC2 wgets take less + than 0.01 seconds, but some might take longer + - after backgrounded wget and sleep, attempt to read the results of the + wget from a cache file; if this succeeds, proceed and use it + - if not, fail through gracefully, and pick up the results from the + cach file on the next run -- Dustin Kirkland Fri, 18 Nov 2011 15:41:36 -0600 diff --git a/usr/lib/byobu/ec2_cost b/usr/lib/byobu/ec2_cost index 31c9de4d..ed24a550 100755 --- a/usr/lib/byobu/ec2_cost +++ b/usr/lib/byobu/ec2_cost @@ -32,18 +32,19 @@ __ec2_cost_detail() { } __ec2_cost() { - local zone type file_to_stat cpu mem rate hours tx_gb rx_gb network_cost uptime_cost total_cost interface + local zone type file_to_stat cpu mem rate hours tx_gb rx_gb network_cost uptime_cost total_cost interface cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ec2_cost" # Try to use metadata service if metadata_available; then - zone=$(wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone) - if [ ! -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ]; then - zone="us-east-1d" - fi + [ -s "$cache.zone" ] || wget -q -O "$cache.zone" http://169.254.169.254/latest/meta-data/placement/availability-zone 2>/dev/null & + sleep 0.02 + [ -s "$cache.zone" ] && read zone < "$cache.zone" + [ -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ] || zone="us-east-1d" . "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" - type=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-type | $SED -e "s/\./_/g") + [ -s "$cache.type" ] || wget -q -O "$cache.type" http://169.254.169.254/latest/meta-data/instance-type 2>/dev/null & + sleep 0.02 + [ -s "$cache.type" ] && type=$($SED -e "s/\./_/g" "$cache.type") eval rate="\$$type" - file_to_stat="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ec2_cost.instance_type" - [ -f "$file_to_stat" ] || wget -q -O "$file_to_stat" http://169.254.169.254/latest/meta-data/instance-type + file_to_stat="$cache.type" fi if [ -z "$rate" ]; then . "$BYOBU_PREFIX/share/$PKG/ec2/rates."* diff --git a/usr/lib/byobu/hostname b/usr/lib/byobu/hostname index b95edca6..57cac139 100755 --- a/usr/lib/byobu/hostname +++ b/usr/lib/byobu/hostname @@ -24,9 +24,13 @@ __hostname_detail() { } __hostname() { + local h= if metadata_available; then - # We're in EC2, so get our public hostname - h=$(wget -q -O - http://169.254.169.254/latest/meta-data/public-hostname) + local cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/hostname" + # Background a retrieval of our public hostname + wget -q -O "$cache" http://169.254.169.254/latest/meta-data/public-hostname 2>/dev/null & + sleep 0.02 + [ -s "$cache" ] && read h < "$cache" else h=$(hostname -s 2>/dev/null || hostname) fi diff --git a/usr/lib/byobu/include/shutil b/usr/lib/byobu/include/shutil index b43f4368..aadd5de1 100755 --- a/usr/lib/byobu/include/shutil +++ b/usr/lib/byobu/include/shutil @@ -209,11 +209,21 @@ metadata_available() { # This is really ugly. We need a reliable, fast way of determining # if a metadata service is available, that does NOT slow down non-ec2 # machines. - if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then - wget -q -O- --timeout=1 --tries=1 http://169.254.169.254 >/dev/null 2>&1 + local x=0 cache="$BYOBU_CONFIG_DIR/.metadata_available" + # First, check the cache + if [ -s "$cache" ]; then + # Metadata is non-empty, so we have metadata available + x=1 else - false + # Must seed the cache + if [ -e /etc/ec2_version ] || [ -e /usr/sbin/update-grub-legacy-ec2 ]; then + # This *looks* like a machine with metadata, so background a potentially slow check + wget -q -O "$cache" --timeout=10 --tries=1 http://169.254.169.254 2>/dev/null & + sleep 0.02 + [ -s "$cache" ] && x=1 + fi fi + [ "$x" = "1" ] } status_freq() { diff --git a/usr/lib/byobu/ip_address b/usr/lib/byobu/ip_address index 0269caec..c272208c 100755 --- a/usr/lib/byobu/ip_address +++ b/usr/lib/byobu/ip_address @@ -26,7 +26,7 @@ __ip_address_detail() { } __ip_address() { - local interface ipaddr + local interface ipaddr cache="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ip_address" # Allow interface overrides in $BYOBU_CONFIG_DIR/statusrc if [ -n "$MONITORED_NETWORK" ]; then interface="$MONITORED_NETWORK" @@ -39,21 +39,29 @@ __ip_address() { case "$IPV6" in 1|true|yes) if [ "$IP_EXTERNAL" = "1" ]; then - ipaddr=$(wget -O- -q http://v6.ipv6-test.com/api/myip.php) + # Background an update + wget -q -O "$cache" http://v6.ipv6-test.com/api/myip.php 2>/dev/null & + sleep 0.02 else - ipaddr=$(LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global 2>/dev/null) - # Print 'None' if we have no global address - [ -z "$ipaddr" ] && ipaddr="None" - ipaddr=${ipaddr#* inet6 } - ipaddr=${ipaddr%%/*} + # Background an update + LC_ALL=C /sbin/ip -6 addr list dev "$interface" scope global >"$cache" 2>/dev/null & fi + [ -s "$cache" ] && read ipaddr < "$cache" + # Print 'None' if we have no global address + [ -z "$ipaddr" ] && ipaddr="None" + ipaddr=${ipaddr#* inet6 } + ipaddr=${ipaddr%%/*} ;; *) if [ "$IP_EXTERNAL" = "1" ]; then - ipaddr=$(wget -O- -q http://v4.ipv6-test.com/api/myip.php) + wget -q -O "$cache" http://v4.ipv6-test.com/api/myip.php 2>/dev/null & + sleep 0.02 + [ -s "$cache" ] && read ipaddr < "$cache" elif metadata_available; then # We're in EC2, so get our public IP address - ipaddr=$(wget -q -O- http://169.254.169.254/latest/meta-data/public-ipv4) + wget -q -O "$cache" http://169.254.169.254/latest/meta-data/public-ipv4 2>/dev/null & + sleep 0.02 + [ -s "$cache" ] && read ipaddr < "$cache" else ipaddr=$(LC_ALL=C /sbin/ip -4 addr list dev "$interface" scope global 2>/dev/null) ipaddr=${ipaddr#* inet }