监测系统的情况,你必须知道的两三事!

       你要是系统运维人员,就要实时的监控系统的一些重要参数,不如僵尸进程,CPU的利用率,内存的使用情况,磁盘空间的使用情况,系统的均衡负载,根据得到的最新信息,我们就能判断系统运行的状态是否良好。本人接触linux系统也有一点时间,在网上看看了,经过整理,将一些常用的系统监控的命令shell脚本拿出来晒,希望给新手提供学习的机会,同时也希望经验丰富者提够些技术知道,多多探讨交流!

获得某个用户的某个进行的信息:

 

function GetPID #user #name

{

	PsUser=$1

	#echo $PsUser

	PsName=$2

	#echo $PsName

	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

}



PID=`GetPID lgy cat`

echo $PID



#The process does not exist.



if [ "-$PID" == "-" ]

then

{

    echo "The process does not exist."

}

fi

       这里面 涉及一些最基本的进程监控命令,在linux中你只要man ps就会得到很多使用说明,grep按照一定的匹配规则进行字符串分割匹配,还有一些简单的控制语句,这些基本的shell语法应该是我们的基本技能,在晚上会有很多的学习资料。

 

获得CPU的使用率:

 

function GetCPU

{

    CPUValue=`ps -p $1 -o pcpu | grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'`

    echo $CPUValue

}



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

}



CheckCPU $PID


        这个 程序和上一个代码片段是一脉相承,本段代码的运行结果一会会有相应的展示!

 

这段脚本的主要目的是用来进行某个进程的内存使用情况的检测:

 

function GetMem

{

    MemUsage=`ps -o vsz -p $1 | grep -v VSZ`

    ((MemUsage /= 1024))

    echo $MemUsage    

}



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


这是用来进行获得此进程的句柄使用量:

 

 

function GetDes

{

    DES=`ls /proc/$1/fd | wc -l`

    echo $DES

}



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


进行 对某个端口的绑定的查询:

 

 

function Listening

{

    TCPListeningNum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l`

    UDPListenNum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l`

    ((ListeningNum = TCPListeningNum + UDPListenNum ))

    if [ $ListeningNum == 0 ]

    then

    {

        echo "0"

    }

    else

    {

        echo "1"

    }

    fi

}



isListen=`Listening 8080`

if [ $isListen -eq 1 ]

then

{

    echo "The port is listening"

}

else

{

    echo "The port is not listening"

}

fi


系统CPU 的使用情况:

 

 

function GetSysCPU

{

    CPUIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{x = x + $15} END {print x/5}' | awk -F. '{print $1}'`

    CPUNum=`echo "100-$CPUIdle" | bc`

    echo $CPUNum

}



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


获得某 制定的磁盘空间

 

 

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

}



Folder="/dev"

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


在本机上代码检测的结果 如下:

 

 

lgy@lgy-HP:~$ ./monitorCPU.sh 

2388

The usage of cpu is normal

The usage of memory is normal

The number of des is normal

The port is not listening

The System CPU is 1

The usage of system cpu is normal

The system /dev disk space is 1%

The usage of system disk(/dev) is normal


 

 

你可能感兴趣的:(系统)