1. Monitor CPU utilization (via Vmstat tool)
#!/bin/bash
#====================================================
# Author:lizhenliang-email:[email Protected]
# Create DATE:2015-02-01
# DESCRIPTION:CPU Utilization Monitor
# blog:lizhenliang.blog.51cto.com
#====================================================
If [' uname '! = "Linux"];then
echo "Check OS not Linux."
Exit 1
Fi
Which Vmstat &>/dev/null
If [$?-ne 0];then
echo "Vmstat command no found, please install the PROCPS package."
Exit 1
Fi
##################################################
Cpu_us= ' Vmstat | awk ' {print $13} ' | Sed-n ' $p '
Cpu_sy= ' Vmstat | awk ' {print $14} ' | Sed-n ' $p '
Cpu_id= ' Vmstat | awk ' {print $} ' | Sed-n ' $p '
Cpu_wa= ' Vmstat | awk ' {print $16} ' | Sed-n ' $p '
cpu_sum=$ (($cpu _us+ $cpu _sy))
Cpu_info ()
{
echo "Cpu_sum: $cpu _sum% (cpu_use:${cpu_us}%, cpu_system:${cpu_sy}%)"
echo "Cpu_idle: ${cpu_id}%"
echo "cpu_wait: ${cpu_wa}"
}
#cpu_info;
If [$cpu _sum-ge];then
echo "CPU utilization $cpu _sum." | Mail-s "CPU Monitor" [email protected]
Fi
2. Monitor Memory utilization
#!/bin/bash
#====================================================
# Author:lizhenliang-email:[email Protected]
# Create DATE:2015-02-01
# Description:memory Utilization Monitor
# blog:lizhenliang.blog.51cto.com
#====================================================
which BC &>/dev/null
If [$?-ne 0];then
echo "BC Command no found, please install the BC package."
Exit 1
Fi
Date= ' Date +%f ' "%h:%m '
ip= ' Ifconfig eth0 | awk '/inet addr/{print $} ' | cut-d:-f2 '
Total= ' Free-m | grep Mem | awk ' {print $} '
Use= ' Free-m | awk '/buffers\//{print $NF} '
free=$ (($Total-$Use))
Total_conv= ' echo ' scale=2; $Total/1024 "| BC | awk ' {print ' "G"} ' #通过bc计算, preserving two digits after the decimal point (scale)
If [$Free-lt];then
Content= ' echo-e ' Date: $Date \nhost: $IP \ntotal: ${total_conv} \nuse: ${use}m \nfree: ${free}m "'
echo "$Content" | Mail-s "Memory Monitor" [email protected]
Fi
3. Monitor Disk Utilization
#!/bin/bash
#====================================================
# Author:lizhenliang-email:[email Protected]
# Create DATE:2015-02-01
# Description:disk Utilization Monitor
# blog:lizhenliang.blog.51cto.com
#====================================================
Date= ' Date +%f ' "%h:%m '
ip= ' Ifconfig eth0 | awk '/inet addr/{print $} ' | cut-d:-f2 '
Total= ' Fdisk-l | grep "disk/dev/sd[a-z]" |awk ' {print $2$3 "GB"} ' |sed ' s/:/=/'
Disk_use= ' df-h |awk ' {print $ ' = ' $ $ ' | Sed ' 1d ' | Sed ' s/%//g '
For I in $Disk _use
Do
A= ' echo $i |awk-f ' = ' {print $} '
If [$A-gt 8];then
Echo-e "Date: $Date \nhost: $IP \ntotal: $Total \nproblem:part use ${i}%" | Mail-s "Disk Mo
Nitor "[Email protected]
Fi
Done
This article is from the "Penguin" blog, please be sure to keep this source http://lizhenliang.blog.51cto.com/7876557/1610415
Shell scripts monitor CPU, memory, and hard disk utilization