详解Zabbix安装部署实践
前言
zabbix是目前最为主流的开源监控方案之一,部署本身并不困难,难的是深入理解。根据在生产环境的实践从新版zabbix 4.0 lts开始全部使用docker部署,我相信未来越来越多的开源组件都会以容器化的形式呈现在我们面前。
操作系统:
[root@mysql ~]# cat /etc/redhat-release centos linux release 7.5.1804 (core)
mysql : 版本5.7.23 192.168.75.251
zabbix server:版本3.4.12 192.168.75.250
一、安装mysql
1.1下载mysql
关闭防火墙
#systemctl stop firewalld # systemctl disable firewalld vi /etc/selinux/config selinux=enforcing 改为 selinux= disabled
centos7 默认数据库为mariadb ,先卸载
rpm –qa |grep mariadb mariadb-libs-5.5.56-2.el7.x86_64 rpm -e --nodeps mariadb-libs-5.5.56-2.el7.x86_64
这个版本需要boost 1.9,也要下载
[root@mysql soft]# ll total 132444 -rw-r--r-- 1 root root 83709983 aug 10 10:26 boost_1_59_0.tar.gz -rw-r--r-- 1 root root 51907462 aug 10 10:26 mysql-5.7.23.tar.gz
添加mysql用户,
groupadd mysql useradd -g mysql -s /sbin/nologin mysql
1.2安装依赖包
yum install -y cmake bison bison-devel libaio-devel gcc gcc-c++ git ncurses-devel
1.3编译
解压mysql ,把boost的压缩版放到解压后的mysql目录内
tar xzvf mysql-5.7.23.tar.gz mv boost_1_59_0.tar.gz mysql-5.7.23
进入mysql源文件目录,新建configure做为编译目录,并进入该目
cd mysql-5.7.23 mkdir configure cd configure
使用cmake进行生成编译环境,
cmake .. -dbuild_config=mysql_release \ -dinstall_layout=standalone \ -dcmake_build_type=relwithdebinfo \ -denable_dtrace=off \ -dwith_embedded_server=off \ -dwith_innodb_memcached=on \ -dwith_ssl=bundled \ -dwith_zlib=system \ -dwith_pam=on \ -dcmake_install_prefix=/var/mysql/ \ -dinstall_plugindir="/var/mysql/lib/plugin" \ -ddefault_charset=utf8 \ -ddefault_collation=utf8_general_ci \ -dwith_editline=bundled \ -dfeature_set=community \ -dcompilation_comment="mysql server (gpl)" \ -dwith_debug=off \ -dwith_boost=..
安装
make && make install
修改配置文件
vim /etc/my.cnf [mysqld] datadir=/var/lib/mysql socket=/var/mysql/mysql.sock port=3306 log_error=/var/mysql/log/error.log basedir=/var/mysql/ [client] socket=/var/mysql/mysql.sock chown mysql.mysql /etc/my.cnf
创建日志文件
mkdir -p /var/mysql/log touch /var/mysql/log/error.log touch /var/mysql/mysql.sock chown -r mysql.mysql /var/mysql/
1.4初始化
#/var/mysql/bin/mysqld --initialize --user=mysql
查看数据文件是否生成
#ll /var/lib/mysql total 110628 -rw-r----- 1 mysql mysql 56 aug 10 11:06 auto.cnf -rw-r----- 1 mysql mysql 417 aug 10 11:06 ib_buffer_pool -rw-r----- 1 mysql mysql 12582912 aug 10 11:06 ibdata1 -rw-r----- 1 mysql mysql 50331648 aug 10 11:06 ib_logfile0 -rw-r----- 1 mysql mysql 50331648 aug 10 11:06 ib_logfile1 drwxr-x--- 2 mysql mysql 4096 aug 10 11:06 mysql drwxr-x--- 2 mysql mysql 8192 aug 10 11:06 performance_schema drwxr-x--- 2 mysql mysql 8192 aug 10 11:06 sys
查看日志文件是否生成
# ll /var/mysql/log
临时密码生成放在 error.log中
# cat /var/mysql/log/error.log 2019-11-18t09:06:22.922254z 0 [warning] timestamp with implicit default value is deprecated. please use --explicit_defaults_for_timestamp server option (see documentation for more details). 2019-11-18t09:06:24.600401z 0 [warning] innodb: new log files created, lsn=45790 2019-11-18t09:06:24.853258z 0 [warning] innodb: creating foreign key constraint system tables. 2019-11-18t09:06:24.915246z 0 [warning] no existing uuid has been found, so we assume that this is the first time that this server has been started. generating a new uuid: 5dbf9eac-9c4a-11e8-8768-000c29a48522. 2019-11-18t09:06:24.916709z 0 [warning] gtid table is not ready to be used. table 'mysql.gtid_executed' cannot be opened. 2019-11-18t09:06:24.917772z 1 [note] a temporary password is generated for root@localhost: ns0+>rzwvhwl
1.5启动
修改初始密码、修改环境变量
从模板文件中复制启动文件:
cp /var/mysql/support-files/mysql.server /etc/init.d/mysqld
修改启动文件:
vim /etc/init.d/mysqld
找到如下二行:
basedir=
datadir=
修改为:
basedir=/var/mysql/ datadir=/var/lib/mysql
更改权限
#chown -r mysql.mysql /var/mysql /var/lib/mysql
启动
/etc/init.d/mysqld start ln -s /var/mysql/bin/mysql /usr/bin
配置mysql环境变量:
vim /root/.bash_profile
找到下面一行:
path=$path:$home/bin
修改为:
path=$path:$home/bin:/var/mysql/bin
利用上面生成的临时密码,登录
# mysql –u root –p ns0+>rzwvhwl
welcome to the mysql monitor. commands end with ; or \g.
your mysql connection id is 6
server version: 5.7.23
copyright (c) 2000, 2018, 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>
更改初始密码
mysql> alter user 'root'@'localhost' identified by '123456';
二、安装zabbix server
2.1 安装php
安装php7.1版本,zabbix3.0版本要求php5.4及以上。
先查看本地php版本
php -v
检查当前php的安装包
yum list installed | grep php
将本地php安装包卸载干净
yum remove php*
默认的yum源无法升级php,需要添加第三方yum源,我们选择webtatic库
rpm -uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm rpm -uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
安装php7.1
yum install -y php71w
安装相关插件
yum install -y php71w-fpm php71w-opcache php71w-cli php71w-gd php71w-imap php71w-mysqlnd php71w-mbstring php71w-mcrypt php71w-pdo php71w-pecl-apcu php71w-pecl-mongodb php71w-pecl-redis php71w-pgsql php71w-xml php71w-xmlrpc php71w-devel mod_php71w php71w-ldap php71w-bcmath
修改php的配置参数
vim /etc/php.ini date.timezone= asia/shanghai max_execution_time = 300 post_max_size = 32m memory_limit = 128m mbstring.func_overload = 1 max_input_time = 300
session调整权限,
chmod -r 777 /var/lib/php/session/
2.2 安装zabbix
安装依赖包
yum -y install gcc* make curl curl-devel net-snmp net-snmp-devel perl-dbi httpd mariadb* mysql-devel libxml2-devel curl-devel unixodbc-devel net-snmp-devel openipmi-devel openldap openldap-devel libevent-develjava-devellibssh2-devel
创建zabbix用户和组
groupadd zabbix useradd -g zabbix zabbix
编译、安装
tar zxvf zabbix-3.4.12.tar.gz cd zabbix-3.4.12/ ./configure --prefix=/usr/local/zabbix --enable-server --enable-proxy --enable-agent --enable-ipv6 --with-mysql --with-net-snmp --with-libcurl --with-openipmi --with-unixodbc --with-ldap --with-ssh2 --enable-java make && make install chown -r zabbix.zabbix /usr/local/zabbix
修改zabbix配置文件
vi /usr/local/zabbix/etc/zabbix_server.conf logfile=/tmp/zabbix_server.log debuglevel=3 dbhost=192.168.75.251 #server和数据库分离的写法 dbname=zabbix dbuser=zabbix dbpassword=zabbix dbsocket=/var/mysql/mysql.sock dbport=3306 startpollers=10 timeout=4 alertscriptspath=/usr/local/zabbix/share/zabbix/alertscripts logslowqueries=3000 cachesize=1024m
后期配置文件的参数,根据需求做调整。
把zabbix程序包中的几个sql文件传到上面的mysql服务器,导入数据库文件
[root@zabbix ~]# cd /opt/soft/zabbix-3.4.12/database/mysql [root@zabbix mysql]# ll total 4876 -rw-r--r--. 1 1001 1001 2877497 jul 30 19:41 data.sql -rw-r--r--. 1 1001 1001 1978341 jul 30 19:41 images.sql -rw-r--r--. 1 1001 1001 134791 jul 30 19:41 schema.sql [root@zabbix mysql]# scp * root@192.168.75.251:/opt/
下面在mysql(192.168.75.251)服务器上操作
#mysql -u root –p
enter password:
创建zabbix数据库,字符集设置为utf8
mysql>create database zabbix character set utf8 collate utf8_bin;
在mysql数据库中创建zabbix用户,赋予zabbix用户对zabbix数据库的全部权限,允许zabbix服务器远程访问mysql
mysql>grant all privileges on zabbix.* to zabbix@192.168.75.250identified by 'zabbix'; mysql>grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; mysql>flush privileges; mysql>show databases ; +--------------------+ | database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | | zabbix | +--------------------+ 5 rows in set (0.00 sec)
导入数据文件,在控制台操作
#mysql -u zabbix -p zabbix <schema.sql #mysql -u zabbix -p zabbix <images.sql #mysql -u zabbix -p zabbix <data.sql
2.3zabbix web部署
回到zabbix server主机操作。在上面已经安装好httpd , 创建apache下创建zabbix目录
mkdir /var/www/html/zabbix
拷贝zabbix包中的web界面到apache zabbix目录下
\cp–rf /opt/soft/zabbix-3.4.12/frontends/php/* /var/www/html/zabbix/
配置web安全
chown -r apache.apache /var/www/html/zabbix chmod a+x /var/www/html/zabbix/conf/
拷贝zabbix包中服务启动脚本(server/agentd)到linux系统服务启动目录
cp /opt/soft/zabbix-3.4.12/misc/init.d/fedora/core/zabbix_server zabbix_agentd/etc/init.d/
修改/etc/init.d/zabbix_server和/etc/init.d/zabbix_agentd
basedir=/usr/local修改成 basedir=/usr/local/zabbix/
设置开机启动
systemctl enable httpd systemctl enable zabbix_server systemctl enable zabbix_agentd /etc/init.d/zabbix_server start /etc/init.d/zabbix_agentd start systemctl start httpd
2.4web服务初始化
http://192.168.75.250/zabbix/setup.php
连接数据库设置,写mysql服务器所在机器,端口,数据库名称,用户、密码
三、安装zabbix proxy
首先,在proxy机器上安装mysql ,跟上面的安装步骤一样,只是在导入mysql文件时,只导入一个文件,
#mysql–uzabbix –p zabbix <schema.sql
安装依赖包
yum -y install gcc* make curl curl-devel net-snmp net-snmp-devel perl-dbi httpd mariadb* mysql-devel libxml2-devel curl-devel unixodbc-devel net-snmp-devel openipmi-devel openldap openldap-devel libevent-develjava-devellibssh2-devel
创建zabbix用户和组
groupadd zabbix useradd -g zabbix zabbix
编译、安装
tar zxvf zabbix-3.4.12.tar.gz cd zabbix-3.4.12/ ./configure --prefix=/opt/server/zabbix_proxy--enable-proxy --enable-agent --enable-ipv6 --with-mysql --with-net-snmp --with-libcurl --with-openipmi --with-unixodbc --with-ldap --with-ssh2 --enable-java make && make install chown -r zabbix.zabbix /opt/server/zabbix_proxy
配置文件修改
vi /usr/local/zabbix/etc/zabbix_proxy.conf server=192.168.75.250 serverport=10051 hostname=zabbix_proxy logfile=/tmp/zabbix_proxy.log dbhost=localhost#因为mysql和proxy安装在同一台机器 dbname=zabbix dbuser=zabbix dbpassword=zabbix dbsocket=/var/mysql/mysql.sock configfrequency=60 datasenderfrequency=60 startpollers=5 timeout=4 logslowqueries=3000
启动proxy(要加入到开机自启)
#/opt/server/zabbix_proxy/sbin/zabbix_proxy-c /opt/server/zabbix_proxy/etc/zabbix_proxy.conf systemctl enable zabbix_proxy
四、启动zabbix_agentd客户端
同上,编译安装zabbix时,客户端也一起编译了,把/usr/local 下面的zabbix目录拷贝到客户端,
修改配置文件
vi /usr/local/zabbix/etc/zabbix_agentd.conf server=zabbix_proxy_ip #建议所以客户端都挂在proxy上,扩展性强 hostname=zabbix_agentd_hostname #填写客户端的主机名
启动(要加入到开机自启)
/usr/local/zabbix/sbin/zabbix_agentd-c /usr/local/zabbix/etc/zabbix_agentd.conf echo "/usr/local/agent/sbin/zabbix_agentd -c /usr/local/agent/etc/zabbix_agentd.conf" >> /etc/rc.local
五、web管理
添加监控主机
选择主机监控模版
主机监控成功
总结
以上所述是小编给大家介绍的zabbix安装部署实践,希望对大家有所帮助