CentOS搭建Zabbix监控

2020-05-23  本文已影响0人  王哈哈就很棒

二进制包安装

修改yum源

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

添加 Zabbix 软件仓库
这里使用了阿里云的zabbix

rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

Zabbix 前端需要额外的基础安装包。 您需要在运行 Zabbix 前端的系统中启用可选 rpms 的软件仓库

yum -y install yum-utils
yum-config-manager --enable rhel-7-server-optional-rpms

更换zabbix国内源
vim /etc/yum.repos.d/zabbix.repo

[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1

添加gpgkey

curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

yum makecache -y


安装 Server/proxy/前端

yum install zabbix-server-mysql -y

安装 Zabbix proxy 并使用 MySQL 数据库

yum install zabbix-proxy-mysql -y

安装 Zabbix 前端

yum install zabbix-web-mysql -y

安装mariadb

yum -y install mariadb-server

开启mariadb

systemctl start mariadb

创建数据库,创建zabbix用户
执行mysql -uroot进入mysql命令行
创建zabbix数据库

create database zabbix character set utf8 collate utf8_bin;

创建zabbix用户

grant all on zabbix.* to zabbix@'localhost' identified by '123456';

退出mysql命令行

导入数据到数据库

zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

为 Zabbix server/proxy 配置数据库
vim /etc/zabbix/zabbix_server.conf

DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=<password>

开启zabbix-server

service zabbix-server start

此时会发现zabbix启动不了, 关闭selinux才可以启动
vim /etc/selinux/config
SELINUX=enforcing改为SELINUX=disabled
重启才能生效

查看selinux状态

getenforce

Zabbix 前端配置
vim /etc/httpd/conf.d/zabbix.conf

php_value max_execution_time 300
php_value memory_limit 128M
php_value post_max_size 16M
php_value upload_max_filesize 2M
php_value max_input_time 300
php_value always_populate_raw_post_data -1
php_value date.timezone Asia/Shanghai

重启服务

systemctl restart httpd

安装zabbix-agent

yum -y install zabbix-agent

修改zabbix-agent配置
vim /etc/zabbix/zabbix_agentd.conf
修改如下选项

Server  # 修改为你的zabbix服务器的IP
ServerActive  # 修改为你的zabbix服务器的IP
ListenIP  # 修改监听的IP,例如0.0.0.0
ListenPort  # 修改你监听的端口
Hostname  # 修改你的主机名称

启动zabbix-agent

service zabbix-agent start

windows zabbix-agent下载 https://www.zabbix.com/downloads/4.0.23/zabbix_agent-4.0.23-windows-amd64-openssl.msi

设置服务自启动

systemctl disable firewalld
systemctl enable httpd
systemctl enable mariadb
systemctl enable zabbix-server
systemctl enable zabbix-agent

访问你的ip/zabbix
默认用户名 Admin
默认密码 zabbix

图表中文字体显示问题
vim /usr/share/zabbix/include/defines.inc.php
/usr/share/zabbix/assets/fonts
替换可用的中文字体即可



问题

点击监控项没有显示,控制台提示500错误处理方法

PHP版本太低,而zabbix的php文件了写了array_column函数,而在items.php中找不到,所以造成这样的错误

vim /usr/share/zabbix/items.php
添加

if (!function_exists('array_column')) {
    function array_column($arr2, $column_key) {
        $data = [];
        foreach ($arr2 as $key => $value) {
            $data[] = $value[$column_key];
        }
        return $data;
    }
}

脚本

一键安装zabbix

#!/bin/bash

# 下载wget
yum -y install wget

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

# 添加 Zabbix 软件仓库
# 这里使用了阿里云的zabbix源
rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm

# Zabbix 前端需要额外的基础安装包。 您需要在运行 Zabbix 前端的系统中启用可选 rpms 的软件仓库
yum -y install yum-utils
yum-config-manager --enable rhel-7-server-optional-rpms

# 更换zabbix国内源
(
cat <<'EOF'
[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=https://mirrors.aliyun.com/zabbix/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1
EOF
) > /etc/yum.repos.d/zabbix.repo

# 添加gpgkey
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX-A14FE591 -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
curl https://mirrors.aliyun.com/zabbix/RPM-GPG-KEY-ZABBIX -o /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX

# yum makecache -y
yum makecache -y

# 安装 Server/proxy/前端
yum install zabbix-server-mysql -y

# 安装 Zabbix proxy 并使用 MySQL 数据库
yum install zabbix-proxy-mysql -y

# 安装 Zabbix 前端
yum install zabbix-web-mysql -y

# 安装mariadb
yum -y install mariadb-server

# 开启mariadb
systemctl start mariadb

# 创建数据库,创建zabbix用户
mysql --user='root' -e "create database zabbix character set utf8 collate utf8_bin;"
mysql --user='root' -e "grant all on zabbix.* to zabbix@'localhost' identified by '123456';"

# 导入数据到数据库
zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p123456 zabbix

# 为 Zabbix server/proxy 配置数据库
sed -i '124,124s/# DBPassword=/DBPassword=123456/g' /etc/zabbix/zabbix_server.conf

# 开启zabbix-server
service zabbix-server start

# 将SELINUX=enforcing改为SELINUX=disabled
sed -i '7,7s/# SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config

# Zabbix 前端配置
sed -i '20,20s/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Shanghai/g' /etc/httpd/conf.d/zabbix.conf

# 重启httpd服务
systemctl restart httpd

# 安装zabbix-agent
yum -y install zabbix-agent

# 启动zabbix-agent
service zabbix-agent start

# 设置服务自启动
systemctl disable firewalld
systemctl enable httpd
systemctl enable mariadb
systemctl enable zabbix-server
systemctl enable zabbix-agent

# 关闭防火墙
systemctl stop firewalld

# 重启
reboot

上一篇下一篇

猜你喜欢

热点阅读