Common System Monitoring command shell script

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 learn more about the technology.

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 1 p | 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


Related Article

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.