Zabbix5.0Yum安装
阅读原文时间:2023年07月08日阅读:1

1.1、操作系统配置:

1、操作系统属性(虚拟机下环境):

(1)操作系统版本:

[root@localhost ~]# cat /etc/redhat-release

CentOS Linux release 7.5.1804 (Core)

(2)ip地址:

eth1:仅主机模式——172.16.1.120/24

etho:Nat模式——10.0.0.120/24

(3)核数及内存:

线程数:2核4线程=8线程

内存:8GB

(4)磁盘容量:

sda磁盘:300GB

boot分区:200MB

swap分区:8GB

/(根)分区:剩余所有容量

2、修改主机名:

[root@localhost ~]# hostnamectl set-hostname controlnode

#关闭终端再打开,主机名就会发生改变了。

3、关闭不常用服务:

service_array=(abrt-ccpp abrt-oops abrt-vmcore abrt-xorg abrtd

auditd chronyd microcode postfix firewalld)

for ((i=0;i<${#service_array[*]};i++)); do

/usr/bin/systemctl stop ${service_array[$i]}

done

service_array=(abrt-ccpp abrt-oops abrt-vmcore abrt-xorg abrtd

auditd chronyd microcode postfix firewalld)

for ((i=0;i<${#service_array[*]};i++)); do

/usr/bin/systemctl disable ${service_array[$i]}

done

4、关闭selinux:

sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config

grep "SELINUX=disabled" /etc/selinux/config

setenforce 0

getenforce

5、安装必备的软件:

yum install lrzsz dos2unix bash-completion nmap telnet tree wget vim net-tools ntpdate salt-minion git -y

6、配置软件的安装源:

(1)配置阿里的yum源:

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

(2)配置阿里的epel源:

yum install epel-release

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

(3)设置yum和epel源的缓存:

yum clean all

yum makecache

(4)查看系统可用的源:

yum repolist enabled

7、修改字符集:

localectl set-locale LANG=zh_CN.UTF-8

source /etc/locale.conf

8、配置时间同步定时任务:

[root@localhost ~]# crontab -e

*/5 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null

9、配置操作系统的文件描述符:

echo '* - nofile 65535' >>/etc/security/limits.conf

ulimit -SHn 65535

ulimit -n

10、优化内核:

cat >>/etc/sysctl.conf<<EOF

net.ipv4.tcp_fin_timeout = 2

net.ipv4.tcp_tw_reuse = 1

#net.ipv4.tcp_tw_recycle = 1

net.ipv4.tcp_syncookies = 1

net.ipv4.tcp_keepalive_time = 600

net.ipv4.tcp_keepalive_probes = 5

net.ipv4.tcp_keepalive_intvl = 15

net.ipv4.ip_local_port_range = 1024 65000

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_max_tw_buckets =5000

net.ipv4.tcp_syn_retries = 1

net.ipv4.tcp_synack_retries = 1

net.core.somaxconn = 16384

net.core.netdev_max_backlog = 16384

net.ipv4.tcp_max_orphans = 16384

EOF

sysctl -p

1.2、配置zabbix5.0的yum源:

1、说明:

zabbix 5.0 版本于 5 月 11 日正式发布,是最新的 LTS(长期支持)版本,5.0 带来很多功能和特性。

5.0 版本对基础环境的要求有大的变化,最大的就是对 php 版本的要求,最低要求 7.2.0 版本,对 php

扩展组件版本也有要求,详见官网文档

https://www.zabbix.com/documentation/current/manual/installation/requirements

2、安装阿里云的zabbix镜像源:

(1)安装源:

[root@controlnode ~]# rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm

[root@controlnode ~]# sed -i 's#http://repo.zabbix.com#https://mirrors.aliyun.com/zabbix#' /etc/yum.repos.d/zabbix.repo

(2)启用 zabbix 前端源,修改/etc/yum.repos.d/zabbix.repo,将[zabbix-frontend]下的 enabled 改为 1:

[zabbix-frontend]

name=Zabbix Official Repository frontend - $basearch

baseurl=https://mirrors.aliyun.com/zabbix/zabbix/5.0/rhel/7/$basearch/frontend

enabled=1

gpgcheck=1

gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

(3)清除yum源之前的缓存:

[root@controlnode ~]# yum clean all

3、验证yum源:

[root@controlnode ~]# yum list | grep zabbix

1.3、安装zabbix-server、zabbix-agent、zabbix-get:

[root@controlnode ~]# yum install zabbix-server-mysql zabbix-agent zabbix-get -y

1.4、安装 Software Collections,便于后续安装高版本的 php,默认 yum 安装的 php 版本为 5.4 过低:

[root@controlnode ~]# yum install centos-release-scl -y

1.5、安装 zabbix 前端和相关环境:

[root@controlnode ~]# yum install zabbix-web-mysql-scl zabbix-apache-conf-scl -y

1.6、到此zabbix相关软件安装完成,查看安装的zabbix软件:

[root@controlnode ~]# rpm -qa | grep zabbix

zabbix-server-mysql-5.0.0-1.el7.x86_64

zabbix-get-5.0.0-1.el7.x86_64

zabbix-web-mysql-scl-5.0.0-1.el7.noarch

zabbix-release-5.0-1.el7.noarch

zabbix-agent-5.0.0-1.el7.x86_64

zabbix-web-5.0.0-1.el7.noarch

zabbix-web-deps-scl-5.0.0-1.el7.noarch

zabbix-apache-conf-scl-5.0.0-1.el7.noarch

1.7、安装mariadb数据库:

1、安装:

[root@controlnode ~]# yum install mariadb-server -y

2、优化my.cnf参数:/etc/my.cnf【2核4G的配置】:

#清空/etc/my.cnf文件中的内容,复制下面全部的参数到/etc/my.cnf中:

[client]

port=3306

default-character-set=utf8

socket=/var/lib/mysql/mysql.sock

[mysql]

no-auto-rehash

[mysqld]

user=mysql

port=3306

socket=/var/lib/mysql/mysql.sock

basedir=/usr

datadir=/var/lib/mysql

open_files_limit = 10240

symbolic-links=0

default-storage-engine = innodb

innodb_file_per_table = on

max_connections = 1000

max_connect_errors = 6000

back_log = 600

table_open_cache = 614

skip-external-locking

max_allowed_packet = 32M

character-set-server = utf8

skip-name-resolve

innodb_buffer_pool_size = 1024M

query_cache_size=16M

join_buffer_size = 2M

query_cache_limit = 4M

query_cache_min_res_unit = 2K

thread_stack = 192K

tmp_table_size = 64M

max_heap_table_size = 64M

read_buffer_size = 1M

read_rnd_buffer_size = 4M

bulk_insert_buffer_size = 16M

key_buffer_size = 384M

server-id = 1

thread_concurrency = 4

thread_cache_size = 64

sort_buffer_size = 2M

slow_query_log = ON

long_query_time = 2

slow_query_log_file = /tmp/mysql-slow.log

innodb_thread_concurrency = 2

innodb_flush_log_at_trx_commit = 2

innodb_log_buffer_size = 16M

innodb_log_file_size = 128M

lower_case_table_names = 1

innodb_additional_mem_pool_size = 16M

innodb_data_file_path = ibdata1:1024M:autoextend

innodb_file_io_threads = 4

innodb_log_files_in_group = 3

innodb_max_dirty_pages_pct = 90

innodb_lock_wait_timeout = 120

innodb_write_io_threads = 2

innodb_read_io_threads = 2

sql-mode=STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

transaction_isolation = READ-COMMITTE

[mysqldump]

quick

max_allowed_packet = 32M

[mysqld_safe]

log-error=/var/log/mariadb/mariadb.log

pid-file=/var/run/mariadb/mariadb.pid

3、启动mariadb数据库,并开机自启动:

[root@controlnode ~]# systemctl start mariadb.service

[root@controlnode ~]# systemctl enable mariadb.service

[root@controlnode ~]# netstat -tunlp | grep 3306

tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN 17226/mysqld

4、初始化Mariadb,并配置root密码:

[root@controlnode ~]# mysql_secure_installation

#root密码为 zabbix5.0@lc

5、创建zabbix库:

[root@controlnode ~]# mysql -uroot -pzabbix5.0@lc

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@"%" identified by "zabbix5.0@lc";

MariaDB [(none)]> flush privileges;

MariaDB [(none)]> quit;

6、初始化 zabbix 数据库:

[root@controlnode ~]# zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uroot -pzabbix5.0@lc zabbix

#验证数据是否导入成功

[root@controlnode ~]# mysql -uroot -pzabbix5.0@lc -e "use zabbix;show tables"

1.8、修改/etc/zabbix/zabbix_server.conf连接数据库的配置:

DBHost=127.0.0.1

DBPassword=zabbix5.0@lc

1.9、修改 zabbix 的 php 配置文件 /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf 里的时区:

php_value[date.timezone] = Asia/Shanghai

1.10、解决zabbix-web中文乱码的问题:

1、查看zabbix-web链接的字体:

[root@controlnode ~]# ls -l /etc/alternatives/zabbix-web-font

lrwxrwxrwx 1 root root 38 6月 16 12:59 /etc/alternatives/zabbix-web-font -> /usr/share/fonts/dejavu/DejaVuSans.ttf

2、在windows中找到需要的字体格式后上传到"/usr/share/fonts/dejavu/"目录下:

(1)在windows中查找需要的字体:

(2)将字体上传到"/usr/share/fonts/dejavu/"目录下:

[root@controlnode ~]# cd /usr/share/fonts/dejavu/

[root@controlnode dejavu]# rz -y

(3)对旧的字体备份后进行替换:

1)备份旧的字体:

[root@controlnode dejavu]# cp -a DejaVuSans.ttf DejaVuSans.ttf.bak

2)替换字体:

[root@controlnode dejavu]# \mv simkai.ttf DejaVuSans.ttf

1.11、启动zabbix相关服务并设置为开机自启动:

[root@controlnode ~]# systemctl start zabbix-server zabbix-agent httpd rh-php72-php-fpm

[root@controlnode ~]# systemctl enable zabbix-server zabbix-agent httpd rh-php72-php-fpm

1.12、在页面中配置连接:

1、访问zabbix-server的配置页面,点击下一步:

http://172.16.1.120/zabbix/setup.php

2、点击下一步:

3、配置mysql数据库地址和密码,点击下一步:

4、配置zabbixServer的连接地址和zabbixServer的名称(可选):

5、点击下一步:

6、配置完成:

7、ZabbixSever安装完成后,和ZabbixServer安装在同一台服务器的zabbixAgent在启动的情况下,会被ZabbixServer

添加为第一台主机,名为"Zabbix server"。

1.13、zabbixServer其它相关配置:

1、user settings 相关设置:

(1)调整前端网页为中文字体和网页的刷新时间:

(2)启用前端网页报警提示信息:

2、修改默认的 Web 访问URL:

[root@controlnode ~]# sed -i 's#DocumentRoot "/var/www/html"#DocumentRoot "/usr/share/zabbix"#g' /etc/httpd/conf/httpd.conf

[root@controlnode ~]# systemctl restart httpd

这样就可以通过http://172.16.1.120直接访问zabbix监控页面了。

3、监控zabbixServer自身所使用的数据库:

(0)Template DB MySQL模板宏和监控项:

1)宏:

2)监控项:

(1)复制MySQL监控模板到/etc/zabbix/zabbix_agentd.d/目录下:

[root@controlnode ~]# cp -a /usr/share/doc/zabbix-agent-5.0.0/userparameter_mysql.conf /etc/zabbix/zabbix_agentd.d/

(2)修改MySQL监控模板内容如下所示(比原文件增加了"-uzbx_monitor -pzabbix5.0@lc"参数):

[root@controlnode ~]# cat /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

#template_db_mysql.conf created by Zabbix for "Template DB MySQL" and Zabbix 4.2

#For OS Linux: You need create .my.cnf in zabbix-agent home directory (/var/lib/zabbix by default)

#For OS Windows: You need add PATH to mysql and mysqladmin and create my.cnf in %WINDIR%\my.cnf,C:\my.cnf,BASEDIR\my.cnf https://dev.mysql.com/doc/refman/5

.7/en/option-files.html#The file must have three strings:

#[client]

#user=zbx_monitor

#password=

#

UserParameter=mysql.ping[*], mysqladmin -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc ping

UserParameter=mysql.get_status_variables[*], mysql -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc -sNX -e "show global status"

UserParameter=mysql.version[*], mysqladmin -s -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc version

UserParameter=mysql.db.discovery[*], mysql -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc -sN -e "show databases"

UserParameter=mysql.dbsize[*], mysql -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc -sN -e "SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM INFORMATION_SCHEMA.TABL

ES WHERE TABLE_SCHEMA='$3'"UserParameter=mysql.replication.discovery[*], mysql -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc -sNX -e "show slave status"

UserParameter=mysql.slave_status[*], mysql -h"$1" -P"$2" -uzbx_monitor -pzabbix5.0@lc -sNX -e "show slave status"

(3)重启zabbix-agent:

[root@controlnode ~]# systemctl restart zabbix-agent.service

(4)找到Template DB MySQL模板,根据提示在要监控的mysql数据库上创建监控用户并赋权:

[root@controlnode ~]# mysql -uroot -pzabbix5.0@lc

MariaDB [(none)]> CREATE USER 'zbx_monitor'@'%' IDENTIFIED BY 'zabbix5.0@lc';

MariaDB [(none)]> GRANT USAGE,REPLICATION CLIENT,PROCESS,SHOW DATABASES,SHOW VIEW ON *.* TO 'zbx_monitor'@'%';

MariaDB [(none)]> quit;

(5)在Zabbix server主机中添加Template DB MySQL模板:

(6)修改Zabbix server主机的宏,mysql IP地址改为127.0.0.1:

(7)修改Template DB MySQL模板的自动发现时间间隔,改为1m(默认时1h),方便快速得到自动发现中的监控项,得到结果后再将时间改回来即可:

自动发现中包括,数据库使用大小的监控项和数据库主从复制监控的监控项,如果有不支持的监控项不会报警,支持的监控项如果异常会报警。

(8)查看检测mysql的最新数据:

1.14、zabbixServer优化操作:

说明:以上zabbixServer和zabbixAgent的配置参数都是默认,如果监控负载过高,对相关参数进行优化,如下:

1、/etc/zabbix/zabbix_server.conf:

#线程-吞吐量

StartPollers=10

StartPollersUnreachable=10

StartTrappers=10

StartPingers=10

StartDiscoverers=10

StartHTTPPollers=10

StartTimers=10

StartEscalators=10

StartAlerters=10

#缓存

CacheSize=256M

CacheUpdateFrequency=60

StartDBSyncers=10

HistoryCacheSize=256M

HistoryIndexCacheSize=256M

TrendCacheSize=256M

ValueCacheSize=256M

Timeout=15

TrapperTimeout=200

2、/etc/zabbix/zabbix-agent:

#线程-吞吐量

StartAgents=10

#缓存

RefreshActiveChecks=120

BufferSend=10

BufferSize=100

Timeout=15