zabbix 构建企业级监控告警平台(1)-安装部署zabbix

it2023-07-04  68

安装部署zabbix

一、环境准备二、安装LNMP架构1、安装nginx2、安装mysql3、安装配置php4、创建zabbix的数据库用户 三、安装zabbix四、web页面配置zabbix五、配置中文界面六、配置被监控端

一、环境准备

zabbix服务器:192.168.245.203 zabbix客户端:192.168.245.204

二、安装LNMP架构

1、安装nginx

[root@zabbix ~]# wget http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm [root@zabbix ~]# vim /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1 [root@zabbix ~]# yum install nginx -y [root@zabbix ~]# systemctl start nginx [root@zabbix ~]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.

2、安装mysql

[root@zabbix ~]# yum install -y mariadb* [root@zabbix ~]# systemctl enable mariadb.service Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service. [root@zabbix ~]# systemctl start mariadb.service [root@zabbix ~]# mysql_secure_installation //初始化mysql NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB SERVERS IN PRODUCTION USE! PLEASE READ EACH STEP CAREFULLY! In order to log into MariaDB to secure it, we'll need the current password for the root user. If you've just installed MariaDB, and you haven't set the root password yet, the password will be blank, so you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password ensures that nobody can log into the MariaDB root user without the proper authorisation. Set root password? [Y/n] y New password: //配置密码为admin123 Re-enter new password: Password updated successfully! Reloading privilege tables.. ... Success! By default, a MariaDB installation has an anonymous user, allowing anyone to log into MariaDB without having to have a user account created for them. This is intended only for testing, and to make the installation go a bit smoother. You should remove them before moving into a production environment. Remove anonymous users? [Y/n] n ... skipping. Normally, root should only be allowed to connect from 'localhost'. This ensures that someone cannot guess at the root password from the network. Disallow root login remotely? [Y/n] n ... skipping. By default, MariaDB comes with a database named 'test' that anyone can access. This is also intended only for testing, and should be removed before moving into a production environment. Remove test database and access to it? [Y/n] n ... skipping. Reloading the privilege tables will ensure that all changes made so far will take effect immediately. Reload privilege tables now? [Y/n] y ... Success! Cleaning up... All done! If you've completed all of the above steps, your MariaDB installation should now be secure. Thanks for using MariaDB!

3、安装配置php

[root@zabbix ~]# yum install epel-release -y [root@zabbix ~]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 获取https://mirror.webtatic.com/yum/el7/webtatic-release.rpm 警告:/var/tmp/rpm-tmp.9bV6SH: 头V4 RSA/SHA1 Signature, 密钥 ID 62e74ca5: NOKEY 准备中... ################################# [100%] 正在升级/安装... 1:webtatic-release-7-3 ################################# [100%] [root@zabbix ~]# yum install -y php72w php72w-devel php72w-fpm php72w-gd php72w-mbstring php72w-mysql [root@zabbix ~]# php -v PHP 7.2.34 (cli) (built: Oct 1 2020 13:37:37) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

默认用户是apache,这里使用LNMP架构所以改成nginx

[root@zabbix ~]# vim /etc/php-fpm.d/www.conf user = nginx group = nginx

在nginx的配置文件里增加index.php让nginx可以访问php页面

[root@zabbix ~]# vim /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.php index.html index.htm; }

修改fastcgi参数

