As system O & M personnel, it is necessary to monitor important system parameters in real time, such as zombie processes, CPU utilization, memory usage, disk space usage, and system load balancing, based on the latest information, we can determine whether the system is running well. I have a little time to contact the Linux system. I checked it online and sorted out some common system monitoring command shell scripts. I hope to give new users a learning opportunity, at the same time, I also hope that experienced users can provide technical knowledge and discuss and exchange more!
Obtain a user's information:
- 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
This involves some of the most basic process monitoring commands. in linux, you only need man ps to get a lot of instructions for use. grep performs string segmentation matching according to certain matching rules, there are also some simple control statements. These basic shell syntax should be our basic skills and there will be a lot of learning materials at night.
CPU usage:
- 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
This program is in the same line with the previous code snippet. The running results of this code segment will be displayed later!
The main purpose of this script is to detect the memory usage of a process:
- 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
This is used to obtain the handle usage of this process:
- 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
Query the binding of a port:
- 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
System CPU usage:
- function GetSysCPU
- {
- CPUIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{xx = 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
Obtain a specified disk space:
- 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
The result of code detection on the local machine is as follows:
- 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