Recently, their business to go online, after the launch, need to consider the various services to monitor, including (httpd services, mysqld services, etc.), now want to take mysqld services as an example to summarize the way the script is the most professional and reasonable:
(1). Monitor MySQL's service according to MySQL's port number. (Common for local services that monitor MySQL)
One of the more common scripts:
1 netstat-natup| grep mysqld| awk -F '[:]+'{print $}'
Above is the MYSQLD port number is filtered, the disadvantage of this method is: There may be a port, but the service may not be normal. For example: The system load is very high, the CPU load is also very high, the connection is full, this is one of its problems.
(2). Monitor MySQL's services according to the MySQL process:
One of the more common scripts:
1 PS -aux | grep mysqld | grep grep| WC -L
The disadvantage of this approach is the same as above: there may be ports on, but the service may not be normal. For example: The system load is very high, the CPU load is also very high, the connection is full, this is one of its problems.
(3). Use the MySQL client's commands and connect to MySQL with the account, and then determine whether MySQL is normal (local or remote connection state) based on the status of the command returned or what is returned.
A common judgment script (just a few of the core content):
1 mysql-uroot-p1234-e'Select version (); '>&/dev/null2echo $?
If the $0 above, the description is able to connect normally, if it is 1, then the connection failed.
The disadvantage of this approach is that you need to have MySQL connection client, to have the data account and password, and to connect the database host authorization.
(4). Monitor MySQL's service status by Php/java the way you connect to MySQL directly.
A more common script is also relatively simple:
1 <? PHP 2 $conn mysql_connect die (' MySQL coulc not connect '. Mysql_error ()); 3 4 ?>
This approach is closest to the user's access, the effect is the best, because this way, even if the port exists, but as long as the server CPU load is high, the user's access is certainly not normal.
So the best way to alarm is not whether the service alarm is turned on, but whether the site's user access is still normal. This is the best alarm principle and method. We should consider the problem from the user's point of view, not to say from the angle of operation and maintenance. So you should use this method at work.
A better record of how MySQL monitoring scripts are written