Ansible部署zabbix(----未完结)

1.管理机安装ansible

ansbile组件安装

[root@m01 ~]# yum install gcc* pcre* -y
[root@m01 ~]# yum install epel-release -y
[root@m01 ~]# yum install ansible –y
1.免交互分发秘钥
#!/bin/bash
###############################################################
# File Name: sshkey.sh
# Version: V1.0
# Author: bing
# Organization: www.b-ing.vip
###############################################################
[ -f /etc/init.d/functions ] && . /etc/init.d/functions
[ `rpm -ql sshpass|wc -l` == "0" ] && yum install sshpass -y
touch /tmp/sshkey.log
#目标IP段
dip="172.16.1"
dpt=`echo {112..114}`
ssh-keygen -f ~/.ssh/id_rsa -P '' -q
RETVAL=0
for ip in $dpt
do 
    sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub "-o StrictHostKeyChecking=no" $dip.$ip &>>/tmp/sshkey.log
    RETVAL=$?
    if [ $RETVAL -eq 0 ]
    then 
        action "add key $dip.$ip" /bin/true
    else
        action "add key $dip.$ip" /bin/false
    fi
done
2.批量添加ansible host
#!/bin/bash
##################################################
# File Name: test.sh
# Version: V1.0
# Author: bing
# Organization: www.b-ing.vip
##################################################
dip=`ifconfig eth1|awk -F "[ .]+" 'NR==2{print $3"."$4"."$5}'`
dpt=`echo {112..114}`
zxserver=112
module="[allserver]\n[zabbix-server]\n[zabbix-agent]"
echo -e $module >>/etc/ansible/hosts
for n in $dpt
do  
    sed -i "/allserver/a $dip.$n" /etc/ansible/hosts
    if [ "$n" = "$zxserver" ]   
    then
        sed -i "/zabbix-server/a $dip.$n" /etc/ansible/hosts
    else
        sed -i "/zabbix-agent/a $dip.$n" /etc/ansible/hosts
    fi  #echo $dip.$n >> /etc/ansible/hosts
done

2.服务端安装zabbix

