[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [hobbit] OSF physical memory, good reference



Henrik,

here is a script which produces the following output on Tru64 V5.1B

hobbit (at) hifpxx31 $ ./memory.sh
... ... "status myhost,my,domaine.memory green Sun Aug 7 10:02:54 MEST 2005 Memory OK
Memory Used Total Percentage
&green Physical 1951M 2048M 95%
&green Swap 1553M 8192M 18% "



--------------------------vvvvvvv-------------------------- #!/bin/ksh

################################################################################
#####  Environment settings
#####

## The following two values could be overwritten by $HCHOME/etc/common-def.sh
#


PERCENT_PHYS_WARN="100"         # go yellow at this level
PERCENT_PHYS_PANIC="101"        # go red and page at this level
PERCENT_SWAP_WARN="80"          # go yellow at this level
PERCENT_SWAP_PANIC="90"         # go red and page at this level

MyP1=$1

MyFCT=memory

ScriptName=`basename $0`; export ScriptName

HCHOME=/usr/local/hobbit/client
. $HCHOME/etc/common-def.sh


MEMDATA=$HCHOME/tmp/memdata.tmp.$$ SWAPDATA=$HCHOME/tmp/swapdata.tmp.$$


MyAGENT=`hostname | sed "s/\./,/g"` MyAGENT=`echo ${MyAGENT}.${MyFCT}"`

### --



################################################################################
#####  Functions
#####

get_status()
{

COLOR="green"
STATUS="Memory OK"

/usr/bin/rm -f $MEMDATA
/usr/bin/rm -f $SWAPDATA

# initialize result values
PHYS_MEMORY=0
PHYS_MEMORY_USED=-1
SWAP_MEMORY=0
SWAP_MEMORY_USED=-1

/usr/bin/vmstat -P >$MEMDATA
/sbin/swapon -s >$SWAPDATA

PHYS_MEMORY=`cat $MEMDATA | grep "Total Physical Memory" | awk {'print $5'} | head -1 | cut -d. -f1`
PHYS_MEMORY_KB=`expr $PHYS_MEMORY \* 1024`
PHYS_MEMORY_FREE_PAGES=`cat $MEMDATA | grep "free pages" | awk {'print $4'}`
PHYS_MEMORY_FREE_KB=`expr $PHYS_MEMORY_FREE_PAGES \* 8`
PHYS_MEMORY_USED_KB=`expr $PHYS_MEMORY_KB - $PHYS_MEMORY_FREE_KB`
PHYS_MEMORY_USED=`expr $PHYS_MEMORY_USED_KB / 1024`


SWAP_MEMORY_PAGES=`cat $SWAPDATA | grep "Allocated space:" | awk {'print $3'} | tail -1`
SWAP_MEMORY=`expr $SWAP_MEMORY_PAGES \* 8 / 1024`
SWAP_MEMORY_USED_PAGES=`cat $SWAPDATA | grep "In-use space:" | awk {'print $3'} | tail -1`
SWAP_MEMORY_USED=`expr $SWAP_MEMORY_USED_PAGES \* 8 / 1024`


if [ $PHYS_MEMORY -eq 0 ]; then
  exit 0
fi

# percentage of memory used

PERCENT_PHYS_MEMORY_USED=0
PERCENT_SWAP_MEMORY_USED=0

if [ $PHYS_MEMORY -gt 0 ]; then
  PERCENT_PHYS_MEMORY_USED=`expr 100 \* $PHYS_MEMORY_USED / $PHYS_MEMORY`
fi

if [ $SWAP_MEMORY -gt 0 ]; then
  PERCENT_SWAP_MEMORY_USED=`expr 100 \* $SWAP_MEMORY_USED / $SWAP_MEMORY`
fi


# calculate physical memory usage

if [ $PHYS_MEMORY_USED -ge 0 ]; then
  if [ $PERCENT_PHYS_MEMORY_USED -ge $PERCENT_PHYS_PANIC ]; then
     COLOR="red"
     PHYS_COLOR="red"
     STATUS="Memory **very** low"
  else
     if [ $PERCENT_PHYS_MEMORY_USED -ge $PERCENT_PHYS_WARN ]; then
        PHYS_COLOR="yellow"
        if [ "$COLOR" != "red" ]; then
           COLOR="yellow"
           STATUS="Memory low"
        fi
     else
        PHYS_COLOR="green"
     fi
  fi
else
  PHYS_COLOR="clear"
fi


# virtual memory usage

if [ $SWAP_MEMORY_USED -ge 0 ]; then
  if [ $PERCENT_SWAP_MEMORY_USED -ge $PERCENT_SWAP_PANIC ]; then
     COLOR="red"
     SWAP_COLOR="red"
     STATUS="Memory **very** low"
  else
     if [ $PERCENT_SWAP_MEMORY_USED -ge $PERCENT_SWAP_WARN ]; then
        SWAP_COLOR="yellow"
        if [ "$COLOR" != "red" ]; then
           COLOR="yellow"
           STATUS="Memory low"
        fi
     else
        SWAP_COLOR="green"
     fi
  fi
else
  STATUS="--"
  SWAP_COLOR="clear"
fi

echo "$STATUS"

echo "   Memory        Used    Total   Percentage"

if [ ! "$PHYS_COLOR" = "clear" ]; then
echo "&${PHYS_COLOR} Physical ${PHYS_MEMORY_USED}M ${PHYS_MEMORY}M ${PERCENT_PHYS_MEMORY_USED}%" | \
awk '{printf "%s %-9s %8s %8s %12s\n",$1,$2,$3,$4,$5}'
else
echo "&${PHYS_COLOR} Unable to calculate virtual memory usage"
fi


if [ ! "$SWAP_COLOR" = "clear" ]; then
echo "&${SWAP_COLOR} Swap ${SWAP_MEMORY_USED}M ${SWAP_MEMORY}M ${PERCENT_SWAP_MEMORY_USED}%" | \
awk '{printf "%s %-9s %8s %8s %12s\n",$1,$2,$3,$4,$5}'
else
echo "&${SWAP_COLOR} Unable to calculate virtual memory usage"
fi


/usr/bin/rm -f $MEMDATA
/usr/bin/rm -f $SWAPDATA


}

/usr/bin/rm -f $HCHOME/tmp/$MyAGENT
touch $HCHOME/tmp/$MyAGENT

get_status > $HCHOME/tmp/$MyAGENT

MyDATE=`/bin/date "+%Y-%m-%d %H:%M"`

### Send the result to all Hobbit display servers
#

# debug version, do not send it ...
echo "... ... \"status $MyAGENT $COLOR `date` `cat $HCHOME/tmp/$MyAGENT` \""



#/usr/bin/rm -f $FDMDATA exit -------------------------------^^^^^^^^^^^^^^^^^^^^^^^-------------------





I use the following function to get processor load informations.


--------------------------vvvvvvvvvvvvvvvvvvvvvvvvvvvvv---------------------


get_status() {

COLOR="green"

set `uptime | awk '{ gsub(/,/,""); printf "%s %s %s\n",$(NF-2),$(NF-1),$NF}'`

AVG1=$1
AVG5=$2; export AVG5
AVG15=$3

NRPROC=`ps -ef | wc -l | sed "s/ //g"`; export NRPROC

# The Hobbit "do_la.c" requires a line like "up: 36 days, 6 users, 239 procs, load=0.72"

uptime | awk '{printf "%s: %s %s %s %s %3s procs, load=%s\n",$2,$3,$4,$(NF-6),$(NF-5),ENVIRON["NRPROC"],ENVIRON["AVG5"]}'

### set condition red yellow green

echo "LOAD average on `hostname` is:"
echo ""
echo " over the last 60 seconds : ${AVG1}"
echo " over the last  5 minutes : ${AVG5}"
echo " over the last 15 minutes : ${AVG15}"

COLOR=`echo "$AVG5 $CPUWARN $CPUPANIC" | awk '{ $1 < $2 ? x="green" : $1 < $3 ? x="yellow" :x="red"; print (x)}'`


}

--------------------------^^^^^^^^^^^^^^^^^^^^^^^^^--------------------------



Regards,
Tony