Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to monitor cup/ memory / disk utilization with bash script

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how bash scripts monitor cup/ memory / disk utilization". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how the bash script monitors cup/ memory / disk utilization.

Catalogue

Check out the top 40 processes that consume the most memory:

Automatic task to make a basic resource alarm script

Use free tool to monitor memory utilization, alarm over 80 and extract the top ten processes that take up the highest memory.

Use the df command to monitor disk utilization and give an alarm over 80.

#! / bin/bash# get mem_total= `free | awk 'NR==2 {print $2}' `# get the total memory size mem _ use= `free | awk 'NR==2 {print $3}'` # get the memory usage # count the memory usage (usage / total size) mem_use_rate= `awk 'BEGIN {print (' $mem_use'/'$mem_total') * 100}'| awk'{print int ($0)}'`# echo $mem_use_rate -# get disk utilization disk_use_rate_str= `df-h | grep / dev/vda1 | awk-F'[%] +'{print $5}'| awk'{print int ($0)}'`# obtain cpu utilization cpu_use_rate= `top-n 1 | grep Cpu | awk'{print $2}'| awk'{print int ($0)}'` # alarm time file_log=/home/error.log # created in advance Now_time= `date'+% F% T``function send_mail () {} function check () {if [["men_use_rate" > 50]] | | [["disk_use_rate" > 50]] | [["cpu_use_rate" > 50]] Then echo "alarm time:" $now_time > $file_log echo "cpu utilization: ${cpu_use_rate}%-> disk utilization: ${disk_use_rate}%-> memory utilization: ${men_use_rate}%" send_mail fi} function main () {check} main

Note: awk'{print int ($0)}'

Floating-point numbers can be converted to integers when comparing sizes shell does not support floating-point direct comparison with integers

The calculation method of bc that needs to be used

If [["men_use_rate" > 50]] is equivalent to if [$men_use_rate > 50]

Bc example:

#! / bin/bashvar1=20var2=3.14159var3= `echo "scale=0;$var2-$var1" | bc`echo $var3var4= `echo "$var1

< $var2"|bc`echo $var4查看消耗内存最多的前40个进程:ps auxw|head -1;ps auxw|sort -rn -k4|head -40自动任务做一个基础资源告警脚本#!/bin/bashnow=`date -u -d"+8 hour" +'%Y-%m-%d %H:%M:%S'`#cpu使用阈值cpu_warn='85'#mem空闲阈值mem_warn='1000'#disk使用阈值disk_warn='90'#---cpuitem_cpu () {cpu_idle=`top -b -n 1 | grep Cpu | awk '{print $8}'|cut -f 1 -d "."`cpu_use=`expr 100 - $cpu_idle`echo "$now 当前cpu使用率为 $cpu_use" >

> / tmp/monitoring.logif [$cpu_use-gt $cpu_warn] then echo "cpu warring gifts!" Else echo "cpu cookies!" fi} #-memitem_mem () {# MB is the unit mem_free= `free-m | grep "Mem" | awk'{print $4 million cards 6} '`echo "$now current memory space is ${mem_free} MB" > > / tmp/monitoring.logif [$mem_free-lt $mem_warn] then echo "mem warningbacks!" Else echo "mem cookies!" Fi} #-diskitem_disk () {disk_use= `df-P | grep / dev/vda2 | grep-v-E'(tmp | boot)'| awk'{print $5}'| cut-f 1-d "% `echo" $now current disk utilization is $disk_use "> > / tmp/monitoring.logif [$disk_use-gt $disk_warn] then echo" disk warningbacks! " Else echo "disk cookies!" Fi} item_cpuitem_memitem_disk uses the free tool to monitor memory utilization, alarm over 80 and extract the top ten processes that take up the most memory.

The code is as follows:

#! / bin/bash#total=$ (free-m | sed-n '2p' | awk' {print $2}') used=$ (free-m | sed-n '2p' | awk' {print $3}') free=$ (free-m | sed-n '2p' | awk' {print $4}') shared=$ (free-m | sed-n '2p' | awk' {print $5}') buff=$ (free-m | sed-n '2p' | awk' {print $6}') cached=$ (free-m | sed- N '2p' | awk' {print $7}') rate= `echo "scale=2 $used/$total "| bc | awk-F. '{print $2}' `echo-e "total\ tused\ tfree\ tshared\ tbuffer\ tavailable" echo-e "${total} M\ t$ {used} M\ t$ {shared} M\ t$ {buff} M\ t ${cached} M\ nrate:$ {rate}%" if [$rate-ge 80] then echo "Memory Warn" ps aux | grep-v USER | sort-rn-K4 | headfi uses df command to monitor disk utilization and give an alarm over 80.

Df: displays the available disk space on the disk partition.

-h is displayed in a more readable manner

-P uses the output format of POSIX.

#! / bin/bash#DEV= `df-hP | grep'^ / dev/*' | cut-d'-F1 | sort`for I in $DEVdo dev= `df-Ph | grep $I | awk'{print $1} '`used= `df-Ph | grep $I | awk' {print $2} '`used= `df-Ph | grep $I | awk' {print $3} '`free= `df-Ph | grep $I | awk' {print $4} '`rate= `df-Ph | grep $I | awk' {print $5} '`mount= `df-| Ph grep $I | awk' {print $6} '`print- E "$I:\ tsize:$size\ tused:$used\ tfree:$free\ trate:$rate\ tmount:$mount" F = `echo $rate | awk-F%'{print $1} '`if [$F-ge 80] Then echo "$mount Warn" else echo "It's OK" fidone here, I believe you have a better understanding of "how bash scripts monitor cup/ memory / disk utilization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report