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 and install:
Http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2? Modtime = 1217206451 & big_mirror = 0
Unzip tarjxvfmsmtp-1.4.16.tar.bz2 # cdmsmtp-1.4.16 #./configure -- prefix =/usr/local/msmtp # make & makeinstall
2. Create the msmtp configuration file and log file (host is the mail domain name, the mail username fuquanjun, And the password fuquanjun)
# Vim/root/. msmtprc
Accountdefaulthostxxxxx.comfromfuquanjun@xxxx.comauthloginuserfuquanjunpasswordfuquanjunlogfile ~ /. Msmtp. log
# Chmod 600/root/. msmtprc
# Touch ~ /. Msmtp. log
3. mutt installation configuration: (mutt is installed by default in linux)
If not, use yum to install
Yum-y install mutt
# Vim/root/. muttrc
Setsendmail = "/usr/local/msmtp/bin/msmtp" setuse_from = yessetrealname = "moniter" setfrom = fuquanjun@xxx.comsetenvelope _ from = yessetrfc2047_parameters = yessetcharset = "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 fuquanjun@xxxx.com
The following error message is displayed:
Msmtp: account default not found: no configuration file available
An error occurred while sending the mail. The sub-process has exited 78 ().
This email cannot be sent.
Solution:
Use msmtp to send a test:/usr/local/msmtp/bin/msmtp-S found that the configuration file was not found
Msmtp: account default not found: no configuration file available
View the current configuration file path:/usr/local/msmtp/bin/msmtp-P
Ignoring system configuration file/work/target/etc/msmtprc: No such file or directory
Ignoring user configuration file/root/. msmtprc: No such file ordirectory
Falling back to default account
Msmtp: account default not found: no configuration file available
Therefore, copy/usr/local/etc/msmtprc to/root/. msmtprc.
Check the mutt File Installation Directory
Rpm-ql mutt
Therefore, copy/etc/Muttrc to/root/. muttrc to send the email.
Ii. Monitoring Server system load:
1. Use the uptime command to view the current load (1 minute, 5 minutes, 15 minutes average load) on Apple's Mac.
# Uptime15: 43: 59up186days, 1 user, loadaverage: 0.01, 0.02, 0.00
"Load average" means the average load of the system within 1 minute, 5 minutes, and 15 minutes.
(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.
Assume that your computer has only one CPU. If your computer has two CPUs, it means that the computer's processing power is doubled, and the number of processes that can be processed at the same time is doubled.
Two CPUs indicate that the system load can reach 2.0, and each CPU has a workload of 100%. To promote it, computers with n CPUs have an acceptable system load of up to n.0.
2. view the total number of server cpu Cores
# Grep-c 'modelname'/proc/cpuinfo or cat/proc/cpuinfo
3. server load is intercepted for 1 minute, 5 minutes, and 15 minutes.
# Uptime | awk '{print $8, $9, $10, $11, $12} 'loadaverage: 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 last part.) # uptime | awk '{print $ NF }'
5. Compile a script file for system load monitoring:
# Vim/scripts/load-check.sh
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.txt the load_day.txt file generated by the system load monitoring is sent to the user by mail # extract the server's IP address information IP = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# extract current date today = 'date-d" 0day "+ % Y % m month % dday' # Send system load monitoring result email echo" this is $ IP server $ today system load monitoring report, download the attachment. "| Mutt-s" $ IP server $ today system load monitoring report "-a/scripts/load_day.txtfuquanjun@xxx.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 # extract the IP address of the server IP = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# capture the total number of cpu cores cpu_num = 'grep-C' modelname'/proc/cpuinfo' # capture the average load value of the current system for 15 minutes load_15 = 'uptime | awk '{print $ NF} ''# calculates the average load of a single core of the current system for 15 minutes, if the result is less than 1.0, the first digit is 0. Average_load = 'echo "scale = 2; a = $ load_15/$ cpu_num; if (length (a) = scale (a) print0; printa "| bc '# obtain the above average load value of a single integer average_int = 'echo $ average_load | cut-f1-d ". "'# Set the alarm value for the average load of a single core of the system for 15 minutes to 0.70 (that is, when more than 70% is used ). Load_warn = 0.70 # When the average load of a single core within 15 minutes is greater than or equal to 1.0 (that is, a single integer greater than 0), an alarm is sent directly; if it is less than 1.0, perform secondary comparison if ($ average_int> 0); thenecho "$ the average system load of the IP server for 15 minutes is $ average_load, exceeding the warning value of 1.0, please handle it now !!! "| Mutt-s" $ severe system load alert for IP servers !!! "Fuquanjun@xxx.comelse # Compare the average load value of the current system for 15 minutes with the alarm value (1 is returned when the alarm value is greater than 0.70, 0 is returned when the alarm value is less) load_now = 'expr $ average_load \> $ load_warn' # If the average load value of a single core in 15 minutes is greater than the alarm value of 0.70 (the return value is 1 ), send an email to the administrator if ($ load_now = 1); thenecho "$ the average system load of the IP server reaches $ average_load within 15 minutes, exceeding the warning value of 0.70. Please handle it in time. "| Mutt-s" $ IP server system load warning "fuquanjun@xxx.comfifi
# 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-n1 | grepCpu (-b-n1 tables only need 1 output result) 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
2. Run the following command to view the percentage value of the idle cpu ):
# Top-b-n1 | grepCpu | awk '{print $5}' | cut-f1-d "."
3. Compile a script file for cpu monitoring:
# Vim/scripts/cpu-check.sh
#! /Bin/bash # use the top command to monitor linux system cpu Changes # 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-n1 | grepCpu>/scripts/cpu-now.txt # line-by-line connection to the above time and cpu-related row data (each re-Write File>) 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/bashhandler sends the generated cpu.txt file to the user by mail # extract the IP address information of the server IP address = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# extract current date today = 'date-d" 0day "+ % Y % m month % dday' # Send cpu Monitoring Result email echo" this is $ IP cpu monitoring report for the server $ today, download the attachment. "| Mutt-s" $ IP server $ today CPU Monitoring Report "-a/scripts/cpu.txtfuquanjun@xxx.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 # monitor the cpu Of the script program # extract the IP address of the server IP = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# Take the percentage of the current idle cpu (only integer) cpu_idle = 'top-b-n1 | grepCpu | awk' {print $5} '| cut-f1-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 to send an alarm if ($ cpu_idle <20 )); thenecho "$ IP server cpu remaining $ cpu_idle %, the usage has exceeded 80%, please promptly handle. "| Mutt-s" $ IP server CPU alarm "fuquanjun@xxx.comfi
# Chmod a + x/scripts/cpu-warning.sh
5. Use the free command to monitor the system memory:
1. Run the free command to view memory usage in linux: (MB)
12345 # free-mtotalusedfreesharedbufferscachedMem: 395234145380168484-/+ buffers/cache: 27601191 Swap: 8191868105
2. Run the following command to view the free value of the remaining memory:
(1) physical memory free value: # free-m | grep Mem | awk '{print $4 }'
(2) free value of the buffer: # free-m | grep-| awk '{print $4 }'
(3) free value of Swap partition: # free-m | grep Swap | awk '{print $4 }'
3. Compile a script file for memory monitoring:
# Vim/scripts/free-mem.sh
# Capture the free value of the Swap partition (write the file in append mode>) echoSwap-free: 'free-m | grepSwap | awk '{print $4}' M>/scripts/swap-free.txt # connect the above time and memory-related row data row by row (each time the file is written again) >) paste/scripts/date-time.txt/scripts/mem-free.txt/scripts/buffers-free.txt/scripts/swap-free.txt>/scripts/freem.txt
# Chmod a + x/scripts/free-mem.sh
4. view the memory Monitoring Result file:
# Cat/scripts/freemem.txt
5. Compile the mail sending script for the free result file:
# Vim/scripts/sendmail-mem.sh
1234567891011 #! /Bin/bash.txt sends the generated freemem.txt file to the user by mail # extract the IP address of the server IP address = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# extract current date time today = 'date-d" 0day "+ % Y % m month % dday' # Send memory Monitoring Result email echo" this is $ IP server $ today memory monitoring report, download the attachment. "| Mutt-s" $ IP server $ today memory Monitoring Report "-a/scripts/freemem.txtfuquanjun@xxx.com
# Chmod a + x/scripts/sendmail-mem.sh
6. Monitor the swap status of the system swap partition, and send an alarm email when more than 80% of the usage is used:
# Vim/scripts/swap-warning.sh
12345678910111213141516171819202122232425262728293031 #! /Bin/bash # extract the IP address of the server IP address = 'ifconfigeth0 | grep "inetaddr" | cut-f2-d ": "| cut-f1-d" "'# Total number of swap partitions allocated by the system swap_total = 'free-m | grepSwap | awk' {print $2}'' # remaining swap partitions free size swap_free = 'free-m | grepSwap | awk '{print $4} ''# used swap partition used size swap_used = 'free-m | grepSwap | awk' {print $ 3} ''if ($ swap_used! = 0); then # If the swap partition has been used, calculate the percentage of the free total amount of the remaining swap partition, expressed in decimal places, add an integer before the decimal point: 0swap_per = 0 'echo "scale = 2; $ swap_free/$ swap_total "| bc '# sets the swap partition's alarm value to 20% (that is, when the value exceeds 80% ). Swap_warn = 0.20 # Compare the percentage of the remaining swap partitions with the alarm value (if the value is greater than the alarm value (that is, the remaining value is more than 20%), 1 is returned, less than (that is, less than 20% remaining) will return 0) swap_now = 'expr $ swap_per \> $ swap_warn' # If the current swap partition is used more than 80% (that is, the remaining is less than 20%, and the above return value is equal to 0 ), send an email immediately if ($ swap_now = 0); thenecho "$ IP server swap partition only $ swap_freeM not used, remaining less than 20%, usage has exceeded 80%, please handle it in time. "| Mutt-s" $ IP server memory alert "fuquanjun@xxx.comfifi
# Chmod a + x/scripts/swap-warning.sh
7. Add to the 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 the load and CPU detection result email at every day.
# Crontab-e
1234567891011 */10 */scripts/load-check.sh>/dev/null2> & 1 */10 */scripts/load-warning.sh08 */scripts/sendmail-load.sh */ 10 */scripts/cpu-check.sh */10 */scripts/cpu-warning.sh08 */scripts/sendmail-cpu.sh */10 */scripts/free-mem.sh */ 10 */scripts/swap-warning.sh08 */scripts/sendmail-mem.sh
# Service crond restart
This article from the "from the Heart" blog, please be sure to keep this source http://fuquanjun.blog.51cto.com/5820068/1418508