CentOS 7 下快速搭建 Zabbix 3.0 环境

本笔记是基于CentOS 7下最小化安装的操作系统搭建的Zabbix3.0环境,用于做一些监控项的测试及运维自动化的测试。

前面是学习步骤的记录,后面有快速部署的命令,安装操作系统后,copy/paste就可以快速搭建一个LAMP或LNMP的Zabbix环境。

环境说明

系统版本:CentOS Linux release 7.2.1511 (Core)
内核版本:3.10.0-327.el7.x86_64
Httpd版本:2.4.6-40.el7.centos
MariaDB版本:5.5.47-1.el7_2
PHP版本:5.4.16-36.el7_1

环境准备

[root@master ~]# setenforce 0
[root@master ~]# sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@master ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
[root@master ~]# yum install epel-release.noarch wget vim gcc gcc-c++ lsof chrony tree nmap unzip rsync -y
[root@master ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

基于LAMP环境的Zabbix3.0


LAMP环境安装

[root@master ~]# yum install httpd mariadb mariadb-server mariadb-client php php-mysql

安装Zabbix服务

[root@master ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
[root@master ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
[root@master ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent

创建数据库

[root@master ~]# mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
MariaDB [(none)]> GRANT ALL ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';
MariaDB [(none)]> quit

导入数据

[root@master ~]# cd /usr/share/doc/zabbix-server-mysql-3.0.3/
[root@master zabbix-server-mysql-3.0.3]# zcat create.sql.gz | mysql -uroot -p zabbix
[root@master zabbix-server-mysql-3.0.3]# cd

修改配置

[root@master ~]# vim /etc/zabbix/zabbix_server.conf

DBHost=localhost

DBName=zabbix

DBUser=zabbix

DBPassword=zabbix

上述配置文件中,我们只需要关注DBHost、DBName、DBUser、DBPassword几项即可。这几项是配置zabbix server连接mysql数据库的参数。

[root@master ~]# vim /etc/httpd/conf.d/zabbix.conf
php_value date.timezone Asia/Chongqing

其中php_value date.timezone Asia/Chongqing主要是定义php的时区

启动

[root@master ~]# systemctl start httpd
[root@master ~]# systemctl start zabbix-server
[root@master ~]# systemctl start zabbix-agent

通过 http://ip/zabbix 访问
默认用户名/密码:Admin/zabbix,注意用户名也区分大小写

# 配置中重写了访问路径
[root@master ~]# vim /etc/httpd/conf.d/zabbix.conf
Alias /zabbix /usr/share/zabbix

基于LNMP环境的Zbbix3.0


LNMP环境安装

[root@master ~]# yum install nginx mariadb mariadb-server mariadb-client php php-mysql php-fpm -y
/run/php-fpm/php-fpm.pid

安装Zabbix服务

[root@master ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
[root@master ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
[root@master ~]# yum install zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent

修改文件属主和属组

zabbix的rpm包里默认编译apache为文件拥有者,把httpd服务替换为nginx服务,相应文件的拥有者需要进行对应的更改。

[root@master ~]# ll /etc/zabbix/
total 28
drwxr-x---. 2 apache apache    32 Apr 20 09:41 web
-rw-r--r--. 1 root   root   10341 Apr 20  2016 zabbix_agentd.conf
drwxr-xr-x. 2 root   root      37 Apr 20 09:41 zabbix_agentd.d
-rw-r-----. 1 root   zabbix 14876 Apr 20  2016 zabbix_server.conf
[root@master ~]# chown nginx:nginx /etc/zabbix/web/

[root@master ~]# ll -d /var/log/php-fpm
drwxrwx---. 2 apache root 6 Jun 23  2015 /var/log/php-fpm
[root@master ~]# chown nginx /var/log/php-fpm
[root@master ~]# ll -d /var/log/php-fpm
drwxrwx---. 2 apache root 22 Apr 20 09:53 /var/log/php-fpm

[root@master ~]# ll -d /var/lib/php/session
drwxrwx---. 2 root apache 44 Apr 20 10:22 /var/lib/php/session
[root@master ~]# chown root:nginx /var/lib/php/session/
[root@master ~]# ll -d /var/lib/php/session
drwxrwx---. 2 root nginx 44 Apr 20 10:28 /var/lib/php/session

[root@master ~]# cp -r /usr/share/zabbix /var/www
[root@master ~]# chown nginx:nginx -R /var/www/zabbix

修改php配置

[root@master ~]# sed -i 's/^user = .*/user = nginx/' /etc/php-fpm.d/www.conf
[root@master ~]# sed -i 's/^group = .*/group = nginx/' /etc/php-fpm.d/www.conf

[root@master ~]# sed -i 's/^;date.timezone =/date.timezone = Asia\/Shanghai/' /etc/php.ini
[root@master ~]# sed -i 's/^post_max_size =.*/post_max_size = 16M/' /etc/php.ini
[root@master ~]# sed -i 's/^max_execution_time =.*/max_execution_time = 300/' /etc/php.ini
[root@master ~]# sed -i 's/^max_input_time =.*/max_input_time = 300/' /etc/php.ini

创建数据库

[root@master ~]# mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
MariaDB [(none)]> GRANT ALL ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';
MariaDB [(none)]> quit

导入数据

[root@master zabbix-server-mysql-3.0.3]# cd /usr/share/doc/zabbix-server-mysql-3.0.3/
[root@master zabbix-server-mysql-3.0.3]# zcat create.sql.gz | mysql -uroot -p zabbix
[root@master zabbix-server-mysql-3.0.3]# cd

配置nginx虚拟主机

[root@master ~]# cat /etc/nginx/conf.d/zabbix.conf
server {
    root /var/www;
    index index.php index.html;
    access_log   /var/log/nginx/access_zabbix.log;
    error_log   /var/log/nginx/error_zabbix.log;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
    }
}

YUM安装的nginx服务,主配置文件里有server项配置,需要进行注销

[root@master ~]# cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
[root@master ~]# sed -i '35,54s/^/#/' /etc/nginx/nginx.conf
[root@master ~]# vim /etc/nginx/nginx.conf

[root@master ~]# cat /etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

#    server {
#        listen       80 default_server;
#        listen       [::]:80 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }
}

启动服务

[root@master ~]# systemctl start mariadb
[root@master ~]# systemctl start php-fpm
[root@master ~]# systemctl start nginx
[root@master ~]# systemctl start zabbix-server

Zabbix Agent


Zabbix Agent安装

[root@client01 ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
[root@client01 ~]# rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
[root@client01 ~]# yum install zabbix-agent

配置修改

[root@client01 ~]# vim /etc/zabbix/zabbix_agentd.conf

Server=192.168.60.103

ServerActive=192.168.60.103

Hostname=client01

启用Zabbix Agent

[root@client01 ~]# systemctl start zabbix-agent

Zabbix-Get的使用

Zabbix-Get是Zabbix中的一个应用程序,用于Zabbix-Server到Zabbix-Agent的数据获取,通常可以用来测试Agent的配置是否正确。

使用帮助

usage:
  zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address] -k item-key
  zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address]
                --tls-connect cert --tls-ca-file CA-file
                [--tls-crl-file CRL-file] [--tls-agent-cert-issuer cert-issuer]
                [--tls-agent-cert-subject cert-subject]
                --tls-cert-file cert-file --tls-key-file key-file -k item-key
  zabbix_get -s host-name-or-IP [-p port-number] [-I IP-address]
                --tls-connect psk --tls-psk-identity PSK-identity
                --tls-psk-file PSK-file -k item-key
  zabbix_get -h
  zabbix_get -V

测试 Agent 配置

[root@master zabbix-server-mysql-3.0.3]# zabbix_get -s 192.168.60.104 -k system.uname
Linux client01 3.10.0-327.el7.x86_64 #1 SMP Thu Nov 19 22:10:57 UTC 2015 x86_64

zabbix用户忘记密码

进入数据库直接更改密码

[root@master ~]# mysql -uroot -p

MariaDB [(none)]> use zabbix
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

MariaDB [zabbix]> select userid, alias, name, passwd from users;
+--------+-------+--------+----------------------------------+
| userid | alias | name   | passwd                           |
+--------+-------+--------+----------------------------------+
|      1 | Admin | Zabbix | *DEEF4D7D88CD046ECA02A80393B7780 |
|      2 | guest |        | d41d8cd98f00b204e9800998ecf8427e |
+--------+-------+--------+----------------------------------+
2 rows in set (0.00 sec)

MariaDB [zabbix]> update users set passwd=md5('zabbix') where userid='1';
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 1

Grafana 2.6

[root@master ~]# yum install https://grafanarel.s3.amazonaws.com/builds/grafana-2.6.0-1.x86_64.rpm
[root@master ~]# systemctl start grafana-server
[root@master ~]# yum install https://grafanarel.s3.amazonaws.com/builds/grafana-2.6.0-1.x86_64.rpm
[root@master ~]# cp -r grafana-zabbix-2.5.1/zabbix/  /usr/share/grafana/public/app/plugins/datasource/
[root@master ~]# systemctl restart grafana-server

[root@master ~]# mkdir /var/lib/grafana/plugins
[root@master ~]# cd /var/lib/grafana/plugins
[root@master plugins]# git clone https://github.com/alexanderzobnin/grafana-zabbix-app
[root@master plugins]# cd grafana-zabbix-app/
[root@master grafana-zabbix-app]# git pull
[root@master grafana-zabbix-app]# systemctl restart grafana-server

CentOS 7下最小化安装系统之快速部署Zabbix3.0测试环境

基于LAMP环境的Zabbix3.0测试环境

# 初始环境调整
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install epel-release.noarch wget vim gcc gcc-c++ lsof chrony tree nmap unzip rsync -y
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

# LAMP环境部署
yum install httpd mariadb mariadb-server mariadb-client php php-mysql -y

systemctl start mariadb

# MariaDB数据库安全调整
mysql -e "DELETE FROM mysql.user WHERE User=''"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -e "DROP DATABASE test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
mysql -e "FLUSH PRIVILEGES"

# 创建Zabbix服务端的数据库和使用的账号
mysql -e "CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;"
mysql -e "GRANT ALL ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';"

# Zabbix应用部署
rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
yum install zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent -y

# 导入Zabbix服务端的表结构
cd /usr/share/doc/zabbix-server-mysql-3.0.3/
zcat create.sql.gz | mysql -uroot zabbix
cd

sed -i '/^# DBPassword=/a \\nDBPassword=zabbix' /etc/zabbix/zabbix_server.conf

#sed -i 's/^;date.timezone =/date.timezone = Asia\/Shanghai/' /etc/php.ini
sed -i 's@# \(php_value date.timezone \).*@\1Asia/Shanghai@' /etc/httpd/conf.d/zabbix.conf

systemctl start httpd
systemctl start zabbix-agent
systemctl start zabbix-server

通过 http://ip/zabbix 访问 默认用户名/密码:Admin/zabbix

基于LNMP环境的Zabbix3.0测试环境

# 初始环境调整
setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install epel-release.noarch wget vim gcc gcc-c++ lsof chrony tree nmap unzip rsync -y
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7

# LNMP环境部署
yum install nginx mariadb mariadb-server mariadb-client php php-mysql php-fpm -y

cat > /etc/nginx/conf.d/zabbix.conf << \EOF
server {
    root /var/www;
    index index.php index.html;
    access_log  /var/log/nginx/access_zabbix.log;
    error_log   /var/log/nginx/error_zabbix.log;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;
    }
}
EOF

sed -i '35,54s/^/#/' /etc/nginx/nginx.conf

sed -i 's/^;date.timezone =/date.timezone = Asia\/Shanghai/' /etc/php.ini
sed -i 's/^post_max_size =.*/post_max_size = 16M/' /etc/php.ini
sed -i 's/^max_execution_time =.*/max_execution_time = 300/' /etc/php.ini
sed -i 's/^max_input_time =.*/max_input_time = 300/' /etc/php.ini

sed -i 's/^user = .*/user = nginx/' /etc/php-fpm.d/www.conf
sed -i 's/^group = .*/group = nginx/' /etc/php-fpm.d/www.conf
chown nginx /var/log/php-fpm

systemctl start mariadb

# MariaDB数据库安全调整
mysql -e "DELETE FROM mysql.user WHERE User=''"
mysql -e "DELETE FROM mysql.user WHERE User='root' AND Host NOT IN ('localhost', '127.0.0.1', '::1')"
mysql -e "DROP DATABASE test;"
mysql -e "DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%'"
mysql -e "FLUSH PRIVILEGES"

# 创建Zabbix服务端的数据库和使用的账号
mysql -e "CREATE DATABASE zabbix DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;"
mysql -e "GRANT ALL ON zabbix.* TO 'zabbix'@'%' IDENTIFIED BY 'zabbix';"

# Zabbix应用部署
rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
yum install zabbix-server-mysql zabbix-web-mysql zabbix-get zabbix-agent -y

# 导入Zabbix服务端的表结构
cd /usr/share/doc/zabbix-server-mysql-3.0.3/
zcat create.sql.gz | mysql -uroot zabbix
cd

# 文件权限修改
chown nginx:nginx -R /etc/zabbix/web/
cp -r /usr/share/zabbix /var/www
chown nginx:nginx -R /var/www/zabbix
chown root:nginx /var/lib/php/session

sed -i '/^# DBPassword=/a \\nDBPassword=zabbix' /etc/zabbix/zabbix_server.conf

systemctl start nginx
systemctl start php-fpm
systemctl start zabbix-agent
systemctl start zabbix-server

通过 http://ip/zabbix 访问 默认用户名/密码:Admin/zabbix

Zabbix Agent快速部署

setenforce 0
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
yum install vim lsof chrony -y

rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-release-3.0-1.el7.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
yum install zabbix-agent -y

sed -i 's/^Server=.*/Server=192.168.60.103/' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^ServerActive=.*/ServerActive=192.168.60.103/' /etc/zabbix/zabbix_agentd.conf
sed -i 's/^Hostname=.*/Hostname=client01/' /etc/zabbix/zabbix_agentd.conf

systemctl start zabbix-agent

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,013评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,205评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,370评论 0 342
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,168评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,153评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,954评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,271评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,916评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,382评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,877评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,989评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,624评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,209评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,199评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,418评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,401评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,700评论 2 345

推荐阅读更多精彩内容