Tags: How to monitor server parameters with shell scripts and send email notifications
Description: This script is installed on the CentOS operating system
First, the monitoring script is as follows
vim /root/script/systemmonitor.sh
#! / bin / bash
#Monitor the system load and the number of CPU, memory, hard disk, and 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 15-minute average load of the current system
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, add the previous single digits to 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 it exceeds 70%).
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 in 15 minutes is $ average_load, which exceeds the alert value of 1.0, please handle it immediately!" | mail -s "The system load of the $ IP server is seriously alarmed !!!" [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 (the 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." | mail -s "$ IP server system load alarm" [email protected]
fi
fi
# 2. Monitor the CPU of the system and send an alert email when the usage exceeds 80%:
#Take the current percentage of idle CPU (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." | mail -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 90%:
#Total swap partition 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 10% (that is, when the usage exceeds 90%).
swap_warn = 0.10
#The current remaining swap partition percentage is compared with the alarm value (when it is greater than the alarm value (that is, the remaining 10% or more) it will return 1 and when it is less (that is, the remaining is less than 10%) it will return 0)
swap_now = `expr $ swap_per \> $ swap_warn`
#If the current swap partition usage exceeds 90% (that is, the remaining value is less than 10%, 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 10%, and the usage has exceeded 90%, please handle it in time." | mail -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 / sda2) (take only the integer part)
disk_sda2 = `df -h | grep / dev / sda2 | 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_sda2> 80)); then
echo "The usage of $ IP server / root partition has exceeded 80%, please handle it in time." | mail -s "$ IP server hard disk alarm" [email protected]
fi
# 5. Monitor the login status of system users 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." | mail -s "$ IP server user number alarm" [email protected]
fi
Add execute permissions to the script
chmod + x /root/script/systemmonitor.sh
Add scripts to scheduled tasks
cat / etc / crontab
SHELL = / bin / bash
PATH = / sbin: / bin: / usr / sbin: / usr / bin
MAILTO = root
HOME = /
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0-59)
# | .------------- hour (0-23)
# | | .---------- day of month (1-31)
# | | | .------- month (1-12) OR jan, feb, mar, apr ...
# | | | | .---- day of week (0-6) (Sunday = 0 or 7) OR sun, mon, tue, wed, thu, fri, sat
# | | | | |
# * * * * * user-name command to be executed
* / 1 * * * * root /root/script/systemmonitor.sh
Fourth, send an email to the registered 163 mailbox
(1) First install mailx
yum install -y mailx
(2) Secondly configure mail.rc, the configuration file is as follows
cat /etc/mail.rc
# This is the configuration file for Heirloom mailx (formerly
# known under the name "nail".
# See mailx (1) for further options.
# This file is not overwritten when ‘make install’ is run in
# the mailx build process again.
# Sccsid @ (#) nail.rc 2.10 (gritter) 3/4/06
# Do not forward to mbox by default since this is likely to be
# irritating for most users today.
set hold
# Append rather than prepend when writing to mbox automatically.
# This has no effect unless ‘hold’ is unset again.
set append
# Ask for a message subject.
set ask
# Assume a CRT-like terminal and invoke a pager.
set crt
# Messages may be terminated by a dot.
set dot
# Do not remove empty mail folders in the spool directory.
# This may be relevant for privacy since other users could
# otherwise create them with different permissions.
set keep
# Do not remove empty private mail folders.
set emptybox
# Quote the original message in replies by ">" as usual on the Internet.
set indentprefix = ">"
# Automatically quote the text of the message that is responded to.
set quote
# Outgoing messages are sent in ISO-8859-1 if all their characters are
# representable in it, otherwise in UTF-8.
set sendcharsets = iso-8859-1, utf-8
# Display sender ‘s real names in header summaries.
set showname
# Display the recipients of messages sent by the user himself in
# header summaries.
set showto
# Automatically check for new messages at each prompt, but avoid polling
# of IMAP servers or maildir folders.
set newmail = nopoll
# If threaded mode is activated, automatically collapse thread.
set autocollapse
# Hid
e some header fields which are uninteresting for most human readers.
ignore received in-reply-to message-id references
ignore mime-version content-transfer-encoding
# Only include selected header fields when forwarding messages.
fwdretain subject date from to
# For Linux and BSD, this should be set.
set bsdcompat
set [email protected]
set smtp = smtp.163.com
set [email protected]
set smtp-auth-password = wxhjshfjsjdfsjdfjdhjdsfh #This is the POP activation code for 163.com
set smtp-auth = login
set ssl-verify = ignore
(3) Restart the mail service. If there is no sendmail, install yum install -y sendmail
service sendmail restart ## Two times, the first time failed
service sendmail restart ## Second success
(4) Verification
echo "test" | mail -s "test" [email protected]
(5) Check the mailbox of 163.com and find that there is a message indicating success
Here you must first open the 163 mail protocol. Before you open, you need to open the verification code. This code only appears once to remember. This is the smtp-auth-password password for mail.rc.