在工作中,安装Zabbix监控平台的Linux服务器需要搭建环境,所以我就花了半天时间重新安装了一台CentOS 7系统,从搭建环境开始安装。
Zabbix监控的范围:硬件监控、系统监控、应用服务监控、性能监控、日志监控、安全监控、网络监控、MySQL数据库监控、URL监控(Zabbix Web监控)
硬件监控:查看硬件的CPU、内存、硬盘使用率、温度、风扇转速等相关性能
Linux服务器系统 CentOS 7
Zabbix 监控平台 4.4版本
[root@localhost ~]# cd /etc/sysconfig/network-scripts
[root@localhost network-scripts]# vim ifcfg-ens33
ONBOOT=yes
[root@localhost ~]# service network restart
Restarting network (via systemctl): [ OK ]
[root@localhost ~]#
[root@localhost ~]# systemctl stop firewlld.service
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disable
[root@localhost ~]# reboot # 重启Linux系统
[root@localhost ~]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates yum zabbix zabbix-non-supported
Cleaning up list of fastest mirrors
[root@localhost ~]# yum makecache
[root@localhost ~]# yum update
[root@localhost ~]# yum install gcc-c++ -y
[root@localhost ~]# yum install wget -y
[root@localhost ~]# ls | grep rpm*
centos-release-7-8.2003.0.el7.centos.x86_64.rpm
libssh2-devel-1.9.0-5.fc33.aarch64.rpm
openssl-libs-1.0.2k-19.el7.x86_64.rpm
php-devel-5.4.16-48.el7.x86_64.rpm
zabbix-agent-3.4.4-2.el7.x86_64.rpm
zabbix-get-3.4.4-2.el7.x86_64.rpm
zabbix-release-3.4-2.el7.noarch.rpm
zabbix-server-mysql-4.4.0-1.el7.x86_64.rpm
zabbix-server-mysql-4.4.1-1.el7.x86_64.rpm
zabbix-web-4.4.0-1.el7.noarch.rpm
[root@localhost ~]# ls | grep tar*
freetype-2.1.10.tar.gz
GD-2.18.tar.gz
libevent-2.1.12-stable.tar.gz
libgd-2.3.0.tar.gz
libmcrypt-2.5.8.tar.gz
libpng-1.2.8-config.tar.gz
zlib-1.2.11.tar.gz
[root@localhost ~]#
[root@localhost ~]# rpm -ivh zabbix-release-3.4-2.el7.noarch.rpm
warning: zabbix-release-3.4-2.el7.noarch.rpm: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:zabbix-release-3.4-2.el7 ################################# [100%]
[root@localhost ~]#
安装Apache
[root@localhost ~]# yum install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
...............省略部分安装过程...............
Installed:
httpd.x86_64 0:2.4.6-93.el7.centos
Dependency Installed:
httpd-tools.x86_64 0:2.4.6-93.el7.centos
Complete!
[root@localhost ~]#
[root@localhost ~]# systemctl start httpd
[root@localhost ~]# systemctl enable httpd
安装Mariadb数据库
[root@localhost ~]# yum install mariadb mariadb-server # 安装 Mariadb数据库
[root@localhost ~]# systemctl start mariadb # 启动 Mariadb数据库
[root@localhost ~]# systemctl enable mariadb
[root@localhost ~]#
[root@localhost ~]# mysql_secure_installation # 初始化 Mariadb
..............省略部分安装步骤...............
Enter current password for root (enter for none): (当前数据库密码为空,直接敲回车)
OK, successfully used password, moving on...
Set root password? [Y/n] y (设置root管理员的数据库密码)
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
... Success!
Remove anonymous users? [Y/n] y (删除匿名账户)
... Success!
Disallow root login remotely? [Y/n] y (禁止root从远程登录)
... Success!
Remove test database and access to it? [Y/n] y (删除test数据库并取消对它的访问)
- Dropping test database...
... Success!
- Removing privileges on test database...
... Success!
Reload privilege tables now? [Y/n] y (刷新授权表,使配置立即生效)
... Success!
Cleaning up...
Thanks for using MariaDB!
[root@localhost ~]#
安装PHP及依赖包
[root@localhost ~]# yum install -y php php-mysql httpd-manual mod_ssl php-gd php-xml php-mbstring php-ldap php-pear php-xmlrpc php-bcmath mysql-connector-odbc mysql-devel libdbi-dbd-mysql net-snmp-devel curl-devel unixODBC-devel OpenIPMI-devel java-devel libevent-devel freetype-devel
[root@localhost ~]# vim /etc/php.ini # 修改 PHP配置文件(主要是修改时区,其它参数可以保持默认)
878 date.timezone = Asia/Shanghai
384 max_execution_time = 300
672 post_max_size = 16M
119 max_input_time = 300
405 memory_limit = 128M
1704 mbstring.func_overload = 1 (注释掉这一行)
[root@localhost ~]# yum install -y curl curl-devel pcre pcre-devel libssh2-devel openldap-devel
...............省略部分安装步骤................
Installed:
libssh2-devel.x86_64 0:1.8.0-3.el7 openldap-devel.x86_64 0:2.4.44-21.el7_6
Dependency Installed:
cyrus-sasl-devel.x86_64 0:2.1.26-23.el7
Complete!
[root@localhost ~]#
[root@localhost ~]# rpm -ivh zabbix-server-mysql-4.4.0-1.el7.x86_64.rpm zabbix-get-3.4.4-2.el7.x86_64.rpm zabbix-agent-3.4.4-2.el7.x86_64.rpm zabbix-web-4.4.0-1.el7.noarch.rpm --nodeps --force
[root@localhost ~]# tar xf zabbix-3.4.8.tar.gz
[root@localhost ~]# cd zabbix-3.4.8/
[root@localhost zabbix-3.4.8]# ./configure --prefix=/usr/local --sysconfdir=/etc/zabbix --enable-server --enable-proxy --enable-agent --enable-ipv6 --with-mysql=/usr/bin/mysql_config --with-net-snmp --with-libcurl --with-openipmi --with-unixodbc --with-ldap --with-ssh2 --enable-java
..................省略部分安装过程................
Enable Java gateway: yes
Java gateway details:
Java compiler: javac
Java archiver: jar
LDAP support: yes
IPv6 support: yes
***********************************************************
* Now run 'make install' *
* *
* Thank you for using Zabbix! *
* *
***********************************************************
[root@localhost zabbix-3.4.8]#
[root@localhost zabbix-3.4.8]# make && make install # 编译安装
增加services配置(默认已经添加)
[root@localhost zabbix-3.4.8]# vim /etc/services
zabbix-agent 10050/tcp # Zabbix Agent
zabbix-agent 10050/udp # Zabbix Agent
zabbix-trapper 10051/tcp # Zabbix Trapper
zabbix-trapper 10051/udp # Zabbix Trapper
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 10
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)]> create database zabbix character set utf8; (创建zabbix数据库并设置utf8字符集)
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by 'zabbix'; (创建用户和密码均为zabbix)
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@localhost ~]#
[root@localhost ~]# cd zabbix-3.4.8/database/mysql/
[root@localhost mysql]# ls
data.sql images.sql schema.sql
[root@localhost mysql]# mysql -u root -p123456 zabbix < ./schema.sql
[root@localhost mysql]# mysql -u root -p123456 zabbix < ./images.sql
[root@localhost mysql]# mysql -u root -p123456 zabbix < ./data.sql
[root@localhost ~]# cd zabbix-3.4.8/
[root@localhost zabbix-3.4.8]# cp misc/init.d/fedora/core/zabbix_* /etc/init.d/
[root@localhost zabbix-3.4.8]# chmod 755 /etc/init.d/zabbix_*
[root@localhost zabbix-3.4.8]# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/#g" /etc/init.d/zabbix_server
[root@localhost zabbix-3.4.8]# sed -i "s#BASEDIR=/usr/local#BASEDIR=/usr/#g" /etc/init.d/zabbix_agentd
[root@localhost zabbix-3.4.8]#
[root@localhost zabbix-3.4.8]# chkconfig zabbix_server on
[root@localhost zabbix-3.4.8]# chkconfig zabbix_agentd on
[root@localhost zabbix-3.4.8]# vim /etc/zabbix/zabbix_server.conf
38 LogFile=/var/log/zabbix/zabbix_server.log
91 DBHost=localhost
100 DBName=zabbix
116 DBUser=zabbix
124 DBPassword=zabbix
131 DBSocket=/var/lib/mysql/mysql.sock
139 DBPort=3306
下面的参数可以保持默认
189 StartPollers=160
228 StartTrappers=20
236 StartPingers=100
244 StartDiscoverers=120
401 MaxHousekeeperDelete=5000
410 CacheSize=1024M
426 StartDBSyncers=16
435 HistoryCacheSize=1024M
453 TrendCacheSize=1024M
516 AlertScriptsPath=/usr/lib/zabbix/alertscripts
563 LogSlowQueries=1000
[root@localhost zabbix-3.4.8]# vim /etc/zabbix/zabbix_agentd.conf
32 LogFile=/var/log/zabbix/zabbix_agentd.log
73 EnableRemoteCommands=0
97 Server=127.0.0.1,192.168.203.179
122 StartAgents=8
138 ServerActive=192.168.203.179:10051
149 Hostname=Zabbix
238 Timeout=30
267 Include=/etc/zabbix/zabbix_agentd.d
286 UnsafeUserParameters=1
[root@localhost zabbix-3.4.8]# service zabbix_server start
Starting zabbix_server (via systemctl): [ OK ]
[root@localhost zabbix-3.4.8]# service zabbix_agentd start
Starting zabbix_agentd (via systemctl): [ OK ]
[root@localhost zabbix-3.4.8]#
[root@localhost zabbix-3.4.8]# cp -r ./frontends/php/ /var/www/html/zabbix # 复制frontend页面文件
[root@localhost zabbix-3.4.8]# chown -R apache.apache /var/www/html/zabbix
[root@localhost zabbix-3.4.8]# service httpd start
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
113 # 这里的IP地址可以用 * 号代替
114 ServerAdmin admin@zabbix
115 DocumentRoot /var/www/html/zabbix
116 ServerName zabbix
117 ErrorLog logs/dummy-zabbix-error_log
118 CustomLog logs/dummy-zabbix-access_log common
119
[root@localhost ~]# systemctl restart httpd # 重启 Apache
处理问题的思路:打开Zabbix_server.log日志文件之后看到一个报错信息
connection to database 'zabbix' failed: [1040] Too many connections
(翻译:与数据库“ zabbix”的连接失败:[1040]连接太多)
[root@localhost ~]# cd /var/log/zabbix/
[root@localhost zabbix]# ls
access_log error_log zabbix_agentd.log zabbix_server.log
[root@localhost zabbix]# vim zabbix_server.log
用vim命令打开/var/log/zabbix/zabbix_server.log日志文件
解决方案:
1.使用vim命令打开 /etc/my.cnf配置文件,在[mysqld]区域内添加下面参数
[root@localhost ~]# vim /etc/my.cnf
[mysqld]
max_connections=10000
2.配置/usr/lib/systemd/system/mariadb.service来调大打开文件数目
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 221
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)]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 214 |
+-----------------+-------+
1 row in set (0.00 sec)
MariaDB [(none)]> quit
Bye
[root@localhost ~]#
[root@localhost ~]# vim /usr/lib/systemd/system/mariadb.service
[Service]
LimitNOFILE=10000
LimitNPROC=10000
[root@localhost ~]# systemctl --system daemon-reload # 重新加载系统服务
[root@localhost ~]# systemctl restart mariadb.service # 重启 Mariadb服务
[root@localhost ~]# mysql -u root -p
Enter password:
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 2
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)]> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
MariaDB [mysql]> set global max_connections=10000;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 10000 |
+-----------------+-------+
1 row in set (0.00 sec)
MariaDB [mysql]> flush privileges;
Query OK, 0 rows affected (0.00 sec)
MariaDB [mysql]> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 9190 |
+-----------------+-------+
1 row in set (0.01 sec)
MariaDB [mysql]> quit
Bye
[root@localhost ~]# service zabbix_server restart
Restarting zabbix_server (via systemctl): [ OK ]
[root@localhost ~]# service zabbix_agentd restart
Restarting zabbix_agentd (via systemctl): [ OK ]
[root@localhost ~]#
打开Zabbix服务运行正常!