star870126 发表于 2018-9-28 10:48:18

zabbix 3.0.2监控mysql

  内网有一台mysql服务器,版本是5.7.14
  关于这个版本安装,有兴趣可以参考
  http://xiao987334176.blog.51cto.com/2202382/1783509
  zabbix自带有一个模板Template App MySQL,用来监控mysql的
  但是不能直接使用,否则会因为没有Key,导致获取不到数据。
  下面介绍详细步骤。
  首先在mysql服务器安装zabbix-agent,请参考
  http://xiao987334176.blog.51cto.com/2202382/1768281
  最下面一部分。
  ###################################################################################
  讲解一个比较重要的问题。
  在shell里面,直接运行mysql相关命令,指定密码时,就会有一个提示
  比如: mysql -u zabbix -p123456 运行之后
  Warning: Using a password on the command line interface can be insecure.
  这个提示在mysql 5.5之后会出现。
  而且,这个提示,会被zabbix-servre捕捉到,在zabbix-server.log日志中会出现
  24123:20160826:101433.609 error reason for "110:mysql.status" changed: Received value Using a password on the command line interface can be insecure.8991074] is not suitable for value type
  提示参数不符合
  在zabbix-server服务器上面,使用zabbix-get命令时
  # /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status
  mysqladmin: Using a password on the command line interface can be insecure.
  57577
  ###################################################################################
  那么为了解决这个问题,必须无密码登录才可以,为了安全起见,限定只能在localhost登录
  先创建一个zabbix用户(创建用户,密码不能为空,否则报错)

  grant all PRIVILEGES on *.* to zabbix@'localhost'>  设置密码为root
  update mysql.user set authentication_string=password('root') where user='zabbix' and Host = 'localhost';
  刷新权限
  flush privileges;
  退出
  exit;
  测试无密码登录
  # mysql -u zabbix
  Welcome to the MySQL monitor.Commands end with ; or \g.

  Your MySQL connection>  Server version: 5.7.14 Source distribution
  Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
  Oracle is a registered trademark of Oracle Corporation and/or its
  affiliates. Other names may be trademarks of their respective
  owners.
  Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  mysql> exit
  Bye
  发现一个很奇怪的现象,把密码设置和root一样,就可以无密码登录了
  mysql> select Host,User,authentication_string from mysql.user;
  +-----------+-----------+-------------------------------------------+
  | Host      | User      | authentication_string                     |
  +-----------+-----------+-------------------------------------------+
  | localhost | root      | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
  | localhost | mysql.sys | *THISISNOTAVALIDPASSWORDTHATCANBEUSEDHERE |
  | localhost | zabbix    | *81F5E21E35407D884A6CD4A731AEBFB6AF209E1B |
  +-----------+-----------+-------------------------------------------+
  3 rows in set (0.00 sec)
  进入Mysql服务器的zabbix-agent目录
  cd /usr/local/zabbix-agent/
  mkdir alertscripts
  编辑脚本
  vim checkmysql.sh
  内容如下:
  #!/bin/sh
  #MYSQL_SOCK="/data/3306/mysqld.sock"
  #MYSQL_PWD=`cat /usr/local/zabbix-agent/alertscripts/.mysqlpassword`
  ARGS=1
  if [ $# -ne "$ARGS" ];then
  echo "Please input one arguement:"
  fi
  case $1 in
  Uptime)
  result=`mysqladmin -uzabbix status|cut -f2 -d":"|cut -f1 -d"T"`
  echo $result
  ;;
  Com_update)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_update"|cut -d"|" -f3`
  echo $result
  ;;
  Slow_queries)
  result=`mysqladmin -uzabbix status |cut -f5 -d":"|cut -f1 -d"O"`
  echo $result
  ;;
  Com_select)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_select"|cut -d"|" -f3`
  echo $result
  ;;
  Com_rollback)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_rollback"|cut -d"|" -f3`
  echo $result
  ;;
  Questions)
  result=`mysqladmin -uzabbix status|cut -f4 -d":"|cut -f1 -d"S"`
  echo $result
  ;;
  Com_insert)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_insert"|cut -d"|" -f3`
  echo $result
  ;;
  Com_delete)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_delete"|cut -d"|" -f3`
  echo $result
  ;;
  Com_commit)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_commit"|cut -d"|" -f3`
  echo $result
  ;;
  Bytes_sent)
  result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_sent" |cut -d"|" -f3`
  echo $result
  ;;
  Bytes_received)
  result=`mysqladmin -uzabbix extended-status |grep -w "Bytes_received" |cut -d"|" -f3`
  echo $result
  ;;
  Com_begin)
  result=`mysqladmin -uzabbix extended-status |grep -w "Com_begin"|cut -d"|" -f3`
  echo $result
  ;;
  *)
  echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions)"
  ;;
  esac
  设置权限
  chmod 755 checkmysql.sh
  chown zabbix:zabbix -R /usr/local/zabbix-agent/
  编辑配置文件
  vim /usr/local/zabbix-agent/etc/zabbix_agentd.conf
  增加紫色部分,内容如下:
  LogFile=/usr/local/zabbix-agent/logs/zabbix_agentd.log
  ###zabbix 服务端地址
  Server=192.168.1.109
  ##agent服务监听地址,也就是本机地址
  #ListenIP=192.168.1.105
  ###ServerActive=192.168.1.110
  ##zabbix-server端主机地址(zabbix server)
  Hostname=zabbix server
  UserParameter=mysql.version,mysql -V
  UserParameter=mysql.status
[*],/usr/local/zabbix-agent/alertscripts/checkmysql.sh $1
  UserParameter=mysql.ping,mysqladmin -uzabbix ping | grep -c alive
  重启zabbix_agentd
  /etc/init.d/zabbix_agentd restart
  在zabbix-server服务器上面检查mysql
  # /usr/local/zabbix/bin/zabbix_get -s 192.168.1.110 -p10050 -k mysql.status
  9479
  #
  如果能直接返回数字,没有多余的字符,就说明成功了。
  在zabbix-server添加一台主机

  添加模板的时候,选择Template App MySQL和Template OS Linux

  等待10分钟,图像就会出来了。



页: [1]
查看完整版本: zabbix 3.0.2监控mysql