1.zabbix介绍...巴拉巴拉
zabbix([`zæbiks])是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案。

zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。

zabbix由2部分构成,zabbix server与可选组件zabbix agent。

zabbix server可以通过SNMP,zabbix agent,ping,端口监视等方法提供对远程服务器/网络状态的监视,数据收集等功能,它可以运行在Linux,Solaris,HP-UX,AIX,Free BSD,Open BSD,OS X等平台上。

2.zabbix部署
#1.安装环境
系统环境:CentOS Linux release 7.8.2003 (Core)
zabbix版本:zabbix-release-5.0-1.el7.noarch.rpm
#zabbix 5.0 版本于 5 月 11 日正式发布,是最新的 LTS(长期支持)版本
#2.服务端安装zabbix 
#安装数据库mariadb
yum install mariadb-server -y
systemctl start mariadb && systemctl enable mariadb
mariadb清空数据库初始化命令行 mysql_secure_installation 一路回车 输入新密码123456
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success


mysql -uroot -p123456 -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -uroot -p123456 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';"

#安装zabbix
#添加zabbix.repo源
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

#启用 zabbix 前端源,修改/etc/yum.repos.d/zabbix.repo,将[zabbix-frontend]下的11行 enabled 改为 1
enabled=1
sed -i '11c enabled=1' /etc/yum.repos.d/zabbix.repo
#安装 zabbix server 和 agent
yum install zabbix-server-mysql zabbix-agent -y
#安装 Software Collections,便于后续安装高版本的 php,默认 yum 安装的 php 版本为 5.4 过低
yum install centos-release-scl -y

#安装 zabbix 前端和相关环境
yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

#导入数据
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
#编辑/etc/zabbix/zabbix_server.conf 文件,修改数据库配置 设置密码
sed -i '/DBPassword=/a DBPassword=zabbix' /etc/zabbix/zabbix_server.conf
#查看配置信息
grep "^[a-Z]" /etc/zabbix/zabbix_server.conf
#egrep -v "#|^$" /etc/zabbix/zabbix_server.conf


#修改Apache 的配置文件/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf,修改时区
echo 'php_value[date.timezone] = Asia/Shanghai' >>/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf

grep "Shanghai" /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
#php_value[date.timezone] = Asia/Shanghai

#启动
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm
#启动
systemctl enable zabbix-server.service && systemctl enable zabbix-agent && systemctl enable httpd && systemctl enable rh-php72-php-fpm

#检查
[root@zabbix /run]# netstat -lntup|grep "php-fpm"
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      4121/php-fpm: maste 
[root@zabbix /run]# netstat -lntup|grep httpd
tcp6       0      0 :::80                   :::*                    LISTEN      4287/httpd          
[root@zabbix /run]# netstat -lntup|grep "php-fpm"
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      4121/php-fpm: maste 
[root@zabbix /run]# netstat -lntup|grep "zabbix_agentd"
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      4124/zabbix_agentd  
tcp6       0      0 :::10050                :::*                    LISTEN      4124/zabbix_agentd  
[root@zabbix /run]# netstat -lntup|grep "zabbix_server"
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      4112/zabbix_server  
tcp6       0      0 :::10051   

#解决方法:安装字体并替换现有字体
yum install wqy-microhei-fonts -y
cp /usr/share/fonts/wqy-microhei/wqy-microhei.ttc /usr/share/zabbix/assets/fonts/graphfont.ttf

3.客户端安装agent2

1.yum安装agent2
#添加zabbix.repo源
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
yum install zabbix-agent2 -y
systemctl enable --now zabbix-agent2
#修改配置文件
/etc/zabbix/zabbix_agent2.conf
Server=172.16.66.11
ServerActive=172.16.66.11
Hostname=node16
#Server 和 ServerActive 配置为 zabibx server 或 zabbix proxy 地址,Hostname 配置为主机名即可。 
2.编译安装agent2(推荐)
#安装 gcc 等基础编译环境,由于使用 go 编写,因此需要配置 go 编译环境,下载并配置 go 语言编译环境
cd /opt
wget https://dl.google.com/go/go1.14.3.linux-amd64.tar.gz
tar zxvf go1.14.3.linux-amd64.tar.gz -C /usr/local/
echo "export PATH=$PATH:/usr/local/go/bin" >>/etc/profile
source /etc/profile
go env

#最后显示如下,表明 go 语言环境配置成功。
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/root/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build821720893=/tmp/go-build -gno-record-gcc-switches"

#开启 go mod,由于编译过程需要联网下载依赖包,配置 go mod 代理
go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direct


#(推荐)Agent2 没有组件依赖,可直接拷贝编译好的二进制文件和配置文件在其他主机上运行即可。
cd /opt
yum -y install net-snmp-devel libxml2-devel libcurl-deve libevent libevent-devel
yum -y install mysql-dev gcc net-snmp-devel curl-devel perl-DBI php-gd php-mysql php-bcmath php-mbstring php-xm
yum -y install pcre*
wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.1.tar.gz
tar zxvf zabbix-5.0.1.tar.gz
cd zabbix-5.0.1
#如果只是要编译 agent2,直接加-enable-agent2 参数即可
./configure --prefix=/etc/zabbix/ -enable-agent2
make install
#编译过程中有错误一定要关注,其中需要联网下载依赖包,耐心等待安装完成。 默认配置文件
/etc/zabbix/etc/zabbix_agent2.conf

#/etc/zabbix/etc/sbin/zabbix_agent2 配置 systemd 启动文件
cat /lib/systemd/system/zabbix-agent2.service
[Unit]
Description=Zabbix Agent 2
After=syslog.target
After=network.target
[Service]
Environment="CONFFILE=/etc/zabbix/etc/zabbix_agent2.conf"
EnvironmentFile=-/etc/sysconfig/zabbix-agent2
Type=simple
Restart=on-failure
PIDFile=/run/zabbix/zabbix_agent2.pid
KillMode=control-group
ExecStart=/etc/zabbix/sbin/zabbix_agent2 -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s
User=zabbix
Group=zabbix
[Install]
WantedBy=multi-user.targe
#创建用户 
useradd zabbix -u 899 -s /sbin/nologin -M
配置启动并设置开机启动
systemctl daemon-reload
systemctl enable zabbix-agent2
systemctl start zabbix-agent2

4.使用ansible安装服务端zabbix和客户端agent2

二进制安装agent2
[root@m01 ~]# tree playbook/
playbook/
├── zabbix-agent2.service
├── zabbix-agent2.tar.gz
└── zabbix_agent.sh

cat zabbinx-agent2-install.yml
- hosts: zabbix-agent
  remote_user: root
  tasks:
  - name: copy install_shell to clien
    copy: src={{ item }} dest=/root/
    with_fileglob:
    - /root/playbook/*
  - name: run shell
    shell: /bin/bash /root/zabbix_agent.sh 

cat zabbix_agent.sh
#!/bin/bash 
tar -xf /root/zabbix-agent2.tar.gz -C /etc &&
useradd zabbix -u 899 -M  -s /sbin/nologin &&
mv /root/zabbix-agent2.service /lib/systemd/system/ &&
#二进制包内修改好可以去掉sed
sed -i 's\Server=127.0.0.1\Server=192.168.31.112\g' /etc/zabbix/etc/zabbix_agent2.conf &&
sed -i 's\ServerActive=127.0.0.1\ServerActive=192.168.31.112\g' /etc/zabbix/etc/zabbix_agent2.conf &&
systemctl start zabbix-agent2.service

已编译的agent2和systemd脚本
云盘下载提取码:bing

rpm安装agent

[root@m01 /opt/playbook_zabbix_install]# tree
.
├── zabbix-agent-5.0.1-1.el7.x86_64.rpm
├── zabbix_agentd.conf.j2
└── zabbix_agent.yml
#下载地址:https://mirrors.huaweicloud.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-agent-5.0.1-1.el7.x86_64.rpm
cat zabbix_agent.yml
- hosts: zabbix-agent
  remote_user: root
  vars: 
    - src_package_path: /opt/playbook_zabbix_install/zabbix-agent-5.0.1-1.el7.x86_64.rpm 
    - dest_package_path: /root/zabbix-agent-5.0.1-1.el7.x86_64.rpm
  tasks:
    - name: Copy package zabbix-agent
      copy: src={{ src_package_path }} dest={{ dest_package_path }} backup=yes
    - name: Install zabbix agent
      yum: name={{ dest_package_path }} state=installed
    - name: Config zabbix agent
      template: src=zabbix_agentd.conf.j2 dest=/etc/zabbix/zabbix_agentd.conf
      notify: Restart zabbix agent
    - name: Start zabbix agent
      service: name=zabbix-agent.service state=started
  handlers:
    - name: Restart zabbix agent
      service: name=zabbix-agent.service state=restarted

cat zabbix_agentd.conf.j2 
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.31.112
ServerActive=192.168.31.112:10051
Hostname={{ ansible_all_ipv4_addresses[0] }}
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
yum安装服务端

#远程主机执行本地脚本
ansible zabbix -m script -a "zabbinx-insatll.sh"

yum install mariadb-server -y
systemctl start mariadb && systemctl enable mariadb
mysql -u root -e "use mysql;update user set password=password("123456") where user='root';flush privileges;"
mysql -uroot -p123456 -e "create database zabbix character set utf8 collate utf8_bin;"
mysql -uroot -p123456 -e "grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix';"
rpm -Uvh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo
sed -i '11c enabled=1' /etc/yum.repos.d/zabbix.repo
yum install zabbix-server-mysql zabbix-agent -y
yum install centos-release-scl -y
yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -pzabbix zabbix
sed -i '/DBPassword=/a DBPassword=zabbix' /etc/zabbix/zabbix_server.conf
echo 'php_value[date.timezone] = Asia/Shanghai' >>/etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
systemctl restart zabbix-server zabbix-agent httpd rh-php72-php-fpm
systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

5.监控状态

监控所有服务端口

#实现服务器上面所有的服务端口监控(包括一个服务多端口状况),有异常并进行告警
[root@m01 /opt/playbook_port_mc]# tree
.
├── playbook_port.yml
├── tcp_port_discovery.py
└── userparameter_tcp_port.conf


#cat playbook_port.yml

- hosts: zabbix-agent
  remote_user: root
  tasks:
    - name: Copy userparameter_tcp_port.conf To zabbix-agent
      copy: src=/opt/playbook_port_mc/userparameter_tcp_port.conf dest=/etc/zabbix/zabbix_agentd.d/userparameter_tcp_port.conf
    - name: Create Config Dir
      file: path=/etc/zabbix/scripts state=directory
    - name: Copy Script File
      copy: src=/opt/playbook_port_mc/tcp_port_discovery.py dest=/etc/zabbix/scripts/tcp_port_discovery.py
    - name: Set Script File Power
      file: path=/etc/zabbix/scripts/tcp_port_discovery.py mode=755 owner=root group=root
      notify: Restart zabbix agent
    - name: Set Cmd netstat 
      shell: "chmod u+s /usr/bin/netstat"
  handlers:
    - name: Restart zabbix agent
      service: name=zabbix-agent.service state=restarted
#u+s避免使用netstat错误
#cat /etc/zabbix/zabbix_agentd.d/userparameter_tcp_port.conf
UserParameter=discovery.tcp_port,/etc/zabbix/scripts/tcp_port_discovery.py


#cat /etc/zabbix/scripts/tcp_port_discovery.py

#!/usr/bin/python
# -*- coding: utf-8 -*-
#python2 commands模块
import re
import commands
import json
#自定义排除端口22,25,111
DROP_LIST = ['22','25','111']
def filterList():
    DROP_str = "|".join(DROP_LIST)
    CMD="netstat -pntl | awk '{print $4,$7}'|grep  [0-9] |egrep -vw '%s'" % (DROP_str)
    Result_Str = commands.getoutput(CMD)
    #print (Result_Str)
    tmp_list = Result_Str.split("\n")
    new_dict = {}
    for line in tmp_list:
       # print (line)
       PORT_REG = re.search(r"(127.0.0.1:|\d+.\d+.\d+.\d+:|:::|0.0.0.0:)(\d+).+\d+/(\S+)",line)
       if PORT_REG is not None:
           match_line =  (PORT_REG.groups())
           new_dict[ match_line[-1]]  =  match_line[-2]
    return new_dict
if __name__ == "__main__":
    Results = filterList()
    ports = []
    for key  in  Results:
        ports += [{'{#PNAME}':key,'{#PPORT}':Results[key]}]
    print json.dumps({'data':ports},sort_keys=True,indent=4,separators=(',',':'))

#执行playbook
ansible-playbook playbook_port.yml












监控TCP11种状态

cat /etc/zabbix/scripts/tcp_conn_status.sh

#!/bin/bash
#this script is used to get tcp and udp connetion status
#tcp status
metric=$1
tmp_file=/tmp/tcp_status.txt
/bin/netstat -an|awk '/^tcp/{++S[$NF]}END{for(a in S) print a,S[a]}' > $tmp_file
 
case $metric in
   closed)
          output=$(awk '/CLOSED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   listen)
          output=$(awk '/LISTEN/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synrecv)
          output=$(awk '/SYN_RECV/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   synsent)
          output=$(awk '/SYN_SENT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   established)
          output=$(awk '/ESTABLISHED/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   timewait)
          output=$(awk '/TIME_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closing)
          output=$(awk '/CLOSING/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   closewait)
          output=$(awk '/CLOSE_WAIT/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
        ;;
   lastack)
          output=$(awk '/LAST_ACK/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait1)
          output=$(awk '/FIN_WAIT1/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
   finwait2)
          output=$(awk '/FIN_WAIT2/{print $2}' $tmp_file)
          if [ "$output" == "" ];then
             echo 0
          else
             echo $output
          fi
         ;;
         *)
          echo -e "\e[033mUsage: sh  $0 [closed|closing|closewait|synrecv|synsent|finwait1|finwait2|listen|established|lastack|timewait]\e[0m"
   
esac

chmod +x /etc/zabbix/scripts/tcp_conn_status.sh
chown zabbix.zabbix /tmp/tcp_status.txt

cat /etc/zabbix/zabbix_agentd.d/userparameter_tcp_conn.conf
UserParameter=tcp.status[*],/etc/zabbix/scripts/tcp_conn_status.sh $1

systemctl restart zabbix-agent.service
#测试
zabbix_get -s 172.24.115.5 -k tcp.status[established]
监控MySQL主从状态
#监控从库
cat /etc/zabbix/script]# cat mysql_slvae_status.sh

#!/bin/bash
#Desc:用于获取主从同步信息,判断主从是否出现异常,然后提交给zabbix
USER="root"
PASSWD="YouPassword"
NAME=$1
function IO {
    Slave_IO_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_IO_Running |awk '{print $2}'`
    if [ $Slave_IO_Running == "Yes" ];then
        echo 0 
    else
        echo 1 
    fi
}

function SQL {
    Slave_SQL_Running=`mysql -u $USER -p$PASSWD -e "show slave status\G;" 2> /dev/null |grep Slave_SQL_Running: |awk '{print $2}'`
    if [ $Slave_SQL_Running == "Yes" ];then
        echo 0 
    else
        echo 1 
    fi
}

case $NAME in
   io)
       IO
   ;;
   sql)
       SQL
   ;;
   *)
        echo -e "Usage: $0 [io | sql]"
