Write your own Nagios monitoring MySQL plug-in

Source: Internet
Author: User
Write the master-slave monitoring plug-in :#! BinbashMYSQLusrlocalmysqlbinmysql-uslave-pslave12345-h127.0.0.1-PVAR_LOGtmpcheck_m

Write the master-slave monitoring plug-in :#! /Bin/bash MYSQL =/usr/local/mysql/bin/mysql-uslave-pslave12345-h127.0.0.1-PVAR_LOG =/tmp/check_m

Write the master-slave monitoring plug-in:
#! /Bin/bash
MYSQL = "/usr/local/mysql/bin/mysql-uslave-pslave12345-h127.0.0.1-P"
VAR_LOG =/tmp/check_mysql_status.log. $2 &> $ VAR_LOG
Function usage (){
Cat < 'Basename $ 0' need at least value: 'basename $ 0'-[s | m] port
The first parameter is-s, so it is the check slave mode.-m indicates that the check master mode cannot be omitted.
The second parameter is the database port, for example, 3308 3309. It cannot be omitted.
For mysql permissions, run the following SQL statement: grant replication client, super on *. * to 'slave '@ '2014. 0.0.1' identified by 'slave12345 ';
EOF
Exit 1
}
Function check_mysql_status (){
# Echo $ MYSQL $ @
Echo "show status;" | $ MYSQL $ @ | perl-p-e's/\ s + (\ w +) /= "$1"/G'> $ VAR_LOG
# Echo "$ mysql_status"
}
Function check_mysql_slave_status (){
# Echo $ MYSQL $ @
Echo "show slave status \ G" | $ MYSQL $ @ | perl-p-e's/: \ s (. *) $/= "$1 usd/g; s/^ \ s + // g; s/^ \*. * \ * //; '> $ VAR_LOG
}
Function check_slave_results (){
If ["$ Slave_IO_Running" = "Yes"] & ["$ Slave_ SQL _Running" = "Yes"]; then
If [$ tmp_code-ne 0] & [$ Seconds_Behind_Master-gt 100]; then
Code = 2
Elif [$ Seconds_Behind_Master-lt 50]; then
Code = 0
Else
Code = 1
Fi
Else
Code = 2
Fi
}
If [$ #-le 0] | ["$1" = "-h"]; then
Usage
Fi
If ["$1" = "-s"]; then
# Echo "Check slave mode"
Check_mysql_slave_status $2
Fi
Code = 0
Check_mysql_status $2
. $ VAR_LOG
PER_SECOND_QUESTION = 'echo "$ Uptime $ Queries" | awk '{printf ("% 4.3f"), $2/$1} END {if ($2/$1 <0.05) exit 0; else exit 1 ;}''
Tmp_code = $?
If [-z $ Uptime]; then
Echo "$ MYSQL $ @ connection failed. Please check the service" & exit 2
Fi
If ["$1" = "-s"]; then # output different information
Check_slave_results
Printf "master database: $ Master_Host: $ Master_Port Uptime: $ Uptime Threads: $ Threads_running Questions: $ Questions Slow_queries: $ export Open_tables: $ Open_tables Queries per second avg: % s seconds: $ Slave_IO_Running Slave_ SQL _Running: $ Slave_ SQL _Running Seconds_Behind_Master: $ response | Threads = $ Threads_running Questions = $ Questions Slowqueries = $ Slow_queries Opens = $ Opened_tables Opentables = $ Open_tables tables = % s BehindMaster = $ response "$ response
Else ## output different status information
Printf "Uptime: $ Uptime Threads: $ Threads_running Questions: $ Questions Slow_queries: $ Slow_queries Open_tables: $ Open_tables Queries per second avg: % s | Threads = $ Threads_running Questions = $ Questions Slowqueries = $ Slow_queries Opens = $ Opened_tables Opentables = $ Open_tables Queriespersecondavg = % s "$ PER_SECOND_QUESTION
Fi
Exit $ code

Mysql connection count monitoring: (monitors the maximum number of connections of mysql (the maximum number of connections is less than 1000 alarm), and the current number of connections, the number of available connections is less than 50 alarm)
#! /Bin/bash
# MYSQL = "/usr/local/mysql/bin/mysql-unagios-pmonitor_slave # status-h $3-P"
MYSQL = "/usr/local/mysql/bin/mysql-unagios-pmonitor_slave # status-h127.0.0.1-P"
VAR_LOG =/tmp/check_mysql_status_con _ $ 2.log &> $ VAR_LOG
# Threads_con_log =/tmp/check_mysql_tread_conn.log. $2 &> $ Threads_con_log
# Max_conn_log =/tmp/check_mysql_max_con.log. $2 &> $ Max_conn_log
Function usage (){
Cat < 'Basename $ 0' need at least value: 'basename $ 0'-[s | m] port
First arg-s: monitor slave mode
-M: moitor master mode
Second arg port: mysqld prot 3306 or 3307 or 3308...
Mysql privilege: you neeed exec: grant replication client, super on *. * to 'slave '@ '2017. 0.0.1' identified by 'slave12345 ';
EOF
Exit 1
}
Function check_mysql_status (){
# Echo $ MYSQL $ @
Echo "show status;" | $ MYSQL $ @ | perl-p-e's/\ s + (\ w +) /= "$1"/G'> $ VAR_LOG
# Echo "$ mysql_status"
}
Function check_mysql_con_status (){
# Echo $ MYSQL $ @
Echo "show status like 'threads _ connected ';" | $ MYSQL $ @ | perl-p-e's/\ s + (\ w +) /= "$1"/G'> $ VAR_LOG
Echo "show variables like 'max _ connections ';" | $ MYSQL $ @ | perl-p-e's/\ s + (\ w +) /= "$1"/G'> $ VAR_LOG
# Echo "$ mysql_status"
}
Function check_mysql_con_results (){
# Echo "$ max_connections, $ Threads_connected"
Residue_conn = $ ($ max_connections-$ Threads_connected ))
# Echo $ Residue_conn
If [$ max_connections-lt 1000]; then
If [$ Residue_conn-lt 50]; then
Code = 2
Else
Code = 1
Fi
Else
Code = 0
Fi
}
If [$ #-le 0] | ["$1" = "-h"]; then
Usage
Fi
Code = 0
Check_mysql_status $2
Check_mysql_con_status $2
. $ VAR_LOG
Residue_conn = $ max_connections
Check_mysql_con_results $2
PER_SECOND_QUESTION = 'echo "$ Uptime $ Queries" | awk '{printf ("% 4.3f"), $2/$1} END {if ($2/$1 <0.05) exit 0; else exit 1 ;}''
Tmp_code = $?
#
If [-z $ Uptime]; then
Echo "$ MYSQL $ @ Connect Failed, Please check! "& Exit 2
Fi
#
Printf "Uptime: $ Uptime Threads_connected: $ Threads_connected Max_connections: $ max_connections Residue_connectios: $ Residue_conn Open_tables: $ Open_tables Queries per second avg: % s | Threads = $ Threads_running Questions = $ Questions Slowqueries = $ Slow_queries Opens = $ Opened_tables Opentables = $ Open_tables Queriespersecondavg = % s "$ PER_SECOND_QUESTION
#
Exit $ code

Nagios details: click here
Nagios: click here

Related reading:

Network Monitor Nagios Overview

Nagios construction and Configuration

Build a Nagios monitoring platform in the Nginx Environment

Configure the basic Nagios System on RHEL5.3 (using Nagios-3.1.2)

CentOS 5.5 + Nginx + Nagios monitoring and control terminal installation and Configuration Guide

Install Nagios Core for Ubuntu 13.10 Server

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.