Published: 05 Sept 2017 › Updated: 05 Sept 2017![[Shell Script] check physical memory monitoring shell script](https://i.ecency.com/p/2gsjgna1uruvUuS7ndh9YqVwYGPLVszbFLwwpAYXYMzNL4Hn1cXyY6di1P4ENKHwALKQEC6kHEmNDLkjjGaFNaRVEeP6B8EXosM78BFN22jg79EgJA?format=match&mode=fit&height=377)
[Shell Script] check physical memory monitoring shell script
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] [-c|--critical]"
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
Leave [Shell Script] check physical memory monitoring shell script to:
Read more #kr-dev posts
Best Posts From capslock
We have not curated any of capslock's posts yet. But you can encourage our curation team to review posts by visiting them regularly and by referring other readers. Because we give priority to frequently read content.
More Posts From capslock
- I like beer!
- Hive? What is it?
- 방갑습니다 하이브!
- ????
- Windows 7에 Python Steem 라이브러리 설치 성공(pycrypto 오류 해결)
- 이거 스티밋에 얼마만인지...
- 제주 선녀와 나무꾼 테마공원
- 제주도 성산일출봉 인근 흑돼지 맛집 광치기 정육식당
- [Oracle] 오라클 SQL에서 정규식 사용하여 문자열 잘라내기(10g 이상)
- [Shell Script] check physical memory monitoring shell script
- [Shell Script] This script connects to a site via curl and searches for a string in the web page.
- [Shell Script] Check SFTP Availability With shell script
- 명동성당에 다녀왔습니다.
- [제주도 필수코스] 성산항에서 우도를 가봅시다!!!
- [Shell Script] process monitoring script, web/was server monitoring script(using curl)
- When & How to Wash Your Hands - 언제 손을 씻어야 할까???
- 제주도 성산일출봉 drone으로 촬영한 영상입니다. [Jeju Island, 濟州島]
- 제주도 서귀포시 산방산 드론사진 구경하세요 [Jeju Island, 濟州島]
- 제주도 가면 꼭 가봐야 할 용눈이 오름
- 너무하네