esac

cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql_slave.conf
# 获取MySQL slave状态
UserParameter=mysql.slave[*],/etc/zabbix/script/mysql_slvae_status.sh $1
systemctl restart zabbix-agent

#测试
zabbix_get -s 192.168.31.113 -k mysql.slave[sql]
zabbix_get -s 192.168.31.113 -k mysql.slave[io]
监控MySQL性能

cat /etc/zabbix/script/mysql_status.sh
#!/bin/bash 
#Desc:监控 MySQL 状态
HOST="localhost"
USER="root"
PASSWORD="IjRMv3l6TQIEjvb9nIX5"

PORT="3306"
CONNECTION="mysqladmin -h ${HOST} -u ${USER} -P ${PORT} -p${PASSWORD}"

if [ $# -ne "1" ];then
    echo "arg error!"
fi

case $1 in
    Uptime)
        result=`${CONNECTION} status 2>/dev/null |awk '{print $2}'`
        echo $result
        ;;
    Questions)
        result=`${CONNECTION} status 2>/dev/null |awk '{print $6}'`
        echo $result
        ;;
    Com_update)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_update" |awk '{print $4}'`
        echo $result
        ;;
    Slow_queries)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Slow_queries" |awk '{print $4}'`
        echo $result
        ;;
    Com_select)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_select" |awk '{print $4}'`
        echo $result
        ;;
    Com_rollback)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_rollback" |awk '{print $4}'`
        echo $result
        ;;
    Com_insert)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_insert" |awk '{print $4}'`
        echo $result
        ;;
    Com_delete)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_delete" |awk '{print $4}'`
        echo $result
        ;;
    Com_commit)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_commit" |awk '{print $4}'`
        echo $result
        ;;
    Bytes_sent)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_sent" |awk '{print $4}'`
        echo $result
        ;;
    Bytes_received)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Bytes_received" |awk '{print $4}'`
        echo $result
        ;;
    Com_begin)
        result=`${CONNECTION} extended-status 2>/dev/null |grep -w "Com_begin" |awk '{print $4}'`
        echo $result
        ;;*)
        echo "Usage:$0(Uptime|Questions|Com_update|Slow_queries|Com_select|Com_rollback|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)"
        ;;
