• zabbix mysql监控项


    zabbix_agentd.conf 文件添加自定义键值

    UserParameter=mysql_list[],/etc/zabbix/scripts/mysql.sh
    UserParameter=mysql.slave[
    ],/etc/zabbix/scripts/mysqlmonitor.sh $1
    UserParameter=mysql.statsvp[],/etc/zabbix/scripts/chk_mysql.sh $1
    UserParameter=discovery.mysqluser,/etc/zabbix/scripts/discovery_mysql_user.sh
    UserParameter=mysqluser.check[
    ],/etc/zabbix/scripts/mysql_user_check.sh $1

    chk_mysql.sh 性能

    1. #!/bin/bash
    2. # -------------------------------------------------------------------------------
    3. # FileName: check_mysql.sh
    4. # Revision: 1.0
    5. # -------------------------------------------------------------------------------
    6. # Copyright:
    7. # License: GPL
    8. # 用户名
    9. MYSQL_USER='root'
    10. # 密码
    11. MYSQL_PWD='密码'
    12. # 主机地址/IP
    13. MYSQL_HOST='localhost'
    14. # 端口
    15. MYSQL_PORT='3306'
    16. # 数据连接
    17. MYSQL_CONN="/usr/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
    18. # 参数是否正确
    19. if [ $# -ne "1" ];then
    20. echo "arg error!"
    21. fi
    22. # 获取数据
    23. case $1 in
    24. Uptime)
    25. result=`${MYSQL_CONN} status 2>/dev/null |cut -f2 -d":"|cut -f1 -d"T"`
    26. echo $result
    27. ;;
    28. Com_update)
    29. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_update"|cut -d"|" -f3`
    30. echo $result
    31. ;;
    32. Slow_queries)
    33. result=`${MYSQL_CONN} status 2>/dev/null |cut -f5 -d":"|cut -f1 -d"O"`
    34. echo $result
    35. ;;
    36. Com_select)
    37. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_select"|cut -d"|" -f3`
    38. echo $result
    39. ;;
    40. Com_rollback)
    41. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_rollback"|cut -d"|" -f3`
    42. echo $result
    43. ;;
    44. Questions)
    45. result=`${MYSQL_CONN} status 2>/dev/null |cut -f4 -d":"|cut -f1 -d"S"`
    46. echo $result
    47. ;;
    48. Com_insert)
    49. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_insert"|cut -d"|" -f3`
    50. echo $result
    51. ;;
    52. Com_delete)
    53. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_delete"|cut -d"|" -f3`
    54. echo $result
    55. ;;
    56. Com_commit)
    57. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_commit"|cut -d"|" -f3`
    58. echo $result
    59. ;;
    60. Bytes_sent)
    61. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_sent" |cut -d"|" -f3`
    62. echo $result
    63. ;;
    64. Bytes_received)
    65. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Bytes_received" |cut -d"|" -f3`
    66. echo $result
    67. ;;
    68. Com_begin)
    69. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Com_begin"|cut -d"|" -f3`
    70. echo $result
    71. ;;
    72. Threads_connected)
    73. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Threads_connected"|cut -d"|" -f3`
    74. echo $result
    75. ;;
    76. Threads_running)
    77. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Threads_running"|cut -d"|" -f3`
    78. echo $result
    79. ;;
    80. Innodb_row_lock_current_waits)
    81. result=`${MYSQL_CONN} extended-status 2>/dev/null |grep -w "Innodb_row_lock_current_waits"|cut -d"|" -f3`
    82. echo $result
    83. ;;
    84. Exec_long_time)
    85. result=`mysql -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT} -e "SELECT count(*) FROM information_schema.processlist WHERE COMMAND<>'Sleep' AND TIME >60 AND info IS NOT NULL AND user NOT IN ('root','event_scheduler','system user ','master','repl','repl1') " 2> /dev/null |sed 1d `
    86. echo $result
    87. ;;
    88. *)
    89. echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Threads_connected|Threads_running|Innodb_row_lock_current_waits|Exec_long_time)"
    90. ;;
    91. esac

    mysqlmonitor.sh 主从状态

    1. #!/bin/bash
    2. #Desc:用于获取主从同步信息,判断主从是否出现异常,然后提交给zabbix
    3. USER="root"
    4. PASSWD="密码"
    5. NAME=$1
    6. function IO {
    7. Slave_IO_Running=`/usr/bin/mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
    8. if [[ $Slave_IO_Running == "Yes" ]];then
    9. echo 0
    10. else
    11. echo 1
    12. fi
    13. }
    14. function SQL {
    15. Slave_SQL_Running=`/usr/bin/mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
    16. if [[ $Slave_SQL_Running == "Yes" ]];then
    17. echo 0
    18. else
    19. echo 1
    20. fi
    21. }
    22. function Second {
    23. Second=`/usr/bin/mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Seconds_Behind_Master: |awk '{print $2}'`
    24. echo $Second
    25. }
    26. case $NAME in
    27. io)
    28. IO
    29. ;;
    30. sql)
    31. SQL
    32. ;;
    33. second)
    34. Second
    35. ;;
    36. *)
    37. echo -e "Usage: $0 [io | sql |second]"
    38. esac

    mysql.sh

    echo `netstat -atnp |grep 3306 |wc -l`
    

    discovery_mysql_user.sh 获取连接用户 自动发现项

    1. # cat discovery_mysql_user.sh
    2. #!/bin/bash
    3. /usr/bin/mysql -uroot -密码 -e"SELECT count(*) num,user FROM information_schema.processlist WHERE user NOT IN ('root','event_scheduler','system user','master','repl','repl1') group by user having num>5 order by num desc ;" > /etc/zabbix/scripts/zabbix_mysql_user_count.txt 2>/dev/null
    4. proc_array=(`tail -n +2 /etc/zabbix/scripts/zabbix_mysql_user_count.txt | awk '{a[$NF]+=$1}END{for(k in a)print a[k],k}'|cut -d" " -f2`)
    5. length=${#proc_array[@]}
    6. printf "{\n"
    7. printf '\t'"\"data\":["
    8. for ((i=0;i<$length;i++))
    9. do
    10. printf "\n\t\t{"
    11. printf "\"{#USER_NAME}\":\"${proc_array[$i]}\"}"
    12. if [ $i -lt $[$length-1] ];then
    13. printf ","
    14. fi
    15. done
    16. printf "\n\t]\n"
    17. printf "}\n"

    mysql_user_check.sh 获取mysql用户连接数量

    1. # cat mysql_user_check.sh
    2. #!/bin/bash
    3. process=$1
    4. mysql_user_count=`tail -n +2 /etc/zabbix/scripts/zabbix_mysql_user_count.txt | awk '{a[$NF]+=$1}END{for(k in a)print a[k],k}' | grep -w $1 | cut -d" " -f1`
    5. echo "$mysql_user_count"

    测试
    chmod 777 discovery_mysql_user.sh mysql_user_check.sh zabbix_mysql_user_count.txt

    zabbix_get -p10050 -k ‘discovery.mysqluser’ -s ip
    zabbix_get -p10050 -k ‘mysqluser.check[user]’ -s ip

  • 相关阅读:
    Android 13.0 Launcher3定制之双层改单层(去掉抽屉式三)
    RPA是什么意思?RPA机器人很厉害吗?
    关系模型知识点总结(2)—— 关系完整性约束&关系操作基础
    linux 火狐浏览器报错Firefox is already running, but is not responding
    java-单列集合List详解
    java 并发编程艺术摘抄
    神经元在人体内如何分布,人体神经元怎么分布的
    [设计模式] 静态代理居然能解决这种问题,我惊讶了!
    CSS------我又回来了
    [hive] 窗口函数 ROW_NUMBER()
  • 原文地址:https://blog.csdn.net/u013091109/article/details/126466862