Search Members Help

» Welcome Guest
[ Log In :: Register ]

Mini-ITX Boards Sale, Fanless BareBones Mini-ITX, Bootable 1G DSL USBs, 533MHz Fanless PC <-- SALE $200 each!
Get The Official Damn Small Linux Book. DSL Market , Great VPS hosting provided by Tektonic
Pages: (6) </ [1] 2 3 4 5 6 ... >/

[ Track this topic :: Email this topic :: Print this topic ]

reply to topic new topic new poll
Topic: Beta testers needed, bash script testing< Next Oldest | Next Newest >
Zucca Offline





Group: Members
Posts: 524
Joined: Feb. 2006
Posted: Jan. 24 2008,16:16 QUOTE

Here it is. A system statistics script. Highly customizable.

Copy and paste this and run it with bash. Then post results and comments. Anything is welcome. ;)

Code Sample
#!/bin/bash
# sysinfo.sh

# This script uses /proc filesystem and at least following utilities:
# sed, perl/bc and grep
#
# Run from command line with --help switch to get more help.

# Feel free to change this if you are too lazy to set an alias.;)
# Note: You can have custom ouput from command line too. Just see --help.
DEFAULT_FORMAT="Uptime: -UPTIME- | % idle since boot: -IDLEPERCENT- | \
-CPUMODEL- @ -CPUSPEED- | \
Bogomips: -BOGOMIPS- | \
Mem total/used/free: -MEMTOTAL-/-MEMUSAGE-/-MEMFREE-"

# No need to edit more. =)
############################################################################


BASENAME=`basename $0`
VERSION="1.10.1b"

case $@ in

*--licence*)
# We saw user requesting lience...
echo "$BASENAME version $VERSION

############################################################################
### LICENCE -start- ########################################################
############################################################################
#
#Copyright (c) 2008, Zucca
#
# All rights reserved.
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions are met:
#   - Redistributions of source code must retain the above copyright notice,
#     this list of conditions and the following disclaimer.
#   - Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#   - Neither the name of the Zucca's company nor the names of its
#     contributors may be used to endorse or promote products derived from
#     this software without specific prior written permission.
#
#       THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
#       CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES,
#       INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#       MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
#       DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
#       CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#       SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
#       NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
#       LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#       HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
#       CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#       OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
#       SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
############################################################################
### LICENCE -end- ##########################################################
############################################################################
";;

*--help*)
# User wants some help...
echo "${BASENAME} version $VERSION

USAGE: ${BASENAME} [--uptimeweeks] [--mmega] [OUTPUT FORMAT]
USAGE: ${BASENAME} [--help]
USAGE: ${BASENAME} [--licence]

OUTPUT FORMAT can have any text you like (${BASENAME} will use echo with \
no argument to print information, so use your head.), but ${BASENAME} will \
replace following:\n\
-UPTIME- will show system uptime. --uptimeweeks will tell uptime in weeks \
if uptime is seven days or more.
-IDLEPERCENT- displays percentage of idle time from uptime.
-CPUMODEL- diplays your CPU model.
-CPUSPEED- displays the clockspeed of your processor.
-CPUCACHE- shows size of your CPU cache.
-BOGOMIPS- see http://en.wikipedia.org/wiki/Bogomips
-MEMTOTAL- will display amout of total physical (RAM) memory in your system.
-MEMUSAGE- same as above but displays used memory
-MEMFREE- same as above but displays free memory

Note: switches can be in any order as long as they are before OUTPUT FORMAT

Example: ${BASENAME} -UPTIME-
will show you uptime in days, hours, minutes and seconds.

If OUTPUT FORMAT does not contain any text it defaults to:
${DEFAULT_FORMAT}"
;;

