Linux configuration email alarm

Record an operation example of configuring email alarms.

Install mutt mail client

First, install the mutt mail client by typing the following command in the terminal:

sudo yum install mutt

Configure email sender

In the mutt mail client, you need to configure the sender's information first. Enter the following command in a terminal to create a  ~/.muttrc configuration file called config and add the following to it:

set realname="Your Name"
set from="[email protected]"
set use_from=yes
set envelope_from="[email protected]"

Be careful to  replace Your Name and  [email protected] with your name and email address to send the email to, respectively.

Configure email recipients

Next, you can configure the email addresses you want to receive emails from. Enter the following command in the terminal to open the mutt mail client:

mutt

Then enter the following command to create a new email:

m

In the new email, enter the email address to receive the email and enter the subject, content and other information of the email. When finished editing, press  CTRL + X and select  yto save the message to the cache folder.

Configure SMTP server

Finally, you need to configure the SMTP server to use, that is, the SMTP server of the mailbox provider used to send mail through mutt. Here we take Gmail as an example, enter the following command, and  ~/.muttrc add the following content to the file:

set smtp_url="smtps://[email protected]:[email protected]:465/"
set ssl_starttls=no
set ssl_force_tls=yes

Note that  replace [email protected] and  your_password with your Gmail address and authorization code ( not your login password ), respectively.

The authorization code can be found on the official website of the mailbox you choose, and you can see it after you log in.

Send test email

Now, you can send a test email by typing the following command in the terminal:

echo "This is a test email!" | mutt -s "Test Email" -- [email protected]

After successfully sending the email, you should receive a test email from the mutt email client.

Next, you can write the code for automatic email alerts in the Shell script, and send emails through the command line tool of the mutt email client. For example:

#!/bin/bash

# 检查系统资源利用率,如果超过 90%,则发送邮件报警
load=$(uptime | awk -F 'load average: ' '{print $2}' | cut -d , -f 1)
limit=0.9

if [ $(echo "$load > $limit" | bc -l) -eq 1 ]; then
  echo "Server load is high" | mutt -s "Server Alert" -- [email protected]
fi

When the script is running, if the system load exceeds 90%, the mutt command will be automatically executed, and an email alarm with the title "Server Alert" and the content "Server load is high" will be sent to [email protected].

Guess you like

Origin blog.csdn.net/m0_72264240/article/details/130636979