Shell scripts monitor system load, CPU, and memory usage _linux shell

Source: Internet
Author: User
Tags bz2 chmod cpu usage server memory


In the absence of nagios monitoring software, as long as the server can be on the Internet, you can send mail to remind the Administrator system load and CPU usage.






First, install the following Linux mail client msmtp software (similar to a foxmail tool)






1. Download and install:
Http://downloads.sourceforge.net/msmtp/msmtp-1.4.16.tar.bz2?modtime=1217206451&big_mirror=0


 code as follows:

# 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 for the mail domain name, mail username fuquanjun, password Fuquanjun)




 code as follows:


# VIM/ROOT/.MSMTPRC


Account Default
Host Xxxxx.com
From fuquanjun@xxxx.com
Auth Login
User Fuquanjun
Password Fuquanjun
LogFile ~/.msmtp.log
# chmod 600/ROOT/.MSMTPRC
# Touch ~/.msmtp.log





3, Mutt Installation configuration: (General Linux has the default installation mutt)



If it is not installed, use the Yum installation





 code as follows:

Yum-y Install Mutt
# VIM/ROOT/.MUTTRC
Set sendmail= "/usr/local/msmtp/bin/msmtp"
Set Use_from=yes
Set Realname= "Moniter"
Set from=fuquanjun@xxx.com
Set Envelope_from=yes
Set Rfc2047_parameters=yes
Set charset= "Utf-8





4, the mail sends the test (S-mail title, a table plus annex)


 code as follows:

# echo "message content 123456" | Mutt-s "Message header test message"-a/scripts/test.txt fuquanjun@xxxx.com

The following error message appears:
 code as follows:

Msmtp:account Default not found:no configuration file available





Error sending letter, child process has exited 78 ().
This letter cannot be sent.
Workaround:



Use MSMTP to send tests alone:/usr/local/msmtp/bin/msmtp-s found that the config file was not found


 code as follows:

Msmtp:account Default not found:no configuration file available

To view the current profile path:/usr/local/msmtp/bin/msmtp-p
 code as follows:

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

So copy the/USR/LOCAL/ETC/MSMTPRC as/ROOT/.MSMTPRC
Check the Mutt file installation directory
 code as follows:

RPM-QL Mutt

So copy/etc/muttrc as/root/.muttrc to send mail.








Second, monitor the server system load situation:






1, with the uptime command to view the current load situation (1 minutes, 5 minutes, 15 minutes average load situation) on the Apple Mac computer also apply





 code as follows:

# uptime
15:43:59 up 186 days, 20:04, 1 user, load average:0.01, 0.02, 0.00

"Load average" means the average load of the system in 1 minutes, 5 minutes, and 15 minutes respectively.
(1) The main observation "15 minutes system load", it as the normal operation of the computer indicators.
(2) If the average load is greater than 1.0 in 15 minutes (after dividing the system load by the number of CPU cores), it indicates that the problem persists, not a temporary phenomenon.
(3) When the system load lasts more than 0.7, you have to start investigating what the problem is and preventing the situation from deteriorating.
(4) When the system load lasts more than 1.0, you must find a solution and drop the value.
(5) When the system load reaches 5.0, it indicates that your system has a very serious problem, long time no response, or close to the crash.
Suppose your computer has only 1 CPUs. If your computer has 2 CPUs, it means that the processing power of the computer is doubled, and the number of processes that can be processed is doubled.
2 CPUs indicate that the system load can reach 2.0, at which point each CPU reaches 100% of the workload. Spread out, n CPU computers, the most acceptable system load for n.0.





2, view the total number of servers CPU audit


 code as follows:

# grep-c ' model name '/proc/cpuinfo or Cat/proc/cpuinfo

3, intercepting the server 1 minutes, 5 minutes, 15 minutes of load
 code as follows:

# Uptime | awk ' {print $8,$9,$10,$11,$12} '
Load average:0.01, 0.02, 0.00





4. View the average load of 15 minutes intercepted





 code as follows:

# Uptime | awk ' {print $} ' is not as accurate as it gets, and if you take the 12th field in awk, the result may be empty. and use the $NF table to output the last paragraph of the content)
# Uptime | awk ' {print $NF} '

5, write the system load monitoring script files:
 code as follows:

