In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to use shell to count the usage of CPU, memory and hard disk. It has certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.
The code is as follows:
#! / 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 ("% s\ n", $13)}'`
System= `echo $temp | awk'{printf ("% s\ n", $14)}'`
Idle= `echo $temp | awk'{printf ("% s\ n", $15)}'`
Total= `echo | awk'{print (c1+c2)} 'c1=$system c2echo $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
In the process of developing CFS, we need to monitor the real-time memory, CPU and hard disk utilization of the machine, because CFS runs on the Linux machine and can use some corresponding virtual files in Linux to calculate the memory CPU utilization, so write the following script, which is very simple, but very practical.
It consists of three parts, namely hard disk utilization, memory utilization and CPU utilization.
The code is as follows:
#! / 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.
# my file is mainly stored in the sda8 partition, so it is extracted.
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`
Thank you for reading this article carefully. I hope the article "how to use shell to calculate the usage of CPU, memory and hard disk" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.