码迷,mamicode.com
首页 > 系统相关 > 详细

监控CPU、内存和硬盘利用率的shell脚本

时间:2016-11-16 03:30:44      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:监控

#!/bin/bash

#createdate:2016-11-15

Date=`date+%F" "%H:%M`

IP=`ifconfigeth0 | awk ‘/inet addr/ {print $2}‘ | cut -d: -f2

`

echo -e"\033[032mDate : $Date \nHost : $IP \n\033[0m"

 

echo-----------------CPU----------------

cpu_us=`vmstat|awk ‘{print $13}‘ |sed -n ‘$p‘`

cpu_sy=`vmstat|awk ‘{print $14}‘ |sed -n ‘$p‘`

cpu_id=`vmstat|awk ‘{print $15}‘ |sed -n ‘$p‘`

cpu_wa=`vmstat|awk ‘{print $16}‘ |sed -n ‘$p‘`

cpu_sum=$(($cpu_us+$cpu_sy))

cpu_info()

{

 echo -e "\033[032mCPU_Sum:$cpu_sum%(CPU_Use:${cpu_us}%,CPU_System:${cpu_sy}%)\033[0m"

 echo -e "\033[032mCPU_Idle:${cpu_id}\033[0m"

 echo -e"\033[032mCPU_Wait:${cpu_wa}\033[0m"

 

}

cpu_info;

if [ $cpu_sum-ge 90 ];then

   echo "CPU utilization$cpu_sum"|mail -s "CPU Monitor"xxxxx.com

fi

 

echo-----------------Mem-----------------

#Date=`date+%F" "%H:%M`

#IP=`ifconfigeth0 | awk ‘/inet addr/ {print $2}‘ | cut -d: -f2`

Total=`free -m |grep Mem | awk ‘{print $2}‘`

Use=`free -m|awk ‘/buffers\//{print $NF}‘`

Free=$(($Total-$Use))

#Total_cony=`echo-e | awk ‘{print $1}‘`

echo -e"\033[032mTotal : ${Total}M \nUse : ${Use}M \nFree : ${Free}M\033[0m"

 

echo-----------------IO------------------

Total=`fdisk -l| grep "Disk /dev/sd[a-z]" |awk ‘{print $1$2$3"GB"}‘`

Disk_Use=`df -h|awk ‘{print $1"="$5}‘ | sed ‘1d‘ | sed ‘s/%/%/g‘`

echo -e"\033[032mTotal : $Total\n$Disk_Use\n\033[0m"

效果图:

技术分享

有不足之处请提出,谢谢

本文出自 “通通的技术博客” 博客,转载请与作者联系!

监控CPU、内存和硬盘利用率的shell脚本

标签:监控

原文地址:http://wangjiatong.blog.51cto.com/11834370/1873190

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!