Shell scripts monitor system load, CPU, and memory usage

Source: Internet
Author: User

#一. Writing script files for system load monitoring
#!/bin/bash

########################################################################
#This scripts is checking the system load,vision 0.1
#Author: Hejunyi
#E-mail:158****[email protected]
########################################################################

#使用uptime命令监控linux系统负载变化

#提取本服务器的IP地址信息
ip= ' Ifconfig eth1 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '

#抓取cpu的总核数
cpu_num= ' grep-c ' model name '/proc/cpuinfo '

#抓取当前系统15分钟的平均负载值
load_15= ' Uptime | awk ' {print $NF} '

#计算当前系统单个核心15分钟的平均负载值, the result is less than 1.0 when the front single digit is 0.
Average_load= ' echo ' scale=2;a= $load _15/$cpu _num;if (Length (a) ==scale (a)) print 0;print a "| BC ' # #scale表示小数精度位数

#取上面平均负载值的个位整数
Average_int= ' echo $average _load | Cut-f 1-d "." `

#设置系统单个核心15分钟的平均负载的告警值为0.70 (i.e. alarm when using more than 70%).
load_warn=0.70

#当单个核心15分钟的平均负载值大于等于1.0 (that is, average_int digits are greater than 0), and two comparisons if less than 1.0
if (($average _int > 0)); Then
echo "$IP server 15min average load reached $average_load, exceeding the alert value of 1.0, please deal with it immediately!"
Else
#当前系统15分钟平均负载值与告警值进行比较 (when greater than set will return 1, less than 0 will be returned)
load_now= ' expr $average _load \> $load _warn '

#如果系统单个核心15分钟的平均负载值大于告警值0.70 (return value is 1), send an email to the administrator
if (($load _now = = 1)); Then
echo "$IP server 15min average load reached $average_load, exceeding the alert value of 0.00, please timely processing"
Fi
Fi


Second, monitor the system CPU situation
#!/bin/bash

########################################################################
#This scripts is checking the cpu,vision 0.1
#Author: Hejunyi
#E-mail:158****[email protected]
########################################################################

#提取本服务器的IP地址信息
ip= ' ifconfig eth1|grep "inet addr" |awk ' {print $} ' |cut-f2-d ': "'

#取当前空闲cpu百份比值
Cpu_id= ' top-b-n1|grep "Cpu" |awk ' {print $} ' |cut-f1-d "%" '

#取当前空闲cpu百份比值的整数部分
Cpu_int= ' echo $cpu _id|cut-f1-d. " `

#设置空闲cpu的告警值为20% (that is, less than 20% remaining)
if (($cpu _id < 20)); Then
echo "$IP Server CPU Surplus: $CPU _id%, please handle it in time"
Else
echo "$IP Server CPU Surplus: $CPU _id%"
Fi


Third, monitoring system memory
#!/bin/bash

########################################################################
#This scripts is checking the free_mem,vision 0.1
#Author: Hejunyi
#E-mail:158****[email protected]
########################################################################

#抓取物理内存free值
echo mem-free: ' Free-m | grep Mem | awk ' {print $4} ' M

#抓取缓冲区的free值
echo buffers/cache-free: ' Free-m | grep-| awk ' {print $4} ' M

#抓取Swap分区free值
echo swap-free: ' Free-m | grep Swap | awk ' {print $4} ' M


Iv. monitoring System Exchange Partition swap situation
#!/bin/bash

########################################################################
#This scripts is checking the swap,vision 0.1
#Author: Hejunyi
#E-mail:158****[email protected]
########################################################################

#提取本服务器的IP地址信息
ip= ' Ifconfig eth1 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '

#系统分配的交换分区总量
Swap_total= ' Free-m | grep Swap | awk ' {print $} '

#当前剩余的交换分区free大小
Swap_free= ' Free-m | grep Swap | awk ' {print $4} '

#当前已使用的交换分区used大小
Swap_used= ' Free-m | grep Swap | awk ' {print $} '


#设置交换分区的告警值为20%
swap_warn=0.20

if (($swap _used! = 0)); Then

#如果交换分区已被使用, calculates the percentage of the total of the current remaining swap partition free, in decimal notation, to complement an integer digit before the decimal point 0
Swap_per= ' echo ' scale=2;b= $swap _free/$swap _total;if (length (b) ==scale (b)) print 0;print B "|BC"

#当前剩余交换分区百分比与告警值进行比较 (1 is returned when the value is less than 20%, and 0 is returned if it is less than)
swap_now= ' expr $swap _per \< $swap _warn '

#如果当前交换分区使用即剩余小于20% (The return value above equals 1)
if ($swap _now ==1); then
echo "$IP Server swap partition utilization rate of 80%, only left" $swap _free "M unused, please timely > manager! "
Fi
Fi

This article from the "12148275" blog, reproduced please contact the author!

Shell scripts monitor system load, CPU, and memory usage

Related Article

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.