location ~ \.php$ { root /usr/share/nginx/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配置文件

[root@zabbix ~]# vim /etc/php.ini 202 short_open_tag = On //支持php短标签 359 expose_php = Off //隐藏php版本 368 max_execution_time = 300 378 max_input_time = 300 389 memory_limit = 128M 656 post_max_size = 16M 799 upload_max_filesize = 2M 800 lways_populate_raw_post_data = -1 //新增一行 877 date.timezone = Asia/Shanghai

启动php

[root@zabbix ~]# systemctl start php-fpm [root@zabbix ~]# systemctl enable php-fpm Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service.

重启nginx

[root@zabbix ~]# systemctl restart nginx

创建php测试网页

[root@zabbix ~]# vim /usr/share/nginx/html/info.php <?php phpinfo(); ?>

真机访问验证:http://192.168.245.203/info.php

测试连接数据库

[root@zabbix ~]# vim /usr/share/nginx/html/info.php <?php $link=mysqli_connect('127.0.0.1','root','abc123'); if ($link) echo "连接成功 !!!"; else echo "连接失败 !!!"; ?>

真机访问验证:http://192.168.245.203/info.php

4、创建zabbix的数据库用户

进入数据库新建zabbix账户,用户名zabbix,密码是admin123,并授予权限

[root@zabbix ~]# mysql -u root -p Enter password: //输入密码abc123 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 7 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> MariaDB [(none)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin; Query OK, 1 row affected (0.00 sec) MariaDB [(none)]> GRANT all privileges ON *.* TO 'zabbix'@'%' IDENTIFIED BY 'admin123'; Query OK, 0 rows affected (0.01 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> quit Bye

创建测试页面

[root@zabbix ~]# vim /usr/share/nginx/html/info.php <?php $link=mysqli_connect('127.0.0.1','zabbix','admin123'); if ($link) echo "Zabbix数据库连接成功!"; else echo "Zabbix数据库连接失败!"; ?>

这时访问网页会提示连接失败

进入数据库发现有2个空用户,删除空用户即可

[root@zabbix ~]# mysql -u root -p Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 9 Server version: 5.5.65-MariaDB MariaDB Server Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> MariaDB [(none)]> MariaDB [(none)]> select user,host from mysql.user; +--------+-----------+ | user | host | +--------+-----------+ | zabbix | % | | root | 127.0.0.1 | | root | ::1 | | | localhost | | root | localhost | | | zabbix | | root | zabbix | +--------+-----------+ 7 rows in set (0.01 sec) MariaDB [(none)]> drop user ''@localhost; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> drop user ''@zabbix; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> flush privileges; Query OK, 0 rows affected (0.00 sec) MariaDB [(none)]> quit Bye

重新访问测试页面就可以连接成功了

三、安装zabbix

[root@zabbix ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm 警告:/var/tmp/rpm-tmp.c1dThd: 头V4 RSA/SHA512 Signature, 密钥 ID a14fe591: NOKEY [root@zabbix ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y

导入zabbix数据库脚本

[root@zabbix ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix Enter password: //密码是admin123

修改配置文件

[root@zabbix ~]# vim /etc/zabbix/zabbix_server.conf 91:DBHost=localhost 124:DBPassword=admin123 //修改这两行内容 [root@zabbix ~]# grep -n '^'[a-Z] /etc/zabbix/zabbix_server.conf 38:LogFile=/var/log/zabbix/zabbix_server.log 49:LogFileSize=0 72:PidFile=/var/run/zabbix/zabbix_server.pid 82:SocketDir=/var/run/zabbix 91:DBHost=localhost 100:DBName=zabbix 116:DBUser=zabbix 124:DBPassword=admin123 356:SNMPTrapperFile=/var/log/snmptrap/snmptrap.log 473:Timeout=4 516:AlertScriptsPath=/usr/lib/zabbix/alertscripts 527:ExternalScripts=/usr/lib/zabbix/externalscripts 563:LogSlowQueries=3000

把zabbix文件复制到nginx的默认主页站点并设置权限

[root@zabbix ~]# cp -r /usr/share/zabbix/ /usr/share/nginx/html/ [root@zabbix ~]# chown -R zabbix:zabbix /etc/zabbix/ [root@zabbix ~]# chown -R zabbix:zabbix /usr/share/nginx/ [root@zabbix ~]# chown -R zabbix:zabbix /usr/lib/zabbix/ [root@zabbix ~]# chmod -R 755 /etc/zabbix/web/ [root@zabbix ~]# chmod -R 777 /var/lib/php/session/

启动zabbix和zabbix-agent

[root@zabbix ~]# systemctl start zabbix-server.service [root@zabbix ~]# systemctl enable zabbix-agent.service Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service. [root@zabbix ~]# systemctl start zabbix-agent.service [root@zabbix ~]# systemctl enable zabbix-agent.service

查看端口状态

[root@zabbix ~]# netstat -anpl | grep zabbix tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 111746/zabbix_agent tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 111646/zabbix_serve tcp6 0 0 :::10050 :::* LISTEN 111746/zabbix_agent tcp6 0 0 :::10051 :::* LISTEN 111646/zabbix_serve

重启php模块和nginx

[root@zabbix ~]# systemctl restart php-fpm.service [root@zabbix ~]# systemctl restart nginx

真机访问http://192.168.245.203/zabbix

四、web页面配置zabbix

配置步骤如下

这里会出现配置失败需要下载文件

这里,我们把要下载的文件已经下载好了,把zabbix.conf.php拷贝到etc/zabbix/web/,配置权限并重启zabbix服务

[root@zabbix ~]# cd /etc/zabbix/web/ [root@zabbix web]# chown zabbix.zabbix /etc/zabbix/web/zabbix.conf.php [root@zabbix web]# systemctl restart zabbix-server.service

重新刷新一下网页就好了

五、配置中文界面

六、配置被监控端

在客户端上下载安装zabbix-agent

[root@localhost ~]# rpm -i https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm [root@localhost ~]# yum install -y zabbix-agent

配置agent端参数

[root@localhost ~]# vim /etc/zabbix/zabbix_agentd.conf 98 Server=192.168.245.203 //指向zabbix服务器 139 ServerActive=192.168.245.203 //指向zabbix服务器 150 Hostname=test //随便起个名字

重启agent

[root@localhost ~]# systemctl enable zabbix-agent.service Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service. [root@localhost ~]# systemctl restart zabbix-agent.service [root@localhost ~]# netstat -anpt | grep zabbix tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 87972/zabbix_agentd tcp6 0 0 :::10050 :::* LISTEN 87972/zabbix_agentd

刷新页面,页面添加被监控的主机

添加要监控哪些服务的模板

因为被监控主机没有安装apache服务,所以这里告警了

如果想取消监控某个服务,点击取消并更新

重新刷新页面告警就没了

最新回复(0)