Zabbix部署篇-Zabbix4.4基于NMP环境搭建

释放双眼,带上耳机,听听看~!

本文索引

Zabbix简介以及特性和原理及优缺点我这里就不讲解了,以下只讲解部署

环境

IP 应用 版本 端口 描述
192.168.31.240 Nginx 1.17.5 80 Web代理
192.168.31.240 PHP 7.3.11 9000 处理zabbix php页面
192.168.31.240 MySQL 8.0.18 8809 存储zabbix数据
192.168.31.240 Redis 5.0.5 6379 缓存php到mysql的数据
192.168.31.240 JDK 1.8.0_232 为zabbix_java提供jdk环境
192.168.31.240 Golang 1.13.5 为zabbix_agent2提供go环境
192.168.31.240 Zabbix_Server 4.4.4 10050 监控服务器
192.168.31.240 Zabbix_agent2 4.4.4 10051 二代监控客户端 golang编写
192.168.31.240 Zabbix_java 4.4.4 10052 通过jmx监控java应用

安装部署Nginx

安装Nginx依赖

[root@zbxsrv01 /]# yum install zlib.x86_64 zlib-devel.x86_64 pcre.x86_64 pcre-devel.x86_64 openssl.x86_64 openssl-devel.x86_64 -y

下载解压

[root@zbxsrv01 /]# wget -P /usr/local/src http://nginx.org/download/nginx-1.17.5.tar.gz
[root@zbxsrv01 /]# tar xf /usr/local/src/nginx-1.17.5.tar.gz -C /usr/local/src/

创建用户组并编译安装

[root@zbxsrv01 /]# useradd -M -s /bin/false -U nginx
[root@zbxsrv01 /]# cd /usr/local/src/nginx-1.17.5
[root@zbxsrv01 nginx-1.17.5]# ./configure --prefix=/usr/local/nginx \
--sbin-path=/usr/local/nginx/sbin/nginx \
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/run/nginx.pid \
--error-log-path=/usr/local/nginx/logs/error.log \
--http-log-path=/usr/local/nginx/logs/access.log \
--with-pcre \
--user=nginx \
--group=nginx \
--with-file-aio \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_v2_module \
--with-threads \
--with-http_realip_module \
--with-http_ssl_module
[root@zbxsrv01 nginx-1.17.5]# make && make install
[root@zbxsrv01 nginx-1.17.5]# echo $?
0

启动并测试Nginx

[root@zbxsrv01 nginx-1.17.5]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zbxsrv01 nginx-1.17.5]# /usr/local/nginx/sbin/nginx

浏览器测试访问

安装部署PHP

安装PHP依赖

[root@zbxsrv01 /]# yum install libxml2-devel bzip2-devel curl-devel libjpeg-devel libpng-devel freetype-devel libxslt-devel libzip-devel -y

下载并创建用户组

[root@zbxsrv01 /]# wget -P /usr/local/src https://www.php.net/distributions/php-7.3.11.tar.gz
[root@zbxsrv01 /]# tar xf /usr/local/src/php-7.3.11.tar.gz -C /usr/local/src/ 
[root@zbxsrv01 /]# useradd -M -s /bin/false -U php

安装libzip1.x版本

在php7.3版本中,要求libzip版本要大于等于0.11,否则配置出错,所以我们先把yum安装的libzip卸载掉

[root@zbxsrv01 /]# yum remove -y libzip.x86_64 libzip-devel.x86_64
[root@zbxsrv01 /]# wget -P /usr/local/src https://libzip.org/download/libzip-1.2.0.tar.gz
[root@zbxsrv01 /]# tar xf /usr/local/src/libzip-1.2.0.tar.gz -C /usr/local/src/
[root@zbxsrv01 /]# cd /usr/local/src/libzip-1.2.0
[root@zbxsrv01 libzip-1.2.0]# ./configure && make && make install
[root@zbxsrv01 libzip-1.2.0]# echo $?
0

添加libzip库路径到ld.so.conf中使其去加载

cat >> /etc/ld.so.conf << EOF
/usr/local/lib64
/usr/local/lib
/usr/lib
/usr/lib64
EOF

