iis服务器助手广告广告
返回顶部
首页 > 资讯 > 服务器 >一个用了统计CPU 内存 硬盘 使用率的shell脚本
  • 555
分享到

一个用了统计CPU 内存 硬盘 使用率的shell脚本

使用率用了脚本 2022-06-04 21:06:58 555人浏览 安东尼
摘要

#!/bin/bash#This script is use for describle CPU Hard Memery Utilizationtotal=0idle=0system=0user=0nic


#!/bin/bash
#This script is use for describle CPU Hard Memery Utilization
total=0
idle=0
system=0
user=0
nice=0
mem=0
vmexec=/usr/bin/vmstat
which sar > /dev/null 2>&1
if [ $? -ne 0 ]
then
ver=`vmstat -V | awk '{printf $3}'`
nice=0
temp=`vmstat 1 3 |tail -1`
user=`echo $temp |awk '{printf("%sn",$13)}'`
system=`echo $temp |awk '{printf("%sn",$14)}'`
idle=`echo $temp |awk '{printf("%sn",$15)}'`
total=`echo|awk '{print (c1+c2)}' c1=$system c2=$user`
fi
echo "#CPU Utilization#"
echo "Total CPU is already use: $total"
echo "CPU user is already use: $user"
echo "CPU system is already use: $system"
echo "CPU nice is already use: $nice"
echo "CPU idle is already use: $idle"
echo
root_use=$(df -lh | awk 'NR==2' | awk '{print $5}')
dev_use=$(df -lh | awk 'NR==3' | awk '{print $5}')
dev_shm_use=$(df -lh | awk 'NR==4' | awk '{print $5}')
echo "#Hard Utilization#"
echo "/ is already use: $root_use"
echo "/dev is already use: $dev_use"
echo "/dev/shm is already use: $dev_shm_use"
echo
memery_used=$(free | awk 'NR==2' | awk '{print $3}')
memery_all=$(free | awk 'NR==2' | awk '{print $2}')
memery_percent=$(echo "scale=4;$memery_used / $memery_all" | bc)
percent_part1=$(echo $memery_percent | cut -c 2-3)
percent_part2=$(echo $memery_percent | cut -c 4-5)
echo "#Memery Utilization#"
echo "system memery is already use: $percent_part1.$percent_part2%"
swap_used=$(free | awk 'NR==4' | awk '{print $3}')
swap_all=$(free | awk 'NR==4' | awk '{print $2}')
swap_percent=$(echo "scale=4;$swap_used / $swap_all" | bc)
swap_part1=$(echo $swap_percent | cut -c 2-3)
swap_part2=$(echo $swap_percent | cut -c 4-5)
echo "swap memery is already use: $swap_part1.$swap_part2%"
echo

开发CFS过程中需要对机器实时的内存,CPU,硬盘使用率进行监控,因为CFS跑在linux机器上,可以利用Linux中一些相应虚拟文件进行内存CPU使用率的计算,于是编写了以下脚本,很简单,但是很实用。

包含三个部分,分别为硬盘使用率,内存使用率和CPU使用率。


#!/bin/sh

#count cpu_used_rate,memory_used_rate,disk_used_rate
#@Jayson 2012-5

#disk_used_rate
#Depend on real storage place the parameter 'Location' need to alter.
#本人文件主要存储于sda8分区,所以以此提取。
Location=/dev/sda8
Disk_Used_Rate=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate

#memory_used_rate
LoadMemory=$(cat /proc/meminfo | awk '{print $2}')
Total=$(echo $LoadMemory | awk '{print $1}')
Free1=$(echo $LoadMemory | awk '{print $2}')
Free2=$(echo $LoadMemory | awk '{print $3}')
Free3=$(echo $LoadMemory | awk '{print $4}')

Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr $Used/$Total*100 | bc -l`
Memory_Used_Rate=`expr $Used_Rate/1 | bc`
echo $Memory_Used_Rate%

#cpu_used_rate
Log1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Sys1=$(echo $Log1 | awk '{print $4}')
Total1=$(echo $Log1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

sleep 0.5

Log2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Sys2=$(echo $Log2 | awk '{print $4}')
Total2=$(echo $Log2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')

Sys=`expr $Sys2 - $Sys1`


本文出自 “Fighting,Jayson!” 博客

--结束END--

本文标题: 一个用了统计CPU 内存 硬盘 使用率的shell脚本

本文链接: https://www.lsjlt.com/news/18133.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作