8个非常实用的Linux系统监控shell脚本
来源:互联网 发布:mac拷贝文件到ntfs 编辑:程序博客网 时间:2024/06/14 10:52
检查进程是否存在
清单 1. 对进程进行监控
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 }
PID=`GetPID root CFTestApp` echo $PID
11426 [dyu@xilinuxbldsrv shell]$
The process does not exist. # 检查进程是否存在 if [ "-$PID" == "-" ] then { echo "The process does not exist." } fi
检测进程 CPU 利用率
清单 2. 对业务进程 CPU 进行实时监控
function GetCpu { CpuValue=`ps -p $1 -o pcpu |grep -v CPU | awk '{print $1}' | awk - F. '{print $1}'` echo $CpuValue }
清单 3. 判断 CPU 利用率是否超过限制
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 11426
The usage of cpu is 75 The usage of cpu is normal [dyu@xilinuxbldsrv shell]$
检测进程内存使用量
清单 4. 对业务进程内存使用量进行监控
function GetMem { MEMUsage=`ps -o vsz -p $1|grep -v VSZ` (( MEMUsage /= 1000)) echo $MEMUsage }
清单 5. 判断内存使用是否超过限制
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
mem=`GetMem 11426` echo "The usage of memory is $mem M" if [ $mem -gt 1600 ] then { echo "The usage of memory is larger than 1.6G" } else { echo "The usage of memory is normal" } fi
The usage of memory is 248 M The usage of memory is normal [dyu@xilinuxbldsrv shell]$
检测进程句柄使用量
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
des=`GetDes 11426` echo "The number of des is $des" if [ $des -gt 900 ] then { echo "The number of des is larger than 900" } else { echo "The number of des is normal" } fi
The number of des is 528 The number of des is normal [dyu@xilinuxbldsrv shell]$
查看某个 TCP 或 UDP 端口是否在监听
清单 6. 端口检测
function Listening { TCPListeningnum=`netstat -an | grep ":$1 " | \n awk '$1 == "tcp" && $NF == "LISTEN" {print $0}' | wc -l` UDPListeningnum=`netstat -an|grep ":$1 " \n |awk '$1 == "udp" && $NF == "0.0.0.0:*" {print $0}' | wc -l` (( Listeningnum = TCPListeningnum + UDPListeningnum )) 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
The port is listening [dyu@xilinuxbldsrv shell]$
tcp: netstat -an|egrep $1 |awk '$6 == "LISTEN" && $1 == "tcp" {print $0}' udp: netstat -an|egrep $1 |awk '$1 == "udp" && $5 == "0.0.0.0:*" {print $0}'
查看某个进程名正在运行的个数
Runnum=`ps -ef | grep -v vi | grep -v tail | grep "[ /]CFTestApp" | grep -v grep | wc -l
检测系统 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 }
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
The system CPU is 87 The usage of system cpu is normal [dyu@xilinuxbldsrv shell]$
检测系统磁盘空间
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="/boot" 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 system /boot disk space is 14% The usage of system disk(/boot) is normal [dyu@xilinuxbldsrv shell]$
总结
阅读全文
0 0
- 8个非常实用的Linux系统监控shell脚本
- 8个非常实用的Linux系统监控shell脚本
- 8个非常实用的Linux系统监控shell脚本
- Linux常用的系统监控shell脚本
- Linux常用的系统监控shell脚本
- Linux系统中编写shell脚本的8个注意事项
- 利用shell脚本来监控linux系统的内存
- 几个常用的Linux系统性能监控shell脚本
- 利用shell脚本来监控linux系统的内存
- 利用shell脚本来监控linux系统的内存
- 利用shell脚本来监控linux系统的内存
- 利用shell脚本来监控linux系统的内存
- linux服务器系统负载监控-shell脚本
- linux服务器系统cpu监控-shell脚本
- Shell脚本监控Linux系统内存使用率
- Shell脚本监控Linux系统CPU使用率
- Shell 脚本实现 Linux 系统监控
- 8个非常实用的Linux命令行性能监测工具
- 使用时间序列分解模型预测商品销量
- Spring Boot使用FastDFS结合Dropzone.js Bootstrap上传图片
- FTP读取文件目录问题
- 条款20:宁以pass-by-reference-to-const替换pass-by-value
- Spring---JDBC
- 8个非常实用的Linux系统监控shell脚本
- 新能源成为国家战略新战场,造车玩家百舸争流
- 以后发博客将发到http://www.ouzengqi.com/
- 使用 ESS SDK 快速创建多可用区伸缩组
- 关于vue2.x的sync坑点
- 南阳SEO:网站SEO更新文章6原则,做好避免无用功
- ESP8266+钛极OS(TiJOS)+面包板=JAVA控制物联网硬件平台[创客版]
- ==和equals的区别
- 微信小程序-五星评分功能