In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the Linux how to use shell script to monitor system memory related knowledge, the content is detailed and easy to understand, the operation is simple and fast, with a certain reference value, I believe you will have something to gain after reading this Linux article on how to use shell script to monitor system memory, let's take a look.
First, install a mail client Msmtp software under Linux (similar to a Foxmail tool)
1. Download and install http://downloads.sourceforge.net. 206451&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 msmtp configuration file and log file (host is the email domain name, email user name test, password 123456)
# vim ~ / .msmtprcaccount defaulthost 126.comfrom test@126.comauth loginuser testpassword 123456logfile ~ / .msmtp.log # chmod ~ / .msmtprc # touch ~ / .msmtprc
3. Mutt installation configuration: (generally, mutt is installed by default under linux)
# vim ~ / .muttrcset sendmail= "/ usr/local/msmtp/bin/msmtp" set use_from=yesset realname= "memory" set from=test@126.comset envelope_from=yesset rfc2047_parameters=yesset charset= "utf-8"
4. Email delivery test (- s email title,-a table with attachment)
# echo "email content 123456" | mutt-s "email header Test email"-a / scripts/test.txt test@126.com II. Use the Free command to monitor system memory
1. Use the free command to view the memory usage of the linux system: (in M)
# free-m (the execution results are as follows) total used free shared buffers cachedMem: 3952 3414 538 0 168 484 buffers/cache: 2760 1191Swap: 8191 86 8105
2. Check the numerical command to intercept the remaining memory free:
(1) Free value of physical memory: # free-m | grep Mem | awk'{print $4}'(2) Free value of buffer: # free-m | grep-| awk'{print $4}'(3) Free value of Swap partition: # free-m | grep Swap | awk'{print $4}'
3. Write a script file for memory monitoring:
# vim / scriptsAfter freemem.shemeandbinappash # use the free command to monitor the memory changes of the linux system # fetch the current time of the system (write to the file by append > >) date > > / scripts/date-time.txt# grab the free value of the physical memory (write to the file by append > >) echo Mem-free: `free-m | grep Mem | awk'{print $4}'`M > > / scripts/mem-free.txt# grab buffer Free value (write file by append >) echo buffers/cache-free: `free-m | grep-| awk'{print $4}'`M > > / scripts/buffers-free.txt# crawls the free value of Swap partition (write file by append > >) echo Swap-free: `free-m | grep Swap | awk'{print $4}'`M > > / scripts/swap-free.txt# connects the above time and memory-related line data line by line (each rewrite Import file >) paste / scripts/date-time.txt / scripts/mem-free.txt / scripts/buffers-free.txt / scripts/swap-free.txt > / scripts/freemem.txt# chmod aquix / scripts/free-mem.sh# / scripts/free-mem.sh
4. View the result file of memory monitoring:
# cat / scripts/freemem.txt
5. Write the free result file email sending script:
# vim / scriptsAccording to the freemem.txt file generated by email to the user # extract the IP address information of the server IP= `ifconfig eth0 | grep "inet addr" | cut-f 2-d ":" | cut-f 1-d "`# extract the current date time today= `date-d" 0 date +% Y% m / d%` # send the memory monitoring result 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 test@126.com# chmod aquix / scripts/sendmail-mem.sh 3. Monitor the system exchange partition Swap. Send an alarm email # vim / scripts to send a warning email # vim / scripts to extract the IP address information of this server: awk `ifconfig eth0 | grep "inet addr" | cut-f 2-d ":" | cut-f 1-d "" `# total swap_total= `free-m assigned by the system | grep Swap | awk'{swap $2}'` # the current remaining free size swap_free= `free-m | | grep Swap | awk'{print $4}'`# used size currently used 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 free of the current remaining swap partition, expressed as a decimal, and add an integer digit 0swapping per0 = 0`echo "scale=2;$swap_free/$swap_total" before the decimal point | bc` # set the alarm value of the swap partition to 20% (that is, alarm when more than 80% is used). Compare the current percentage of swap_warn=0.20# remaining swap with the alarm value (1 is returned when the alarm value is greater than 20%, and 0 is returned if less than 20% is left)) swap_now= `expr $swap_per > $swap_ warn` # if the current swap uses more than 80% (that is, the remaining 20% is less than 20%, the above return value is equal to 0), email alarm if (($swap_now= = 0) immediately) Thenecho "$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" test@126.comfifi# chmod aquix / scripts/swap- warning.sh 4. Join the task schedule: memory is checked every 10 minutes, and an alarm is sent immediately (once every 10 minutes). The result of memory test is emailed every morning at 8: 00 a. M. # crontab-estrangement 10 * / scripts/free-mem.sh*/10 * / scripts/swap-warning.sh0 8 * / scripts/sendmail-mem.sh# service crond restart this article on "how to use shell script to monitor system memory in Linux" ends here, thank you for reading! I believe that everyone has a certain understanding of "how to use shell script monitoring system memory in Linux". If you want to learn more, you are welcome to follow the industry information channel.
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.