Shell编程---监控检查系统某进程句柄使用量

题目要求:使用Shell脚本监控检查系统某进程句柄使用量

分析:

在对应用服务进行维护时,也经常遇到由于句柄使用 过量导致业务中断的情况。每个平台对进程的句柄使用都是有限的,例如在 Linux 平台,我们可以使用 ulimit – n 命令(open files (-n) 1024)或者对 /etc/security/limits.conf 的内容进行查看,得到进程句柄限制。句柄使用过高可能由于负载过高,句柄泄露等情况,通过脚本对业务进程句柄使用量进行时时监控,可以在异常时及时发送告警(例如通过短信),便于维护人员及时处理。
我们可以通过指定进程pid获得此进程句柄使用量,如果此进程句柄使用量超过 900(可以根据实际情况进行调整),则输出告警,否则输出正常信息。

解答:

命令行测试:
[root@myhost ~]# egrep -v "#|^$" /etc/security/limits.conf
root soft nofile 65535
root hard nofile 65535
* soft nofile 65535
* hard nofile 65535
[root@myhost ~]# ulimit -n
65535
[root@myhost  ~]# ps -ef | egrep tomcat | egrep root |  egrep -v "grep|vi|tail" | sed -n 1p | awk '{print $2}'   
18430
[root@myhost ~]# ls /proc/18430/fd 
0    105  113  125  135  19   23   3    340  357  37   393  400  408  418  426  437  460  51  59  66   699  74  81  89  96
1    106  114  126  136  2    24   30   341  358  374  395  401  409  42   427  438  461  52  6   67   7    75  82  9   97
10   108  115  127  14   20   25   31   342  359  38   396  402  41   420  428  439  462  53  60  68   70   76  83  90  98
100  109  116  128  15   21   26   32   343  36   388  397  403  410  421  429  44   47   54  61  69   700  77  84  91  99
101  11   117  129  16   212  266  33   35   360  389  398  404  411  422  43   45   48   55  62  694  701  78  85  92
102  110  118  13   17   22   27   337  354  364  39   399  405  412  423  430  458  49   56  63  695  71   79  86  93
103  111  12   131  18   222  28   339  355  365  390  4    406  416  424  431  459  5    57  64  696  72   8   87  94
104  112  121  134  189  224  29   34   356  367  391  40   407  417  425  432  46   50   58  65  698  73   80  88  95
[root@myhost ~]# ls /proc/18430/fd | wc -l
204

脚本:
#!/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   

DES=`ls /proc/${pid}/fd | wc -l`

echo ${DES} 

if [ ${DES} -ge 900 ];
	then
		echo “The number of des is larger than 900”
	else
		echo “The usage of des is ok”
fi

猜你喜欢

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