• Zabbix自定义脚本监控MySQL数据库


    一、MySQL数据库配置

    1.1 创建Mysql数据库用户

    [root@mysql ~]# mysql -uroot -p
    create user zabbix@'127.0.0.1' identified by '123456';
    flush privileges;

    1.2 添加用户密码到mysql client的配置文件中

    [root@mysql ~]# vim /etc/my.cnf.d/client.cnf 
    [client]
    host='127.0.0.1'
    user='zabbix'
    password='123456'

    二、创建自定义监控脚本 

    [root@mysql ~]# vim /etc/zabbix/script/check_mysql.sh  

    1. #!/bin/bash
    2. #Mysql主机地址
    3. MYSQL_HOST='127.0.0.1'
    4. #Mysql端口
    5. MYSQL_PORT='3306'
    6. #数据连接
    7. MYSQL_CONN="/usr/bin/mysqladmin -h${MYSQL_HOST} -P${MYSQL_PORT}"
    8. #参数是否正确
    9. if [ $# -ne "1" ];then
    10. echo "arg error!"
    11. fi
    12. #获取数据
    13. case $1 in
    14. Uptime)
    15. result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"`
    16. echo $result
    17. ;;
    18. Com_update)
    19. result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3`
    20. echo $result
    21. ;;
    22. Slow_queries)
    23. result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"`
    24. echo $result
    25. ;;
    26. Com_select)
    27. result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3`
    28. echo $result
    29. ;;
    30. Com_rollback)
    31. result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
    32. echo $result
    33. ;;
    34. Questions)
    35. result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"`
    36. echo $result
    37. ;;
    38. Com_insert)
    39. result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3`
    40. echo $result
    41. ;;
    42. Com_delete)
    43. result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3`
    44. echo $result
    45. ;;
    46. Com_commit)
    47. result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3`
    48. echo $result
    49. ;;
    50. Bytes_sent)
    51. result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
    52. echo $result
    53. ;;
    54. Bytes_received)
    55. result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
    56. echo $result
    57. ;;
    58. Com_begin)
    59. result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3`
    60. echo $result
    61. ;;
    62. *)
    63. echo "Unknown options."
    64. ;;
    65. esac

    三、zabbix agent添加自定义键值

    [root@mysql ~]# vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
    UserParameter=mysql.status[*],bash /etc/zabbix/script/check_mysql.sh $1
    UserParameter=mysql.ping,/usr/bin/mysqladmin ping 2>/dev/null | grep -c alive
    UserParameter=mysql.version,/usr/bin/mysql -V
    [root@mysql ~]# systemctl restart zabbix-agent

    四、链接Mysql模板

    配置–>主机–>添加模板–>选择“Template DB MySQL”

    五、数据查看

  • 相关阅读:
    关于STM32的启动,boot0和boot1的取值说明
    一个用java的get请求
    我说MySQL联合索引遵循最左前缀匹配原则,面试官让我回去等通知
    PLC中ST编程的IF判断
    1.spring框架-----spring framework
    SELF-INSTRUCT: Aligning Language Models with Self-Generated Instructions
    Java包装类和泛型
    python实现将图片数据以LMDB方式存储
    《JAVA EE》内部类(上篇)
    数据库链接池示实例
  • 原文地址:https://blog.csdn.net/weixin_42054864/article/details/133574459