【网络监控】Zabbix详细安装部署(最全)

文章目录

  • Zabbix详细安装部署
  • 环境准备
  • 安装依赖组件
  • 访问初始化配置

Zabbix详细安装部署

Zabbix 是一个高度集成的网络监控解决方案,可以提供企业级的开源分布式监控解决方案,由一个国外的团队持续维护更新,软件可以自由下载使用,运作团队靠提供收费的技术支持赢利。

环境准备

CentOS Linux release 7.5.1804 (Core)

下载地址
http://mirrors.sohu.com/centos/7.5.1804/isos/x86_64/CentOS-7-x86_64-DVD-1804.iso

Zabbix5.0源码包

下载地址
https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.1.tar.gz

安装依赖组件

[root@bogon ~]# yum install net-snmp-devel libevent-devel -y

创建zabbix用户组和用户

[root@bogon ~]# groupadd zabbix
[root@bogon ~]# useradd -r -g zabbix zabbix

安装zabbix服务

[root@bogon ~]# cd /usr/local/src/
[root@bogon src]# wget https://cdn.zabbix.com/zabbix/sources/stable/5.0/zabbix-5.0.1.tar.gz

或者通过上传的方式上传文件

[root@bogon src]# mkdir /usr/local/data/
[root@bogon src]# tar -xzf zabbix-5.0.1.tar.gz -C /usr/local/data/
[root@bogon src]# cd /usr/local/data/zabbix-5.0.1/

[root@bogon zabbix-5.0.1]# ./configure --prefix=/usr/local/data/zabbix --enable-server --enable-agent --with-mysql=/usr/local/data/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2

此处报错:

  checking for mysql_config... /usr/local/data/mysql/bin/mysql_config
  configure: error: MySQL library not found

解决办法:
互联网状态下
Centos7.5安装MySQL57

  [root@bogon zabbix-5.0.1]# cd ..
  [root@bogon data]# wget http://repo.mysql.com/mysql57-community-release-el7-10.noarch.rpm
  [root@bogon data]# ls
  mysql57-community-release-el7-10.noarch.rpm  zabbix-5.0.1
  [root@bogon data]# rpm -Uvh mysql57-community-release-el7-10.noarch.rpm
  [root@bogon data]# yum install -y mysql-community-server
非互联网状态下
安装MySql57
必要以下几个包安装就可以了
mysql-community-client-5.7.30-1.el7.x86_64
mysql-community-libs-compat-5.7.30-1.el7.x86_64
mysql57-community-release-el7-10.noarch
mysql-community-server-5.7.30-1.el7.x86_64
mysql-community-common-5.7.30-1.el7.x86_64
mysql-community-libs-5.7.30-1.el7.x86_64

启动MySQL服务

[root@bogon data]# systemctl start mysqld.service

查看MySQL状态

[root@bogon data]# systemctl status mysqld.service

获取MySQL临时用户名密码

[root@bogon data]# grep 'temporary password' /var/log/mysqld.log 
A temporary password is generated for root@localhost: nc(9k0u;Fzff

输入MySQL的临时密码

[root@bogon data]# mysql -uroot -p		  
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.30

因为MySQL的密码规则需要很复杂,我们一般自己设置的不会设置成这样,所以我们全局修改一下

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_length=1;
Query OK, 0 rows affected (0.00 sec)

设置自己想要的密码

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

授权其他机器远程登录

mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;            
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.01 sec)

mysql> exit
Bye

开启开机自启动

[root@bogon data]# systemctl enable mysqld
[root@bogon data]# systemctl daemon-reload	

设置MySQL的字符集为UTF-8,令其支持中文

[root@bogon data]# vim /etc/my.cnf

改成如下,然后保存

# For advice on how to change settings please see
# http://dev.mysql.com/doc/refman/5.7/en/server-configuration-defaults.html
 
[mysql]
default-character-set=utf8
 
[mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
default-storage-engine=INNODB
character_set_server=utf8
 
symbolic-links=0
 
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

重启一下MySQL,令配置生效

[root@bogon data]# systemctl restart mysqld

永久关闭selinux和防火墙

[root@bogon data]# vi /etc/selinux/config 将SELINUX=enforcing改为SELINUX=disabled 设置后需要重启才能生效
[root@bogon data]# systemctl stop firewalld.service 
[root@bogon data]# systemctl disable firewalld.service

再次执行部署zabbix服务

[root@bogon zabbix-5.0.1]# ./configure --prefix=/usr/local/data/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2			
Package libxml-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml-2.0' found
Package libxml-2.0 was not found in the pkg-config search path.
Perhaps you should add the directory containing `libxml-2.0.pc'
to the PKG_CONFIG_PATH environment variable
No package 'libxml-2.0' found
checking for xmlReadMemory in -lxml2... no
configure: error: Not found libxml2 library

解决办法:
互联网状态下

[root@bogon zabbix-5.0.1]# yum install libxml2 libxml2-devel -y

非互联网状态下
安装以下包

libxml2-python-2.9.1-6.el7.4.x86_64
libxml2-devel-2.9.1-6.el7.4.x86_64
libxml2-2.9.1-6.el7.4.x86_64

再次执行部署zabbix服务

[root@bogon zabbix-5.0.1]# ./configure --prefix=/usr/local/data/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2			
configure: error: Curl library not found

解决办法:
互联网状态下

[root@bogon zabbix-5.0.1]# yum install curl-devel

非互联网状态下
安装以下包

libcurl-7.29.0-57.el7.x86_64
libcurl-devel-7.29.0-57.el7.x86_64
python-pycurl-7.19.0-19.el7.x86_64
curl-7.29.0-57.el7.x86_64

再次执行部署zabbix服务

[root@bogon zabbix-5.0.1]# ./configure --prefix=/usr/local/data/zabbix --enable-server --enable-agent --with-mysql --with-net-snmp --with-libcurl --with-libxml2			
[root@bogon zabbix-5.0.1]# make
[root@bogon zabbix-5.0.1]# make install
[root@bogon data]# vim /etc/profile

添加以下内容

# Zabbix Environment
export PATH=$PATH:/usr/local/data/zabbix/sbin/:/usr/local/data/zabbix/bin/
[root@localhost zabbix-4.2.6]# source /etc/profile

创建用户导入数据库

[root@bogon data]# mysql -uroot -p
mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)
mysql> grant all privileges on zabbix.* to zabbix@‘localhost’ identified by ‘123456’;
ERROR 1819 (HY000): Your password does not satisfy the current policy requirements

解决办法:

mysql> show variables like “%validate%”;
±-------------------------------------±-------+
| Variable_name | Value |
±-------------------------------------±-------+
| query_cache_wlock_invalidate | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 8 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | MEDIUM |
| validate_password_special_char_count | 1 |
±-------------------------------------±-------+
8 rows in set (0.00 sec)
mysql> set global validate_password_length=6;
Query OK, 0 rows affected (0.00 sec)

mysql> set global validate_password_policy=0;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like “%validate%”;
±-------------------------------------±------+
| Variable_name | Value |
±-------------------------------------±------+
| query_cache_wlock_invalidate | OFF |
| validate_password_check_user_name | OFF |
| validate_password_dictionary_file | |
| validate_password_length | 6 |
| validate_password_mixed_case_count | 1 |
| validate_password_number_count | 1 |
| validate_password_policy | LOW |
| validate_password_special_char_count | 1 |
±-------------------------------------±------+
8 rows in set (0.00 sec)
mysql> grant all privileges on zabbix.* to zabbix@‘localhost’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)
mysql> use zabbix;
Database changed
mysql> source /usr/local/data/zabbix-5.0.1/database/mysql/schema.sql
mysql> source /usr/local/data/zabbix-5.0.1/database/mysql/data.sql
mysql> source /usr/local/data/zabbix-5.0.1/database/mysql/images.sql

编辑zabbix_server的配置文件

[root@bogon mysql]# find / -name zabbix_server.conf
/usr/local/data/zabbix-5.0.1/conf/zabbix_server.conf
/usr/local/data/zabbix/etc/zabbix_server.conf
[root@bogon mysql]# vim /usr/local/data/zabbix/etc/zabbix_server.conf
###Option: LogFile
LogFile=/usr/local/data/zabbix/logs/zabbix_server.log
###Option: LogFileSize
LogFileSize=0
###Option: DBHost
DBHost=localhost
###Option: DBName
DBName=zabbix
###Option: DBUser
DBUser=zabbix
###Option: DBPassword
DBPassword=lixiaozhe
###Option: DBSocket
DBSocket=/tmp/mysql.sock
###Option: DBPort
DBPort=3306
###Option: Timeout
Timeout=30
###Option: AlertScriptsPath
AlertScriptsPath=/usr/local/data/zabbix/alertscripts
###Option: ExternalScripts
ExternalScripts=/usr/local/data/zabbix/externalscripts
###Option: LogSlowQueries
LogSlowQueries=3000

授权建立相关文件夹

[root@bogon mysql]# mkdir /usr/local/data/zabbix/logs
[root@bogon mysql]# mkdir /usr/local/data/zabbix/pid
[root@bogon mysql]# chown -R zabbix:zabbix /usr/local/data/zabbix/

安装PHP73
互联网状态下
首先安装 EPEL 源

yum install epel-release

安装 REMI 源

yum install http://rpms.remirepo.net/enterprise/remi-release-7.rpm

安装 Yum 源管理工具:

yum install yum-utils

非互联网状态下
安装remi-release-7.rpm
安装 PHP7.3:

yum install -y php73-php-fpm php73-php-cli php73-php-bcmath php73-php-gd php73-php-json php73-php-mbstring php73-php-mcrypt php73-php-mysqlnd php73-php-opcache php73-php-pdo php73-php-pecl-crypto php73-php-pecl-mcrypt php73-php-pecl-geoip php73-php-recode php73-php-snmp php73-php-soap php73-php-xmll

设置开机自启

systemctl enable php73-php-fpm

启动PHP服务

systemctl start php73-php-fpm

[root@bogon data]# vim /etc/ld.so.conf
/usr/local/lib /添加此路径/
[root@bogon data]# ldconfig

启动zabbix

[root@bogon data]# zabbix_server

杀死进程

[root@bogon data]# ps -ef | grep zabbix_server | grep -v grep | awk ‘{print $2}’ | xargs kill -9

创建服务文件

[root@bogon data]# vim /usr/lib/systemd/system/zabbix-server.service
[Unit]
Description=Zabbix
After=network.target

[Service]
Environment=“CONFFILE=/usr/local/data/zabbix/etc/zabbix_server.conf”
Type=forking
Restart=on-failure
PIDFile=/tmp/zabbix_server.pid
KillMode=control-group
ExecStart=/usr/local/data/zabbix/sbin/zabbix_server -c $CONFFILE
ExecStop=/bin/kill -SIGTERM $MAINPID
RestartSec=10s

[Install]
WantedBy=multi-user.target

[root@bogon zabbix-5.0.1]# systemctl daemon-reload
[root@bogon zabbix-5.0.1]# systemctl enable zabbix-server
[root@bogon zabbix-5.0.1]# systemctl stop zabbix-server
[root@bogon zabbix-5.0.1]# systemctl start zabbix-server
[root@bogon zabbix-5.0.1]# systemctl restart zabbix-server

安装nginx

[root@bogon data]# wget http://nginx.org/download/nginx-1.9.9.tar.gz
[root@bogon data]# tar -zxvf nginx-1.9.9.tar.gz
[root@bogon data]# cd nginx-1.9.9/
[root@bogon data]# ./configure
[root@bogon data]# make
[root@bogon data]# make install
[root@bogon data]# cd /usr/local/nginx/sbin/
[root@bogon data]# ./nginx
[root@bogon data]# ps -ef | grep nginx
[root@bogon zabbix-5.0.1]# mkdir -p /usr/local/nginx/html/zabbix
[root@bogon zabbix-5.0.1]# cp -a /usr/local/data/zabbix-5.0.1/ui/* /usr/local/nginx/html/zabbix/

zabbix_web需要php配置优化,编辑php.ini文件

[root@bogon zabbix-5.0.1]# find / -name php.ini
/etc/opt/remi/php73/php.ini

修改如下内容:

max_execution_time = 30      修改为350-(zabbix_web优化)
max_input_time = 60        修改为350-(zabbix_web优化)
post_max_size = 8M        修改为32M-(zabbix_web优化)
date.timezone = Asia/Shanghai 添加-(zabbix_web优化)

找到设置cgi.fix_pathinfo的参数, 这将用分号(;)注释掉,默认设置为“1”

cgi.fix_pathinfo=0
[root@bogon zabbix-5.0.1]# systemctl restart php73-php-fpm
[root@bogon zabbix-5.0.1]# vim /lib/systemd/system/nginx.service
[UNIT]

Deion=nginx - high performance web server

Documentation=http://nginx.org/en/docs/

After=network-online.target remote-fs.target nss-lookup.target

Wants=network-online.target

[Service]

Type=forking

PIDFile=/usr/local/nginx/logs/nginx.pid

ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

ExecReload=/bin/kill -s HUP $MAINPID

ExecStop=/bin/kill -s TERM $MAINPID

[Install]

WantedBy=multi-user.target
[root@bogon zabbix-5.0.1]# systemctl daemon-reload
[root@bogon zabbix-5.0.1]# systemctl enable nginx
[root@bogon zabbix-5.0.1]# systemctl stop nginx
[root@bogon zabbix-5.0.1]# systemctl start nginx

若启动失败,重启服务器

[root@bogon zabbix-5.0.1]# systemctl restart nginx

配置nginx文件使PHP页面生效

[root@bogon zabbix-5.0.1]# vim /usr/local/nginx/conf/nginx.conf

listen - 定义Nginx将侦听的端口。
root - 定义存储网站服务的文档根目录。
index- 配置Nginx请求索引文件时优先处理index.php命名的文件。
server_name - 将此指令指向服务器的域名或公共IP地址。
location /- 第一个位置块包括一个try_files指令,该指令检查是否存在满足URI请求的文件。如果Nginx找不到合适的文件,则会返回404错误。
location ~ .php$- 此位置块通过将Nginx指向fastcgi-php.conf配置文件和php7.2-fpm.sock文件来处理实际的PHP处理,该文件声明了与哪个套接字相关联php-fpm。检查/etc/php/7.0/fpm/pool.d/www.conf文件并查找“listen”行。
location ~ /.ht- 通过添加deny all指令,如果任何.htaccess文件碰巧进入文档根目录,它们将不会被提供给访问者。
修改如下内容:
user root;
server {
listen 80;
server_name localhost;

  #charset koi8-r;

    #access_log  logs/host.access.log  main;

root	/usr/local/nginx/html/zabbix;
index 	index.php index.html index.htm;

    location / {
    
}

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    #error_page   500 502 503 504  /50x.html;
    #location = /50x.html {
     #   root   /usr/local/nginx/html;
    #}

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    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;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    location ~ /\.ht {
        deny  all;
    }
}

验证nginx配置文件语法错误
[root@bogon zabbix-5.0.1]# sudo nginx -t
[root@bogon zabbix-5.0.1]# systemctl reload nginx
[root@bogon zabbix-5.0.1]# nginx -s reload #重新加载配置文件
[root@bogon zabbix-5.0.1]# systemctl restart nginx

然后访问http://IP
默认点击下一步
出现报错
PHP xmlwriter extension missing zabbix
PHP xmlreader extension missing zabbix
解决办法:
检查/etc/php.ini
修改内容如下:
找到自己的xmlwriter.so和xmlreader.so路径位置
extension=/usr/lib64/php/modules/xmlwriter.so
extension=/usr/lib64/php/modules/xmlreader.so
再次启动
查看是否有报错
[root@bogon zabbix-5.0.1]# php73 -v
最终保存文件需要上传到服务器本地路径
字体显示异常
解决办法:
/usr/local/nginx/html/zabbix/assets/fonts
将Win10操作系统C:\Windows\Fonts目录下的任意字体,拷贝到此目录,更改后缀为’.ttf’
修改php脚本文件/usr/local/nginx/html/zabbix/include/defines.inc.php
define('ZBX_GRAPH_FONT_NAME',           'msyh'); // font file name
define('ZBX_FONT_NAME', 'msyh');
重启zabbix服务
重启nginx服务
[root@bogon zabbix-5.0.1]# systemctl restart zabbix-server
[root@bogon zabbix-5.0.1]# systemctl restart nginx

访问初始化配置

浏览器访问server ip

http://8.130.31.182/

选择中文 - 下一步 -
【网络监控】Zabbix详细安装部署(最全)_第1张图片
【网络监控】Zabbix详细安装部署(最全)_第2张图片

配置DB连接(端口和密码根据情况填写)

【网络监控】Zabbix详细安装部署(最全)_第3张图片
【网络监控】Zabbix详细安装部署(最全)_第4张图片

都点击下一步,完成安装

【网络监控】Zabbix详细安装部署(最全)_第5张图片

登录:

用户名:Admin

密码:zabbix

【网络监控】Zabbix详细安装部署(最全)_第6张图片
【网络监控】Zabbix详细安装部署(最全)_第7张图片
【网络监控】Zabbix详细安装部署(最全)_第8张图片

至此最新版本zabbix5.4的server就已经部署完毕

增加agent监控节点

# zabbix_agent01
[root@zabbix_agent01 ~]# dnf install zabbix-agent
[root@zabbix_agent01 ~]# vim /etc/zabbix/zabbix_agentd.conf 
[root@zabbix_agent01 ~]# cat /etc/zabbix/zabbix_agentd.conf |grep -vE "^$|^#"
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.28.54.185				#server ip
ServerActive=172.28.54.185			#不配端口默认是使用的10051,否则需要写成172.28.54.185:port
Hostname=zabbix_agent01
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@zabbix_agent01 ~]# systemctl restart zabbix-agent

# zabbix_agent02
[root@zabbix_agent02 ~]# dnf install zabbix-agent
[root@zabbix_agent02 ~]# vim /etc/zabbix/zabbix_agentd.conf 
[root@zabbix_agent02 ~]# cat /etc/zabbix/zabbix_agentd.conf |grep -vE "^$|^#"
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=172.28.54.185
ServerActive=172.28.54.185
Hostname=zabbix_agent02
Include=/etc/zabbix/zabbix_agentd.d/*.conf
[root@zabbix_agent02 ~]# systemctl restart zabbix-agent

界面添加主机( 添加完agent01后,克隆一下换agent02的ip即可)

【网络监控】Zabbix详细安装部署(最全)_第9张图片

等待agent成功被发现监控后查看页面

【网络监控】Zabbix详细安装部署(最全)_第10张图片

点击主机,选择图形;可以查看常规指数图形 (管理中一般设置可以更改主题风格)

【网络监控】Zabbix详细安装部署(最全)_第11张图片

监控使用介绍
案例: 监控某个端口是否异常

首先在服务器上启动一个测试端口 6666

[root@zabbix_server ~]# yum install -y httpd
[root@zabbix_server ~]# vim /etc/httpd/conf/httpd.conf 
Listen 6666				# 把listen改成6666端口

[root@zabbix_server ~]# systemctl start httpd
[root@zabbix_server ~]# netstat -tnlpu|grep 6666
tcp6       0      0 :::6666                 :::*                    LISTEN      31923/httpd  

# 测试端口已经正常运行

选择配置 - 主机 - 选择zabbix-server节点 - 右上角创建监控项

【网络监控】Zabbix详细安装部署(最全)_第12张图片

键值 : net.tcp.listen[port] 检查 TCP 端口 是否处于侦听状态。返回 0 - 未侦听;1 - 正在侦听 把port换成实际需要监控的端口 添加

有了监控项也就具备了持续监控状态,还需要一个状态变化触发告警的配置:触发器
【网络监控】Zabbix详细安装部署(最全)_第13张图片

在表达式中点击添加,选择 匹配上我们配置的监控项:httpd_6666端口监控 插入即可

现在把httpd服务禁用

[root@zabbix_server ~]# systemctl stop httpd
[root@zabbix_server ~]# netstat -tnlpu|grep 6666
[root@zabbix_server ~]# 
# 6666端口已经没了

等待一会查看仪表盘:

【网络监控】Zabbix详细安装部署(最全)_第14张图片

可以看到,监控告警已经报告了对应监控项

回到服务器命令行,重新运行服务,模拟修复故障

[root@zabbix_server ~]# systemctl start httpd
[root@zabbix_server ~]# netstat -tnlpu|grep 6666
tcp6       0      0 :::6666                 :::*                    LISTEN      32628/httpd         
[root@zabbix_server ~]# 

这时候可以看到告警项已经不再告警
【网络监控】Zabbix详细安装部署(最全)_第15张图片

可以在监测项中查看问题,有问题历史记录

【网络监控】Zabbix详细安装部署(最全)_第16张图片

到此,相当于流程 定义监控 - 发现问题抛出告警 - 相关人员处理问题 - 监控告警状态解除 已经完毕

你可能感兴趣的:(DevOps,网络,zabbix,centos)