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

简介:

1、监控CPU利用率(通过vmstat工具)

1
<span style= "color:rgb(0,0,0);font-family:'宋体', SimSun;" > #!/bin/bash<br>#====================================================<br># Author: lizhenliang - EMail:zhenliang369@163.com<br># Create Date: 2015-02-01<br># Description: cpu utilization monitor<br># blog:lizhenliang.blog.51cto.com<br>#====================================================<br>if [ `uname` != "Linux" ];then<br>        echo "check os not linux."<br>        exit 1<br>fi<br>which vmstat &>/dev/null<br>if [ $? -ne 0 ];then<br>        echo "vmstat command no found, please install procps package." <br>        exit 1<br>fi<br>##################################################<br>cpu_us=`vmstat | awk '{print $13}' | sed -n '$p'`<br>cpu_sy=`vmstat | awk '{print $14}' | sed -n '$p'`<br>cpu_id=`vmstat | awk '{print $15}' | sed -n '$p'`<br>cpu_wa=`vmstat | awk '{print $16}' | sed -n '$p'`   #等待I/0完成<br>cpu_sum=$(($cpu_us+$cpu_sy))<br>cpu_info()<br>{<br>echo "CPU_Sum : $cpu_sum% ( CPU_Use:${cpu_us}% , CPU_System:${cpu_sy}% )" <br>echo "CPU_Idle : ${cpu_id}%"<br>echo "CPU_Wait : ${cpu_wa}"<br>}<br>#cpu_info;<br>if [ $cpu_sum -ge 90 ];then<br>        echo "CPU utilization $cpu_sum." | mail -s "CPU Monitor" baojingtongzhi@163.com<br>fi<br></span>

2、监控内存利用率

1
<span style= "color:rgb(0,0,0);font-family:'宋体', SimSun;" > #!/bin/bash<br>#====================================================<br># Author: lizhenliang - EMail:zhenliang369@163.com<br># Create Date: 2015-02-01<br># Description: memory utilization monitor<br># blog:lizhenliang.blog.51cto.com<br>#====================================================<br>which bc &>/dev/null<br>if [ $? -ne 0 ];then<br>        echo "bc command no found, Please install bc package." <br>        exit 1<br>fi<br>Date=`date +%F" "%H:%M`<br>IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2`<br>Total=`free -m | grep Mem | awk '{print $2}'`<br>Use=`free -m | awk '/buffers\// {print $NF}'`<br>Free=$(($Total-$Use))<br>Total_conv=`echo "scale=2;$Total/1024" | bc | awk '{print $1"G"}'`  #通过bc计算,保留小数点后两位(scale)<br>if [ $Free -lt 200 ];then<br>        Content=`echo -e "Date : $Date \nHost : $IP \nTotal : ${Total_conv} \nUse : ${Use}M \nFree : ${Free}M"`<br>        echo "$Content" | mail -s "Memory Monitor" baojingtongzhi@163.com<br>fi<br></span>

3、监控磁盘利用率

1
<span style= "color:rgb(0,0,0);font-family:'宋体', SimSun;" > #!/bin/bash<br>#====================================================<br># Author: lizhenliang - EMail:zhenliang369@163.com<br># Create Date: 2015-02-01<br># Description: disk utilization monitor<br># blog:lizhenliang.blog.51cto.com<br>#====================================================<br>Date=`date +%F" "%H:%M`<br>IP=`ifconfig eth0 | awk '/inet addr/ {print $2}' | cut -d: -f2`<br>Total=`fdisk -l | grep "Disk /dev/sd[a-z]" |awk '{print $2$3"GB"}' |sed 's/:/=/' |xargs echo -n |sed 's/[ ]/,/g'`    #去掉换行符,并以逗号分隔在邮件显示总每个分区大小<br>Disk_Use=`df -h |awk '{print $1"="$5}' | sed '1d' | sed 's/%//g'`<br>for i in $Disk_Use<br>do<br>        A=`echo $i |awk -F'=' '{print $2}'`<br>        if [ $A -gt 8 ];then<br>                echo -e "Date : $Date \nHost : $IP \nTotal : $Total \nProblem : Part Use ${i}%" | mail -s "Disk Mo<br>nitor" baojingtongzhi@163.com<br>        fi<br>done<br></span>
目录
相关文章
|
4天前
|
监控 Unix Shell
shell脚本编程学习
shell脚本编程
22 12
|
7天前
|
Shell
shell脚本变量 $name ${name}啥区别
shell脚本变量 $name ${name}啥区别
|
7天前
|
存储 关系型数据库 MySQL
查询服务器CPU、内存、磁盘、网络IO、队列、数据库占用空间等等信息
查询服务器CPU、内存、磁盘、网络IO、队列、数据库占用空间等等信息
53 5
|
10天前
|
人工智能 监控 Shell
常用的 55 个 Linux Shell 脚本(包括基础案例、文件操作、实用工具、图形化、sed、gawk)
这篇文章提供了55个常用的Linux Shell脚本实例,涵盖基础案例、文件操作、实用工具、图形化界面及sed、gawk的使用。
26 2
|
1月前
|
Shell
Shell脚本有哪些基本语法?
【9月更文挑战第4天】
43 17
|
1月前
|
网络协议 关系型数据库 MySQL
Shell 脚本案例
Shell 脚本案例
36 8
|
1月前
|
Shell Linux 开发工具
linux shell 脚本调试技巧
【9月更文挑战第3天】在Linux中调试shell脚本可采用多种技巧:使用`-x`选项显示每行命令及变量扩展情况;通过`read`或`trap`设置断点;利用`echo`检查变量值,`set`显示所有变量;检查退出状态码 `$?` 进行错误处理;使用`bashdb`等调试工具实现更复杂调试功能。
|
6天前
|
Prometheus Kubernetes 监控
使用kubectl快速查看各个节点的CPU和内存占用量
在Kubernetes集群中,安装metrics-server,并使用kubectl快速查看集群中各个节点的资源使用情况。
19 0
|
2月前
|
存储 编译器 C语言
【C语言篇】数据在内存中的存储(超详细)
浮点数就采⽤下⾯的规则表⽰,即指数E的真实值加上127(或1023),再将有效数字M去掉整数部分的1。
|
3月前
|
存储 分布式计算 Hadoop
HadoopCPU、内存、存储限制
【7月更文挑战第13天】
215 14
下一篇
无影云桌面