*)
   # Allright! Let's bang a gong and get it on!
   
   if [ ! -d "/proc" ]
   then
       echo -e "No proc filesystem found.\nCannot continue."
       exit 1
   fi
   
   # calculator function
   function calc {
       
       # First test if $CALCEXE contains any information if not then set a
       # value. Is this a good (fast/efficient) way to do it?
       test -n "$CALCEXE" || CALCEXE=`which perl` || CALCEXE=`which bc` || CALCEXE="FAIL"
       
       case $CALCEXE in
       *perl)
           $CALCEXE -e "printf '%.${DECIMALS}f',${@}"
       ;;
       *bc)
           echo -e "scale=$DECIMALS\n${@}" | $CALCEXE
       ;;
       FAIL)
           echo -n "Could not calculate: excutable missing"
           exit 1
       ;;
       esac
   }
   
   function addresult {
       # Search and replace marked places of OUTPUT FORMAT with
       # correcponding values.
       OUTPUT_FORMAT=`echo "$OUTPUT_FORMAT" | sed -e "s/-$1-/$2/g"`
   }
   
   if [ -n "$*" ]
   then
       ARGS="$*"
   else
       ARGS="."
   fi
   
   if echo $ARGS | grep "\-\-" > /dev/null
   then
       OUTPUT_FORMAT=${ARGS##*--}
       OUTPUT_FORMAT=${OUTPUT_FORMAT#* }
       if echo "$OUTPUT_FORMAT" | grep -v "\-*\-" > /dev/null
       then
           OUTPUT_FORMAT="."
       fi
   else
       OUTPUT_FORMAT=$ARGS
   fi
   
   if [ "$OUTPUT_FORMAT" == "." ]
   then
       OUTPUT_FORMAT="$DEFAULT_FORMAT"
   fi
   
   # Case-hell starts here.;P
   
   case $OUTPUT_FORMAT in *-UPTIME-*)
       
       UPTIME=""
       
       TOTSECS=`PROCUPTIME=$(cat /proc/uptime) && echo ${PROCUPTIME%%.*}`
       let "SECS=${TOTSECS}%60"
       let "MINS=${TOTSECS}/60%60"
       let "HOURS=${TOTSECS}/3600%24"
       
       
       if echo "$ARGS" | grep -i "\-\-uptimeweeks" > /dev/null
       then
           let "DAYS=${TOTSECS}/86400%7"
           let "WEEKS=${TOTSECS}/604800"
       else
           let "DAYS=${TOTSECS}/86400"
       fi
       
       if [ -n "$WEEKS" ] && [ "$WEEKS" != "0" ]
       then
           UPTIME="${UPTIME}${WEEKS}w"
       fi
       if [ "$DAYS" != "0" ]
       then
           UPTIME="${UPTIME}${DAYS}d"
       fi
       UPTIME="${UPTIME}${HOURS}h${MINS}m${SECS}s"
       
       addresult "UPTIME" "$UPTIME"
   ;; esac
   
   
   case $OUTPUT_FORMAT in *-IDLEPERCENT-*)
   
       DECIMALS="4"
       
       # This tests if PROCUPTIME is already set.
       # It already should have some value if user has requested -UPTIME- from
       # the command line
       if [ -z $PROCUPTIME ]
       then
           PROCUPTIME=`cat /proc/uptime`
           TOTSECS=${PROCUPTIME%%.*}
       fi
       
       # Parse and calculate
       IDLESECS=${PROCUPTIME#* }
       IDLESECS=${IDLESECS%.*}
       IDLEPERCENT=`calc $IDLESECS/$TOTSECS*100`
       IDLEPERCENT="${IDLEPERCENT}%"
       
       addresult "IDLEPERCENT" "$IDLEPERCENT"
   ;; esac

############################################################################
### Memory related part - start ############################################
############################################################################

   function parsememinfo {
       
       # Get memory information if there isn't already
       test -z "$MEMINFO" && MEMINFO=`cat /proc/meminfo`
       PARSED=${MEMINFO#*$*:}
       PARSED=${PARSED%% kB*}
       # echo $PARSED
   }
   
   function memfree {
       parsememinfo "MemFree"
       MEMFREE=$PARSED
       parsememinfo "Cached"
       CACHED=$PARSED
       parsememinfo "Buffers"
       BUFFERS=$PARSED
       let "MEMFREE=${MEMFREE}+${CACHED}+${BUFFERS}"
       MEMFREERAW=$MEMFREE
   }
   
   function memsuffix {
       test -n "$MEMSUFFIX" || case $ARGS in
       *--mmega*)
           MEMSUFFIX="MB";;
       *)
           MEMSUFFIX="kB";;
       esac
   }

   case $OUTPUT_FORMAT in *-MEMFREE-*)
       # Count free memory

       # As far as I know anything about memory
       # egrep "^Cached:|^MemFree:|^Buffers:" /proc/meminfo
       # shows the actual free memory.
       # So this script will count the sum of those values
       
       memfree
       memsuffix
       test "$MEMSUFFIX" == "MB" && let "MEMFREE=$MEMFREE/1024"
       MEMFREE="${MEMFREE}${MEMSUFFIX}"
       
       addresult "MEMFREE" "$MEMFREE"
   ;; esac
   
   case $OUTPUT_FORMAT in *-MEMTOTAL-*)
       # Count total memory
       parsememinfo "MemTotal"
       MEMTOTAL=$PARSED
       
       memsuffix
       MEMTOTALRAW=$MEMTOTAL
       test "$MEMSUFFIX" == "MB" && let "MEMTOTAL=$MEMTOTAL/1024"
       MEMTOTAL="${MEMTOTAL}${MEMSUFFIX}"
       
       addresult "MEMTOTAL" "$MEMTOTAL"
   ;; esac
   
   case $OUTPUT_FORMAT in *-MEMUSAGE-*)
       # Count memory usage
       
       test -n $MEMFREERAW || memfree
       test -n $MEMTOTALRAW || parsememinfo MemTotal && MEMTOTALRAW=$PARSED
       let "MEMUSAGE=$MEMTOTALRAW-$MEMFREERAW"
       memsuffix
       test "$MEMSUFFIX" == "MB" && let "MEMUSAGE=$MEMUSAGE/1024"
       MEMUSAGE="${MEMUSAGE}${MEMSUFFIX}"
       
       addresult "MEMUSAGE" "$MEMUSAGE"
   ;; esac
   
