Linux server memory monitoring - check every hour & over send mail & restart the most expensive Java program

Keywords: Linux crontab SSL Java less

Introduction and advantages

Using this script, you can judge whether the system memory usage exceeds the set percentage
It can restart the program when the warning value is exceeded
It can record the restart process and send the specific LOG mail to the designated addressee
Crontab schedule can be set to run every other period of time

preparation

Turn on SMTP function of mailbox

This step bloggers will not screenshot do stupid tutorial, but every step will not be missed, do not understand the small partners can baidu find a map tutorial. Take QQ mailbox as an example, enter settings - > General - > POP3 / imap / SMTP... IMAP / SMTP service, click open, and an authorization code will be generated, which will be saved for future use

Modify the mail.rc configuration file

Enter the following command to edit the mail.rc file

vim /etc/mail.rc

Add the following configuration at the end of the file

set from=heicaijun@qq.com              #Set the sender's email here
set smtp=smtp.qq.com                   #Set the SMTP server of QQ here. For other mailboxes, please refer to Baidu
set smtp-auth-user=heicaijun@qq.com    #Set user name here
set smtp-auth-password=sahflkhsaffshaf #Here is the authorization code you saved in the previous step
set smtp-auth=login                    #This is set to login
#Here are the SSL encryption related configurations
#Set SMTP use starttls doesn't need to be configured here. It's not explained in many places. If it is configured, it will fail to verify, so I'll comment it out;
set ssl-verify=ignore
set nss-config-dir=/root/.certs

Test whether the message was sent successfully

Use the following command to test whether the email is sent successfully. Fill in your own email. If there is an error, please directly deal with the error part of Baidu.

echo "Test message body"|mail -s "Test message header section" heicaijun@qq.com

MemMonitor.sh

Download address The following is the script file. Please modify it according to your actual situation.

#!/bin/bash  
# Powered By heicaijun
# 2020/04/27
######################################################
# The following is the configuration part
##############
# The set alarm value is 50% (i.e. alarm when more than 50% is used).   
mem_warn=0.50
# Log record location, record on one log every day
DATETIME=$(date -d "today" +"%Y-%m-%d")
log_dir="/home/p4fdc/PeakPerformance/FWlog/MemHistory_${DATETIME}.log"
mail_temp=/home/p4fdc/PeakPerformance/MemMonitor.log
mail_addr=heicaijun@qq.com
#####################################################

# This function is the function that starts the java program
function startFW(){
	startResult=`/home/p4fdc/PeakPerformance/start${fwid}.sh`
	#Record release memory OK! In the log
	DATA4="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Restart program successful, detailed LOG as follows:\n[stop The operation result is:] ${stopResult};\n[start The operation result is:] ${startResult}"
	echo -e "${DATA4}" >> ${log_dir}
	echo "--->The first ${restartTime}Free memory completed ! " >> ${log_dir}
}
# This function is a function of sending mail
function sendMail(){
	# Record the content to be sent in the cache Log of mailTemp
	echo -e "${DATA1}\n${DATA2}\n${DATA4}" >> ${mail_temp}
	# Output the content to be sent to the body, and send the previously cached Log to the receiver as an attachment, - a followed by the attachment part
	echo -e "${DATA1}\n${DATA2}\n${DATA3}\n\n Above, please pay attention!" | mail -s "${mailTitle}" -a ${mail_temp} ${mail_addr}
	# After sending
	rm -f ${mail_temp}
}

echo "============================" >> ${log_dir}
echo "Start MemMonitor.sh..." >> ${log_dir}

#Total area allocated by the system   
mem_total=`free -m | awk 'NR==2' | awk '{print $2}'`   
  
#Current remaining size   
mem_free=`free -m | awk 'NR==2' | awk '{print $4}'`  
   
#Used size currently used   
mem_used=`free -m | awk 'NR==2' | awk '{print  $3}'`   


#Then calculate the percentage of the total amount of the current remaining used, expressed as decimal, and fill an integer 0 before the decimal point   
mem_per=0`echo "scale=2;$mem_used/$mem_total" | bc`
DATA1="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Percentage of memory used:${mem_per#*.}%, used: ${mem_used}MB, total memory: ${mem_total}MB“  
echo -e $DATA1 >> ${log_dir}  

