linux--常用shell脚本
程序员文章站
2024-02-19 19:11:28
...
1、侦测程序句柄泄漏
#对进程采样数据3000次,每次间隔10秒,监控进程打开句柄的情况,
#若出现持续上升的情况,说明进程异常
#!/bin/sh
set -x
echo "">total_handler
psid=`ps -ef|grep $1|head -1|awk '{print $2}'`
count=0
while [ $count -lt 3000 ]
do
lsof -p $psid|wc -l >> total_handler
sleep 10
count=`expr $count + 1`
done
2、检查进程是否存在
#获取PsUser下PsName进程的pid
function GetPID() #User #Name
{
PsUser=$1
PsName=$2
pid=`ps -u $PsUser|grep $PsName|grep -v grep|grep -v vi|grep -v dbx\n
|grep -v tail|grep -v start|grep -v stop |sed -n 1p |awk '{print $1}'`
echo $pid
}
3、进程CPU占用率
function GetCpu()
{
PID=$1
CpuValue=`ps -p $PID -o pcpu |grep -v CPU | awk '{print $1}' | awk - F. '{print $1}'`
echo $CpuValue
}
4、内存使用情况
function GetMem()
{
PID=$1
MEMUsage=`ps -o vsz -p $PID|grep -v VSZ`
(( MEMUsage /= 1000))
echo $MEMUsage
}
5、进程使用数量
function GetDes()
{
PID=$1
DES=`ls /proc/$PID/fd | wc -l`
echo $DES
}
6、TCP/UDP端口是否在被监听
function Listening()
{
port=$1
TCPListeningnum=`netstat -an | grep ":$port " | \n
awk '$port == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`
UDPListeningnum=`netstat -an|grep ":$port " \n
|awk '$port == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l`
(( Listeningnum = TCPListeningnum + UDPListeningnum ))
if [ $Listeningnum == 0 ]
then
{
echo "0"
}
else
{
echo "1"
}
fi
}
7、cpu负载
function GetSysCPU
{
CpuIdle=`vmstat 1 5 |sed -n '3,$p' \n
|awk '{x = x + $15} END {print x/5}' |awk -F. '{print $1}'
CpuNum=`echo "100-$CpuIdle" | bc`
echo $CpuNum
}
8、磁盘使用情况
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
}