[size=1.166em]
在对进程进行监控时,我们一般需要得到该进程的 ID,进程 ID 是进程的唯一标识,但是有时可能在服务器上不同用户下运行着多个相同进程名的进程,下面的函数 GetPID 给出了获取指定用户下指定进程名的进程 ID 功能(目前只考虑这个用户下启动一个此进程名的进程),它有两个参数为用户名和进程名,它首先使用 ps 查找进程信息,同时通过 grep 过滤出需要的进程,最后通过 sed 和 awk 查找需要进程的 ID 值(此函数可根据实际情况修改,比如需要过滤其它信息等)。
The process does not exist.
# 检查进程是否存在
if [ "-$PID" == "-" ]
then
{
echo "The process does not exist."
}
fi
检测进程 CPU 利用率
[size=1.166em]
在对应用服务进行维护时,我们经常遇到由于 CPU 过高导致业务阻塞,造成业务中断的情况。CPU 过高可能由于业务量过负荷或者出现死循环等异常情况,通过脚本对业务进程 CPU 进行时时监控,可以在 CPU 利用率异常时及时通知维护人员,便于维护人员及时分析,定位,以及避免业务中断等。下面的函数可获得指定进程 ID 的进程 CPU 利用率。它有一个参数为进程 ID,它首先使用 ps 查找进程信息,同时通过 grep -v 过滤掉 %CPU 行,最后通过 awk 查找 CPU 利用百分比的整数部分(如果系统中有多个
CPU,CPU 利用率可以超过 100%)。
清单 2. 对业务进程 CPU 进行实时监控
function GetCpu
{
CpuValue=`ps -p $1 -o pcpu |grep -v CPU | awk '{print $1}' | awk - F. '{print $1}'`
echo $CpuValue
}
[size=1.166em]
下面的功能是通过上面的函数 GetCpu 获得此进程的 CPU 利用率,然后通过条件语句判断 CPU 利用率是否超过限制,如果超过 80%(可以根据实际情况进行调整),则输出告警,否则输出正常信息。
清单 3. 判断 CPU 利用率是否超过限制
function CheckCpu
{
PID=$1
cpu=`GetCpu $PID`
if [ $cpu -gt 80 ]
then
{
echo “The usage of cpu is larger than 80%”
}
else
{
echo “The usage of cpu is normal”
}
fi
}
[size=1.166em]
示例演示:
[size=1.166em]
1)源程序(假设上面已经查询出 CFTestApp 的进程 ID 为 11426)
CheckCpu 11426
[size=1.166em]
2)结果输出
The usage of cpu is 75
The usage of cpu is normal
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:CFTestApp 程序当前的 CPU 使用为 75%,是正常的,没有超过 80% 的告警限制。
mem=`GetMem $PID`
if [ $mem -gt 1600 ]
then
{
echo “The usage of memory is larger than 1.6G”
}
else
{
echo “The usage of memory is normal”
}
fi
[size=1.166em]
示例演示:
[size=1.166em]
1)源程序(假设上面已经查询出 CFTestApp 的进程 ID 为 11426)
mem=`GetMem 11426`
echo "The usage of memory is $mem M"
if [ $mem -gt 1600 ]
then
{
echo "The usage of memory is larger than 1.6G"
}
else
{
echo "The usage of memory is normal"
}
fi
[size=1.166em]
2)结果输出
The usage of memory is 248 M
The usage of memory is normal
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:CFTestApp 程序当前的内存使用为 248M,是正常的,没有超过 1.6G 的告警限制。
检测进程句柄使用量
[size=1.166em]
在对应用服务进行维护时,也经常遇到由于句柄使用 过量导致业务中断的情况。每个平台对进程的句柄使用都是有限的,例如在 Linux 平台,我们可以使用 ulimit – n 命令(open files (-n) 1024)或者对 /etc/security/limits.conf 的内容进行查看,得到进程句柄限制。句柄使用过高可能由于负载过高,句柄泄露等情况,通过脚本对业务进程句柄使用量进行时时监控,可以在异常时及时发送告警(例如通过短信),便于维护人员及时处理。下面的函数可获得指定进程 ID 的进程句柄使用情况。它有一个参数为进程
ID,它首先使用 ls 输出进程句柄信息,然后通过 wc -l 统计输出句柄个数。
des=` GetDes $PID`
if [ $des -gt 900 ]
then
{
echo “The number of des is larger than 900”
}
else
{
echo “The number of des is normal”
}
fi
[size=1.166em]
示例演示:
[size=1.166em]
1)源程序(假设上面查询出 CFTestApp 的进程 ID 为 11426)
des=`GetDes 11426`
echo "The number of des is $des"
if [ $des -gt 900 ]
then
{
echo "The number of des is larger than 900"
}
else
{
echo "The number of des is normal"
}
fi
[size=1.166em]
2)结果输出
The number of des is 528
The number of des is normal
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:CFTestApp 程序当前的句柄使用为 528 个,是正常的,没有超过 900 个的告警限制。
[size=1.166em]
4)命令介绍
isListen=`Listening 8080`
if [ $isListen -eq 1 ]
then
{
echo "The port is listening"
}
else
{
echo "The port is not listening"
}
fi
[size=1.166em]
2)结果输出
The port is listening
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:这个 Linux 服务器的 8080 端口处在监听状态。
[size=1.166em]
4)命令介绍
[size=1.166em]
在对服务器进行维护时,有时也遇到由于系统 CPU(利用率)负载 过量导致业务中断的情况。服务器上可能运行多个进程,查看单个进程的 CPU 都是正常的,但是整个系统的 CPU 负载可能是异常的。通过脚本对系统 CPU 负载进行时时监控,可以在异常时及时发送告警,便于维护人员及时处理,预防事故发生。下面的函数可以检测系统 CPU 使用情况 . 使用 vmstat 取 5 次系统 CPU 的 idle 值,取平均值,然后通过与 100 取差得到当前 CPU 的实际占用值。
cpu=`GetSysCPU`
echo "The system CPU is $cpu"
if [ $cpu -gt 90 ]
then
{
echo "The usage of system cpu is larger than 90%"
}
else
{
echo "The usage of system cpu is normal"
}
fi
[size=1.166em]
2)结果输出
The system CPU is 87
The usage of system cpu is normal
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:当前 Linux 服务器系统 CPU 利用率为 87%,是正常的,没有超过 90% 的告警限制。
[size=1.166em]
4)命令介绍
function GetDiskSpc
{
if [ $# -ne 1 ]
then
return 1
fi
Folder="$1$"
DiskSpace=`df -k |grep $Folder |awk '{print $5}' |awk -F% '{print $1}'
echo $DiskSpace
}
[size=1.166em]
示例演示:
[size=1.166em]
1)源程序(检测目录为 /boot)
Folder="/boot"
DiskSpace=`GetDiskSpc $Folder`
echo "The system $Folder disk space is $DiskSpace%"
if [ $DiskSpace -gt 90 ]
then
{
echo "The usage of system disk($Folder) is larger than 90%"
}
else
{
echo "The usage of system disk($Folder) is normal"
}
fi
[size=1.166em]
2)结果输出
The system /boot disk space is 14%
The usage of system disk(/boot) is normal
[dyu@xilinuxbldsrv shell]$
[size=1.166em]
3)结果分析
[size=1.166em]
从上面的输出可见:当前此 Linux 服务器系统上 /boot 目录的磁盘空间已经使用了 14%,是正常的,没有超过使用 90% 的告警限制。
[size=1.166em]
4)命令介绍
df:检查文件系统的磁盘空间占用情况。可以利用该命令来获取硬盘被占用了多少空间,目前还剩下多少空间等信息。 参数:-k 以 k 字节为单位显示。
[size=1.166em]
总结
[size=1.166em]
在 Linux 平台下,shell 脚本监控是一个非常简单,方便,有效的对服务器,进程进行监控的方法,对系统开发以及进程维护人员非常有帮助。它不仅可以对上面的信息进行监控,发送告警,同时也可以监控进程的日志等等的信息,希望本文对大家有帮助。
[size=1.166em]
原文链接:http://www.ibm.com/developerworks/cn/linux/l-cn-shell-monitoring/