# HG changeset patch
# User Simon Horman <ho...@verge.net.au>
# Date 1278569160 -32400
# Node ID 110d056193472fa64ffabd3069d5ed20d32b01c2
# Parent  e823bf55e0d875bcd9dc1668f24547345b9bdf81
low: Use awk instead of bash to calculate memory and disk sizes

The initial motivation for this work was to remove bashisms.
However it also simplifies the code and fixes some correctness issues.

Signed-off-by: Simon Horman <ho...@verge.net.au>

diff -r e823bf55e0d8 -r 110d05619347 extra/resources/SysInfo
--- a/extra/resources/SysInfo   Mon Jul 05 17:17:24 2010 +0900
+++ b/extra/resources/SysInfo   Thu Jul 08 15:06:00 2010 +0900
@@ -185,6 +185,17 @@
     fi
 }
 
+SysInfo_megabytes() {
+    # Size in megabytes
+    echo $1 | awk '{ n = $0;
+                    sub(/[0-9]+(.[0-9]+)?/, "");
+                    split(n, a, $0);
+                     n=a[1];
+                     if ($0 == "G" || $0 == "") { n *= 1024 };
+                     if (/^kB?/) { n /= 1024 };
+                     printf "%d\n", n }' # Intentionaly round to an integer
+}
+
 SysInfo_mem_units() {
     mem=$1
 
@@ -192,92 +203,20 @@
        return
     fi
 
-    memlen=`expr ${#mem} - 1`
-    memlen_alt=`expr ${#mem} - 2`    
-    if [ ${mem:$memlen:1} = "G" ]; then
-       mem="${mem:0:$memlen}"
-       if [ $mem != ${mem/./} ]; then
-           mem_before=${mem/.*/}
-           mem_after=${mem/*./}
-           mem=$[mem_before*1024]
-           if [ ${#mem_after} = 0 ]; then
-               :
-           elif [ ${#mem_after} = 1 ]; then
-               mem=$[mem+100*$mem_after]
-           elif [ ${#mem_after} = 2 ]; then
-               mem=$[mem+10*$mem_after]
-           elif [ ${#mem_after} = 3 ]; then
-               mem=$[mem+$mem_after]
-           else 
-               mem_after=${mem_after:0:3}
-               mem=$[mem+$mem_after]
-           fi
-       fi
-    elif [ ${mem:$memlen:1} = "M" ]; then
-       mem=${mem/.*/}
-       mem="${mem:0:$memlen}"
-    elif [ ${mem:$memlen:1} = "k" ]; then
-       mem="${mem:0:$memlen}"
-       mem=${mem/.*/}
-       mem=`expr $mem / 1024`
-    elif [ ${mem:$memlen_alt:2} = "kB" ]; then
-       mem="${mem:0:$memlen_alt}"
-       mem=${mem/.*/}
-       mem=`expr $mem / 1024`
-    elif [ ${mem:$memlen_alt:2} = "Mb" ]; then
-       mem="${mem:0:$memlen_alt}"
-       mem=${mem/.*/}
-    elif [ ${mem:$memlen_alt:2} = "MB" ]; then
-       mem="${mem:0:$memlen_alt}"
-       mem=${mem/.*/}
+    mem=$(SysInfo_megabytes "$1")
+
+    # Round to the next multiple of 50
+    r=$(($mem % 50))
+    if [ $r != 0 ]; then
+       mem=$(($mem + 50 - $r))
     fi
 
-    # Round to the next multiple of 50
-    memlen=`expr ${#mem} - 2`
-    mem_round="${mem:$memlen:2}"
-    if [ x$mem_round = x ]; then 
-       :
-    elif [ $mem_round = "00" ]; then 
-       :
-    elif [ $mem_round -lt "50" ]; then 
-       mem=$[mem+50]
-       mem=$[mem-$mem_round]
-
-    else 
-       mem=$[mem+100]
-       mem=$[mem-$mem_round]
-    fi
     echo $mem    
 }
 
 SysInfo_hdd_units() {
-    disk=$1
-    disklen=`expr ${#disk} - 1`
-    disklen_alt=`expr ${#disk} - 2`
-    if [ ${disk:$disklen:1} = "G" ]; then
-       disk="${disk:0:$disklen}"
-    elif [ ${disk:$disklen:1} = "M" ]; then
-       disk="${disk:0:$disklen}"
-       disk=${disk/.*/}
-       disk=`expr $disk / 1024`
-    elif [ ${disk:$disklen:1} = "k" ]; then
-       disk="${disk:0:$disklen}"
-       disk=${disk/.*/}
-       disk=`expr $disk / 1048576`
-    elif [ ${disk:$disklen_alt:2} = "kB" ]; then
-       disk="${disk:0:$disklen_alt}"
-       disk=${disk/.*/}
-       disk=`expr $disk / 1048576`
-    elif [ ${disk:$disklen_alt:2} = "Mb" ]; then
-       disk="${disk:0:$disklen_alt}"
-       disk=${disk/.*/}
-       disk=`expr $disk / 1024`
-    elif [ ${disk:$disklen_alt:2} = "MB" ]; then
-       disk="${disk:0:$disklen_alt}"
-       disk=${disk/.*/}
-       disk=`expr $disk / 1024`
-    fi
-    echo $disk    
+    # Size in gigabytes
+    echo $(($(SysInfo_megabytes "$1") / 1024))
 }
 
 SysInfo_usage() {

_______________________________________________
Pacemaker mailing list: Pacemaker@oss.clusterlabs.org
http://oss.clusterlabs.org/mailman/listinfo/pacemaker

Project Home: http://www.clusterlabs.org
Getting started: http://www.clusterlabs.org/doc/Cluster_from_Scratch.pdf
Bugs: http://developerbugs.linux-foundation.org/enter_bug.cgi?product=Pacemaker

Reply via email to