~ubuntu-branches/debian/wheezy/byobu/wheezy

« back to all changes in this revision

Viewing changes to usr/lib/byobu/ec2_cost

  • Committer: Package Import Robot
  • Author(s): Alexander Chernyakhovsky
  • Date: 2012-01-11 19:37:34 UTC
  • mfrom: (0.5.3) (0.1.162 precise)
  • Revision ID: package-import@ubuntu.com-20120111193734-89083z97py39gj05
Tags: 5.2-1
* Syncing from Ubuntu. Remaining changes:
  - Switch to dh short syntax
  - Added a substvar ${perl:Depends} dependency into debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
}
33
33
 
34
34
__ec2_cost() {
35
 
        local zone type file_to_stat cpu mem rate hours tx_gb rx_gb network_cost uptime_cost total_cost interface
 
35
        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"
36
36
        # Try to use metadata service
37
37
        if metadata_available; then
38
 
                zone=$(wget -q -O - http://169.254.169.254/latest/meta-data/placement/availability-zone)
39
 
                if [ ! -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ]; then
40
 
                        zone="us-east-1d"
41
 
                fi
 
38
                [ -s "$cache.zone" ] || wget -q -O "$cache.zone" http://169.254.169.254/latest/meta-data/placement/availability-zone 2>/dev/null &
 
39
                sleep 0.02
 
40
                [ -s "$cache.zone" ] && read zone < "$cache.zone"
 
41
                [ -r "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}" ] || zone="us-east-1d"
42
42
                . "$BYOBU_PREFIX/share/$PKG/ec2/rates.${zone%?}"
43
 
                type=$(wget -q -O - http://169.254.169.254/latest/meta-data/instance-type | $SED -e "s/\./_/g")
 
43
                [ -s "$cache.type" ] || wget -q -O "$cache.type" http://169.254.169.254/latest/meta-data/instance-type 2>/dev/null &
 
44
                sleep 0.02
 
45
                [ -s "$cache.type" ] && type=$($SED -e "s/\./_/g" "$cache.type")
44
46
                eval rate="\$$type"
45
 
                file_to_stat="$BYOBU_RUN_DIR/cache.$BYOBU_BACKEND/ec2_cost.instance_type"
46
 
                [ -f "$file_to_stat" ] || wget -q -O "$file_to_stat" http://169.254.169.254/latest/meta-data/instance-type
 
47
                file_to_stat="$cache.type"
47
48
        fi
48
49
        if [ -z "$rate" ]; then
49
50
                . "$BYOBU_PREFIX/share/$PKG/ec2/rates."*
73
74
        hours=$(((`date +%s` - `stat --printf %Y $file_to_stat`) / 60 / 60 + 1))
74
75
        # Auto detect network interface
75
76
        interface=`tail -n1 /proc/net/route  | awk '{print $1}'`
76
 
        ifconfig_out=`LC_ALL=C /sbin/ifconfig "$interface"`
77
 
        # Calculate bandwidth cost
78
 
        tx_gb=${ifconfig_out#*RX bytes:}
79
 
        tx_gb=$(echo ${tx_gb%% *} | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }')
80
 
        rx_gb=${ifconfig_out#*TX bytes:}
81
 
        rx_gb=$(echo ${rx_gb%% *} | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }')
 
77
        local iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed
 
78
        while read iface rbytes rpackets rerrs rdrop rfifo rframe rcompressed rmulticast tbytes tpackets terrs tdrop tfifo tcolls tcarrier tcompressed; do
 
79
                if [ "$iface" = "${interface}:" ]; then                 
 
80
                        tx_gb=${tbytes}
 
81
                        rx_gb=${rbytes}
 
82
                        break;                                         
 
83
                fi                                                     
 
84
        done < /proc/net/dev
 
85
        tx_gb=$(echo ${tx_gb} | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }')
 
86
        rx_gb=$(echo ${rx_gb} | awk '{ printf "%f", $1 / 1024 / 1024 / 1024 }')
82
87
        network_cost=`echo "$tx_gb" "$TX_RATE" "$rx_gb" "$RX_RATE" | awk '{printf "%f %f", $1*$2, $3*$4}' | awk '{printf "%f", $1 + $2}'`
83
88
        # Calculate uptime cost
84
89
        uptime_cost=`echo "$hours" | awk "{printf \"%f\", "$rate" * $hours}"`