zabbix01-zabbix4.0安装部署
监控系统对比
Zabbix
- 所有配置WEB化,WEB采用php开发
- 支持分布式监控
- 支持多种数据采集:简单监控,agent监控,snmp接口监控,JMX接口监控
- 告警配置WEB化:邮件,短信,钉钉,微信
- Zabbix和Grafana结合,方便监控数据的可视化
Nagios
- 重在监控告警
- 添加监控需要修改配置文件
- 无监控历史数据,图形支持差
- 不支持分布式监控
Cacti
- 重在采集服务器,网络设备的监控数据并绘图
- 依赖于snmp协议
- 不好自定义监控
- 告警支持不好
搭建概述
zabbix搭建理论
- Zabbix server(主程序),去采集数据,让后写入SQL数据库
- Zabbix web 使用php开发,所有配置信息,和用户认证信息写入SQL数据库
- 企业级zabbix的搭建环境:主流采用LNMP环境(centos7+nginx+mysql+php)
LNMP web环境用户请求流程
用户-->nginx-->php-fpm-->运行php程序-->操作mysql
部署顺序说明
- Nginx,php-fpm
- Mysql
- Zabbix server
- Zabbix web
安装lnmp
安装nginx
- 官网下载nginx 下载stable版本 网址 http://nginx.org
- Nginx 编译安装
# yum install -y wget gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel # wget http://nginx.org/download/nginx-1.16.1.tar.gz # tar zxvf nginx-1.16.1.tar.gz # cd nginx-1.16.1 # ./configure --prefix=/usr/local/nginx # make && make install设置环境变量
# vim /etc/profile 增加 # export PATH=$PATH:/usr/local/nginx/sbin # source /etc/profilenginx基本命令
1,启动nginx nginx
2,测试配置文件 nginx -t
3,关闭nginx nginx -s stop
验证nginx安装是否成功
- 查看进程 ps -ef|grep nginx
- 查看端口 netstat -nltp|grep 80
- nginx日志
- 浏览器访问
使用systemctl 管理nginx
# vim /usr/lib/systemd/system/nginx.service [Unit] Description=nginx After=network.target [Service] Type=forking ExecStart=/usr/local/nginx/sbin/nginx [Install] WantedBy=multi-user.target
安装php-fpm
- php官网 http://php.net
- php编译安装
启动php-fpm# 安装依赖 # wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo # yum install epel-release.noarch # yum -y install gcc gcc-c++ make pcre pcre-devel openssl openssl-devel libxml2 libxml2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel openldap openldap-devel libmcrypt libmcrypt-devel sqlite3-devel # wget http://hk1.php.net/distributions/php-5.6.34.tar.gz # tar -zxvf php-5.6.34.tar.gz # ./configure --prefix=/usr/local/php --with-config-file-path=/usr/local/php/etc --enable-ctype --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-freetype-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --with-mcrypt --with-gd --with-jpeg-dir=/usr/lib64 --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-ldap-sasl --with-xmlrpc --enable-zip --enable-soap --with-gettext --enable-fpm # make && make install # cp php.ini-production /usr/local/php/etc/php.ini
设环境变量 export PATH=$PATH:/usr/local/php/sbin:/usr/local/php/bin
检测配置文件,默认报错 cd /usr/local/php/etc;cp php-fpm.conf.default php-fpm.conf;php-fpm –t
配置监听,php-fpm.conf 默认配置 listen = 127.0.0.1:9000
使用systemctl管理php-fpm,/usr/lib/systemd/system/php-fpm.service
# vim /usr/lib/systemd/system/php-fpm.service [Unit] Description=php-fpm After=network.target [Service] Type=forking ExecStart=/usr/local/php/sbin/php-fpm [Install] WantedBy=multi-user.target验证php-fpm是否启动
进程 ps -ef|grep php-fpm
端口 netstat -nltp | grep 9000
日志 tail -f /usr/local/php/var/log/php-fpm.log
Nginx 结合 php 配置
- 修改nginx配置
location / { root html; index index.html index.htm index.php; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; }
- 测试php网页
# cat test.php <?php echo "hello world test";
- 验证
安装mysql
- 下载mysql 官网 http://mysql.com 下载步骤略
- 编译安装
# yum install -y gcc gcc-c++ make tar openssl openssl-devel cmake ncurses ncurses-devel perl perl-devel autoconf # tar -zxvf mysql-5.6.34-linux-glibc2.5-x86_64.tar.gz -C /usr/local/mysql # useradd mysql # echo 123456 |passwd --stdin mysql # chown -R mysql:mysql /usr/local/mysql/ # cd /usr/local/mysql # ./scripts/mysql_install_db --user=mysql --basedir=/usr/local/mysql --datadir=/data/mysql # cp sport-files/mysql_server /etc/init.d/mysqld # cp my-default.cnf /etc/my.cnf # cp mysql.server /etc/init.d/mysql
- 修改配置文件
# vim /etc/init.d/mysql basedir=/usr/local/mysql datadir=/data/mysql # vim /etc/my.cnf basedir = /usr/local/mysql\ bind-address = 0.0.0.0 port = 3306datadir = /data/mysql user = mysql skip-name-resolve long_query_time=2 slow_query_log_file = /data/mysql/mysql-slow.log expire_logs_days = 7 innodb-file-per-table = 1 innodb_flush_log_at_trx_commit = 2 log_warnings = 1 max_allowed_packet = 512M connect_timeout = 60 net_read_timeout = 120
- 配置环境变量
# vi /etc/profile export MYSQL_HOME=/usr/local/mysql export PATH=$MYSQL_HOME/bin:$PATH # source /etc/profile
- 启动mysql
# chkconfig --add mysql # chkconfig mysql on # service mysql start
- 设置root密码,授权
/usr/local/mysql/bin/mysqladmin -u root password '123456' mysql -uroot -p123456 mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION; mysql> FLUSH PRIVILEGES;
- 设置开机启动
# cat /usr/lib/systemd/system/mysql.service [Unit] Description=mysql After=network.target [Service] Type=forking ExecStart=/etc/init.d/mysql start [Install] WantedBy=multi-user.target # systemctl daemon-reload # systemctl enable mysql
- 测试PHP链接MYSQL
# 编写测试mysql链接程序 # cat test_mysql.php <?php $link=mysql_connect("10.80.0.20","root","123456"); if(!$link){ echo "mysql connect failed"; }else{ echo "mysql connect success"; } ?>
- 验证mysql链接
部署zabbix(server+web)
zabbix部署原理
- Zabbix server 需要把监控数据写入sql数据库,所以需要mysql
- Zabbix 的web是基于php开发的,所以需要LNMP环境
- 部署zabbix server 和zabbix web
Zabbix 的官网
Zabbix server编译安装
- 编译安装
# yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel # useradd -s /sbin/nologin zabbix # cd /usr/local/src/ # tar -zxvf zabbix-4.0.27.tar.gz # cd zabbix-4.0.27 # ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp -with-libcurl --with-libxml2 # make && make install
- 安装验证
# vim /etc/profile export PATH=$PATH:/usr/local/zabbix/bin:/usr/local/zabbix/sbin # source /etc/profile # zabbix_server --version zabbix_server (Zabbix) 4.0.27 Revision ff3187cec7 30 November 2020, compilation time: Dec 5 2020 09:51:21
- mysql数据初始化
# mysql -uroot -p123456 mysql> create database zabbix character set utf8 collate utf8_bin; mysql> grant all privileges on zabbix.* to zabbix@'%' identified by '123456'; mysql> flush privileges; mysql> set names utf8; mysql> use zabbix mysql> source /usr/local/src/zabbix-4.0.27/database/mysql/schema.sql mysql> source /usr/local/src/zabbix-4.0.27/database/mysql/data.sql mysql> source /usr/local/src/zabbix-4.0.27/database/mysql/images.sql
- 配置zabbixserver,主要配置链接mysql的用户和密码
# cat zabbix_server.conf|grep -v ^# |grep -v ^$ LogFile=//usr/local/zabbix/zabbix_server.log DBHost=10.80.0.20 DBName=zabbix DBUser=zabbix DBPassword=123456 DBPort=3306 Timeout=30 AlertScriptsPath=/usr/local/zabbix/alertscripts ExternalScripts=/usr/local/zabbix/externalscripts LogSlowQueries=3000
- 启动zabbix server
# chown -R zabbix:zabbix /usr/local/zabbix # zabbix_server
- 验证zabbix 启动
# ps -ef|grep zabbix # netstat -nltp|grep 10051 # tail /usr/local/zabbix/zabbix_server.log
Zabbix web安装
- 安装zabbix web
# mkdir /usr/local/nginx/html/zabbix # cp -a /usr/local/src/zabbix-4.0.27/frontends/php/* /usr/local/nginx/html/zabbix/
- 访问zabbix web 初始化配置
http://10.80.0.20/zabbix/setup.php
- Zabbix web 对php的配置有要求,优化php配置
# 根据上图提示的 需要优化的参数逐个调整为 required的值。 # vim /usr/local/php/etc/php.ini date.timezone=Asia/shanghai # systemctl restart php-fpm
- 登录zabbix
默认用户名/密码 Admin/zabbix,禁用zabbix server监控
禁用guest用户
更改Admin密码,改成自己的密码
zabbix乱码问题
Zabbix 默认使用英文
- 如果英文好,建议直接用英文
- 把admin 改为中文
zabbix默认的字段问题
- 中文会有部分乱码的情况
- 检测-->图形里面会有乱码的产生
解决zabbix部分乱码问题
- 下载微软雅黑字体 https://raw.githubusercontent.com/chenqing/ng-mini/master/font/msyh.ttf 或者C:/windows/fonts下获取
- Zabbix字体目录: /usr/local/nginx/html/zabbix/assets/fonts/
- 上传到这个目录
- 配置修改:/usr/local/nginx/html/zabbix/include/defines.inc.php
- 默认是DejaVuSans字体,改为msyh微软雅黑字体
- 验证中文是否正常