[Shell Script] check physical memory monitoring shell script

in #kr-dev7 years ago (edited)

Check physical memory using shell script.

Memory is displayed as a percentage.
Quick and easy to implement, does not require any additional packages.

Usage: check_mem [-w|--warning] [-c|--critical]



example:

check_mem -w 10 -c 5

Gives WARNING on 10% free phsical memory and CRITICAL on 5% free memory

#!/bin/bash
USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
calc=/tmp/memcalc
percent_free=/tmp/mempercent
critical=""
warning=""
STATE_OK=0
STATE_WARNING=1
STATE_CRITICAL=2
STATE_UNKNOWN=3
# print usage
if [[ $# -lt 4 ]]
then
    echo ""
    echo "Wrong Syntax: `basename $0` $*"
    echo ""
    echo "Usage: $USAGE"
    echo ""
    exit 0
fi
# read input
while [[ $# -gt 0 ]]
  do
        case "$1" in
               -w|--warning)
               shift
               warning=$1
        ;;
               -c|--critical)
               shift
               critical=$1
        ;;
        esac
        shift
  done
# verify input
if [[ $warning -eq $critical || $warning -lt $critical ]]
then
    echo ""
    echo "$THRESHOLD_USAGE"
    echo ""
        echo "Usage: $USAGE"
    echo ""
        exit 0
fi
# Total memory available
total=`free -m | head -2 |tail -1 |gawk '{print $2}'`
# Total memory used
used=`free -m | head -2 |tail -1 |gawk '{print $3}'`
# Calc total minus used
free=`free -m | head -2 |tail -1 |gawk '{print $2-$3}'`
# normal values
#echo "$total"MB total
#echo "$used"MB used
#echo "$free"MB free
# make it into % percent free = ((free mem / total mem) * 100)
echo "5" > $calc # decimal accuracy
echo "k" >> $calc # commit
echo "100" >> $calc # multiply
echo "$free" >> $calc # division integer
echo "$total" >> $calc # division integer
echo "/" >> $calc # division sign
echo "*" >> $calc # multiplication sign
echo "p" >> $calc # print
percent=`/usr/bin/dc $calc|/bin/sed 's/^\./0./'|/usr/bin/tr "." " "|/usr/bin/gawk {'print $1'}`
#percent1=`/usr/bin/dc $calc`
#echo "$percent1"
if [[ "$percent" -le  $critical ]]
    then
        echo "CRITICAL - $free MB ($percent%) Free Memory"
        exit 2
fi
if [[ "$percent" -le  $warning ]]
        then
                echo "WARNING - $free MB ($percent%) Free Memory"
                exit 1
fi
if [[ "$percent" -gt  $warning ]]
        then
                echo "OK - $free MB ($percent%) Free Memory"
                exit 0
fi
Sort:  

우와~ 스크립트 대단합니다. 뒤에 파라미터로 워닝인지 크리티컬인지 판단할 수 있군요.

네 저도 요즘 많이 배우고 있는 입장입니다. 좋게 봐주셔서 고맙습니다.

Cheer Up!

  • from Clean STEEM activity supporter

Congratulations @capslock! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of posts published

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Goooooood~~~

shell script 대학교때 많이 썼었는데, 잘 안쓰게 되더라고요.
perl, python 이 나오면서 차라리 이걸 쓰게 되더라고요...

네 맞습니다. 좀 복잡한 코딩을 해야하는 것들은 kdj님처럼 perl이나 tcl, python으로 코딩을 하는게 오히려 더 편할 수 있겠습니다. 하지만 쉘로 간단히 처리가능한 것들은 저는 아직까지는 쉘이 더 편하더라구요 업보팅 및 댓글 정말 감사드립니다.

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 64513.75
ETH 3146.11
USDT 1.00
SBD 3.95