agent played on more than mysql instance, takes a different port, agent only in the initial condition, into the script and key configuration, and then reboot. After the maintenance time (mysql port change), to do not move agent, and strive to end or only at the web server end can automatically make changes to the corresponding monitor port. |
Host macro format: $ MACRO (can be directly fill value) automatically discovers the macro: #MACRO (need to cooperate to create a key value)
1.agent host - agent host to create a macro variable $ MYSQLPORT, the macro variable is Discovery rules of automatic discovery script parameters
2. Templates - template to create automatic discovery rules, relying on the automatic discovery script, macro variables get automatic discovery .
Item 3. Monitoring - Monitoring item created in the Discovery rules, the monitoring of key names and key values in the macro variables are automatically discover, monitor will automatically generate the relevant ports of entry
4. Create a client script and keys, to support the entire infrastructure running
Because this template is a clone of the default template mysql monitor, you can ignore items, triggers. Discovery rules are monitored item inside.
The figure in the mysql.discovery is to get automatic discovery macro script, the script is on the agent side.
For this reason behind the $ MYSQLPORT execution of the script with the parameters of the time, $ MYSQLPORT is the host macro.
discovery rule 右边的 filters 是过滤规则的意思,比方你 只想取脚本里的 某某 自动发现宏,就可以在里面设置。 这里就只有一个自动发现宏,所有不做 配置。
点开主机,添加主机宏,宏名称是 自动发现脚本里的 参数, 宏的值就是 此 主机上需要监控的端口。
这样就实现了 只要改动主机宏的 值,就可以改变主机宏的监控项。
res=`echo $1| sed "s/_/\n/g"`;
port=($res)
printf '{\n' printf '\t"data":[\n' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]]; then printf '\t {\n' printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"},\n" else [[ "${key}" -eq "((${#port[@]}-1))" ]] printf '\t {\n' printf "\t\t\t\"{#MYSQLPORT}\":\"${port[${key}]}\"}\n" fi done printf '\t ]\n' printf '}\n'
执行实例: 以_ 为分隔符,格式化输出 自动发现宏
./discovery_mysql.sh 3306_3307_3308
{
"data":[
{
"{#MYSQLPORT}":"3306"},
{
"{#MYSQLPORT}":"3307"},
{
"{#MYSQLPORT}":"3308"} ] }
var=$1
MYSQL_PORT=$2
MYSQL_NAME=`ps -ef |grep '/mysql/app/bin/mysqld'| grep "$MYSQL_PORT" |grep -v grep | awk -F" " '{print $11}' |awk -F"/" '{print $2}'` ; MYSQL_SOCk_DIR="/$MYSQL_NAME/" ; df -h "${MYSQL_SOCk_DIR}${var}" |grep -v Filesystem |awk -F" " '{print $5}' | awk -F"%" '{print $1}' ;
MYSQL_PORT=$1;
[ "${MYSQL_USER}" = '' ] && MYSQL_USER=xxx
[ "${MYSQL_PASSWORD}" = '' ] && MYSQL_PASSWORD=xxxxx mysqladmin=/mysql/app/bin/mysqladmin ; MYSQL_SOCk_DIR=`ps -ef |grep 'mysql'| grep "${MYSQL_PORT}" |grep -v 'grep' | awk -F" " '{print $16}'` ; ${mysqladmin} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_SOCk_DIR} ping 2> /dev/null |grep 'alive'|wc -l ;
注意: 这边连接实例 都是 mysql -u -p -S /xxx/mysql.sock
但是后来发现部分客户端总是无法正确显示值,而且就是这条 语句报错, 感觉是环境变量的问题
所以拿 --socket=/xxx/mysql.sock 来代替 -S/xxx/mysql.sock 。 这里的变量${MYSQL_SOCk_DIR} 就是--socket=/xxx/mysql.sock 。
var=$1
MYSQL_PORT=$2
MYSQL_SOCk_DIR=`ps -ef |grep 'mysql'| grep "${MYSQL_PORT}" |grep -v 'grep' | awk -F" " '{print $16}'` ; mysql=/mysql/app/bin/mysql [ "${MYSQL_USER}" = '' ] && MYSQL_USER=xxx [ "${MYSQL_PASSWORD}" = '' ] && MYSQL_PASSWORD=xxxx ${mysql} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_SOCk_DIR} -e "show slave status\G;" 2> /dev/null |grep "\b${var}\b"|awk -F" " '{print $2}' ;
主从状态 有几个端口的值是 空的, 这个得 具体问题具体设置
mysql=/mysql/app/bin/mysql
var=$1
MYSQL_PORT=$2
[ "${MYSQL_USER}" = '' ] && MYSQL_USER=xxx
[ "${MYSQL_PASSWORD}" = '' ] && MYSQL_PASSWORD=xxxxx MYSQL_SOCk_DIR=`ps -ef |grep 'mysql'| grep "${MYSQL_PORT}" |grep -v 'grep' | awk -F" " '{print $16}'` ; ${mysql} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_SOCk_DIR} -e "show global status;" 2> /dev/null |grep -v Variable_name|grep "\b${var}\b"|awk '{print $2}' ;
发现有些监控项 或因为数字过长而无法显示,待解决
MYSQL_PORT=$1;
MYSQL_SOCk_DIR=`ps -ef |grep 'mysql'| grep "${MYSQL_PORT}" |grep -v 'grep' | awk -F" " '{print $16}'` ; mysql=/mysql/app/bin/mysql ; [ "${MYSQL_USER}" = '' ] && MYSQL_USER=xxx [ "${MYSQL_PASSWORD}" = '' ] && MYSQL_PASSWORD=xxxx ${mysql} -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_SOCk_DIR} -e "select version();" 2> /dev/null |awk 'END {print}'
UserParameter=mysql.discovery[*],/patrol/zabbix/bin/duoshili_discovery/discovery_mysql.sh $1
UserParameter=mysql.status_many[*],/patrol/zabbix/bin/duoshili_discovery/mysql_status2 $1 $2
UserParameter=mysql.ping_many[*],/patrol/zabbix/bin/duoshili_discovery/mysql_ping.sh $1 UserParameter=mysql.version_many[*],/patrol/zabbix/bin/duoshili_discovery/mysql_version.sh $1 UserParameter=mysqlcheck_repl[*],/patrol/zabbix/bin/duoshili_discovery/mysql_repl.sh $1 $2 UserParameter=mysql.filestyle_many[*],/patrol/zabbix/bin/duoshili_discovery/mysql_filestype