############################################################################
### Memory related part - end # CPU related part - start ###################
############################################################################
   
   # function to parse /proc/cpuinfo
   function parsecpuinfo {
       
       # test -n "$TAB" || TAB=`echo -ne "\t"`
       
       # Get CPUinfo if we don't have it yet
       test -z "$CPUINFO" && CPUINFO=`cat /proc/cpuinfo`
       PARSED=`echo "$CPUINFO" | grep "$@"`
       PARSED=${PARSED##*: }
       # echo $PARSED
   }
   
   case $OUTPUT_FORMAT in *-CPUMODEL-*)
       parsecpuinfo "model name"
       addresult "CPUMODEL" "$PARSED"
   ;; esac
   
   case $OUTPUT_FORMAT in *-CPUSPEED-*)
       parsecpuinfo "cpu MHz"
       addresult "CPUSPEED" "${PARSED}MHz"
   ;; esac
   
   case $OUTPUT_FORMAT in *-BOGOMIPS-*)
       parsecpuinfo "bogomips"
       addresult "BOGOMIPS" "$PARSED"
   ;; esac
   
   case $OUTPUT_FORMAT in *-CPUCACHE-*)
       parsecpuinfo "cache size"
       addresult "CPUCACHE" "$PARSED"
   ;; esac

############################################################################
### CPU related part - end #################################################
############################################################################
   
   # Finally echo all the requested statistics
   echo -e "$OUTPUT_FORMAT"
;;
# Here it ends.
esac


--------------
Do you have it? - http://dy.fi/mak
Back to top
Profile PM WEB ICQ MSN 
mikshaw Offline





Group: Members
Posts: 4856
Joined: July 2004
Posted: Jan. 24 2008,22:29 QUOTE

I attempted to test it, but DSL doesn't have bc and I haven't bothered to install it.

I did get a little info, though:
Uptime: 9h54m23s | Uptime load average:  | Mem total/used/free: 515304kB/105724kB/409580kB


--------------
http://www.tldp.org/LDP/intro-linux/html/index.html
Back to top
Profile PM WEB 
Zucca Offline





Group: Members
Posts: 524
Joined: Feb. 2006
Posted: Jan. 24 2008,22:43 QUOTE

You can customize how it outputs statistics. run it with --help.
Anyway. I have developed it much. I want to finish it fast. ;)
New version will be here soon.
BTW. if DSL does not have bc, then is it possible to have decimals when calculating with "let"?