# vim/scripts/load-check.sh
[Code]
#!/bin/bash
#Using uptime command to monitor Linux system load change
#Write file as append > >
Date >>/scripts/datetime-load.txt
#Extract the server for 1 minute, 5 minutes, 15 minutes of load
Uptime | awk ' {print $8,$9,$10,$11,$12} ' >>/scripts/load.txt
#Each time the file is rewritten >)
Paste/scripts/datetime-load.txt/scripts/load.txt >/scripts/load_day.txt
# chmod a+x/scripts/load-check.sh
6, write the system load result file mail Send script:
code as follows:
# vim/scripts/sendmail-load.sh
/bin/bash!
#Load. TXT file is sent to the user by mail
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
#Extract current date
today= ' date-d ' 0 day ' +%y year%m month%d '
#Send system load monitoring result email
echo "This is the system load monitoring report for the $IP server $today, please download the attachment." " | Mutt-s "$IP server $today System load Monitoring Report"-a/scripts/load_day.txt fuquanjun@xxx.com
# chmod a+x/scripts/sendmail-load.sh
7, write the system load monitoring script files:
code as follows:
# vim/scripts/load-warning.sh
/bin/bash!
#Using uptime command to monitor Linux system load change
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
#Grab the total number of CPU cores
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 $NF} '
#The result is less than 1.0 in front of the single digital complex 0
Average_load= ' echo ' scale=2;a= $load _15/$cpu _num;if (Length (a) ==scale (a)) print 0;print a "| BC '
#Take the bitwise integer of the above average load value
Average_int= ' echo $average _load | Cut-f 1-d "." `
#Set that is, when using more than 70% alarm for the average load of a single core of the system for 15 minutes
load_warn=0.70
#When the average load of a single core in 15 minutes is greater than or equal to 1.0 (that is, a single integer greater than 0), direct e-mail alerts, if less than 1.0 to do two comparisons
if (($average _int > 0)); Then
echo "$IP Server 15-minute system average load is $average_load, exceeding the alert value of 1.0, please immediately handle!!! " | Mutt-s "$IP Server system load critical alarm!!! "Fuquanjun@xxx.com
Else
#1 is returned when the alarm value is greater than 0.70, and 0 is returned when less than
load_now= ' expr $average _load \> $load _warn '
#If the average load value of a single core of the system in 15 minutes is greater than the alarm value of 0.70 (the return value is 1), send the message to the administrator
if (($load _now = 1)); Then
echo "$IP server 15 minute system average load reaches $average _load, exceeding the alert value of 0.70, please handle it in time. " | Mutt-s "$IP Server system load Alarm" fuquanjun@xxx.com
Fi
Fi
# chmod a+x/scripts/load-warning.sh
Third, monitor the server system CPU consumption:
1, use the top command to view the Linux system CPU usage:
code as follows:
# top-b-N 1 | grep Cpu (-b-n 1 tables only need 1 output)
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, view the percentage of the Intercept Idle CPU value command (take only the integer part):
code as follows:
# top-b-N 1 | grep Cpu | awk ' {print $} ' | Cut-f 1-d "."
3, write the CPU monitoring script files:
code as follows:
# vim/scripts/cpu-check.sh
/bin/bash!
#Using top command to monitor the change of Linux CPU
#Write file as append > >
Date >>/scripts/datetime-cpu.txt
#Grab the current CPU value (write file as append > >)
Top-b-N 1 | grep Cpu >>/scripts/cpu-now.txt
#Connect the above time and CPU related row data line by line (each time the file is rewritten >)
Paste/scripts/datetime-cpu.txt/scripts/cpu-now.txt >/scripts/cpu.txt
# chmod a+x/scripts/cpu-check.sh
4, view the CPU monitoring results file:
code as follows:
# Cat/scripts/cpu.txt
5, write the CPU results file mail Send script:
code as follows:
# vim/scripts/sendmail-cpu.sh
/bin/bash!
#The generated CPU. TXT file is sent to the user by mail
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
#Extract current date
today= ' date-d ' 0 day ' +%y year%m month%d '
#Send CPU monitoring result email
echo "This is the CPU monitoring report for the $IP server $today, please download the attachment." " | Mutt-s "$IP Server $today CPU Monitoring Report"-a/scripts/cpu.txt fuquanjun@xxx.com
# chmod a+x/scripts/sendmail-cpu.sh
Four, monitor the system CPU situation, when using more than 80% time to send the alarm mail:
code as follows:
# vim/scripts/cpu-warning.sh
/bin/bash!
#Script program of monitoring system CPU
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep











, if the current CPU uses more than 80% (that is, the remaining less than 20%), immediately send an email alert
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" fuquanjun@xxx.com
Fi
# chmod a+x/scripts/cpu-warning.sh








