Original works can be reprinted. During reprinting, you must mark the original source, author information, and this statement in hyperlink form. Otherwise, legal liability will be held. Http://huangrs.blog.51cto.com/2677571/788379
These days I have been studying shell scripts and writing some system load and CPU monitoring script programs. Without Nagios monitoring software, as long as the server can access the Internet, you can send an email to remind the administrator of the system load and CPU usage.
1. Install a mail client msmtp software (similar to a Foxmail tool) in Linux)
1, download installation: 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 the msmtp configuration file and log file (host is the mail domain name, mail username test, password 123456)
# Vim ~ /. Msmtprc
Account default
Host 126.com
From test@126.com
AUTH LOGIN
User Test
Password 123456
Logfile ~ /. Msmtp. Log
# Chmod 600 ~ /. Msmtprc
# Touch ~ /. Msmtp. Log
3. Mutt installation configuration: (mutt is installed by default in Linux)
# Vim ~ /. Muttrc
Set Sendmail = "/usr/local/msmtp/bin/msmtp"
Set use_from = Yes
Set realname = "moniter"
Set From = test@126.com
Set envelope_from = Yes
Set rfc2047_parameters = Yes
Set charset = "UTF-8"
4. Mail sending test (-s mail title,-a table with attachments)
# Echo "Mail content 123456" | mutt-s "mail Title Test mail"-A/scripts/test.txt test@126.com
Ii. Monitoring Server system load:
1. Run the uptime command to view the current load (1 minute, 5 minutes, 15 minutes average load)
# Uptime
15:43:59 up 186 days, 1 user, load average: 0.01, 0.02, 0.00
Empirical rules of system load: (from http://www.ruanyifeng.com/blog/2011/07/linux_load_average_explained.html)
(1) Observe "15-minute system load" and use it as an indicator for normal computer operation.
(2) If the average load within 15 minutes (after the system load is divided by the number of CPU cores) exceeds 1.0, the problem persists, not a temporary phenomenon.
(3) When the system load continues to exceed 0.7, you must start investigating where the problem is and prevent the situation from deteriorating.
(4) When the system load continues to exceed 1.0, you must find a solution to reduce this value.
(5) When the system load reaches 5.0, it indicates that your system has a very serious problem, does not respond for a long time, or is close to a dead end.
2. view the total number of server CPU Cores
# Grep-C 'model name'/proc/cpuinfo
3. server load is intercepted for 1 minute, 5 minutes, and 15 minutes.
# Uptime | awk '{print $8, $9, $10, $11, $12 }'
Load average: 0.01, 0.02, 0.00
4. view the average load intercepted for 15 minutes
# Uptime | awk '{print $12}' (using '{print $12}' is not accurate enough. If you use awk to retrieve 12th fields, the result may be empty. Use the $ NF table to output the content of the last segment)
# Uptime | awk '{print $ NF }'
5. Compile a script file for system load monitoring:
# Vim/scripts/load-check.sh
#! /Bin/bash
# Use the uptime command to monitor Linux system load changes
# Obtain the current system time (write files in append mode>)
Date>/scripts/datetime-load.txt
# Extract the server load for 1 minute, 5 minutes, and 15 minutes
Uptime | awk '{print $8, $9, $10, $11, $12}' >>/ scripts/load.txt
# Connect the above time and load-related row data (re-write the file each time>)
Paste/scripts/datetime-load.txt/scripts/load.txt>/scripts/load_day.txt
# Chmod A + x/scripts/load-check.sh
6. Compile an email sending script for the system load result file:
# Vim/scripts/sendmail-load.sh
#! /Bin/bash
Upload the load_day.txt file generated by the system load monitoring to the user by email.
# Extracting IP address information of the current server
IP = 'ifconfig eth0 | grep "Inet ADDR" | cut-F 2-D ":" | cut-F 1-d ""'
# Extract the current date
Today = 'date-d "0 day" + % Y % m month % dday'
# Send a system load monitoring result email
Echo "this is the system load monitoring report for $ IP server $ today. Please download the attachment. "| Mutt-s" $ IP server $ today system load monitoring report "-A/scripts/load_day.txt test@126.com
# Chmod A + x/scripts/sendmail-load.sh
7. Compile a script file for system load monitoring:
# Vim/scripts/load-warning.sh
#! /Bin/bash
# Use the uptime command to monitor Linux system load changes
# Extracting IP address information of the current server
IP = 'ifconfig eth0 | grep "Inet ADDR" | cut-F 2-D ":" | cut-F 1-d ""'
# Capture the total number of CPU Cores
Cpu_num = 'grep-C' model name'/proc/cpuinfo'
# Capture the average load value of the current system for 15 minutes
Load_15 = 'uptime | awk' {print $ NF }''
# Calculate the average load of a single core in the current system for 15 minutes. If the result is smaller than 1.0, the previous single digit is supplemented with 0.
Average_load = 'echo "scale = 2; A = $ load_15/$ cpu_num; If (length (A) = Scale (A) print 0; print a" | BC'
# Take the one-digit integer of the above average load value
Average_int = 'echo $ average_load | cut-F 1-d "."'
# Set the alarm value for the average load of a single core for 15 minutes to 0.70 (that is, an alarm is triggered when the load exceeds 70% ).
Load_warn = 0.70
# If the average load of a single core within 15 minutes is greater than or equal to 1.0 (that is, the single-digit integer is greater than 0), an alarm is sent directly. If the load is less than 1.0, a secondary comparison is performed.
If ($ average_int> 0); then
Echo "$ the average system load of the IP server in 15 minutes is $ average_load, which exceeds the warning value of 1.0. Please handle it now !!! "| Mutt-s" $ severe system load alert for IP servers !!! Test@126.com
Else
# Compare the average load of the current system within 15 minutes with the alarm value (1 is returned when the alarm value is greater than 0.70, and 0 is returned if the alarm value is smaller)
Load_now = 'expr $ average_load \> $ load_warn'
# If the average load of a single core within 15 minutes is greater than 0.70 of the alarm value (the return value is 1), send an email to the Administrator.
If ($ load_now = 1); then
Echo "$ the average system load of the IP server in 15 minutes reaches $ average_load, which exceeds the warning value of 0.70. Please handle it in time. "| Mutt-s" $ IP server system load warning "test@126.com
Fi
Fi
# Chmod A + x/scripts/load-warning.sh
Iii. Monitor the CPU usage of the server system:
1. Run the TOP command to view the CPU usage in Linux:
# Top-B-N 1 | grep CPU (only one output result is required for the-B-N 1 Table)
CPU (s): 0.0% us, 0.0% Sy, 0.0% Ni, 99.9% ID, 0.0% wa, 0.0% hi, 0.0% Si, 0.0% St
(Idle value)
2. Run the following command to view the percentage value of the idle CPU ):
# Top-B-N 1 | grep CPU | awk '{print $5}' | cut-F 1-d "."
3. Compile a script file for CPU monitoring:
# Vim/scripts/cpu-check.sh
#! /Bin/bash
# Use the top command to monitor CPU changes in Linux
# Obtain the current system time (write files in append mode>)
Date>/scripts/datetime-cpu.txt
# Capture the current CPU value (write the file in append mode>)
Top-B-N 1 | grep CPU>/scripts/cpu-now.txt
# Connect the preceding time and CPU-related data row by row (re-write the file each time>)
Paste/scripts/datetime-cpu.txt/scripts/cpu-now.txt>/scripts/cpu.txt
# Chmod A + x/scripts/cpu-check.sh
4. view the result file of CPU monitoring:
# Cat/scripts/cpu.txt
5. write an email sending script for the CPU result file:
# Vim/scripts/sendmail-cpu.sh
#! /Bin/bash
Worker sends the generated cpu.txt file to the user by email
# Extracting IP address information of the current server
IP = 'ifconfig eth0 | grep "Inet ADDR" | cut-F 2-D ":" | cut-F 1-d ""'
# Extract the current date
Today = 'date-d "0 day" + % Y % m month % dday'
# Sending CPU Monitoring Result emails
Echo "this is the CPU monitoring report for $ IP server $ today. Please download the attachment. "| Mutt-s" $ IP server $ today CPU Monitoring Report "-A/scripts/cpu.txt test@126.com
# Chmod A + x/scripts/sendmail-cpu.sh
4. Monitor the CPU usage of the system and send an alarm email when the CPU usage exceeds 80%:
# Vim/scripts/cpu-warning.sh
#! /Bin/bash
# Script program for monitoring system CPU usage
# Extracting IP address information of the current server
IP = 'ifconfig eth0 | grep "Inet ADDR" | cut-F 2-D ":" | cut-F 1-d ""'
# Obtain the ratio of the current idle CPU to a hundred (only an integer)
Cpu_idle = 'top-B-N 1 | grep CPU | awk '{print $5}' | cut-F 1-d "."'
# Set the alarm value of idle CPU to 20%. If the current CPU usage exceeds 80% (that is, the remaining CPU usage is less than 20%), send an email immediately.
If ($ cpu_idle <20); then
Echo "$ IP server CPU remaining $ cpu_idle %, usage has exceeded 80%, please handle it in time. "| Mutt-s" $ IP server CPU alarm "test@126.com
Fi
# Chmod A + x/scripts/cpu-warning.sh
5. Add a task plan: Check the system load and CPU usage every ten minutes. If an alarm is triggered, send an email immediately (once every minute). send an email to the load and CPU detection result at every day.
# Crontab-e
*/10 */scripts/load-check.sh
*/10 */scripts/load-warning.sh
0 8 ***/scripts/sendmail-load.sh
*/10 */scripts/cpu-check.sh
*/10 */scripts/cpu-warning.sh
0 8 ***/scripts/sendmail-cpu.sh
# Service crond restart
This article is from my technology dream road blog, please be sure to keep this source http://huangrs.blog.51cto.com/2677571/788379