Here's a "shot" from new version:
Quote
zucca@zelan ~/scripts/bash $ sh sysinfo.sh --help
sysinfo.sh version 1.8b

USAGE: sysinfo.sh [--uptimeweeks] [--mmega] [OUTPUT FORMAT]
USAGE: sysinfo.sh [--help]

OUTPUT FORMAT can have any text you like (sysinfo.sh will use echo with no argument to print information, so use your head.), but sysinfo.sh will replace following:
-UPTIME- will show system uptime. --uptimeweeks will tell uptime in weeks if uptime is seven days or more.
-UPTIMELA- will show load average of whole uptime (this may not be accurate)
-CPUMODEL- diplays your CPU model
-CPUSPEED- displays the clockspeed of your processor
-CPUCACHE- shows size of your CPU cache
-BOGOMIPS- see http://en.wikipedia.org/wiki/Bogomips
-MEMTOTAL- will be replaced with amout of total physical (RAM) memory in your system
-MEMUSAGE- same as above but displays used memory
-MEMFREE- same as above but displays free memory

Note: switches can be in any order as long as they are before OUTPUT FORMAT

Example: sysinfo.sh -UPTIME-
will show you uptime in days, hours, minutes and seconds.

If OUTPUT FORMAT does not contain any text it defaults to:
Uptime: -UPTIME- | Uptime load average: -UPTIMELA- | -CPUMODEL- @ -CPUSPEED- | Bogomips: -BOGOMIPS- | Mem total/used/free: -MEMTOTAL-/-MEMUSAGE-/-MEMFREE-
zucca@zelan ~/scripts/bash $ sh sysinfo.sh --mmega
Uptime: 1d3h10m47s | Uptime load average: 0.3740 | Pentium III (Coppermine) @ 896.126MHz | Bogomips: 1793.84 | Mem total/used/free: 499MB/290MB/209MB


--------------
Do you have it? - http://dy.fi/mak
Back to top
Profile PM WEB ICQ MSN 
curaga Offline





Group: Members
Posts: 2163
Joined: Feb. 2007
Posted: Jan. 25 2008,22:52 QUOTE

Quote
is it possible to have decimals when calculating with "let"?
Nah


--------------
There's no such thing as life. Those mean little jocks invented it ;)
-
Windows is not a virus. A virus does something!
Back to top
Profile PM 
WDef Offline





Group: Members
Posts: 798
Joined: Sep. 2005
Posted: Jan. 26 2008,01:21 QUOTE

Hi Zucca

Suggestions:

1.  You can use the -q option to grep to shut it up rather than redirecting to /dev/null all the time.

2.   You might want to investigate the use of "case" instead of repeatedly parsing the arguments which is inefficient (or use getopts).

3. Command substitution using Perl or awk are easy ways to do floating point arithmetic (ie decimals) in a shell script  - there's no need to resort to bc (but everyone seems to forget this and pull out bc).

eg .. say I want to add 1.35 to 6.78 and divide by 9.1 and these numbers are in  shell vars a,b,c respectively, and I want to put the result in var d:

Code Sample
d=$(echo "$a $b $c" | awk '{printf ($1 + $2 )/ $3 }')


echo $d gives 0.913187

To round to 2 dec places:

Code Sample
d=$(echo "$a $b $c" | awk '{printf "%1.2f", ($1 + $2 )/ $3 }')


Or doing a similar command substitution using Perl (no echo needed):

Code Sample
d=$(perl -e "print (($a + $b) / $c)")


To round to 2 dec places:

Code Sample
d=$(perl -e "printf  '%.2f', (($a + $b) / $c)")


echo $d yields:

0.91

Look up printf to understand the precision modifier (number of dec places).

No bc needed!
Back to top
Profile PM 
28 replies since Jan. 24 2008,16:16 < Next Oldest | Next Newest >

[ Track this topic :: Email this topic :: Print this topic ]

Pages: (6) </ [1] 2 3 4 5 6 ... >/
reply to topic new topic new poll
Quick Reply: Beta testers needed

Do you wish to enable your signature for this post?
Do you wish to enable emoticons for this post?
Track this topic
View All Emoticons
View iB Code