[root@zbxsrv01 /]# ldconfig -v
[root@zbxsrv01 /]# cp /usr/local/lib/libzip/include/zipconf.h /usr/local/include/zipconf.h

编译安装php

[root@zbxsrv01 /]# cd /usr/local/src/php-7.3.11
[root@zbxsrv01 php-7.3.11]# 
./configure --prefix=/usr/local/php \
--with-fpm-user=php \
--with-fpm-group=php \
--with-curl \
--with-freetype-dir \
--with-gd \
--with-gettext \
--with-iconv-dir \
--with-kerberos \
--with-libdir=lib64 \
--with-libxml-dir \
--with-mysqli \
--with-openssl \
--with-pcre-regex \
--with-pdo-mysql \
--with-pdo-sqlite \
--with-pear \
--with-png-dir \
--with-jpeg-dir \
--with-xmlrpc \
--with-xsl \
--with-zlib \
--with-bz2 \
--with-mhash \
--enable-fpm \
--enable-bcmath \
--enable-libxml \
--enable-inline-optimization \
--enable-mbregex \
--enable-mbstring \
--enable-opcache \
--enable-pcntl \
--enable-shmop \
--enable-soap \
--enable-sockets \
--enable-sysvsem \
--enable-sysvshm \
--enable-xml \
--enable-zip \
--enable-fpm
[root@zbxsrv01 php-7.3.11]# make && make install

建立PHP配置文件

[root@zbxsrv01 /]# cp /usr/local/src/php-7.3.11/php.ini-production /usr/local/php/lib/php.ini
[root@zbxsrv01 /]# cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
[root@zbxsrv01 /]# cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf
[root@zbxsrv01 /]# cp /usr/local/src/php-7.3.11/sapi/fpm/php-fpm.service /usr/lib/systemd/system
[root@zbxsrv01 /]# ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/
[root@zbxsrv01 /]# /usr/local/src/php-7.3.11/build/shtool install -c ext/phar/phar.phar /usr/local/php/bin

优化PHP配置

php-fpm.conf

