Shell scripts to monitor Linux system load vs. CPU, memory, hard disk, number of users

Source: Internet
Author: User
Tags disk usage server memory
Main contents of this section:
Use Shell script to monitor Linux system load, CPU, memory, hard disk, user logins.

First, the Linux system alarm mail script

# vim /scripts/sys-warning.sh
#! / bin / bash
#site: www.jquerycn.cn
#Monitor the system load and CPU, memory, hard disk, and the number of logged-in users. If the alert value is exceeded, an email alert will be sent.
 
#Extract the IP address information of this server
IP = `ifconfig eth0 | grep" inet addr "| cut -f 2 -d": "| cut -f 1 -d" "`
 
# 1. Monitor the change of system load and send an email alert when it exceeds:
 
#Grab the total number of CPUs
cpu_num = `grep -c‘ model name ’/ proc / cpuinfo`
 
#Grab the average load value of the current system for 15 minutes
load_15 = `uptime | awk‘ {print $ 12} ’`
 
#Calculate the average load value of a single core of the current system for 15 minutes. If the result is less than 1.0, the previous single digits are filled with 0.
average_load = `echo" scale = 2; a = $ load_15 / $ cpu_num; if (length (a) == scale (a)) print 0; print a "| bc`
 
#Take the single-digit integer of the above average load value
average_int = `echo $ average_load | cut -f 1 -d". "`
 
#Set the 15-minute average load alarm value of a single core of the system to 0.70 (that is, the alarm is used when more than 70% is used).
load_warn = 0.70
 
#When the average load value of a single core for 15 minutes is greater than or equal to 1.0 (that is, the single-digit integer is greater than 0), send an email alert directly; if it is less than 1.0, perform a second comparison
if (($ average_int> 0)); then
echo "The average load of the $ IP server for 15 minutes is $ average_load, which exceeds the alert value of 1.0, please handle it immediately!" | mutt -s "$ IP server system load severe alarm !!!" [email protected]
else
 
#Compare the current 15-minute average load value with the alarm value (it will return 1 when it is greater than 0.70, and 0 if it is less than)
load_now = `expr $ average_load \> $ load_warn`
 
#If the 15-minute average load of a single core of the system is greater than the alarm value of 0.70 (return value is 1), send an email to the administrator
if (($ load_now == 1)); then
echo "The average load of the $ IP server in 15 minutes reaches $ average_load, which exceeds the alert value of 0.70, please handle it in time." | mutt -s "$ IP server system load alarm" [email protected]
fi
 
fi
 
# 2. Monitor the CPU status of the system and send an alert email when the usage exceeds 80%:
 
#Take the current idle CPU percentage ratio (take only the integer part)
cpu_idle = `top -b -n 1 | grep Cpu | awk‘ {print $ 5} ‘| cut -f 1 -d". "`
 
#Set the alarm value of the idle CPU to 20%. If the current CPU usage exceeds 80% (that is, the remaining is less than 20%), an email alert will be sent immediately.
if (($ cpu_idle <20)); then
echo "The remaining $ cpu_idle% of the $ IP server cpu, the usage rate has exceeded 80%, please handle it in time." | mutt -s "$ IP server CPU alarm" [email protected]
fi
 
# 3. Monitor the swap of the system swap partition, and send an alert email when the usage exceeds 80%:
 
#Total allocation of swap partitions allocated by the system
swap_total = `free -m | grep Swap | awk‘ {print $ 2} ’`
 
#Current remaining swap partition free size
swap_free = `free -m | grep Swap | awk‘ {print $ 4} ’`
 
#Currently used swap partition used size
swap_used = `free -m | grep Swap | awk‘ {print $ 3} ’`
 
if (($ swap_used! = 0)); then
#If the swap partition has been used, calculate the percentage of the total remaining swap partition free, expressed as a decimal, and you must add an integer 0 before the decimal point
swap_per = 0`echo "scale = 2; $ swap_free / $ swap_total" | bc`
 
#Set the alarm value of the swap partition to 20% (that is, when the usage exceeds 80%).
swap_warn = 0.20
 
#The current remaining swap partition percentage is compared with the alarm value (when it is greater than the alarm value (that is, the remaining 20% or more) it will return 1 and when it is less (that is, the remaining is less than 20%) it will return 0)
swap_now = `expr $ swap_per \> $ swap_warn`
 
#If the current swap partition usage exceeds 80% (that is, the remaining is less than 20%, the return value above is equal to 0), send an email alert immediately
if (($ swap_now == 0)); then
echo "Only $ swap_free M of the $ IP server swap partition is left unused, the remaining is less than 20%, and the usage has exceeded 80%, please handle it in time." | mutt -s "$ IP server memory alarm" [email protected]
fi
 
fi
 
# 4. Monitor the usage of the system hard disk root partition and send an alert email when the usage exceeds 80%:
 
#Take the used percentage value of the current root partition (/ dev / sda3) (take only the integer part)
disk_sda3 = `df -h | grep / dev / sda3 | awk‘ {print $ 5} ‘| cut -f 1 -d"% "`
 
#Set the alert value of the free hard disk capacity to 80%. If the current hard disk usage exceeds 80%, send an email alert immediately.
if (($ disk_sda3> 80)); then
echo "The usage of $ IP server / root partition has exceeded 80%, please handle it in time." | mutt -s "$ IP server hard disk alarm" [email protected]
fi
 
# 5. Monitor the user login status of the system, and send an alert email when the number of users exceeds 3:
 
#Take the current user login number (take only the value part)
users = `uptime | awk‘ {print $ 6} ’`
 
#Set the alarm value for the number of login users to 3. If the current number of users exceeds 3, send an email alert immediately
if (($ users> = 3)); then
echo "The number of $ IP server users has reached $ users, please handle it in time." | mutt -s "Alarm of $ IP server users" [email protected]
fi
# chmod a + x /scripts/sys-warning.sh
Second, join the task plan: Detect every ten minutes, send an email immediately if there is an alarm (once every ten minutes).

# crontab -e
* / 10 * * * * /scripts/sys-warning.sh
# service crond restart
Third, start the Sendmail service or install the mail client msmtp software under linux (a tool similar to foxmail)
1. Download and install: http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0
 

# tar jxvf msmtp-1.4.16.tar.bz2
# cd msmtp-1.4.16
# ./configure --prefix = / usr / local / msmtp
# make
# make install
2. Create msmtp configuration file and log file (host is the mail domain name, mail user name test, password 123456)

# vim ~ / .msmtprc
account default
host 126.com
from [email protected]
auth login
user test
password 123456
logfile ~ / .msmtp.log
# chmod 600 ~ / .msmtprc
# touch ~ / .msmtp.log
3, Mutt installation configuration: (Generally there is a default installation of Mutt under Linux)

set sendmail = "/ usr / local / msmtp / bin / msmtp"
set use_from = yes
set realname = "memory"
set from = [email protected]
set envelope_from = yes
set rfc2047_parameters = yes
set charset = "utf-8"
4. Email sending test (-s email header)

# echo "Email content 123456" | mutt -s "Email header test message" [email protected]
 

Shell script to monitor Linux system load and CPU, memory, hard disk, and number of users
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.