基于Linux+zabbix等构建分布式运维监控系统
前言:
zabbix是一个基于WEB界面的提供分布式系统监视以及网络监视功能的企业级的开源解决方案
zabbix能监视各种网络参数,保证服务器系统的安全运营;并提供灵活的通知机制以让系统管理员快速定位/解决存在的各种问题。
zabbix特点:
- 安装与配置简单,学习成本低
- 支持多语言(包括中文)
- 免费开源
- 自动发现服务器与网络设备
- 分布式监视以及WEB集中管理功能
- 可以无agent监视
- 用户安全认证和柔软的授权方式
- 通过WEB界面设置或查看监视结果
- email等通知功能
zabbix主要功能:
- CPU负荷
- 内存使用
- 磁盘使用
- 网络状况
- 端口监视
- 日志监视
- 插件开发自定义
一、基础环境
1.yum源:
webtatic源:
rpm -Uvh http://mirror.webtatic.com/yum/el6/latest.rpm #php/mysql
mysql源:
yum -y localinstall https://repo.mysql.com//mysql57-community-release-el6-11.noarch.rpm #修改mysql源为5.7关闭,5.6 开启
zabbix.repo
[zabbix] name=CentOS-$releasever - zabbix failovermethod=priority baseurl=https://mirrors.aliyun.com/zabbix/zabbix/3.2/rhel/6/x86_64/ enable=1 gpgcheck=0 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-79EA5ED4
备注:由于zabbix3.26配置smtp ssl/tls要求libcurl 版本>7.20,所以需要执行下面的操作
#安装libcurl最新版本源 rpm -Uvh http://www.city-fan.org/ftp/contrib/yum-repo/rhel6/x86_64/city-fan.org-release-1-13.rhel6.noarch.rpm #安装libnghttp2 最新版本源(依赖包) rpm -Uvh http://dl.fedoraproject.org/pub/epel/6/x86_64/libnghttp2-1.6.0-1.el6.1.x86_64.rpm
2.所需环境安装:
yum install -y gcc gcc-c++ openssl* pcre pcre-devel gd gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel libcurl autoconf automake imake expat-devel cmake libaio libaio-devel bzr bison libtool ncurses ncurses-devel net-snmp net-snmp-devel curl-devel
3.安装LAMP环境
yum -y install httpd httpd-tools httpd-devel httpd-itk httpd-manual gd fontconfig-devel libjpeg-devel libpng-devel gd-devel perl-GD openssl-devel perl-DBD-MySQL mysql-community-server mysql-community-devel php71w php71w-devel php71w-mysql php71w-gd php71w-ldap php71w-xml php71w-mbstring php71w-bcmath.x86_64
4.php.ini配置(/etc/php.ini)
post_max_size = 16M max_execution_time = 300 max_input_time = 300 date.timezone = Asia/Shanghai
二、zabbix-server端部署:
1.下载源码:
cd /usr/local/src && wget https://ncu.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.6/zabbix-3.2.6.tar.gz
2.创建用户及用户组
groupadd -g 9002 zabbix && useradd -u 9002 -g zabbix -d /home/zabbix -c "zabbix Admin" zabbix && usermod -G zabbix apache #执行上面的语句限于在Centos6,验证httpd运行用户:grep "^User" /etc/httpd/conf/httpd.conf
3.编译安装zabbix
cd /usr/local/src && wget https://ncu.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.6/zabbix-3.2.6.tar.gz cd /usr/local/src && tar xf zabbix-3.2.6.tar.gz && cd zabbix-3.2.6 ./configure --prefix=/usr/local/zabbix/ --enable-server --enable-proxy --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-snmp --with-mbstring && make && make install
4.安装web页面
复制php页面到指定位置
/usr/local/src/zabbix-3.2.6 && cp -rp frontends/php /usr/local/zabbix/www && cp /usr/local/zabbix/www/conf/zabbix.conf.php.example /usr/local/zabbix/www/conf/zabbix.conf.php
vim /usr/local/zabbix/www/include/classes/setup/CFrontendSetup.php
添加$current = -1; public function checkPhpAlwaysPopulateRawPostData() { $current = ini_get('always_populate_raw_post_data'); $current = -1; return [ 'name' => _s('PHP option "%1$s"', 'always_populate_raw_post_data'), 'current' => ($current != -1) ? _('on') : _('off'), 'required' => _('off'), 'result' => ($current != -1) ? self::CHECK_FATAL : self::CHECK_OK, 'error' => _s('PHP option "%1$s" must be set to "%2$s"', 'always_populate_raw_post_data', -1) ]; }
配置中文字体
复制C:\Windows\Fonts(楷体)到/usr/local/zabbix/www/fonts sed -i 's/DejaVuSans/simkai/g' /usr/local/zabbix/www/include/defines.inc.php
数据库初始化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
[client] default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] explicit_defaults_for_timestamp join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M datadir=/home/mysql/data #设置mysql数据存储路径 port=33070 #设置mysql端口为33070 skip-name-resolve #设置跳过域名检查 character-set-server=utf8 collation-server=utf8_general_ci socket = /var/lib/mysql/mysql.sock max_connections=1000 #设置最大连接数为1000 max_user_connections=500 #设置每用户最大连接数为500 wait_timeout=200 #设置200秒后关闭空闲(IDLE)的连接,但是对正在工作的连接不影响 symbolic-links=0 innodb_file_per_table=1 #mater server-id = 1 log-bin=mysqlmaster-bin binlog-do-db = zabbix#指定需要进行主从的数据库 binlog_cache_size = 4M binlog_format=mixed expire_logs_days=3 binlog-ignore-db=test #不记录test库的binlog replicate-ignore-db=test #不复制test库的binlog sync_binlog=1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
#启动mysqld服务 service mysqld start #设置mysql创建数据库zabbix并授权zabbix用户访问 mysql>create database zabbix default charset utf8; mysql>grant all on zabbix.* to zabbix@'%' Identified by "zabbix"; mysql>grant all on zabbix.* to zabbix@'localhost' Identified by "zabbix"; mysql>flush privileges; mysql>exit #导入数据表 注意:这里root用户位置密码 mysql zabbix < /usr/local/src/zabbix-3.2.6/database/mysql/schema.sql mysql zabbix < /usr/local/src/zabbix-3.2.6/database/mysql/images.sql mysql zabbix < /usr/local/src/zabbix-3.2.6/database/mysql/data.sql
修改zabbix_server.conf
DBUser=zabbix DBPassword=zabbix DBPort=33070
zabbix_agentd.conf文件默认就好,在agent端涉及到修改。
配置apache访问
Alias /zabbix "/usr/local/zabbix/www" <Directory "/usr/local/zabbix/www"> # SSLRequireSSL Options None AllowOverride None # Order allow,deny # Allow from all Order deny,allow Deny from all Allow from 127.0.0.1 Allow from 10.124.151.0/24 AuthName "zabbix Access" AuthType Basic AuthUserFile /usr/local/zabbix/etc/htpasswd.users Require valid-user </Directory>
重启httpd之后浏览器访问http://IP/zabbix进行配置
出现这些错误需要一项项的修改需要配置PHP.ini。已将该步骤提前至第一步基础环境配置。
如果出现这个问题,按照提示进行操作。为了方便,这里已经将该问题出现所属的文件在“web安装”步骤的最开始一步“复制php文件到指定路径”执行。(cp /usr/local/zabbix/www/conf/zabbix.conf.php.example /usr/local/zabbix/www/conf/zabbix.conf.php)
最后登录控制台:admin:zabbix
在首页发现下面报错:
A non well formed numeric value encountered [zabbix.php:21 → require_once() → ZBase->run() → ZBase->processRequest() → CView->getOutput() → include() → make_status_of_zbx() → CFrontendSetup->checkRequirements() → CFrontendSetup->checkPhpMemoryLimit() → str2mem() in include/func.inc.php:410] A non well formed numeric value encountered [zabbix.php:21 → require_once() → ZBase->run() → ZBase->processRequest() → CView->getOutput() → include() → make_status_of_zbx() → CFrontendSetup->checkRequirements() → CFrontendSetup->checkPhpPostMaxSize() → str2mem() in include/func.inc.php:410] A non well formed numeric value encountered [zabbix.php:21 → require_once() → ZBase->run() → ZBase->processRequest() → CView->getOutput() → include() → make_status_of_zbx() → CFrontendSetup->checkRequirements() → CFrontendSetup->checkPhpUploadMaxFilesize() → str2mem() in include/func.inc.php:410]
解决办法:(目前已知道的php7.1安装3.x版本的出现这个错误:参考:http://www.linuxidc.com/Linux/2017-02/140146.htm)
sed -i '/$last = strtolower(substr($val, -1));/a$val = substr($val,0,-1);' /home/www/zabbix/include/func.inc.php
5.配置启动脚本
/usr/local/src/zabbix-3.2.6 && cp -f misc/init.d/fedora/core5/zabbix_* /etc/init.d/ sed -i 's/ZABBIX_BIN=\"\/usr\/local\//&zabbix\//' /etc/init.d/zabbix_server sed -i 's/ZABBIX_BIN=\"\/usr\/local\//&zabbix\//' /etc/init.d/zabbix_agentd
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # # /etc/rc.d/init.d/zabbix_server # # Starts the zabbix_server daemon # # chkconfig: - 95 5 # description: Zabbix Monitoring Server # processname: zabbix_server # pidfile: /tmp/zabbix_server.pid # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # Source function library. . /etc/init.d/functions RETVAL=0 prog="Zabbix Server" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_server" if [ ! -x ${ZABBIX_BIN} ] ; then echo -n "${ZABBIX_BIN} not installed! " # Tell the user this has skipped exit 5 fi start() { echo -n $"Starting $prog: " daemon $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zabbix_server echo } stop() { echo -n $"Stopping $prog: " killproc $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zabbix_server echo } case "$1" in start) start ;; stop) stop ;; reload|restart) stop sleep 10 start RETVAL=$? ;; condrestart) if [ -f /var/lock/subsys/zabbix_server ]; then stop start fi ;; status) status $ZABBIX_BIN RETVAL=$? ;; *) echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}" exit 1 esac exit $RETVAL
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # # /etc/rc.d/init.d/zabbix_agentd # # Starts the zabbix_agentd daemon # # chkconfig: - 95 5 # description: Zabbix Monitoring Agent # processname: zabbix_agentd # pidfile: /tmp/zabbix_agentd.pid # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # Source function library. . /etc/init.d/functions RETVAL=0 prog="Zabbix Agent" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" if [ ! -x ${ZABBIX_BIN} ] ; then echo -n "${ZABBIX_BIN} not installed! " # Tell the user this has skipped exit 5 fi start() { echo -n $"Starting $prog: " daemon $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zabbix_agentd echo } stop() { echo -n $"Stopping $prog: " killproc $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zabbix_agentd echo } case "$1" in start) start ;; stop) stop ;; reload|restart) stop sleep 10 start RETVAL=$? ;; condrestart) if [ -f /var/lock/subsys/zabbix_agentd ]; then stop start fi ;; status) status $ZABBIX_BIN RETVAL=$? ;; *) echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}" exit 1 esac exit $RETVAL
chmod +x /etc/init.d/zabbix_server && chmod +x /etc/init.d/zabbix_agentd && chkconfig zabbix_server on && chkconfig zabbix_agentd on
6.启动服务
service zabbix_server start service zabbix_agentd start
三、zabbix-proxy端部署:
1.所需环境安装:
yum install -y gcc gcc-c++ openssl* pcre pcre-devel gd gd-devel libjpeg-devel libpng-devel libxml2-devel bzip2-devel libcurl-devel autoconf automake imake expat-devel cmake libaio libaio-devel bzr bison libtool ncurses ncurses-devel net-snmp net-snmp-devel curl-devel
2.下载源码:
cd /usr/local/src && wget https://ncu.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.6/zabbix-3.2.6.tar.gz
3.创建用户及用户组
groupadd -g 9002 zabbix && useradd -u 9002 -r -g zabbix -s /etc/nologin zabbix
4.编译安装zabbix
cd /usr/local/src && tar xf zabbix-3.2.6.tar.gz && cd zabbix-3.2.6 ./configure --prefix=/usr/local/zabbix/ --enable-proxy --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --enable-snmp --with-mbstring && make && make install
5.安装和配置mysql
yum -y localinstall https://repo.mysql.com//mysql57-community-release-el6-11.noarch.rpm #修改mysql源为5.7关闭,5.6 开启 yum -y install fontconfig-devel libjpeg-devel libpng-devel gd-devel perl-GD openssl-devel perl-DBD-MySQL mysql-community-server mysql-community-devel php71w php71w-devel php71w-mysql php71w-gd php71w-ldap php71w-xml php71w-mbstring php71w-bcmath.x86_64
配置my.cnf
[client] default-character-set=utf8 socket=/var/lib/mysql/mysql.sock [mysqld] explicit_defaults_for_timestamp join_buffer_size = 128M sort_buffer_size = 2M read_rnd_buffer_size = 2M datadir=/home/mysql/data #设置mysql数据存储路径 port=33070 #设置mysql端口为33070 skip-name-resolve #设置跳过域名检查 character-set-server=utf8 collation-server=utf8_general_ci socket = /var/lib/mysql/mysql.sock max_connections=1000 #设置最大连接数为1000 max_user_connections=500 #设置每用户最大连接数为500 wait_timeout=200 #设置200秒后关闭空闲(IDLE)的连接,但是对正在工作的连接不影响 symbolic-links=0 innodb_file_per_table=1 #mater server-id = 1 log-bin=mysqlmaster-bin binlog-do-db = zabbix#指定需要进行主从的数据库 binlog_cache_size = 4M binlog_format=mixed expire_logs_days=3 binlog-ignore-db=test #不记录test库的binlog replicate-ignore-db=test #不复制test库的binlog sync_binlog=1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid
启动mysql并操作
#启动mysqld服务 service mysqld start #设置mysql创建数据库zabbix并授权zabbix用户访问 mysql>create database zabbix_proxy default charset utf8; mysql>grant all on zabbix_proxy.* to zabbix@'localhost' Identified by "zabbix"; mysql>flush privileges; mysql>exit #导入数据表 注意:这里root用户位置密码 mysql zabbix_proxy < /usr/local/src/zabbix-3.2.6/database/mysql/schema.sql #代理只需要配置这一个表
5.配置启动脚本
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/sh # chkconfig: 345 95 95 # desctription: Zabbix Proxy # Zabbix # Copyright (C) 2001-2013 Zabbix SIA # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. # Start/Stop the Zabbix agent daemon. # Place a startup script in /sbin/init.d, and link to it from /sbin/rc[023].d SERVICE="Zabbix proxy" PIDFILE=/tmp/zabbix_proxy.pid BASEDIR=/usr/local/zabbix3.0.4 DAEMON=$BASEDIR/sbin/zabbix_proxy CONFIGFILE=$BASEDIR/etc/zabbix_proxy.conf case $1 in 'start') if [ -x ${DAEMON} ] then $DAEMON -c $CONFIGFILE # Error checking here would be good... echo "${SERVICE} started." else echo "Can't find file ${DAEMON}." echo "${SERVICE} NOT started." fi ;; 'stop') if [ -s ${PIDFILE} ] then if kill `cat ${PIDFILE}` >/dev/null 2>&1 then echo "${SERVICE} terminated." rm -f ${PIDFILE} fi else echo "${PIDFILE} is not found." echo "${SERVICE} NOT stopd." fi ;; 'restart') $0 stop sleep 10 $0 start ;; *) echo "Usage: $0 start|stop|restart" ;; esac
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # # /etc/rc.d/init.d/zabbix_agentd # # Starts the zabbix_agentd daemon # # chkconfig: - 95 5 # description: Zabbix Monitoring Agent # processname: zabbix_agentd # pidfile: /tmp/zabbix_agentd.pid # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # Source function library. . /etc/init.d/functions RETVAL=0 prog="Zabbix Agent" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" if [ ! -x ${ZABBIX_BIN} ] ; then echo -n "${ZABBIX_BIN} not installed! " # Tell the user this has skipped exit 5 fi start() { echo -n $"Starting $prog: " daemon $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zabbix_agentd echo } stop() { echo -n $"Stopping $prog: " killproc $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zabbix_agentd echo } case "$1" in start) start ;; stop) stop ;; reload|restart) stop sleep 10 start RETVAL=$? ;; condrestart) if [ -f /var/lock/subsys/zabbix_agentd ]; then stop start fi ;; status) status $ZABBIX_BIN RETVAL=$? ;; *) echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}" exit 1 esac exit $RETVAL
chmod +x /etc/init.d/zabbix_proxy && chmod +x /etc/init.d/zabbix_agentd && chkconfig zabbix_proxy on && chkconfig zabbix_agentd on
6.配置/usr/local/zabbix/etc/zabbix_proxy.conf配置文件
Server=X.X.X.X(zabbix_server IP) ProxyMode=0 DBname=zabbix_proxy DBUser=zabbix DBPassword=zabbix DBPort=33070
7.启动服务
service zabbix_proxy start
service zabbix_agentd start
四、zabbix-agentd端部署:
1.源码部署:
cd /usr/local/src && wget https://ncu.dl.sourceforge.net/project/zabbix/ZABBIX%20Latest%20Stable/3.2.6/zabbix-3.2.6.tar.gz cd /usr/local/src && tar xf zabbix-3.2.6.tar.gz && cd zabbix-3.2.6 ./configure --prefix=/usr/local/zabbix/ --enable-agent && make && make install
配置/usr/local/zabbix/etc/zabbix_agentd.conf
Server=x.x.x.x (zabbix_server|zabbix_proxy IP) (被动) ServerActive=x.x.x.x (zabbix_server|zabbix_proxy IP) (主动) Hostname=agent-1 (独立主机名)
启动脚本:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#!/bin/bash # # /etc/rc.d/init.d/zabbix_agentd # # Starts the zabbix_agentd daemon # # chkconfig: - 95 5 # description: Zabbix Monitoring Agent # processname: zabbix_agentd # pidfile: /tmp/zabbix_agentd.pid # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # Source function library. . /etc/init.d/functions RETVAL=0 prog="Zabbix Agent" ZABBIX_BIN="/usr/local/zabbix/sbin/zabbix_agentd" if [ ! -x ${ZABBIX_BIN} ] ; then echo -n "${ZABBIX_BIN} not installed! " # Tell the user this has skipped exit 5 fi start() { echo -n $"Starting $prog: " daemon $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && touch /var/lock/subsys/zabbix_agentd echo } stop() { echo -n $"Stopping $prog: " killproc $ZABBIX_BIN RETVAL=$? [ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/zabbix_agentd echo } case "$1" in start) start ;; stop) stop ;; reload|restart) stop sleep 10 start RETVAL=$? ;; condrestart) if [ -f /var/lock/subsys/zabbix_agentd ]; then stop start fi ;; status) status $ZABBIX_BIN RETVAL=$? ;; *) echo $"Usage: $0 {condrestart|start|stop|restart|reload|status}" exit 1 esac exit $RETVAL
启动&&开机自启
service zabbix_agentd start && ckconfig zabbix_agentd on
2.yum安装:
安装zabbix.repo
[zabbix] name=CentOS-$releasever - zabbix failovermethod=priority baseurl=https://mirrors.aliyun.com/zabbix/zabbix/3.2/rhel/6/x86_64/ enable=1 gpgcheck=0 gpgkey=https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-79EA5ED4
yum安装
yum install zabbix-agent.x86_64 -y
配置/etc/zabbix/zabbix_agentd.conf
Server=x.x.x.x (zabbix_server|zabbix_proxy IP) (被动) ServerActive=x.x.x.x (zabbix_server|zabbix_proxy IP) (主动) Hostname=agent-1 (独立主机名)
启动&&开机自启
service zabbix_agent start && ckconfig zabbix_agent on
四、配置与使用