Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement shell script to monitor memory usage of linux system

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the knowledge of "how to implement shell script to monitor the memory usage of linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, install a mail client msmtp software under linux (similar to a foxmail tool)

1. Download and install:

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 test, password 123456)

The code is as follows:

# 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: (generally, mutt is installed by default under linux)

The code is as follows

# vim ~ / .muttrc

Set sendmail= "/ usr/local/msmtp/bin/msmtp"

Set use_from=yes

Set realname= "memory"

Set from=test@126.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.txttest@126.com

Second, use the free command to monitor system memory:

1. Use the free command to view the memory usage of the linux system: (in M)

The code is as follows:

# free-m

(the execution result is as follows)

The code is as follows:

Total used free sharedbuffers cached

Mem:3952 34145380168484

-/ + buffers/cache: 2760 1191

Swap: 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 the 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:

The code is as follows:

# vim / scripts/free-mem.sh

#! / bin/bash

# use free command to monitor memory changes in 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 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

# / 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 / 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 test@126.com

# chmod axix / scripts/sendmail-mem.sh

3. 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" test@126.com |

Fi

Fi

# chmod axix / scripts/swap-warning.sh

4. Join the task plan: memory is checked every 10 minutes, if there is an alarm, email is sent immediately (once every 10 minutes), and the email of memory test result is sent at 8 o'clock every morning.

The code is as follows:

# crontab-e

* / 10 * / scripts/free-mem.sh

* / 10 * / scripts/swap-warning.sh

0 8 * / scripts/sendmail-mem.sh

# service crond restart

This is the end of the content of "how to implement shell scripts to monitor the memory usage of linux systems". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report