Monitor system memory using free command:






1, use the free command to view the Linux system memory usage: (in m)


 code as follows:

# free-m
Total used free shared buffers Cached
mem:3952 3414 538 0 168 484
-/+ buffers/cache:2760 1191
SWAP:8191 86 8105





2, view the Intercept the remaining memory Free value command:



(1) Free value of physical memory: # Free-m | grep Mem | awk ' {print $} '
(2) Free value of the buffer: # Free-m | grep-| awk ' {print $} '
(3) Swap partition Free value: # Free-m | grep Swap | awk ' {print $} '



3, write the Memory monitoring script file:


 code as follows:

# vim/scripts/free-mem.sh
#!/bin/bash
#Using free command to monitor Linux system memory changes
#Write file as append > >
Date >>/scripts/date-time.txt
#Grab the free value of physical memory (write file as append > >
echo mem-free: ' Free-m | grep Mem | awk ' {print $} ' M >>/scripts/mem-free.txt
#Grab the free value of the buffer (write file as append > >
echo buffers/cache-free: ' Free-m | grep-| awk ' {print $} ' M >>/scripts/buffers-free.txt
#Grab the free value of swap partition (write file as append > >
echo swap-free: ' Free-m | grep Swap | awk ' {print $} ' M >>/scripts/swap-free.txt
#Each time the file is rewritten >)
Paste/scripts/date-time.txt/scripts/mem-free.txt/scripts/buffers-free.txt/scripts/swap-free.txt >/scripts/fr Eemem.txt
# chmod a+x/scripts/free-mem.sh
4, view the memory monitoring results file:
code as follows:
# Cat/scripts/freemem.txt
5. Write free result file mail Send script:
code as follows:
# vim/scripts/sendmail-mem.sh
/bin/bash!
#Put the generated Freemem. TXT file is sent to the user by mail
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
#Extract current date time
today= ' date-d ' 0 day ' +%y year%m month%d '
#Send memory monitoring result mail
echo "This is the memory monitoring report for the $IP server $today, please download the attachment." " | Mutt-s "$IP Server $today Memory Monitoring Report"-a/scripts/freemem.txt fuquanjun@xxx.com
# chmod a+x/scripts/sendmail-mem.sh
Six, monitoring system swap partition swap, when using more than 80% of the time to send a warning message:
code as follows:
# vim/scripts/swap-warning.sh
/bin/bash!
#Extract the IP address information of this server
ip= ' Ifconfig eth0 | grep "inet addr" | Cut-f 2-d ":" | Cut-f 1-d "" '
#Total number of swap partitions allocated by the system
Swap_total= ' Free-m | grep Swap | awk ' {print $} '
#Current remaining free size of swap partition
Swap_free= ' Free-m | grep Swap | awk ' {print $} '
#Used size of the currently used swap partition
Swap_used= ' Free-m | grep Swap | awk ' {print $} '
if (($swap _used!= 0)); Then
#If the swap partition has been used, calculate the percentage of the total amount of free for the current retaining swap partition, in decimal notation, to make an integer digit 0 before the decimal point
Swap_per=0 ' echo ' scale=2 $swap _free/$swap _total "| BC '
#Set the alarm value of the switch partition to 20% (that is, when using more than 80% alarm)
swap_warn=0.20
#The current percentage of remaining switching partitions is compared with the alarm value (1 is returned when it is greater than the alarm value (i.e., more than 20%), and 0 is returned when less than (i.e., less than 20% remaining)
swap_now= ' expr $swap _per \> $swap _warn '
#If the current swap partition uses more than 80%




























(that is, the remaining is less than 20%, the return value above equals 0), send an email alert immediately
if (($swap _now = 0)); Then
echo "$IP server swap partition is left $swap _free M unused, remaining less than 20%, the utilization rate has exceeded 80%, please deal with it in time. " | Mutt-s "$IP server memory Alert" fuquanjun@xxx.com
Fi
Fi
# chmod a+x/scripts/swap-warning.sh








Vii. Join the Mission plan: system load and CPU occupancy rate every 10 minutes, the alarm will be sent immediately mail (10 minutes), load and CPU detection results mail every morning 8.





 code as follows:
# CRONTAB-E
*/10 * * * */scripts/load-check.sh >/dev/null 2>&1
*/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

*/10 * * * */scripts/free-mem.sh
*/10 * * * */scripts/swap-warning.sh
0 8 * * */scripts/sendmail-mem.sh
# Service Crond Restart




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.