Shell scripts monitor CPU, memory, and hard disk utilization

Source: Internet
Author: User

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

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.