Shell programming checks and monitors the CPU usage of MySQL

Source: Internet
Author: User

Shell programming is very powerful!

When the site traffic is large, MySQL is under a high pressure. When the CPU usage of MySQL exceeds 300%, it cannot provide services, and it is almost stuck. At this time, the best way is to restart the MySQL service. This is unpredictable. We don't know when MySQL's usage reaches 300%.ProgramRegular judgment is more reliable.

I learned shell programming and wrote the following script:

#! /Bin/Bashcpu='PSAux |Grep 'Mysqld $'|Cut-D" "-F6 |Cut-D .-F1'If[$ CPU-GT300]ThenService MySQL restart&&Date>/Tmp/MySQL. LogFi

 

A little explanation. First, run the "PS aux" command to obtain the status information of all system processes, including CPU and memory, such:

Then, the information is transmitted to grep through the pipeline. $ indicates the end of the regular expression. The process ending with "mysqld" is actually MySQL, here, the information of the MySQL process is returned, one line, for example:

The next cut is the truncation string. We need to calculate the CPU usage. Of course, we need to take the MySQL CPU value. The cut command is separated by tab by default, however, the blank space in the string displayed by PS aux is a space rather than a tab. We need to use spaces to separate them. Some strings contain multiple spaces, here, the-F8 parameter intercepts 8th strings (some systems may have 6th strings). This string is the CPU usage of MySQL!

Maybe you have doubts, since you have obtained the CPU usage of MySQL, why is there a cut? Good question! I didn't think of this problem at the beginning. Because we need to compare the CPU usage, the string we obtain here is a floating point number (with a decimal point), but the shell programming does not support comparing floating point numbers. What should I do? Then compare the integer. Separate the floating point number with ".". The first part is the integer part, so that we can obtain the integer part of MySQL usage.

The following is a judgment. If it is greater than 300, the MySQL service will be restarted. There is another command, which is a simple "log". The purpose is to record the restart time of MySQL once the service is restarted, so that you can analyze the website pressure in the future.

The next step is to regularly execute this program. You can execute the program once every 5 or 10 minutes as needed. to edit the/etc/crontab file, add the following statement:

 
*/5 * root/home/ma6174/mysql_restart.sh
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.