Zabbix monitoring mysql multi-instance

Copyright: technology is shared, and if reproduced the drawing infringement, please inform, will be the first time to delete https://blog.csdn.net/qq_22917163/article/details/91579717

Foreword

In practice, many of our internal services such as zabbix, zentao, wiki, jumpserver and other mysql database to be used in service, the library built in all instances under a little out of place, put on a different server but also a waste of server resources , mysql performance requirements for these services is low, they mysql way to deploy multiple instances on a single server, we can use to meet our needs, and make full use of hardware resources of the server. The attendant is necessary to monitor these mysql instance, the following will introduce how to use zabbix to implement monitoring of multiple mysql on the same server instance.

1. preparation of related scripts

1.1 mysql multi-instance storage of files listening port

[admin@oneecar-qa017 ~]$ cat /etc/zabbix/scripts/mysql_multiple/mysql_port.txt

3304
3305
3306
3307
3308
3309

Wherein the port number is stored in each instance is listening

1.2 port auto-discovery script

[admin@oneecar-qa017 ~]$ cat /etc/zabbix/scripts/mysql_multiple/discovery_mysql.py

#/usr/bin/python
import json
import os
import subprocess

mysql_multi_port_file='/etc/zabbix/scripts/mysql_multiple/mysql_port.txt'
mysql_ports=[]


for port in open(mysql_multi_port_file):
	if len(port) != 0:
		mysql_ports.append({'{#MYSQLPORT}':port.strip('\n')})

print json.dumps({'data':mysql_ports},indent=4,separators=(',',':'))

Script execution results as shown below
Here Insert Picture Description

1.3 Monitoring item script

cat /etc/zabbix/scripts/mysql_multiple/check_Multimysql.sh

#!/bin/bash

MYSQL_USER='zabbix'
MYSQL_PWD='123456'
MYSQL_HOST='127.0.0.1'
MYSQL_PORT=$2
MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} --port=${MYSQL_PORT}"

help() {
	echo "Usage:$0  [ping|Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin]  port"
}

if [ $# -lt "2" ];then
	echo "....!"
	help
	exit 2
fi

case $1 in
	ping)
		result=`${MYSQL_CONN} ping 2>/dev/null | grep -c alive`
		echo $result
		;;
	Uptime)
		result=`${MYSQL_CONN} status 2>/dev/null | cut -f2 -d":"|cut -f1 -d"T"`
		echo $result
		;;
	Com_update)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_update"|cut -d"|" -f3`  
		echo $result
		;;
	Slow_queries)
		result=`${MYSQL_CONN} status 2>/dev/null | cut -f5 -d":"|cut -f1 -d"O"`
		echo $result
		;;
	Com_select)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_select"|cut -d"|" -f3`
		echo $result
		;;
	Com_rollback)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_rollback"|cut -d"|" -f3`
		echo $result
		;;
	Questions)
		result=`${MYSQL_CONN} status 2>/dev/null | cut -f4 -d":"|cut -f1 -d"S"`
		echo $result
		;;
	Com_insert)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_insert"|cut -d"|" -f3`
		echo $result
		;;
	Com_delete)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_delete"|cut -d"|" -f3`
		echo $result
		;;
	Com_commit)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_commit"|cut -d"|" -f3`
		echo $result
		;;
	Bytes_sent)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Bytes_sent"|cut -d"|" -f3`
		echo $result
		;;
	Bytes_received)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Bytes_received" |cut -d"|" -f3`
		echo $result
		;;
	Com_begin)
		result=`${MYSQL_CONN} extended-status 2>/dev/null | grep -w "Com_begin"|cut -d"|" -f3`
		echo $result
		;;
     *)
		help
		;;
esac

2.zabbix Configuration

2.1 zabbix client configuration

Write cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_multi.conf

UserParameter=mysql_discovery[*],/usr/bin/python /etc/zabbix/scripts/mysql_multiple/discovery_mysql.py
UserParameter=mysql_status[*],/bin/bash /etc/zabbix/scripts/mysql_multiple/check_Multimysql.sh $1 $2
UserParameter=mysql.ping[*],/bin/bash /etc/zabbix/scripts/mysql_multiple/check_Multimysql.sh ping $1

2.2 zabbix server configuration

2.2.1 Creating Monitoring Templates

Template download link

2.2.2 template will be connected to the server to be monitored

Here Insert Picture Description

Then wait a while to see the latest monitoring data, we can see the latest monitoring information
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/qq_22917163/article/details/91579717