[root@zbxsrv01 /]# grep -Ev ";|^$" /usr/local/php/etc/php-fpm.conf
[global]
pid = run/php-fpm.pid                                   #pid文件位置
error_log = log/php-fpm.log                             #日志路径
log_level = notice                                      #日志级别   {alert警报, error错误, warning警告, notice注意, debug详细}
log_limit = 65536                                       #单行字符数的日志限制(日志条目)
include=/usr/local/php/etc/php-fpm.d/*.conf             #关联文件

www.conf

[root@zbxsrv01 /]# grep -Ev ";|^$" /usr/local/php/etc/php-fpm.d/www.conf
[www]
user = php
group = php
listen = 127.0.0.1:9000
pm = dynamic
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 30
pm.max_requests = 5000
slowlog = log/$pool.log.slow
rlimit_files = 65536

参赛解释:
pm = dynamic
php-fpm分为两种模式,静态和动态,当pm = static是为静态模式,pm = dynamic时为动态模式。
php-fpm可以根据所处于的模式对进程数量进行减少或增加,在静态模式下,无法增加或减少进程,在动态模式下可以根据配置的参数进行自动调整进程数量,在繁忙的时候可以多打开几个进程数,在请求少时使用较少的进程数量,从而合理的使用资源。

pm.max_children
静态方式下开启的php-fpm进程数量,此参数指定了php-fpm的进程数量,只有在 pm = static的状态下生效

pm.start_servers
动态方式下的起始php-fpm进程数量,在pm = dynamic下,刚开始启动的php-fpm进程数量,只有在 pm = dynamic的状态下生效

pm.min_spare_servers
动态方式下的最小php-fpm进程数量,随机调整到最小的php-fpm进程数量,只有在 pm = dynamic的状态下生效

pm.max_spare_servers
动态方式下的最大php-fpm进程数量,随机调整到最大的php-fpm进程数量,只有在 pm = dynamic的状态下生效
注:pm.max_spare_servers指定的进程数量必须等于或小于pm.max_children进程数量,否则启动失败

pm.max_requests
设置每个子进程重生之前服务的请求数。对于可能存在内存泄漏的第三方模块来说是非常有用的。如果设置为 ‘0’ 则一直接受请求,等同于 PHP_FCGI_MAX_REQUESTS 环境变量。默认值:0

rlimit_files
php-fpm进程的文件打开数,此数量需要php-fpm运行用户php具备这个打开数

php.ini
需要修改的参数如下,其它参数保持默认,以下参数是Zabbix官方文档指明需要修改的地方,详情见:https://www.zabbix.com/documentation/4.4/manual/installation/install

[root@zbxsrv01 /]# vim /usr/local/php/lib/php.ini
date.timezone = Asia/Shanghai                   #指定时区
memory_limit = 512M                             #脚本消耗的内存量,
post_max_size = 100M                            #PHP最大能接受POST请求的大小
upload_max_filesize = 100M                      #PHP最大能接受上传请求的大小,在我们把模版或清单通过导入Zabbix时就是这里来限制上传大小
max_execution_time = 300                        #每个脚本最大的执行时间,以秒为单位
max_input_time = 300                            #脚本在解析请求数据花费的最大时间,以秒为单位
session.auto_start = 0                          #在请求启动时初始化时间,如果是0则为禁用
mbstring.func_overload = 0                      #用mstring函数替换单字节函数,mail()、ereg()等被mb_send_mail()、mb_ereg()等重载,0为没有超载

启动PHP

[root@zbxsrv01 /]# systemctl start php-fpm
[root@zbxsrv01 /]# systemctl enable php-fpm
[root@zbxsrv01 /]# systemctl status php-fpm
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2019-12-31 14:05:05 CST; 9s ago
 Main PID: 32304 (php-fpm)
    Tasks: 6
   Memory: 15.6M
   CGroup: /system.slice/php-fpm.service
           ├─32304 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
           ├─32305 php-fpm: pool www
           ├─32306 php-fpm: pool www
           ├─32307 php-fpm: pool www
           ├─32308 php-fpm: pool www
           └─32309 php-fpm: pool www 

Nginx代理PHP进行访问

nginx.conf

[root@zbxsrv01 /]# cat /usr/local/nginx/conf/nginx.conf
user  nginx nginx;
worker_processes  auto;
error_log logs/error.log info;
pid run/nginx.pid;
worker_rlimit_nofile 65535;

events {
    use epoll;
    worker_connections  65535;
    multi_accept on;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    charset utf-8;
    server_tokens off;

    client_header_buffer_size 4096;
    large_client_header_buffers 4 128k;
    client_header_timeout 15;
    client_body_timeout 15;
    send_timeout 65;
    client_max_body_size 1024m;
    open_file_cache max=65535 inactive=60s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 1;
    open_file_cache_errors on;
    server_names_hash_bucket_size 128;

   log_format  main  '$remote_addr" "$remote_user" "[$time_local]" "$request"'
                     ' "$status" "$body_bytes_sent" "$http_referer"'
                     ' "$http_user_agent" "$http_x_forwarded_for" "$gzip_ratio"'
                     ' "$upstream_addr" "$request_time" "$upstream_response_time" "$http_host"';
    access_log  logs/access.log  main;

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

    gzip on;
    gzip_min_length 1k;
    gzip_buffers 16 64K;
    gzip_http_version 1.1;
    gzip_comp_level 6;
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript;
    gzip_vary on;
    gzip_proxied any;
    underscores_in_headers on;
    proxy_ignore_client_abort on;

    include /usr/local/nginx/conf/conf.d/*.conf;
}

创建目录

[root@zbxsrv01 /]# mkdir /usr/local/nginx/conf/conf.d

写入代理配置

[root@zbxsrv01 /]# vim /usr/local/nginx/conf/conf.d/vhost.conf
server {
        listen 80;
        access_log /usr/local/nginx/logs/access.log main;

        location / {
            root /usr/local/nginx/html;
            index index.php index.html index.htm;
        }
        location ~* \.php$ {
            root /usr/local/nginx/html;
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9000;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
} 

创建index.php文件

cat >> /usr/local/nginx/html/index.php << EOF
<?php
phpinfo();
?>
EOF

重启Nginx

[root@zbxsrv01 /]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zbxsrv01 /]# /usr/local/nginx/sbin/nginx -s reload 

访问测试
http://192.168.31.242/index.php

安装部署Redis

下载解压编译安装

[root@zbxsrv01 /]# wget -P /usr/local/src http://download.redis.io/releases/redis-5.0.5.tar.gz
[root@zbxsrv01 /]# tar xf /usr/local/src/redis-5.0.5.tar.gz -C /usr/local/src/
[root@zbxsrv01 /]# cd /usr/local/src/redis-5.0.5
[root@zbxsrv01 redis-5.0.5]# make
[root@zbxsrv01 redis-5.0.5]# make install PREFIX=/usr/local/redis

创建配置目录

[root@zbxsrv01 /]# mkdir /usr/local/redis/{etc,data,logs,run} -p 
[root@zbxsrv01 /]# mkdir /usr/local/redis/data/rdb
[root@zbxsrv01 /]# cp /usr/local/src/redis-5.0.5/redis.conf /usr/local/redis/etc/

修改redis配置文件

cat > /usr/local/redis/etc/redis.conf << EOF
daemonize yes
port 6379
bind 0.0.0.0
protected-mode yes
pidfile "/usr/local/redis/run/redis.pid"
loglevel verbose
logfile "/usr/local/redis/logs/redis.log"
databases 16
save 900 1
save 300 10
save 60  10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum  yes
dbfilename dump.rdb
dir "/usr/local/redis/data/rdb/"
timeout 300
tcp-keepalive 300
requirepass aRqcOyx40oxG
EOF

启动并连接redis

[root@zbxsrv01 /]# /usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
[root@zbxsrv01 /]# /usr/local/redis/bin/redis-cli -a "aRqcOyx40oxG"
127.0.0.1:6379> SELECT 1
OK

加入开机自启

[root@zbxsrv01 ~]# cat >> /etc/rc.local << EOF
/usr/local/redis/bin/redis-server /usr/local/redis/etc/redis.conf
EOF

PHP安装Redis扩展

PHP官网下载redis并进行安装

[root@zbxsrv01 ~]# wget -P /usr/local/src http://pecl.php.net/get/redis-5.1.0.tgz
[root@zbxsrv01 ~]# tar xf /usr/local/src/redis-5.1.0.tgz -C /usr/local/src/
[root@zbxsrv01 ~]# cd /usr/local/src/redis-5.1.0
[root@zbxsrv01 redis-5.1.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

报错如上,“请检查您的autoconf安装和$PHP_AUTOCONF环境变量。然后,重新运行此脚本。”
解决如下:

[root@zbxsrv01 redis-5.1.0]# yum install m4.x86_64 autoconf.noarch -y 
[root@zbxsrv01 redis-5.1.0]# /usr/local/php/bin/phpize
Configuring for:
PHP Api Version:         20180731
Zend Module Api No:      20180731
Zend Extension Api No:   320180731
[root@zbxsrv01 redis-5.1.0]# ./configure --with-php-config=/usr/local/php/bin/php-config
[root@zbxsrv01 redis-5.1.0]# make && make install

修改php配置文件以载入redis模块

[root@zbxsrv01 /]# echo 'extension=redis.so' >> /usr/local/php/lib/php.ini
[root@zbxsrv01 /]# systemctl restart php-fpm
[root@zbxsrv01 /]# php-fpm -m | grep redis

再次通过phpinfo页面来搜索redis

PHP连接Redis

[root@zbxsrv01 /]# cat /usr/local/nginx/html/php-redis.php
<?php
   $redis = new Redis();
   $redis->connect('127.0.0.1', 6379);
   $redis->auth('aRqcOyx40oxG');
   $redis->select(1);
   $redis->set( "testKey" , "Hello Redis");
   echo $redis->get("testKey");
?> 

访问验证PHP连接Redis

安装部署MySQL

卸载CentOS7默认自带的mariadb

[root@zbxsrv01 /]# rpm -qa | grep mariadb
mariadb-libs-5.5.60-1.el7_5.x86_64  
[root@zbxsrv01 /]# rpm -e mariadb-libs-5.5.60-1.el7_5.x86_64 --nodeps 

下载MySQL官方yum源

[root@zbxsrv01 /]# wget -P /usr/local/src https://repo.mysql.com//mysql80-community-release-el7-1.noarch.rpm
[root@zbxsrv01 /]# cd /usr/local/src/
[root@zbxsrv01 src]# rpm -ivh mysql80-community-release-el7-1.noarch.rpm
[root@zbxsrv01 src]# yum clean all
[root@zbxsrv01 src]# yum makecache 

查看MySQL源

[root@zbxsrv01 src]# yum repolist all | grep mysql 
或
[root@zbxsrv01 src]# yum list | grep mysql-community

安装MySQL8

指定包安装

[root@zbxsrv01 src]# yum install mysql-community-server.x86_64 mysql-community-client.x86_64 mysql-community-common.x86_64 mysql-community-libs.x86_64 mysql-community-devel.x86_64 -y

设置my.cnf文件

[root@zbxsrv01 ~]# vim /etc/my.cnf
[client]
port = 8809
socket = /usr/local/mysql/run/mysql.sock

[mysqld]
server-id = 1
port = 8809
datadir = /usr/local/mysql/data
pid-file = /usr/local/mysql/run/mysqld.pid
socket = /usr/local/mysql/run/mysql.sock
log_error = /usr/local/mysql/logs/mysql_error.log
default_authentication_plugin=mysql_native_password
tmpdir = /tmp
user = mysql
bind-address = 0.0.0.0
#skip-grant-tables
skip-external-locking
skip_name_resolve = 1
character-set-server = utf8mb4
collation-server = utf8mb4_general_ci
init_connect='SET NAMES utf8mb4'
lower_case_table_names = 1
max_connections = 100
max_connect_errors = 200
max_allowed_packet = 128M
interactive_timeout = 1800
wait_timeout = 1800
tmp_table_size = 134217728
max_heap_table_size = 134217728
slow_query_log = 1
slow_query_log_file = /usr/local/mysql/logs/mysql_slow_query.log
long_query_time = 5
log_queries_not_using_indexes = 1
log_throttle_queries_not_using_indexes = 5
min_examined_row_limit = 100

创建mysql配置目录及用户组

[root@zbxsrv01 /]# mkdir /usr/local/mysql/{data,logs,run} -p
[root@zbxsrv01 /]# userdel -r mysql
[root@zbxsrv01 /]# useradd -M -s /bin/false -U mysql
[root@zbxsrv01 /]# chown -Rf mysql.mysql /usr/local/mysql/*
[root@zbxsrv01 /]# chown -Rf mysql.mysql /var/log/mysqld.log
[root@zbxsrv01 /]# chown -Rf mysql.mysql /var/lib/mysql-*

启动并加入开机自启

[root@zbxsrv01 /]# systemctl start mysqld
[root@zbxsrv01 /]# systemctl enable mysqld

查看初始化密码并修改

[root@zbxsrv01 /]# cat /usr/local/mysql/logs/mysql_error.log | grep password
2019-12-31T06:46:11.096157Z 5 [Note] [MY-010454] [Server] A temporary password is generated for root@localhost: 1J=eUQbrShBF
[root@zbxsrv01 /]# mysql -u root -p
Enter password:

mysql> ALTER USER 'root'@'localhost' IDENTIFIED BY '3edc#EDC';
Query OK, 0 rows affected (0.01 sec)

数据库加固

[root@zbxsrv01 /]# mysql_secure_installation 

数据库加密

[root@zbxsrv01 /]# mysql_ssl_rsa_setup --datadir=/usr/local/mysql/data 

查看数据库用户加密方式

[root@zbxsrv01 /]# mysql -u root -p
Enter password: 
mysql> use mysql;
mysql> select Host,User,Plugin from user;
+-----------+------------------+-----------------------+
| Host      | User             | Plugin                |
+-----------+------------------+-----------------------+
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | mysql_native_password |
+-----------+------------------+-----------------------+
4 rows in set (0.00 sec)

PHP连接MySQL

修改php.ini文件

[root@zbxsrv01 /]# vim /usr/local/php/lib/php.ini
mysqli.default_port = 8809                                      #php使用mysqli工具去连接mysql的默认端口
mysqli.default_socket = /usr/local/mysql/run/mysql.sock         #php使用mysqli工具去连接mysql的默认socket文件位置
mysqli.default_host = localhost                                 #php使用mysqli工具去连接mysql的默认host
mysqli.default_user =                                           #php使用mysqli工具去连接mysql的默认用户,可以为空

php连接mysql的语句

[root@zbxsrv01 /]# cat /usr/local/nginx/html/php-mysql.php
<?php
$servername = "localhost";
$username = "root";
$password = "3edc#EDC";

// 创建连接
$conn = new mysqli($servername, $username, $password);

// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
echo "连接成功";
?>

测试PHP连接MySQL

1)先重启php-fpm

[root@zbxsrv01 /]# systemctl restart php-fpm

2)访问地址 http://192.168.31.242/php-mysql.php


                                **END 自此 LNMP 环境部署完成,下面开始部署Zabbix**

安装部署Zabbix

安装Zabbix依赖包及环境

Zabbix Server依赖包
[root@zbxsrv01 ~]# yum install net-snmp.x86_64 net-snmp-devel.x86_64 curl.x86_64 libxml2.x86_64 libxml2-devel.x86_64 libcurl.x86_64 libcurl-devel.x86_64 fping.x86_64 OpenIPMI.x86_64 OpenIPMI-devel.x86_64 libssh2.x86_64 libssh2-devel.x86_64 openssl.x86_64 openssl-devel.x86_64 -y

Zabbix_Java依赖包
[root@zbxsrv01 ~]# yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 -y 

Zabbix_agent2代采用go开发所以要部署Golang环境
[root@zbxsrv01 ~]# wget -P /usr/local/src https://studygolang.com/dl/golang/go1.13.5.linux-amd64.tar.gz
[root@zbxsrv01 ~]# tar xf /usr/local/src/go1.13.5.linux-amd64.tar.gz -C /usr/local
cat > /etc/profile.d/go.sh << EOF
export GOROOT=/usr/local/go
export PATH=\$PATH:\$GOROOT/bin
EOF
[root@zbxsrv01 ~]# source /etc/profile 

下载解压并创建zabbix用户

[root@zbxsrv01 /]# wget -P /usr/local/src https://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/4.4.4/zabbix-4.4.4.tar.gz
[root@zbxsrv01 /]# tar xf /usr/local/src/zabbix-4.4.4.tar.gz -C /usr/local/src/
[root@zbxsrv01 /]# groupadd --system zabbix
[root@zbxsrv01 /]# useradd --system -g zabbix -d /usr/lib/zabbix -s /bin/false -c "Zabbix Monitoring System" zabbix

创建Zabbix数据库及账户和授权

[root@zbxsrv01 /]# mysql -u root -p
Enter password:  

mysql> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected, 2 warnings (0.01 sec)

mysql> create user 'zabbix'@'localhost' identified by '1qaz!QAZ';
Query OK, 0 rows affected (0.01 sec)

mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost';
Query OK, 0 rows affected, 1 warning (0.01 sec) 

导入Zabbix提供的SQL脚本到zabbix数据库

[root@zbxsrv01 /]# cd /usr/local/src/zabbix-4.4.4/database/mysql/
[root@zbxsrv01 mysql]# mysql -u zabbix -p'1qaz!QAZ' zabbix < schema.sql
[root@zbxsrv01 mysql]# mysql -u zabbix -p'1qaz!QAZ' zabbix < images.sql
[root@zbxsrv01 mysql]# mysql -u zabbix -p'1qaz!QAZ' zabbix < data.sql 

查看导入的表

[root@zbxsrv01 mysql]# mysql -u zabbix -p
Enter password:
mysql> use zabbix;

mysql> show tables;

编译安装Zabbix

[root@zbxsrv01 /]# cd /usr/local/src/zabbix-4.4.4/
[root@zbxsrv01 zabbix-4.4.4]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent2 --enable-ipv6 --enable-java --with-mysql --with-net-snmp --with-libcurl --with-libxml2 --with-zlib --with-libevent --with-libpcre --with-iconv --with-openipmi --with-openssl --with-ssh2
[root@zbxsrv01 zabbix-4.4.4]# make install
[root@zbxsrv01 zabbix-4.4.4]# echo $?
0

复制并修改启动脚本文件

agent2代不能使用传统的启动脚本来启动,所以下面没有复制agent的启动文件

[root@zbxsrv01 zabbix-4.4.4]# cp -rf /usr/local/src/zabbix-4.4.4/misc/init.d/fedora/core5/zabbix_server /etc/init.d/ 
[root@zbxsrv01 zabbix-4.4.4]# chmod u+x /etc/init.d/zabbix_server
[root@zbxsrv01 zabbix-4.4.4]# sed -i 's@\/usr\/local\/sbin@\/usr\/local\/zabbix\/sbin@g' /etc/init.d/zabbix_server

修改Zabbix_Server配置

[root@zbxsrv01 /]# grep -Ev "#|^$" /usr/local/zabbix/etc/zabbix_server.conf
ListenPort=10051
LogType=file
LogFile=/tmp/zabbix_server.log
LogFileSize=100
DebugLevel=3
PidFile=/tmp/zabbix_server.pid
SocketDir=/tmp
DBHost=localhost
DBName=zabbix
DBUser=zabbix
DBPassword=1qaz!QAZ
DBSocket=/usr/local/mysql/run/mysql.sock
DBPort=8809
HistoryStorageDateIndex=1
StartPollers=15
StartIPMIPollers=3
StartPreprocessors=10
StartPollersUnreachable=5
StartTrappers=5
StartPingers=10
StartDiscoverers=5
StartHTTPPollers=15
StartTimers=3
StartEscalators=3
StartAlerters=10
JavaGateway=192.168.31.242
JavaGatewayPort=10052
StartJavaPollers=10
StartVMwareCollectors=3
VMwareFrequency=60
VMwarePerfFrequency=60
VMwareCacheSize=64M
VMwareTimeout=10
SNMPTrapperFile=/tmp/zabbix_traps.tmp
StartSNMPTrapper=1
ListenIP=0.0.0.0
HousekeepingFrequency=12
MaxHousekeeperDelete=10000
CacheSize=1024M
CacheUpdateFrequency=60
StartDBSyncers=5
HistoryCacheSize=256M
HistoryIndexCacheSize=256M
TrendCacheSize=256M
ValueCacheSize=256M
Timeout=15
TrapperTimeout=60
UnreachablePeriod=60
UnavailableDelay=60
UnreachableDelay=45
AlertScriptsPath=/usr/local/zabbix/share/zabbix/alertscripts
ExternalScripts=/usr/local/zabbix/share/zabbix/externalscripts
FpingLocation=/sbin/fping
SSHKeyLocation=
LogSlowQueries=3000
TmpDir=/tmp
StartProxyPollers=5
ProxyConfigFrequency=60
ProxyDataFrequency=60
StartLLDProcessors=5
AllowRoot=0
User=zabbix
Include=/usr/local/zabbix/etc/zabbix_server.conf.d/*.conf
StatsAllowedIP=127.0.0.1

创建临时文件

[root@zbxsrv01 ~]# touch /tmp/zabbix_traps.tmp
[root@zbxsrv01 ~]# chown -Rf zabbix.zabbix /tmp/zabbix_traps.tmp

将Zabbix的前端页面文件复制到Nginx网页目录中

[root@zbxsrv01 /]# mkdir /usr/local/nginx/html/zabbix
[root@zbxsrv01 /]# cp -rf /usr/local/src/zabbix-4.4.4/frontends/php/* /usr/local/nginx/html/zabbix/
[root@zbxsrv01 /]# cp -rf /usr/local/nginx/html/zabbix/conf/zabbix.conf.php.example /usr/local/nginx/html/zabbix/conf/zabbix.conf.php 

修改Zabbix前端页面

[root@zbxsrv01 /]# grep -Ev "//|^$" /usr/local/nginx/html/zabbix/conf/zabbix.conf.php
<?php
global $DB, $HISTORY;
$DB['TYPE']                             = 'MYSQL';
$DB['SERVER']                   = 'localhost';
$DB['PORT']                             = '8809';
$DB['DATABASE']                 = 'zabbix';
$DB['USER']                             = 'zabbix';
$DB['PASSWORD']                 = '1qaz!QAZ';
$DB['SCHEMA']                   = '';
$ZBX_SERVER                             = 'localhost';
$ZBX_SERVER_PORT                = '10051';
$ZBX_SERVER_NAME                = '';
$IMAGE_FORMAT_DEFAULT   = IMAGE_FORMAT_PNG;

修改Nginx配置文件以代理Zabbix

[root@zbxsrv01 /]# vim /usr/local/nginx/conf/conf.d/vhost.conf
server {
        listen 80;
        access_log /usr/local/nginx/logs/access.log main;

        location / {
            root /usr/local/nginx/html/zabbix;                                  #指定我们复制的zabbix前端页面位置
            index index.php index.html index.htm;
        }
        location ~* \.php$ {
            root /usr/local/nginx/html/zabbix;                                  #指定我们复制的zabbix前端页面位置
            fastcgi_index   index.php;
            fastcgi_pass    127.0.0.1:9000;
            include         fastcgi_params;
            fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
            fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
        }
}

重启Nginx

[root@zbxsrv01 /]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@zbxsrv01 /]# /usr/local/nginx/sbin/nginx -s reload  

启动zabbix并访问

[root@zbxsrv01 /]# service zabbix_server start
[root@zbxsrv01 /]# chkconfig --add zabbix_server
[root@zbxsrv01 /]# chkconfig zabbix_server on

访问地址:http://192.168.31.242

配置Zabbix Agent2

修改zabbix agent2配置文件

[root@zbxsrv01 /]# grep -Ev "#|^$" /usr/local/zabbix/etc/zabbix_agent2.conf
PidFile=/tmp/zabbix_agent2.pid
LogType=file
LogFile=/tmp/zabbix_agent2.log
LogFileSize=100
DebugLevel=3
Server=127.0.0.1,192.168.31.242
ListenPort=10050
ServerActive=127.0.0.1,192.168.31.242
Hostname=192.168.31.242
Timeout=10
Include=/usr/local/zabbix/etc/zabbix_agentd.conf.d/*.conf
ControlSocket=/tmp/agent.sock

启动zabbix agent2

agent2不能以传统的agent使用管理脚本来启动,默认启动需放置前台

#启动agent2
[root@zbxsrv01 /]# /usr/local/zabbix/sbin/zabbix_agent2
Starting Zabbix Agent 2 [192.168.31.242]. (4.4.4)
Press Ctrl+C to exit.

#或者是用nohup来放置后台或是用Supervidord来管理
[root@zbxsrv01 /]# nohup /usr/local/zabbix/sbin/zabbix_agent2 & 
[root@zbxsrv01 /]# echo 'nohup /usr/local/zabbix/sbin/zabbix_agent2 &' >> /etc/rc.local

修改Zabbix Web页面的监控Hostname

将如下Hostname与zabbix_agent2.conf中的hostname配置一致,Visible name为可见名称

配置Zabbix_Java

修改Zabbix_java配置文件

[root@zbxsrv01 /]# grep -Ev "#|^$" /usr/local/zabbix/sbin/zabbix_java/settings.sh
LISTEN_IP="0.0.0.0"
LISTEN_PORT=10052
PID_FILE="/tmp/zabbix_java.pid"
START_POLLERS=10
TIMEOUT=15 

启动Zabbix_java

[root@zbxsrv01 /]# /usr/local/zabbix/sbin/zabbix_java/startup.sh
[root@zbxsrv01 /]# echo '/usr/local/zabbix/sbin/zabbix_java/startup.sh' >> /etc/rc.local 

Zabbix_java通过JMX监控Tomcat或者Java进程

[root@zbxsrv01 /]# cat /usr/local/tomcat/bin/catalina.sh | more
#!/bin/sh
CATALINA_OPTS="$CATALINA_OPTS \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.authenticate=false \
-Dcom.sun.management.jmxremote.port=12345 \
-Dcom.sun.management.jmxremote.ssl=false \
-Djava.rmi.server.hostname=192.168.31.242"

启动Java进程并确保环境变量载入成功

人已赞赏
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
有新消息 消息中心
搜索