一、zabbix说明
zabbix监控系统是c/s或c-proxy-s架构,通过agent(广义)从各监控端主动或被获取监控数据并存储下来,通过web GUI进行展示,通过定义的触发条件与报警系统进行事件的报警;
1、zabbix架构:
2、zabbix逻辑组件:
主机组(host groups)
主机(hosts)
应用(applications)
监控项(items)
触发器(triggers)
事件(events)
动作(actions):条件(conditions)和操作(operations)
媒介(media):发送通知通道;
通知(notifications)
远程命令(remote command)
报警升级(escalation)
模板(template)
图形(graph)
屏幕(screens)
幻灯(side show)
3、部署环境说明
系统环境CentOS7.2_x64 yum安装数据库:mariadb 5.5 yum安装 zabbix 3.2.8
编译安装nginx 1.2.1(最新稳定版) 编译安装 php 5.6.31最新稳定版。测试的机器ip:
172.16.3.152
关闭selinux
防火墙添加好规则,tcp/10051(进出)tcp/10050(出)或为了便于测试直接关闭。
约定说明
以下实践是本人整理基于以上提到的环境,不完全保证没有遗漏之处,如有错误请留言,
涉及到/etc/init.d/中的脚本请在复制后 chmod +x添加可执行权限。nginx,php-fpm等参数
在本次测试中除了非必要没有做性能调优,如生产线上使用,勿必修改对应的参数,提高性能.
/charts.php?fullscreen=0&groupid=0&hostid=10295&graphid=954
二、安装nginx
1、编译安装nginx
#yum install vim gcc wget unzip pcre-devel openssl-devel gcc-c++ pcre-dev zlib-devel -y #wget http://101.96.10.63/nginx.org/download/nginx-1.12.1.tar.gz (不要问我这个链接怎么是ip,官方就是这样的)
##添加系统nginx用户
#groupadd -g 108 -r nginx #useradd -u 108 -r -g 108 nginx #解压并编译安装
#tar xvf nginx-1.12.1.tar.gz cd nginx-1.12.1 ./configure --prefix=/usr \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --pid-path=/var/run/nginx/nginx.pid \ --lock-path=/var/lock/nginx.lock \ --user=nginx \ --group=nginx \ --with-http_ssl_module \ --with-http_flv_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-pcre \ #make && make install #编译并安装nginx
2、添加系统服务(sysv)
cat /etc/init.d/nginx
#!/bin/sh # nginx - this script starts and stops the nginx daemon # chkconfig: - 85 15 # description: Nginx is an HTTP(S) server, HTTP(S) reverse \ # proxy and IMAP/POP3 proxy server # processname: nginx # config: /etc/nginx/nginx.conf # config: /etc/sysconfig/nginx # pidfile: /var/run/nginx.pid # Source function library. . /etc/rc.d/init.d/functions # Source networking configuration. . /etc/sysconfig/network # Check that networking is up. [ "$NETWORKING" = "no" ] && exit 0 nginx="/usr/sbin/nginx" prog=$(basename $nginx) NGINX_CONF_FILE="/etc/nginx/nginx.conf" [ -f /etc/sysconfig/nginx ] && . /etc/sysconfig/nginx lockfile=/var/lock/subsys/nginx make_dirs() { # make required directories user=`nginx -V 2>&1 | grep "configure arguments:" | sed 's/[^*]*--user=\([^ ]*\).*/\1/g' -` options=`$nginx -V 2>&1 | grep 'configure arguments:'` for opt in $options; do if [ `echo $opt | grep '.*-temp-path'` ]; then value=`echo $opt | cut -d "=" -f 2` if [ ! -d "$value" ]; then mkdir -p $value && chown -R $user $value fi fi done } start() { [ -x $nginx ] || exit 5 [ -f $NGINX_CONF_FILE ] || exit 6 make_dirs echo -n $"Starting $prog: " daemon $nginx -c $NGINX_CONF_FILE retval=$? echo [ $retval -eq 0 ] && touch $lockfile return $retval } stop() { echo -n $"Stopping $prog: " killproc $prog -QUIT retval=$? echo [ $retval -eq 0 ] && rm -f $lockfile return $retval } restart() { configtest || return $? stop sleep 1 start } reload() { configtest || return $? echo -n $"Reloading $prog: " killproc $nginx -HUP RETVAL=$? echo } force_reload() { restart } configtest() { $nginx -t -c $NGINX_CONF_FILE } rh_status() { status $prog } rh_status_q() { rh_status >/dev/null 2>&1 } case "$1" in start) rh_status_q && exit 0 $1 ;; stop) rh_status_q || exit 0 $1 ;; restart|configtest) $1 ;; reload) rh_status_q || exit 7 $1 ;; force-reload) force_reload ;; status) rh_status ;; condrestart|try-restart) rh_status_q || exit 0 ;; *) echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload|configtest}" exit 2 esac
3、添加开机启动及加载到 systemd管理
#chkconfig --add nginx #chkconfig nginx on #systemctl daemon-reload
4、配置及启动nginx
cd /etc/nginx
cp nginx.conf nginxconf_def
cat nginx.conf
user nginx; worker_processes 2; error_log /data1/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /data1/log/nginx/access.log main; server_tokens off; sendfile on; tcp_nopush on; client_max_body_size 5m; keepalive_timeout 60; gzip on; include /etc/nginx/conf.d/*.conf; }
#mkdir -pv /etc/nginx/conf.d
#cat conf.d/test.conf
server { listen 80; server_name _; access_log /data1/log/nginx/test.access.log main; index index.php index.html index.html; root /data1/site/test; location / { try_files $uri $uri/ /index.php?$args; } location ~ .*\.(php)?$ { expires -1s; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } }
##创建对应目录及授权
#mkdir -pv /data1/log/nginx #mkdir -pv /data1/site/test #echo "hello" >/data1/site/test/index.html #chown nginx.nginx /data1/ -R #systemctl start nginx
测试访问http://172.16.3.152 出现hello页面表示nginx配置成功。
三、安装maridb
#yum -y install mariadb-server mariadb-devel #systemctl enable mariadb #systemctl start mariadb
添加zabbix数据库及zabbix用户名密码
#mysql -uroot -p
>create database zabbix character set utf8 collate utf8_bin;
>grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by '123456';
#mysqladmin -u root password "redhat" #设置数据的root密码(测试时非必要) 数据库的设置先到这里,到zabbix时再导入结构初始化表。
四、编译安装php5.6.31
1、安装依赖组件
#yum install -y gcc gcc-c++ openssl openssl-devel libxml2 libxml2-devel autoconf libjpeg libjpeg-devel libpng libpng-devel gd bzip2 bzip2-devel curl curl-devel freetype freetype-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel
2、下载php源码及编译安装
#wget http://cn2.php.net/distributions/php-5.6.31.tar.bz2 #tar xvf php-5.6.31.tar.gz #cd php-5.6.31 #./configure --prefix=/usr/local/php5631 \ --with-config-file-path=/usr/local/php5631/etc \ --with-bz2 \ --with-curl \ --enable-ftp \ --enable-sockets \ --disable-ipv6 \ --with-gd \ --with-jpeg-dir=/usr/local \ --with-png-dir=/usr/local \ --with-freetype-dir=/usr/local \ --enable-gd-native-ttf \ --with-iconv-dir=/usr/local \ --enable-mbstring \ --enable-calendar \ --with-gettext \ --with-libxml-dir=/usr/local \ --with-zlib \ --with-pdo-mysql=mysqlnd \ --with-mysqli=mysqlnd \ --with-mysql=mysqlnd \ --enable-dom \ --enable-xml \ --enable-fpm \ --with-libdir=lib64 \ --enable-bcmath
#make && make install #编译并安装php
2、添加环境变量
#cat /etc/profile.d/php.sh
export PATH=/usr/local/php5631/bin:$PATH
3、添加php-fpm系统服务
#cat /etc/init.d/php-fpm
#! /bin/sh # chkconfig: - 85 25 ### BEGIN INIT INFO # Provides: php-fpm # Required-Start: $remote_fs $network # Required-Stop: $remote_fs $network # Short-Description: starts php-fpm # Description: starts the PHP FastCGI Process Manager daemon ### END INIT INFO prefix=/usr/local/php5631 exec_prefix=${prefix} php_fpm_BIN=${exec_prefix}/sbin/php-fpm php_fpm_CONF=${prefix}/etc/php-fpm.conf php_fpm_PID=${prefix}/var/run/php-fpm.pid php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID" wait_for_pid () { try=0 while test $try -lt 35 ; do case "$1" in 'created') if [ -f "$2" ] ; then try='' break fi ;; 'removed') if [ ! -f "$2" ] ; then try='' break fi ;; esac echo -n . try=`expr $try + 1` sleep 1 done } case "$1" in start) echo -n "Starting php-fpm " $php_fpm_BIN --daemonize $php_opts if [ "$?" != 0 ] ; then echo " failed" exit 1 fi wait_for_pid created $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; stop) echo -n "Gracefully shutting down php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -QUIT `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed. Use force-quit" exit 1 else echo " done" fi ;; status) if [ ! -r $php_fpm_PID ] ; then echo "php-fpm is stopped" exit 0 fi PID=`cat $php_fpm_PID` if ps -p $PID | grep -q $PID; then echo "php-fpm (pid $PID) is running..." fi ;; force-quit) echo -n "Terminating php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -TERM `cat $php_fpm_PID` wait_for_pid removed $php_fpm_PID if [ -n "$try" ] ; then echo " failed" exit 1 else echo " done" fi ;; restart) $0 stop $0 start ;; reload) echo -n "Reload service php-fpm " if [ ! -r $php_fpm_PID ] ; then echo "warning, no pid file found - php-fpm is not running ?" exit 1 fi kill -USR2 `cat $php_fpm_PID` echo " done" ;; *) echo "Usage: $0 {start|stop|force-quit|restart|reload|status}" exit 1 ;; esac
#添加开机启动重新加载systemd
#chkconfig --add php-fpm #chkconfig php-fpm on #systemctl daemon-reload
4、修改php-fpm.conf并启动php-fpm
#cd /usr/local/php5631/etc #cp php-fpm.conf.default php-fpm.conf #egrep -v '(^$|^;)' php-fpm.conf
user = nginx group = nginx listen = 127.0.0.1:9000 pm = dynamic pm.max_children = 5 pm.start_servers = 2 pm.min_spare_servers = 1 pm.max_spare_servers = 3
#systemctl start php-fpm
五、安装zabbix 3.2
1、安装zabbix官方yum库
#rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-release-3.2-1.el7.noarch.rpm
2、安装zabbix server agent及mysql插件
#yum install zabbix-get zabbix-sender zabbix-server-mysql zabbix-web zabbix-agent
3、配置zabbix_server.conf
egrep -v '(^$|^#)' /etc/zabbix/zabbix_server.conf
LogFile=/var/log/zabbix/zabbix_server.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_server.pid DBHost=localhost DBName=zabbix DBUser=zabbix DBPassword=123456 DBSocket=/var/lib/mysql/mysql.sock DBPort=3306 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 AlertScriptsPath=/usr/lib/zabbix/alertscripts ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000
4、初始化导入zabbix的表结构
#cd /usr/share/doc/zabbix-server-mysql-3.2.8/ #gunzip create.sql.gz #解压 #mysql -uzabbix -p zabbix < create.sql #导入(也可以登录zabbix,source create.sql)
至此数据库部分设置完毕
5、修改/etc/zabbix/web目录权限
#cd /etc/zabbix #chown zabbix.nginx web -R #chmod 775 web -R
注:此目录没有权限,后面web安装配置zabbix时提示没有权限类似如下信息:
Warning: require_once(/etc/zabbix/web/maintenance.inc.php): failed to open stream: Permission denied in /usr/local/nginx/html/zabbix/include/classes/core/ZBase.php on line 269 Fatal error: require_once(): Failed opening required '/etc/zabbix/web/maintenance.inc.php' (include_path='.:/usr/local/php/lib/php') in/usr/local/nginx/html/zabbix/include/classes/core/ZBase.php on line 269
6、配置zabbix-server,zabbix-agent服务并启动
#systemctl enable zabbix-server #systemctl start zabbix-server #systemctl enable zabbix-agent #systemctl start zabbix-agent
六、zabbix web nginx配置
1、创建zabbix web目录并复制内容
#mkdir /data1/site/zabbix -pv #chown nginx.nginx /data1/site/zabbix -R #cp -rf /usr/share/zabbix/* /data1/site/zabbix
2、创建zabbix.conf配置文件
#cd /etc/nginx/conf.d/ #egrep -v '(^$|^#)' zabbix.conf
server { listen 80; server_name zabbix.pkey.cn; #这个是测试域名在本地机器上添加 hosts文件,也可用ip access_log /data1/log/nginx/zabbix.access.log main; index index.php index.html index.html; root /data1/site/zabbix; location / { try_files $uri $uri/ /index.php?$args; } location ~ .*\.(php)?$ { expires -1s; try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; include fastcgi_params; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass 127.0.0.1:9000; } }
3、重启nginx
#systemctl restart nginx
七、web配置安装zabbix
如图:
点 “next step” 如图:
注意:此时如果有fail项请按上面的提示找到php.ini文件对应着修改,
获取php.ini文件位置
#php -i |grep php.ini
通常需要修改如下项:
max_execution_time = 300 memory_limit = 128M post_max_size = 16M upload_max_filesize = 2M max_input_time = 300 always_populate_raw_post_data = -1 date.timezone = Asia/Shanghai
直到如上所有的检测项均ok时再点next setp
出现如下图:
输入zabbix数据库的zabbix用户密码 ,如提示找不到文件夹之类的提示,Database host可以填写127.0.0.1,netxt setp 如下图:
next step
此此zabbix web安装配置完成
出现如下图:
默认的登录用户名密码:Admin/zabbix
登录进去默认是英文如图:
修改为中文界面:
从windows 系统复制 c:/windows/fontes/simkai.ttf 字体上传到
/data1/site/zabbix/fonts/中
并修改配置文件
#cd /data1/site/zabbix/fonts
#sed -i 's/DejaVuSans/simkai/g' ../include/defines.inc.php
再在用户设置中修改为中文字体如图:
更新后,zabbixweb页默认字体就是中文且不乱码,如图:
至此一个基于CentOS7 LNMP环境 zabbix3.2.8 中文界面配置完成。
安装zabbix-server后启动过程中出现的一个错误提示:
症状:zabbix-server服务是启动的,就是不侦听端口,日志显示如下:
错误日志/var/log/zabbix/zabbix_server.log描述:
2597:20171009:103156.940 database is down: reconnecting in 10 seconds
2597:20171009:103159.761 Got signal [signal:15(SIGTERM),sender_pid:2646,sender_uid:0,reason:0]. Exiting ...
2597:20171009:103201.762 [Z3001] connection to database 'zabbix' failed: [0] received invalid response to SSL negotiation: R
手动连接数据库是正常的,最后发现安装的包不对
如下:
正常的两个基于mysql(mariadb)的如下:
zabbix-server-mysql-3.2.8-1.el7.x86_64 zabbix-release-3.2-1.el7.noarch zabbix-web-pgsql-3.2.8-1.el7.noarch zabbix-sender-3.2.8-1.el7.x86_64 zabbix-get-3.2.8-1.el7.x86_64 zabbix-web-3.2.8-1.el7.noarch zabbix-agent-3.2.8-1.el7.x86_64
安装了正常的包后故障排除,zabbix-server服务正常启动侦听10051端口。
定期清理历史数据(超过30天):
#!/bin/bash
#!/bin/bash
User="root"
Passwd="PASSWORD"
Date=`date -d $(date -d "-30 day" +%Y%m%d) +%s`
$(which mysql) -u${User} -p${Passwd} -e "
use zabbix;
DELETE FROM history WHERE 'clock' < $Date;
optimize table history;
DELETE FROM history_str WHERE 'clock' < $Date;
optimize table history_str;
DELETE FROM history_uint WHERE 'clock' < $Date;
optimize table history_uint;
DELETE FROM trends WHERE 'clock' < $Date;
optimize table trends;
DELETE FROM trends_uint WHERE 'clock' < $Date;
optimize table trends_uint;
DELETE FROM events WHERE 'clock' < $Date;
optimize table events;
"
转载于:https://blog.51cto.com/dyc2005/1970986