A shell script used to count the hard disk usage of CPU memory

Source: Internet
Author: User

Copy codeThe Code is as follows :#! /Bin/bash
# This script is used 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 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

During the development of CFS, You need to monitor the machine's real-time memory, CPU, and hard disk usage, because CFS runs on a Linux machine, some virtual files in Linux can be used to calculate the memory CPU usage. Therefore, the following script is compiled, which is simple but practical.

It consists of three parts: hard disk usage, memory usage, and CPU usage.

Copy codeThe 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 files are mainly stored in the sda8 partition, so this 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'

This article is from "Fighting, Jayson !" Blog

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.