“Reader welfare! 2 TB of all kinds of technical resources for free”
The application scenario of the program is as follows: The program is used to monitor program logs of the Linux server. If the keyword is abnormal, corresponding actions or alarms are triggered to notify email contacts.
First, install mail service
1. Unpack
Tar -jxf mailx-12.4.tar.bz2 -c /usr/src/&& CD /usr/src/mailx-12.4/
Copy the code
2, compile,
make install UCBINSTALL=/usr/bin/install
Copy the code
Abnormal compilation error -1
Exception error resolution
Download the mailx patch file mailx-12.4-openSSL_1.0.0_build_fix-1. Patch and save the patch file and the mailx decompressed directory in the same path
Run the patch installation command:
Patch - p0 < mailx - 12.4 - openssl_1. 0.0 _build_fix - 1. The patch
Copy the code
Go to mailx-12.4 and run the following command again:
make install UCBINSTALL=/usr/bin/install
Copy the code
Check whether the Mailx is installed successfully: mailx -v
3, configuration,
Vi /etc/nail.rc, added at the end of the file
set from=
set smtp=
set smtp-auth-user=
set smtp-auth-password=
set smtp-auth=login
As shown in the following figure, save the configuration after modification
4. Test sending emails
Echo ‘E-mail’ | mailx – v – “email subject” s email address
So far, the email function test is normal. The next step is to write the keyword monitoring script. You can use the log keyword script to trigger the email alarm script to monitor logs
Second, keyword monitoring script development
Checklogs. sh Log monitor
Sendmail. sh Mail sending script, which can be customized
Conf Configuration file directory. Chklist is the configuration file
The first execution of the checklogs.sh script reads the log file and records the number of lines currently read
The script reads the update log. For example, if nine lines of data are updated in the current log, the script obtains the keyword from the latest nine lines
Test insert keyword error
If you continue executing the script, you can see that the keyword has been captured and an alarm is triggered
Test for multiple keywords
Execute again and you can see that the latest updated log information has been captured
Test frequency control
At this point the relevant functional tests have been completed.
Almost forgot to tell you the complete script code, just in the [migrant elder sister technology road] public account dialog box reply keyword “log monitoring” can get the complete code.
Original: https://www.toutiao.com/a6770513931053040131/
The latest 2TB technology dry goods: including system operation and maintenance, database, Redis, MogoDB, e-books, Java foundation course, Java practice project, architect comprehensive tutorial, architect practice project, big data, Docker container, ELK Stack, machine learning, BAT interview intensive video, etc. In the wechat public account dialog box reply keyword: 1024 you can get all the information.
Being is the END being
Excellent article recommendation:
The 37-year-old farmer’s experience of looking for a job made him cry.
The star + 5.2 k! Open source, powerful WAF VeryNginx!
Cow force! This programmer has created a “licking dog” must-have artifact (open source)!
Code cloud suffered DDoS attacks, the official issued solutions and reported
Mind mapping for Linux
Essential skills! Shell programming practice case sharing (PDF version)!
Click [read article] to find out more
Click a look, forward to support it ↓↓ ↓