Mysql 监控 支持 mysql 多实例自动发现以及主从监控

  1. 在[/usr/local/zabbix327/bin] 目录下新建python文件,并增加执行权限,如下:

    #!/usr/bin/env /usr/bin/python
    
    # _*_ coding:utf-8 _*_
    
    import subprocess
    import json
    import re
    import ConfigParser
    
    
    def main():
        # mysqld is started with mysqld_safe ,and with the configfile,or use 3306 port
        command = "ps -eo command |grep mysqld_safe|grep -v grep"
        process = subprocess.Popen(command, shell=True, stdin=subprocess.PIPE,
                                stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        out, err = process.communicate()
    
        return_code = process.wait()
        if return_code != 0:
            print("error found!" + err)
            return
    
        all_mysqld = list(str(out).splitlines())
    
        mysql_config_arg = "--defaults-file="
        mysql_socket_arg = "--socket="
        mysql_bin_reg = "\\s(.+)mysqld_safe\\s"
    
        list_mysql = []
    
        for mysqld in all_mysqld:
            socket_file = None
            socket_port = None
            if mysql_config_arg in mysqld:
                for arg in list(mysqld.split()):
                    if mysql_config_arg in arg:
                        mysql_config_file = arg.replace(mysql_config_arg, "")
                        cf = ConfigParser.ConfigParser(allow_no_value=True)
                        cf.read(mysql_config_file)
                        socket_file = cf.get("mysqld", "socket")
                        socket_port = int(cf.get("mysqld", "port"))
    
            elif mysql_socket_arg in mysqld:
                for arg in list(mysqld.split()):
                    if mysql_socket_arg in arg:
                        socket_file = arg.replace(mysql_socket_arg, "").strip()
                        # netstat -tulpn | grep  `cat /var/run/mysqld/mysqld.pid` |grep -P ':(\d{3,6})(?=\s)' -o | awk  'BEGIN {FS=":"}; {print $2}'
                        socket_port = 3306
    
            bin_path = str(re.compile(mysql_bin_reg).findall(mysqld)[0]).strip()
    
            list_mysql.append({'{#BIN_PATH}': bin_path, '{#SOCKET_PATH}': socket_file, '{#PORT}': socket_port})
    
        print json.dumps({'data': list_mysql})
    
    
    if __name__ == '__main__':
        main()
    
    
    
  2. copy get_mysql_stats_wrapper.sh mysql_innodb_trx_status.sh ss_get_mysql_stats.php 到 /usr/local/zabbix327/bin 目录下,增加可执行权限.

    # chmod +x get_mysql_stats_wrapper.sh

  3. 安装php 环境

    # yum install php php-mysql

  4. 确定php 解释引擎的位置,一般在/usr/bin/php ,如果未能发现,可通过 which php 或 find 命令来查找。

  5. 如果php解释引擎未在 /usr/bin/php,则需要修改 get_mysql_stats_wrapper.sh 文件中的php解释引擎的位置

    CMD="/usr/bin/php -q $DIR/ss_get_mysql_stats.php --host $HOST --items gg"

  6. copy userparameter_percona_mysql.conf、userparameter_mysql.conf 到 zabbix_agentd.conf.d,并在zabbix_aentd.conf 中include文件(注意userparameter_percona_mysql.conf文件中脚本路径,需要根据实际情况修改)

  7. 导入模板

  8. host link template 模板,测试。

备注

  1. 本例对 原percona MySQL Server Template 中监控的指标进行了删减(根据DBA的需求),如果需要其他指标,请参考userparameter_percona_mysql.conf文件中的监控项以及模板中现有的监控项,在模板中新增 Item prototypes .

  2. 增加了slave的 Last_Errno 的监控(修改了php文件)

posted on 2019-04-04 17:01  非法操作  阅读(163)  评论(0编辑  收藏  举报

导航