Shell script detection monitors MySQL CPU usage

Source: Internet
Author: User
Tags cpu usage

When the Web site visits a large amount of time MySQL pressure is relatively large, when the CPU utilization of MySQL more than 300% can not provide services, near the state of death, the best way is to restart the MySQL service. Because this kind of thing is unpredictable, we do not know when MySQL occupancy rate reaches 300%, or write a program regularly judge more reliable.

Learned shell programming and wrote the following script:

#!/bin/bashcpu= ' ps aux | grep ' mysqld$ ' | Cut-d ""-f6 | cut-d.-f1 ' If [$cpu-gt]then    service mysql restart && date >>/tmp/mysql.logfi

Explain it a little bit. First, perform the "PS aux" command to obtain status information for all system processes, including CPU, memory, etc., such as:

Then through the pipeline to send information to grep,$ is the meaning of the end of the regular expression, from all the process to find the "mysqld" end of the process, in fact, is MySQL, here is the MySQL process information, a row, such as:

The next cut is truncated string, we want to count the CPU occupancy rate, of course, to intercept the MySQL CPU value, Cut command By default is tab-delimited, but PS aux display the string blank is a space instead of tab, we will use a space to split, Some of the string is a number of spaces, here the-f8 parameter is to intercept the 8th string (some system may be 6th), this string is the CPU utilization of MySQL!

Maybe you have a question, now that you've got the CPU usage of MySQL, why is there a cut in the back? That's a good question! I didn't think of it at the beginning. Because we're comparing CPU occupancy, the string we get here is a floating-point number (with a decimal point), but the size of the floating-point number is not supported in shell programming. What to do? Then compare the integers to the "." Dividing this floating point number, the first one is the integer part, so we get the integer part of MySQL occupancy rate.

The following is a judgment, if more than 300, then restart the MySQL service. There is also a command, which is a simple "log" that is designed to record the time it takes to restart MySQL once the service is restarted, allowing you to analyze the stress of the site later.

The next step is to execute the program regularly, depending on the situation can be 5 minutes or 10 minutes to execute once, just edit the/etc/crontab file, add the following statement:

? */5 *   * * *   root    /home/ma6174/mysql_restart.sh

Shell script detection monitors MySQL CPU usage

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.