zabbix 4.0 LTS agent部署及percona mysql plugin安装

第一部分 agent部署
1.解压源码包到/usr/local
tar -xvf /dfile/zabbix-4.0.7.tar.gz -c /usr/local/
2.安装依赖包
yum install -y gcc
3.创建用户
useradd -r -s /sbin/nologin zabbix
4.编译并安装
./configure --prefix=/usr/local/zabbix --enable-agent
make install
5.创建目录
mkdir -p /usr/local/zabbix/{logs,tmps}
chown -R zabbix:zabbix /usr/local/zabbix/{logs,tmps}
6.修改配置文件
egrep -v "^#|^$" /usr/local/zabbix/etc/zabbix_agentd.conf
PidFile=/usr/local/zabbix/tmps/zabbix_agentd.pid
LogFile=/usr/local/zabbix/logs/zabbix_agentd.log
EnableRemoteCommands=0
Server=172.18.0.10
ListenPort=10050
StartAgents=1
ServerActive=172.18.0.10
Hostname=172.18.0.150
Timeout=30
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
UnsafeUserParameters=1
7.配置启动文件
cp /usr/local/zabbix-4.0.7/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/zabbix_agentd
[root@dzst150 etc]# egrep "BASEDIR=|PIDFILE=" /etc/init.d/zabbix_agentd
BASEDIR=/usr/local/zabbix
PIDFILE=$BASEDIR/tmps/$BINARY_NAME.pid
8.启动 agent
/etc/init.d/zabbix_agentd start
9.在web界面添加主机信息
10.等一会,就可以查看主机负载了
第二部分  部署percona mysql plugin
1.上传监控的目标服务武器进行安装
[root@dzst150 dfile]# rpm -ivh percona-zabbix-templates-1.1.8-1.noarch.rpm
Preparing... ################################# [100%]
package percona-zabbix-templates-1.1.8-1.noarch is already installed
2.安装依赖包
yum install -y php php-mysql
2./var/lib/zabbix会有个percona目录,具体文件大概功能说明
[root@dzst150 zabbix]# tree percona/
percona/
|-- scripts
| |-- get_mysql_stats_wrapper.sh
| `-- ss_get_mysql_stats.php
`-- templates
|-- userparameter_percona_mysql.conf
`-- zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
scripts/get_mysql_stats_wrapper.sh ## 是监控获取MySQL状态
scripts/ss_get_mysql_stats.php ## 是配置连接数据库用户名密码等,用shell来调用该PHP文件 templates/userparameter_percona_mysql.conf ## 实际zabbix agent需要的配置文件 templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml ## zabbix 模板
3.将 userparameter_percona_mysql.conf 复制到/usr/local/zabbix/etc/zabbix_agentd.conf.d,注意userparameter_percona_mysql.conf这个文件里记录的脚本位置
cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /usr/local/zabbix/etc/zabbix_agentd.conf.d/
4.修改zabbix agent配置文件,添加如下信息
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/
4.修改/var/lib/zabbix/percona/ss_get_mysql_stats.php.cnf配置信息
$mysql_user = 'zabbix';
$mysql_pass = 'zabbix';
$mysql_port = 3306;
$mysql_socket = '/tmp/mysql3306.sock';
$cache_file = "$cache_dir/$sanitized_host-mysql_cacti_stats.txt.3306" . ($port != 3306 ? ":$port" : '');
5.修改get_mysql_stats_wrapper.sh 文件
CACHEFILE="/tmp/$HOST-mysql_cacti_stats.txt:3306"
RES=`HOME=~zabbix /usr/local/mysql/bin/mysql -uzabbix -pzabbix -S /tmp/mysql3306.sock -e 'SHOW SLAVE STATUS\G' | egrep '(Slave_IO_Running|Slave_SQL_Running):' | awk -F: '{print $2}' | tr '\n' ','`
TIMEFLM=`stat -c %Y /tmp/$HOST-mysql_cacti_stats.txt:3306`
6.目标db创建监控账号
GRANT SELECT, PROCESS, SUPER, REPLICATION CLIENT ON *.* TO 'zabbix'@'%' IDENTIFIED BY "zabbix";
flush privileges;
7.修改percona目录及脚本的权限
chown -R zabbix:zabbix /var/lib/zabbix/
chmod -R 755 /var/lib/zabbix/percona/scripts/*
8.创建临时文件,并授权
touch 172.18.0.150-mysql_cacti_stats.txt.3306
chown -R zabbix.zabbix /tmp/172.18.0.150-mysql_cacti_stats.txt.3306
8.重启zabbix agent
/etc/init.d/zabbix_agentd restart
9.验证
agent端验证,返回数字即可
/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh gg
64

server端验证
/usr/local/zabbix/bin/zabbix_get -s 172.18.0.150 -p 10050 -k "MySQL.Threads-connected"
3
10.配置web模板信息
官方下载的文档导入,会直接报错
11.在zabbix2.4版本导入,然后导出后,再导入4.0即可
具体操作不过多说明,搭建基于5.5版本的mysql,然后搭建2.4 zabbix server即可;可以从https://github.com/trsenzhang/db_monitor/zabbix4.0_percona_monitor_mysql_plugin下载1.1.8版本
12.可以看到buffer里的信息了

zabbix 4.0.7 LTS安装

本次安装采用源码方式部署,将其部署到容器中
第一部分:初始化基础环境
1.首先创建一个docker container环境,要80端口映射出来,便于访问zabbix web界面,22端口映射出来,方便运维管理
docker run -d --privileged -v /data/ddata/zabbix:/data -v /data/ddata/dfile:/dfile -p1010:22 -p8080:80 --net zstnet --ip 172.18.0.10 --cap-add=NET_ADMIN --name zabbix -h zabbix trsen/centos76_mysql:20190605
2.查看container已经启动
[root@trsenzhangdb logs]# docker ps -a|grep zabbix
4874fd8d2beb trsen/centos76_mysql:20190605 "/usr/bin/supervisor…" 14 seconds ago Up 11 seconds (healthy) 0.0.0.0:1010->22/tcp, 0.0.0.0:8080->80/tcp zabbix
3.容器可以登录和使用
通过telnet 22端口测试,80端还没有监听,所以不通正常
若不能进测试不通,建议通过iptables看一下宿主机的转发配置
[root@trsenzhangdb ~]# iptables -t nat -nL |grep "0.10"
MASQUERADE tcp -- 172.18.0.10 172.18.0.10 tcp dpt:80
MASQUERADE tcp -- 172.18.0.10 172.18.0.10 tcp dpt:22
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 to:172.18.0.10:80
DNAT tcp -- 0.0.0.0/0 0.0.0.0/0 tcp dpt:1010 to:172.18.0.10:22
4.部署mysql,我这边部署的是mysql5.7.24官方版本
groupadd mysql
useradd -g mysql -d /usr/local/mysql -s /sbin/nologin -MN mysql
chown mysql:mysql /usr/local/mysql -R
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/mysql3306.cnf --initialize-insecure
/usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my3306.cnf &
[root@zabbix data]# ps -ef |grep mysql
mysql 11156 5224 11 11:57 pts/1 00:00:19 /usr/local/mysql/bin/mysqld --defaults-file=/data/mysql/my3306.cnf
第二部分安装zabbix server
1.创建zabbix用户
groupadd --system zabbix
useradd --system -g zabbix -d /usr/lib/zabbix -s /sbin/nologin -c "Zabbix Monitoring System" zabbix
我这里有yum配置,所以很多东西都是通过安装,个人建议在自己的环境中配置一个yum服务器
2.安装php和apache
yum install httpd php -y
3.安装依赖包
yum install gcc net-snmp-devel curl-devel perl-DBI php-gd php-mysql php-bcmath php-mbstring php-xml libxml2-devel libxml2 libevent-devel -y
4.解压源码文件包到/usr/local下
tar -xvf zabbix-4.0.7.tar.gz -C /usr/local/
5.进入/usr/local/zabbix-4.0.7进行源码编译配置和安装
./configure --prefix=/usr/local/zabbix \
--enable-server \
--enable-agent \
--with-mysql=/usr/local/mysql/bin/mysql_config \
--enable-ipv6 \
--with-net-snmp \
--with-libcurl \
--with-libxml2

make install
编译完成后会生成/usr/local/zabbix目录里面包含运行相关命令及配置
6.提高zabbix ping性能,编译安装fping工具
tar -xvf fping-4.2.tar.gz -C /usr/local/
cd /usr/local/fping-4.2
./configure && make install
chown root.zabbix /usr/local/sbin/fping
chmod 4710 /usr/local/sbin/fping
7.在存放zabbix的mysql数据库中创建db和利用源码包中sql脚本初始化数据,初始化脚本在源码文件目录/usr/local/zabbix-4.0.7/database/mysq中三个*.sql文件
create database if not exists zabbix default character set utf8 collate utf8_general_ci;
use zabbix
source /usr/local/zabbix-4.0.7/database/mysql/schema.sql;
source /usr/local/zabbix-4.0.7/database/mysql/images.sql;
source /usr/local/zabbix-4.0.7/database/mysql/data.sql;
create user zabbix@'%' identified by 'zabbix';
grant all privileges on *.* to zabbix@'%';
8.创建zabbix服务存放日志和临时文件地方
mkdir -p /usr/local/zabbix/{logs,tmps}
chown zabbix:zabbix /usr/local/zabbix/{logs,tmps} -R
9.配置zabbix server配置文件
vi /usr/local/zabbix/etc/zabbix_server.conf
egrep -v "^#|^$" /usr/local/zabbix/etc/zabbix_server.conf

ListenPort=10051
LogFile=/usr/local/zabbix/logs/zabbix_server.log
PidFile=/usr/local/zabbix/tmps/zabbix_server.pid
SocketDir=/tmp
DBHost=172.18.0.10
DBName=zabbix
DBUser=zabbix
DBPassword=zabbix
DBSocket=/tmp/mysql3306.sock
DBPort=3306
Timeout=4
FpingLocation=/usr/local/sbin/fping
LogSlowQueries=3000
10.复制并配置自启动脚本
cp /usr/local/zabbix-4.0.7/misc/init.d/fedora/core/zabbix_server /etc/init.d/zabbix_server

[root@zabbix etc]# cat /etc/init.d/zabbix_server |egrep "BASEDIR=|PIDFILE="
BASEDIR=/usr/local/zabbix
PIDFILE=$BASEDIR/tmps/$BINARY_NAME.pid
11.创建软连接,并启动zabbix server
ln -s /usr/local/mysql/lib/libmysqlclient.so.20 /usr/lib64/

[root@zabbix etc]# /etc/init.d/zabbix_server start
Starting zabbix_server: [ OK ]


chkconfig --add zabbix_server
chkconfig --level 35 zabbix_server on
chkconfig --list
12.配置web
cp -a /usr/local/zabbix-4.0.7/frontends/php/* /var/www/html/
chown -R apache:apache /var/www/html/
13.修改参数/etc/php.ini参数
sed -i "s#post_max_size = 8M#post_max_size = 32M#g" /etc/php.ini
sed -i "s/;date.timezone =/date.timezone = Asia\/Shanghai/g" /etc/php.ini
sed -i "s#max_execution_time = 30#max_execution_time = 300#g" /etc/php.ini
sed -i "s#max_input_time = 60#max_input_time = 300#g" /etc/php.ini
sed -i "s#memory_limit = 128M#memory_limit = 128M#g" /etc/php.ini
14.配置apache,并启动httpd
[root@zabbix logs]# grep "^ServerName" /etc/httpd/conf/httpd.conf
ServerName 172.18.0.10:80
15.进入web界面配置http://132.232.101.188:8080

16.check list,如果上面不修改php.ini这个里会有很多error,无法下一步
17.配置db连接
18.配置zabbix server信息
19.复检,没有问题就下一步
20.成功配置
21.登录 zabbix默认账密是admin/zabbix