In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "how to use shell script to monitor system load, CPU and memory usage". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use shell script to monitor system load, CPU and memory usage".
First, install a mail client msmtp software under linux (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
The code is 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 is the email domain name, email user name fuquanjun, password fuquanjun)
The code is 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: (generally, mutt is installed by default under linux)
If it is not installed, install using yum
The code is 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. Email delivery test (- s email title,-a table with attachment)
The code is as follows:
# echo "message content 123456" | mutt-s "message title Test message"-a / scripts/test.txt fuquanjun@xxxx.com
The following error message appears:
The code is as follows:
Msmtp: account default not found: no configuration file available
There was an error sending the letter and the child process has exited 78 ().
This message cannot be sent.
Solution:
Send the test using msmtp alone: / usr/local/msmtp/bin/msmtp-S found that the configuration file was not found
The code is as follows:
Msmtp: account default not found: no configuration file available
View the current profile path: / usr/local/msmtp/bin/msmtp-P
The code is 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 / usr/local/etc/msmtprc to / root/.msmtprc
Check the mutt file installation directory
The code is as follows:
Rpm-ql mutt
So you can send mail by copying / etc/Muttrc to / root/.muttrc.
Second, monitor the server system load:
1. Using the uptime command to check the current load (1 minute, 5 minutes, 15 minutes average load) is also applicable on Apple's Mac computer.
The code is 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 within 1 minute, 5 minutes, and 15 minutes, respectively.
The main results are as follows: (1) the "15-minute system load" is mainly observed and used as an indicator of the normal operation of the computer.
(2) if the average load (after dividing the system load by the number of CPU cores) is greater than 1.0 within 15 minutes, it indicates that the problem persists and is not temporary.
(3) when the system load continues to be greater than 0.7, you must start investigating what the problem is to prevent the situation from getting worse.
(4) when the system load continues to be greater than 1.0, you must start looking for a solution to bring this value down.
(5) when the system load reaches 5.0, it indicates that your system has a serious problem, has not responded for a long time, or is close to a crash.
Suppose your computer has only one CPU. If your computer has two CPU, it means that the computer's processing power has doubled, and the number of processes that can be processed at the same time has doubled.
2 CPU indicates that the system load can reach 2.0, and each CPU reaches 100% of the workload. To generalize, the maximum acceptable system load for n CPU computers is n. 0.
2. Check the total number of cores of the server cpu
The code is as follows:
# grep-c 'model name' / proc/cpuinfo or cat / proc/cpuinfo
3. Intercept the load of the server for 1, 5 and 15 minutes
The code is as follows:
# uptime | awk'{print $8, 9, 10, 11, 12}'
Load average: 0.01, 0.02, 0.00
4. Check the average load intercepted for 15 minutes
The code is as follows:
# uptime | awk'{print $12}'(the result is not accurate enough with'{print $12}'. If you all use awk to get the 12th field, the result may be empty. And use the $NF table to output the last paragraph)
# uptime | awk'{print $NF}'
5. Write the script file for system load monitoring:
The code is as follows:
# vim / scripts/load-check.sh
[code]
#! / bin/bash
# use uptime commands to monitor linux system load changes
# fetch the current time of the system (write to the file by append > >)
Date > > / scripts/datetime-load.txt
# extract 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 line by line (rewrite the file each time >)
Paste / scripts/datetime-load.txt / scripts/load.txt > / scripts/load_day.txt
# chmod axix / scripts/load-check.sh
6. Write the system load result file mail sending script:
The code is as follows:
# vim / scripts/sendmail-load.sh
#! / bin/bash
# send the load_day.txt file generated by system load monitoring to the user by email
# 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% m / m% d dd`
# send the email of system load monitoring result
Echo "this is the system load monitoring report of $IP server $today, please download the attachment." | | mutt-s "system load Monitoring report of $IP Server $today"-a / scripts/load_day.txt fuquanjun@xxx.com
# chmod axix / scripts/sendmail-load.sh
7. Write the script file for system load monitoring:
The code is as follows:
# vim / scripts/load-warning.sh
#! / bin/bash
# use uptime commands to monitor linux system load changes
# 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 cores of crawling cpu
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}'`
# 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 first digit is 0.
Average_load= `echo "scale=2;a=$load_15/$cpu_num;if (length (a) = = scale (a)) print 0X print a" | bc`
# take the integer of the average load value above
Average_int= `echo $average_load | cut-f 1-d "." `
# set the alarm value of the average load of a single core of the system for 15 minutes to 0.70 (that is, the alarm is used more than 70%).
Load_warn=0.70
# send an email alarm directly when the average load value of a single core in 15 minutes is greater than or equal to 1.0 (that is, the integer of each bit is greater than 0); if it is less than 1.0, make a second comparison
If ($average_int > 0); then
The average system load of echo "$IP server for 15 minutes is $average_load, which exceeds the warning value of 1.0.Please deal with it immediately!" | | mutt-s "$IP server system load serious alarm!!" Fuquanjun@xxx.com
Else
# the aPCge load value of the current system in 15 minutes is compared with the alarm value (1 is returned when it is greater than the alarm value of 0.70, and 0 is returned when it is less than)
Load_now= `expr $average_load\ > $load_ Warn`
# if the average 15-minute load of a single core of the system is greater than the alarm value of 0.70 (the return value is 1), email the administrator
If (($load_now = = 1); then
The average system load of echo "$IP server reaches $average_load in 15 minutes, exceeding the warning value of 0.70. please deal with it in time." | | mutt-s "$IP server system load alarm" fuquanjun@xxx.com |
Fi
Fi
# chmod axix / scripts/load-warning.sh
3. Monitor the cpu usage of the server system:
1. Use the top command to view the cpu usage of linux systems:
The code is as follows:
# top-b-n 1 | grep Cpu (- b-n 1 table only needs the output result once)
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. Check the command to intercept the percentage of idle cpu (only take the integer part):
The code is as follows:
# top-b-n 1 | grep Cpu | awk'{print $5}'| cut-f 1-d "."
3. Write the script file for cpu monitoring:
The code is as follows:
# vim / scripts/cpu-check.sh
#! / bin/bash
# use top command to monitor cpu changes in linux system
# fetch the current time of the system (write to the file by append > >)
Date > > / scripts/datetime-cpu.txt
# grab the value of the current cpu (write to the file by appending)
Top-b-n 1 | grep Cpu > > / scripts/cpu-now.txt
# connect the above time and cpu-related line data line by line (rewrite the file each time >)
Paste / scripts/datetime-cpu.txt / scripts/cpu-now.txt > / scripts/cpu.txt
# chmod axix / scripts/cpu-check.sh
4. View the result file of CPU monitoring:
The code is as follows:
# cat / scripts/cpu.txt
5. Write the cpu result file email sending script:
The code is as follows:
# vim / scripts/sendmail-cpu.sh
#! / bin/bash
# send the generated cpu.txt file to the user by email
# 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% m / m% d dd`
# send cpu monitoring result email
Echo "this is the cpu monitoring report of $IP server $today, please download the attachment." | | mutt-s "$IP server $today CPU monitoring report"-a / scripts/cpu.txt fuquanjun@xxx.com
# chmod axix / scripts/sendmail-cpu.sh
4. Monitor the situation of the system cpu and send an alarm email when it is used more than 80%:
The code is as follows:
# vim / scripts/cpu-warning.sh
#! / bin/bash
# script for monitoring system cpu
# extract the IP address information of this server
IP= `ifconfig eth0 | grep "inet addr" | cut-f 2-d ":" | cut-f 1-d "" `
# take the current free cpu percentage ratio (only take the integer part)
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 is more than 80% (that is, the rest is less than 20%), send an email alarm immediately
If ($cpu_idle
< 20)); then echo "$IP服务器cpu剩余$cpu_idle%,使用率已经超过80%,请及时处理。" | mutt -s "$IP 服务器CPU告警" fuquanjun@xxx.com fi # chmod a+x /scripts/cpu-warning.sh 五、使用free命令监控系统内存: 1、使用free命令查看linux系统内存使用情况:(以M为单位) 代码如下: # free -m total used free shared buffers cached Mem: 3952 3414 538 0 168 484 -/+ buffers/cache: 2760 1191 Swap: 8191 86 8105 2、查看截取剩余内存free的数值命令: (1) 物理内存free值: # free -m | grep Mem | awk '{print $4}' (2) 缓冲区的free值: # free -m | grep - | awk '{print $4}' (3) Swap分区free值: # free -m | grep Swap | awk '{print $4}' 3、编写内存监控的脚本文件: 代码如下: # vim /scripts/free-mem.sh #!/bin/bash #使用free命令监控linux系统内存变化 #取系统当前时间(以追加的方式写入文件>>)
Date > > / scripts/date-time.txt
# grab the free value of physical memory (write to the file by append > >)
Echo Mem-free: `free-m | grep Mem | awk'{print $4}'`M > > / scripts/mem-free.txt
# grab the free value of the buffer (write to the file by append > >)
Echo buffers/cache-free: `free-m | grep-| awk'{print $4}'`M > > / scripts/buffers-free.txt
# grab the free value of Swap partition (write to the file by append > >)
Echo Swap-free: `free-m | grep Swap | awk'{print $4}'`M > > / scripts/swap-free.txt
# connect the above time and memory-related rows of data line by line (rewrite the file each time >)
Paste / scripts/date-time.txt / scripts/mem-free.txt / scripts/buffers-free.txt / scripts/swap-free.txt > / scripts/freemem.txt
# chmod axix / scripts/free-mem.sh
4. View the result file of memory monitoring:
The code is as follows:
# cat / scripts/freemem.txt
5. Write the free result file email sending script:
The code is as follows:
# vim / scripts/sendmail-mem.sh
#! / bin/bash
# send the generated freemem.txt file to the user by email
# 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 and time
Today= `date-d "0 day" +% Y% m / m% d dd`
# send the result email of memory monitoring
Echo "this is the memory monitoring report of $IP server $today, please download the attachment." | | mutt-s "$IP server $today memory monitoring report"-a / scripts/freemem.txt fuquanjun@xxx.com
# chmod axix / scripts/sendmail-mem.sh
6. Monitor the exchange partition swap of the system, and send alarm email when it is used more than 80%:
The code is 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 assigned 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 is already in use, calculate the percentage of the total free of the current remaining swap partition, expressed as a decimal, with an integer digit 0 before the decimal point
Swap_per= 0`echo "scale=2;$swap_free/$swap_total" | bc`
# set the alarm value of swap partition to 20% (that is, alarm when using more than 80%).
Swap_warn=0.20
# the current percentage of remaining swap zones is compared with the alarm value (1 is returned when it is greater than the alarm value (that is, more than 20%), and 0 is returned when it is less than (that is, less than 20%).
Swap_now= `expr $swap_per\ > $swap_ Warn`
# if the current swap uses more than 80% (that is, the rest is less than 20%, and the above return value is equal to 0), send an email alarm immediately
If (($swap_now = = 0); then
Echo "$IP server swap exchange partition only $swap_free M is unused, the remaining less than 20%, and the utilization rate has exceeded 80%. Please deal with it in a timely manner." | | mutt-s "$IP server memory alarm" fuquanjun@xxx.com |
Fi
Fi
# chmod axix / scripts/swap-warning.sh
7. Join the task schedule: the system load and CPU occupancy rate are checked every 10 minutes, the alarm is sent immediately (once every 10 minutes), and the load and CPU test result email is sent at 8 o'clock every morning.
The code is 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
Thank you for reading, the above is the content of "how to use shell script to monitor system load, CPU and memory usage". After the study of this article, I believe you have a deeper understanding of how to use shell script to monitor system load, CPU and memory usage. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.