esac

chmod +x /etc/zabbix/script/mysql_status.sh

cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
#获取MySQL性能指标
UserParameter=mysql.status[*],/etc/zabbix/script/mysql_status.sh $1
#获取MySQL运行状态
UserParameter=mysql.ping,mysqladmin -u root -h localhost -pIjRMv3l6TQIEjvb9nIX5 ping 2>/dev/null | grep -c alive
#获取MySQL版本
UserParameter=mysql.version,mysql -V

#重启
systemctl restart zabbix-agent

6.分布式监控proxy

1.使用场景
zabbix proxy 可以代替 zabbix server 收集性能和可用性数据,然后把数据汇报给 zabbix server,并且在一定程度上分担了zabbix server 的压力.此外,当所有agents和proxies报告给一个Zabbix server并且所有数据都集中收集时,使用proxy是实现集中式和分布式监控的最简单方法。
监控远程区域设备
监控本地网络不稳定区域
当 zabbix 监控上千设备时,使用它来减轻 server 的压力
简化分布式监控的维护
2.proxy端配置

#1.部署proxy数据库
create database zabbix_proxy character set utf8 collate utf8_bin;
grant all on zabbix_proxy.* to zabbix_proxy@127.0.0.1 identified by 'zabbix_proxy';
flush privileges;
mysql -h 127.0.0.1 -u zabbix_proxy -pzabbix_proxy
#2.安装zabbix-proxy
rpm -ivh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
yum -y install zabbix-proxy-mysql zabbix-get
#3.编辑配置文件
cat /etc/zabbix/zabbix_proxy.conf 
ProxyMode=0             //开启主动模式
Server=192.168.31.112   //zabbix-server端IP
ServerPort=10051        //zabbix-server端口
Hostname=zabbix-proxy
ListenPort=10051        //proxy端监听的端口
LogFile=/var/log/zabbix/zabbix_proxy.log
LogFileSize=0
PidFile=/var/run/zabbix/zabbix_proxy.pid
SocketDir=/var/run/zabbix
DBHost=127.0.0.1
DBName=zabbix_proxy
DBUser=zabbix_proxy
DBPassword=zabbix_proxy
SNMPTrapperFile=/var/log/snmptrap/snmptrap.log
Timeout=4
ExternalScripts=/usr/lib/zabbix/externalscripts
LogSlowQueries=3000
#4.导入数据
zcat /usr/share/doc/zabbix-proxy-mysql-4.0.7/schema.sql.gz |mysql -h 127.0.0.1 -u zabbix_proxy -pzabbix_proxy zabbix_proxy
#5.启动zabbix-proxy
systemctl start zabbix-proxy
3.agent端配置

#1.安装 zabbix-agent 软件包
yum -y install zabbix-agent
#2.编辑配置文件 zabbix-agent
cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.16.1.113        //填写zabbix-proxy的内网IP
StartAgents=5
Hostname=srt-hy
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf
UnsafeUserParameters=1
#3.启动zabbix-agent
systemctl start zabbix-agent 
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注