Shell编程---监控检查系统某进程内存使用量

题目要求:使用shell脚本监控检查系统某进程内存使用量

分析:

在对应用服务进行维护时,也经常遇到由于内存使用过大导致进程崩溃,造成业务中断的情况。例如:32 位程序可寻址的最大内存空间为 4G,如果超出将申请内存失败,同时物理内存也是有限的。内存使用过高可能由于内存泄露,消息堆积等情况。通过脚本对业务进程内存使用量进行时时监控,可以在内存使用量异常时及时发送告警,便于维护人员及时处理。
我们可以通过指定进程pid获得此进程内存使用量,如果此进程内存使用量超过 1.6G(可以根据实际情况进行调整),则输出告警,否则输出正常信息。

解答:

命令行测试:
[root@myhost~]# ps -ef | egrep tomcat | egrep root |  egrep -v "grep|vi|tail" | sed -n 1p | awk '{print $2}'
18430
[root@myhost~]# ps -p 18430 -o vsz
   VSZ
14896208
[root@myhost~]# ps -p 18430 -o vsz | egrep -v VSZ
14896208

脚本:
#!/bin/sh
source /etc/profile

#define variable

psUser=$1
psProcess=$2
pid= `ps -ef | egrep ${psProcess} | egrep ${psUser} |  egrep -v "grep|vi|tail" | sed -n 1p | awk '{print $2}'`
echo ${pid}
if [ -z ${pid} ];then
	echo "The process does not exist."
	exit 1
fi   

MemUsage=`ps -p ${pid} -o vsz |egrep -v VSZ` 
 (( ${MemUsage} /= 1000)) 
echo ${MemUsage} 

if [ ${MemUsage} -ge 1600 ];
	then
		echo “The usage of memory is larger than 1.6G”
	else
	 	echo “The usage of memory is ok”
fi

猜你喜欢

转载自blog.csdn.net/yuki5233/article/details/84029901
今日推荐