#Compare the current occupation percentage with the alarm value (when it is greater than the alarm value, it will return 1; when it is less than, it will return 0)   
mem_now=`expr $mem_per \> $mem_warn`   

echo "Whether it is greater than the alarm value[1 Greater than, 0 less than]:${mem_now}" >> ${log_dir}

#If the current usage exceeds 50% (the return value above is equal to 0), free memory  
if (($mem_now == 1)); then  
        # Set the title of the message to the Alarm level
	mailTitle="[ALARM]T4 AP1 Out Of Memory"
	echo -e "Over the set alarm value ${mem_warn#*.}%..." >> ${log_dir}
	#Gets the DFID of the program that currently uses the most memory
	fwid=`ps -eo pid,args --sort=-pmem  |head -n 2|awk 'NR==2' | awk '{print $4}'`
	fwid=${fwid#*_}
	fwmem=`ps -eo pmem --sort=-pmem  |head -n 2|awk 'NR==2'`
	echo -e "${fwid}Up to ${fwmem}%" >> ${log_dir}

	DATA2="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Over the set alarm value ${mem_warn#*.}%, where ${fwid} takes up ${fwmem}% of memory and performs the restart ${fwid} operation“
	#Running the restart program
	echo "Start reboot ${fwid}operation..." >> ${log_dir}
	stopResult=`/home/p4fdc/PeakPerformance/stop${fwid}.sh -f`
	if [[ $stopResult =~ "Successfully" ]]; then
		restartTime="One"
		startFW
		DATA3="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Restart program successful, detailed LOG Please see the attachment"
		sendMail
	else
		# The following judgment is made twice more. In fact, one time is enough, because with kill-9, there is no problem that the program can't be killed.
		stopResult=`/home/p4fdc/PeakPerformance/stop${fwid}.sh -f`
		if [[ $stopResult =~ "Successfully" ]]; then
			restartTime="Two"
			startFW
			DATA3="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Restart program successful, detailed LOG Please see the attachment"
			sendMail
		else
			stopResult=`/home/p4fdc/PeakPerformance/stop${fwid}.sh -f`
			if [[ $stopResult =~ "Successfully" ]]; then
				restartTime="Three"
				startFW
				DATA3="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Restart program successful, detailed LOG Please see the attachment"
				sendMail
			else
				startResult=`/home/p4fdc/PeakPerformance/start${fwid}.sh`
				DATA4="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Restart program failed, detailed LOG as follows\n[stop The operation result is:] ${stopResult};\n[start The operation result is:] ${startResult}"
				echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!! " >> ${log_dir}
				echo -e "${DATA4}" >> ${log_dir}
				echo "--->Three attempts to free memory failed, please check manually ! " >> ${log_dir}
				echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!! " >> ${log_dir}
				mailTitle="[ERROR]T4 AP1 Out Of Memory"
				DATA3="[$(date -d "today" +"%Y-%m-%d-%H-%M-%S")] Three attempts to free memory failed, restart program failed, detailed LOG Please see the attachment"
				sendMail
			fi
		fi
	fi
fi

echo "============================" >> ${log_dir}

Set Crontab schedule

When setting crontab schedule, be sure to note that crontab does not load environment variables by default, so when running java programs, there will be a problem that environment variables cannot be found. So you need to call./etc/profile before starting the script; in addition, because the scheduling is run by default in the user directory, you may make mistakes when your script appears relative path, so it is recommended to add a cd / script directory / script.sh.
The following is the complete process

  1. Enter the following command to set the schedule
crontab -e
  1. Add the following schedule at the end of the file
# The following six segments (separated by spaces) represent the code to be run in the time-sharing day month year, * represents all time segments, 30 represents 30 minutes, and * / 1 represents the hours divisible by 1. So the complete meaning of the following schedule is to run MEMMonitor.sh every 30 minutes, 
30 */1 * * * ./etc/profile;cd /home/p4fdc/PeakPerformance/;/home/p4fdc/PeakPerformance/MemMonitor.sh > /dev/null

Posted by iamthebugman on Mon, 27 Apr 2020 02:17:59 -0700