Linux system monitoring knowledge sharing

Source: Internet
Author: User

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:

 
 
  1. function GetPID #user #name 
  2. PsUser=$1 
  3. #echo $PsUser 
  4. PsName=$2 
  5. #echo $PsName 
  6. 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}'` 
  7. echo $pid 
  8. PID=`GetPID lgy cat` 
  9. echo $PID 
  10. #The process does not exist. 
  11. if [ "-$PID" == "-" ] 
  12. then 
  13. echo "The process does not exist." 
  14. 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:

 
 
  1. function GetCPU 
  2. CPUValue=`ps -p $1 -o pcpu | grep -v CPU | awk '{print $1}' | awk -F. '{print $1}'` 
  3. echo $CPUValue 
  4. function CheckCPU 
  5. PID=$1 
  6. cpu=`GetCPU $PID` 
  7. if [ $cpu -gt 80 ] 
  8. then 
  9. echo "The usage of cpu is larger than 80%" 
  10. else 
  11. echo "The usage of cpu is normal" 
  12. fi 
  13. 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:

 
 
  1. function GetMem 
  2. MemUsage=`ps -o vsz -p $1 | grep -v VSZ` 
  3. ((MemUsage /= 1024)) 
  4. echo $MemUsage 
  5. mem=`GetMem $PID` 
  6. if [ $mem -gt 1600 ] 
  7. then 
  8. echo "The usage of memory is larger than 1.6G" 
  9. else 
  10. echo "The usage of memory is normal" 
  11. fi 

This is used to obtain the handle usage of this process:

 
 
  1. function GetDes 
  2. DES=`ls /proc/$1/fd | wc -l` 
  3. echo $DES 
  4. des=`GetDes $PID` 
  5. if [ $des -gt 900 ] 
  6. then 
  7. echo "The number of des is larger than 900" 
  8. else 
  9. echo "The number of des is normal" 
  10. fi 

Query the binding of a port:

 
 
  1. function Listening 
  2. TCPListeningNum=`netstat -an | grep ":$1 " | awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l` 
  3. UDPListenNum=`netstat -an | grep ":$1 " | awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l` 
  4. ((ListeningNum = TCPListeningNum + UDPListenNum )) 
  5. if [ $ListeningNum == 0 ] 
  6. then 
  7. echo "0" 
  8. else 
  9. echo "1" 
  10. fi 
  11. isListen=`Listening 8080` 
  12. if [ $isListen -eq 1 ] 
  13. then 
  14. echo "The port is listening" 
  15. else 
  16. echo "The port is not listening" 
  17. fi 

System CPU usage:

 
 
  1. function GetSysCPU 
  2. CPUIdle=`vmstat 1 5 | sed -n '3,$p' | awk '{xx = x + $15} END {print x/5}' | awk -F. '{print $1}'` 
  3. CPUNum=`echo "100-$CPUIdle" | bc` 
  4. echo $CPUNum 
  5. cpu=`GetSysCPU` 
  6. echo "The System CPU is $cpu" 
  7. if [ $cpu -gt 90 ] 
  8. then 
  9. echo "The usage of system cpu is larger than 90%" 
  10. else 
  11. echo "The usage of system cpu is normal" 
  12. fi 

Obtain a specified disk space:

 
 
  1. function GetDiskSpc 
  2. if [ $# -ne 1 ] 
  3. then 
  4. return 1 
  5. fi 
  6. Folder="$1$" 
  7. DiskSpace=`df -k | grep $Folder | awk '{print $5}' | awk -F% '{print $1}'` 
  8. echo $DiskSpace 
  9. Folder="/dev" 
  10. DiskSpace=`GetDiskSpc $Folder` 
  11. echo "The system $Folder disk space is $DiskSpace%" 
  12. if [ $DiskSpace -gt 90 ] 
  13. then 
  14. echo "The usage of system disk($Folder) is larger than 90%" 
  15. else 
  16. echo "The usage of system disk($Folder) is normal" 
  17. fi 

The result of code detection on the local machine is as follows:

 
 
  1. lgy@lgy-HP:~$ ./monitorCPU.sh 
  2. 2388 
  3. The usage of cpu is normal 
  4. The usage of memory is normal 
  5. The number of des is normal 
  6. The port is not listening 
  7. The System CPU is 1 
  8. The usage of system cpu is normal 
  9. The system /dev disk space is 1% 
  10. The usage of system disk(/dev) is normal 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.