Zabbix分布式监控应用

基础环境

[root@zabbix-server ~]# hostnamectl set-hostname  zabbix-server
[root@zabbix-agent ~]# hostnamectl set-hostname  zabbix-agent
[root@zabbix-server ~]# systemctl disable firewalld --now && setenforce 0
[root@zabbix-server ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@zabbix-agent ~]# systemctl disable firewalld --now && setenforce 0
[root@zabbix-agent ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@zabbix-server ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.169.204 zabbix-server
192.168.169.205 zabbix-agent

[root@zabbix-server ~]# ssh-keygen
[root@zabbix-server ~]# ssh-copy-id zabbix-server
[root@zabbix-server ~]# ssh-copy-id zabbix-agent

二进制包地址 Zabbix Cloud Images and Appliances

上传LNMP环境包(zabbix服务端)

[root@zabbit-server ~]# ll
total 420
-rw-------. 1 root root   1219 Jun 16 01:02 anaconda-ks.cfg
drwxr-xr-x. 7 root root    251 Dec  8  2023 lnmp1.6
-rw-r--r--. 1 root root 161833 Nov 18  2020 lnmp1.6.tar.gz
-rw-r--r--. 1 root root 184351 Dec  7 21:43 lnmp-install.log

[root@zabbit-server ~]# cd lnmp1.6
[root@zabbit-server lnmp1.6]# cat lnmp.conf 

Download_Mirror='https://soft.vpser.net'

Nginx_Modules_Options=''
PHP_Modules_Options=''

##MySQL/MariaDB database directory##
MySQL_Data_Dir='/usr/local/mysql/var'
MariaDB_Data_Dir='/data/mysql'		### 修改路径
##Default website home directory##
Default_Website_Dir='/home/wwwroot/default'

Enable_Nginx_Openssl='y'
Enable_PHP_Fileinfo='n'
Enable_Nginx_Lua='n'
Enable_Swap='y'

### 创建目录
[root@zabbit-server lnmp1.6]# mkdir -p /data/mysql

### 运行脚本
[root@http lnmp1.6]# ./install.sh lnmp

### 设置MySQL版本						4
### 设置MySQL密码						000000
### 选择InnoDB引擎是否开启		y
### 选择PHP版本							6
### 选择不安装内存分配器			1
### 按Enter键开始安装
### 部署成功显示

任务实施

### 安装依赖包
[root@zabbix-server ~]# yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel

### 创建 zabbix 用户
[root@zabbix-server ~]# useradd -s /sbin/nologin zabbix

[root@zabbix-server ~]# mv zabbix-4.0.3.tar.gz /usr/local/src/
[root@zabbix-server src]# tar -zxvf zabbix-4.0.3.tar.gz
[root@zabbix-server src]# mkdir /usr/local/zabbix
[root@zabbix-server src]# mv /usr/local/src/zabbix-4.0.3/* /usr/local/zabbix/
[root@zabbix-server src]# cd /usr/local/zabbix/
[root@zabbix-server zabbix]# ./configure --prefix=/usr/local/zabbix --enable-server --enable-agent --with-mysql=/usr/local/mysql/bin/mysql_config --with-net-snmp --with-libcurl --with-libxml2

......
***********************************************************
*            Now run 'make install'                       *
*                                                         *
*            Thank you for using Zabbix!                  *
*              <http://www.zabbix.com>                    *
***********************************************************

--prefix 指定安装目录
--enable-server 安装 Zabbix server
--enable-agent 安装 Zabbix agent
--with-mysql 用 MySQL 来存储

环境变量

[root@zabbix-server zabbix]# vim /etc/profile
export PATH=$PATH:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/
[root@zabbix-server zabbix]# source /etc/profile

[root@zabbix-server zabbix]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/:/usr/local/zabbix/sbin/:/usr/local/zabbix/bin/

[root@zabbix-server zabbix]# zabbix_server --version
zabbix_server (Zabbix) 4.0.3
Revision 87993 20 December 2018, compilation time: Dec 12 2023 23:42:12

Copyright (C) 2018 Zabbix SIA
License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it according to
the license. There is NO WARRANTY, to the extent permitted by law.

配置启动脚本

[root@zabbix-server zabbix]# cp /usr/local/zabbix/misc/init.d/fedora/core/zabbix_server /etc/init.d/
[root@zabbix-server zabbix]# cp /usr/local/zabbix/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/

[root@zabbix-server zabbix]# cat /etc/init.d/zabbix_server | grep BASEDIR
        BASEDIR=/usr/local/zabbix/		### zabbix安装目录
        FULLPATH=$BASEDIR/sbin/$BINARY_NAME
        
[root@zabbix-server zabbix]# cat /etc/init.d/zabbix_agentd | grep BASEDIR   
        BASEDIR=/usr/local/zabbix/		### zabbix安装目录
        FULLPATH=$BASEDIR/sbin/$BINARY_NAME
        
### 修改权限
[root@zabbix-server zabbix]# chmod 755 /etc/init.d/zabbix_*
[root@zabbix-server zabbix]# ll /etc/init.d/zabbix_*
-rwxr-xr-x 1 root root 2178 Dec 12 23:48 /etc/init.d/zabbix_agentd
-rwxr-xr-x 1 root root 2189 Dec 12 23:47 /etc/init.d/zabbix_server

### 启动服务
[root@zabbix-server zabbix]# /etc/init.d/zabbix_server start
Starting zabbix_server (via systemctl):                    [  OK  ]
[root@zabbix-server zabbix]# /etc/init.d/zabbix_agentd start
Starting zabbix_agentd (via systemctl):                    [  OK  ]

[root@zabbix-server zabbix]# /etc/init.d/zabbix_server status
● zabbix_server.service - SYSV: Starts and stops Zabbix Server using chkconfig
   Loaded: loaded (/etc/rc.d/init.d/zabbix_server; bad; vendor preset: disabled)
   Active: active (running) since Tue 2023-12-12 23:50:16 CST; 48s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 11716 ExecStart=/etc/rc.d/init.d/zabbix_server start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/zabbix_server.service
           └─11727 /usr/local/zabbix//sbin/zabbix_server

Dec 12 23:50:16 zabbix-server systemd[1]: Starting SYSV: Starts and stops Zabbix Server using chkconfig...
Dec 12 23:50:16 zabbix-server zabbix_server[11716]: Starting zabbix_server:  [  OK  ]
Dec 12 23:50:16 zabbix-server systemd[1]: Started SYSV: Starts and stops Zabbix Server using chkconfig.

[root@zabbix-server zabbix]# /etc/init.d/zabbix_agentd status
● zabbix_agentd.service - SYSV: Starts and stops Zabbix Agent using chkconfig
   Loaded: loaded (/etc/rc.d/init.d/zabbix_agentd; bad; vendor preset: disabled)
   Active: active (running) since Tue 2023-12-12 23:50:23 CST; 44s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 11742 ExecStart=/etc/rc.d/init.d/zabbix_agentd start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/zabbix_agentd.service
           ├─11753 /usr/local/zabbix//sbin/zabbix_agentd
           ├─11755 /usr/local/zabbix//sbin/zabbix_agentd: collector [idle 1 sec]
           ├─11756 /usr/local/zabbix//sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─11757 /usr/local/zabbix//sbin/zabbix_agentd: listener #2 [waiting for connection]
           ├─11758 /usr/local/zabbix//sbin/zabbix_agentd: listener #3 [waiting for connection]
           └─11759 /usr/local/zabbix//sbin/zabbix_agentd: active checks #1 [idle 1 sec]

Dec 12 23:50:23 zabbix-server systemd[1]: Starting SYSV: Starts and stops Zabbix Agent using chkconfig...
Dec 12 23:50:23 zabbix-server zabbix_agentd[11742]: Starting zabbix_agentd:  [  OK  ]
Dec 12 23:50:23 zabbix-server systemd[1]: Started SYSV: Starts and stops Zabbix Agent using chkconfig.
数据库

登录数据库创建 Zabbix 用户和导入 Zabbix 库

[root@zabbix-server zabbix]# mysql -h 127.0.0.1 -uroot -p000000
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.5.62-log Source distribution

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database zabbix character set utf8 collate utf8_bin;	### 创建 Zabbix 库和设置格式
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on zabbix.* to 'zabbix'@'localhost' identified by 'zabbix';
Query OK, 0 rows affected (0.00 sec)

mysql> grant all privileges on zabbix.* to 'zabbix'@'%' identified by 'zabbix';           
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> set names utf8;
Query OK, 0 rows affected (0.00 sec)

mysql> use zabbix;
Database changed

mysql> source /usr/local/zabbix/database/mysql/schema.sql;

mysql> source /usr/local/zabbix/database/mysql/data.sql;

mysql> source /usr/local/zabbix/database/mysql/images.sql;

mysql> show tables;
+----------------------------+
| Tables_in_zabbix           |
+----------------------------+
| acknowledges               |
| actions                    |
| alerts                     |
| application_discovery      |
| application_prototype      |
| application_template       |
| applications               |
| auditlog                   |
| auditlog_details           |
| autoreg_host               |
| conditions                 |
| config                     |
| corr_condition             |
| corr_condition_group       |
| corr_condition_tag         |
| corr_condition_tagpair     |
| corr_condition_tagvalue    |
| corr_operation             |
| correlation                |
| dashboard                  |
| dashboard_user             |
| dashboard_usrgrp           |
| dbversion                  |
| dchecks                    |
| dhosts                     |
| drules                     |
| dservices                  |
| escalations                |
| event_recovery             |
| event_suppress             |
| event_tag                  |
| events                     |
| expressions                |
| functions                  |
| globalmacro                |
| globalvars                 |
| graph_discovery            |
| graph_theme                |
| graphs                     |
| graphs_items               |
| group_discovery            |
| group_prototype            |
| history                    |
| history_log                |
| history_str                |
| history_text               |
| history_uint               |
| host_discovery             |
| host_inventory             |
| hostmacro                  |
| hosts                      |
| hosts_groups               |
| hosts_templates            |
| housekeeper                |
| hstgrp                     |
| httpstep                   |
| httpstep_field             |
| httpstepitem               |
| httptest                   |
| httptest_field             |
| httptestitem               |
| icon_map                   |
| icon_mapping               |
| ids                        |
| images                     |
| interface                  |
| interface_discovery        |
| item_application_prototype |
| item_condition             |
| item_discovery             |
| item_preproc               |
| items                      |
| items_applications         |
| maintenance_tag            |
| maintenances               |
| maintenances_groups        |
| maintenances_hosts         |
| maintenances_windows       |
| mappings                   |
| media                      |
| media_type                 |
| opcommand                  |
| opcommand_grp              |
| opcommand_hst              |
| opconditions               |
| operations                 |
| opgroup                    |
| opinventory                |
| opmessage                  |
| opmessage_grp              |
| opmessage_usr              |
| optemplate                 |
| problem                    |
| problem_tag                |
| profiles                   |
| proxy_autoreg_host         |
| proxy_dhistory             |
| proxy_history              |
| regexps                    |
| rights                     |
| screen_user                |
| screen_usrgrp              |
| screens                    |
| screens_items              |
| scripts                    |
| service_alarms             |
| services                   |
| services_links             |
| services_times             |
| sessions                   |
| slides                     |
| slideshow_user             |
| slideshow_usrgrp           |
| slideshows                 |
| sysmap_element_trigger     |
| sysmap_element_url         |
| sysmap_shape               |
| sysmap_url                 |
| sysmap_user                |
| sysmap_usrgrp              |
| sysmaps                    |
| sysmaps_elements           |
| sysmaps_link_triggers      |
| sysmaps_links              |
| tag_filter                 |
| task                       |
| task_acknowledge           |
| task_check_now             |
| task_close_problem         |
| task_remote_command        |
| task_remote_command_result |
| timeperiods                |
| trends                     |
| trends_uint                |
| trigger_depends            |
| trigger_discovery          |
| trigger_tag                |
| triggers                   |
| users                      |
| users_groups               |
| usrgrp                     |
| valuemaps                  |
| widget                     |
| widget_field               |
+----------------------------+
144 rows in set (0.00 sec)
编辑 Zabbix server 的配置
### 先备份
[root@zabbix-server zabbix]# cp /usr/local/zabbix/etc/zabbix_server.conf{,.bak}

[root@zabbix-server zabbix]# ll /usr/local/zabbix/etc/
total 44
-rw-r--r-- 1 root root 10630 Dec 12 23:42 zabbix_agentd.conf
drwxr-xr-x 2 root root     6 Dec 12 23:42 zabbix_agentd.conf.d
-rw-r--r-- 1 root root 16332 Dec 12 23:42 zabbix_server.conf
-rw-r--r-- 1 root root 16332 Dec 12 23:59 zabbix_server.conf.bak
drwxr-xr-x 2 root root     6 Dec 12 23:42 zabbix_server.conf.d

# vi /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log
DBHost=127.0.0.1 					### 指定数据库信息(需配合参数 DBSocket 使用) 
DBName=zabbix							###服务端连接数据库的库名
DBUser=zabbix							###连接数据库的用户
DBPassword=zabbix					###连接数据库的密码
DBPort=3306								###指定连接数据库的端口
Timeout=30								###与 AGNET 和其它外部设备通信超时设置,单位为秒
LogSlowQueries=3000#			##用于服务端数据库慢查询功能,单位是毫秒
DBSocket=/tmp/mysql.sock	###指定 MYSQL 的 SOCK 连接路径

### 修改目录权限
[root@zabbix-server zabbix]# chown -R zabbix:zabbix /usr/local/zabbix/

### 重启服务
[root@zabbix-server zabbix]# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl):                  [  OK  ]
Zabbix Web 界面部署

1. 配置 Nginx 虚拟主机

[root@zabbix-server ~]# cat /usr/local/nginx/conf/nginx.conf
user  www www;

worker_processes auto;
worker_cpu_affinity auto;

error_log  /home/wwwlogs/nginx_error.log  crit;

pid        /usr/local/nginx/logs/nginx.pid;

#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;

events
    {
        use epoll;
        worker_connections 51200;
        multi_accept off;
        accept_mutex off;
    }

http
    {
        include       mime.types;
        default_type  application/octet-stream;

        server_names_hash_bucket_size 128;
        client_header_buffer_size 32k;
        large_client_header_buffers 4 32k;
        client_max_body_size 50m;

        sendfile on;
        sendfile_max_chunk 512k;
        tcp_nopush on;

        keepalive_timeout 60;

        tcp_nodelay on;

        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
        fastcgi_buffer_size 64k;
        fastcgi_buffers 4 64k;
        fastcgi_busy_buffers_size 128k;
        fastcgi_temp_file_write_size 256k;

        gzip on;
        gzip_min_length  1k;
        gzip_buffers     4 16k;
        gzip_http_version 1.1;
        gzip_comp_level 2;
        gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
        gzip_vary on;
        gzip_proxied   expired no-cache no-store private auth;
        gzip_disable   "MSIE [1-6]\.";

        #limit_conn_zone $binary_remote_addr zone=perip:10m;
        ##If enable limit_conn_zone,add "limit_conn perip 10;" to server section.

        server_tokens off;
        access_log off;

#server
#    {
#        listen 80 default_server reuseport;
#        #listen [::]:80 default_server ipv6only=on;
#        server_name _;
#        index index.html index.htm index.php;
#        root  /data/nginx/zabbix/;
#
#        #error_page   404   /404.html;
#
#        # Deny access to PHP files in specific directory
#        #location ~ /(wp-content|uploads|wp-includes|images)/.*\.php$ { deny all; }
#
#        include enable-php.conf;
#
#        location /nginx_status
#        {
#            stub_status on;
#            access_log   off;
#        }
#
#        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
#        {
#            expires      30d;
#        }
#
#        location ~ .*\.(js|css)?$
#        {
#            expires      12h;
#        }
#
#        location ~ /.well-known {
#            allow all;
#        }
#
#        location ~ /\.
#        {
#            deny all;
#        }
#
#        access_log  /home/wwwlogs/access.log;
#    }
include vhost/*.conf;
}
[root@zabbix-server zabbix]# vim /usr/local/nginx/conf/vhost/zabbix.com.conf
server {
    listen 80;
    server_name zabbix.com www.zabbix.com;
    index index.html index.htm index.php;
    root /data/nginx/zabbix;

    ##Zabbix Web 界面数据目录
    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;
    }
}

源码安装包路径下的文件 cp 到 Zabbix Web 文件目录当中

[root@zabbix-server zabbix]# mkdir -p /data/nginx/zabbix
[root@zabbix-server zabbix]# /etc/init.d/nginx reload
Reload nginx...  done

[root@zabbix-server zabbix]# cp -rf /usr/local/zabbix/frontends/php/* /data/nginx/zabbix/

2. 修改 php.ini 的配置文件

[root@zabbix-server zabbix]# vi /usr/local/php/etc/php.ini
post_max_size = 32M
max_execution_time = 350
max_input_time = 350
date.timezone = Asia/Shanghai

3. 修改 php-fpm.conf 的配置文件

[root@zabbix-server zabbix]# vi /usr/local/php/etc/php-fpm.conf
[www]
listen = 127.0.0.1:9000

### 重启 php-fpm 服务
[root@zabbix-server zabbix]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@zabbix-server ~]# ps -ef | grep mysql
root      33547      1  0 05:27 pts/0    00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zabbix-server.pid
mysql     34042  33547  1 05:27 pts/0    00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=zabbix-server.err --open-files-limit=65535 --pid-file=/data/mysql/zabbix-server.pid --socket=/tmp/mysql.sock --port=3306
root      34131   1761  0 05:28 pts/0    00:00:00 grep --color=auto mysql

[root@zabbix-server ~]# ps -ef | grep nginx
root        976      1  0 Dec12 ?        00:00:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
www       12082    976  0 00:22 ?        00:00:02 nginx: worker process
www       12083    976  0 00:22 ?        00:00:02 nginx: worker process
root      34170   1761  0 05:28 pts/0    00:00:00 grep --color=auto nginx

[root@zabbix-server ~]# ps -ef | grep php-fpm
root      12004      1  0 00:12 ?        00:00:01 php-fpm: master process (/usr/local/php/etc/php-fpm.conf)
www       12005  12004  0 00:12 ?        00:00:09 php-fpm: pool www
www       12006  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12007  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12008  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12009  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12010  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12011  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12012  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12013  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12014  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12015  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12016  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12017  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12018  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12019  12004  0 00:12 ?        00:00:11 php-fpm: pool www
www       12020  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12021  12004  0 00:12 ?        00:00:11 php-fpm: pool www
www       12022  12004  0 00:12 ?        00:00:11 php-fpm: pool www
www       12023  12004  0 00:12 ?        00:00:11 php-fpm: pool www
www       12024  12004  0 00:12 ?        00:00:10 php-fpm: pool www
www       12525  12004  0 00:34 ?        00:00:10 php-fpm: pool www
www       13712  12004  0 00:54 ?        00:00:10 php-fpm: pool www
www       14740  12004  0 01:08 ?        00:00:10 php-fpm: pool www
www       19705  12004  0 02:30 ?        00:00:08 php-fpm: pool www
www       19706  12004  0 02:30 ?        00:00:08 php-fpm: pool www
www       19707  12004  0 02:30 ?        00:00:08 php-fpm: pool www
www       20150  12004  0 02:38 ?        00:00:07 php-fpm: pool www
root      34176   1761  0 05:29 pts/0    00:00:00 grep --color=auto php-fpm

[root@zabbix-server ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      22151/sendmail: acc 
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      11753/zabbix_agentd 
tcp        0      0 0.0.0.0:10051           0.0.0.0:*               LISTEN      14566/zabbix_server 
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      12004/php-fpm: mast 
tcp        0      0 0.0.0.0:3306            0.0.0.0:*               LISTEN      34042/mysqld        
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      976/nginx: master p 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      942/sshd            
tcp6       0      0 :::22                   :::*                    LISTEN      942/sshd

4. Web 界面部署安装

初始页面

Zabbix分布式监控应用_第1张图片

Zabbix分布式监控应用_第2张图片

Zabbix分布式监控应用_第3张图片

Zabbix分布式监控应用_第4张图片

Zabbix分布式监控应用_第5张图片

Zabbix分布式监控应用_第6张图片

[root@zabbix-server ~]# mv zabbix.conf.php /data/nginx/zabbix/conf

[root@zabbix-server ~]# ll /data/nginx/zabbix/conf
total 12
-rw-r--r-- 1 root root 1036 Dec 13 00:09 maintenance.inc.php
-rw-r--r-- 1 root root  428 Dec 12 16:27 zabbix.conf.php
-rw-r--r-- 1 root root  741 Dec 13 00:09 zabbix.conf.php.example

Zabbix分布式监控应用_第7张图片

登录界面:(账号:Admin 密码:zabbix)
Zabbix分布式监控应用_第8张图片

Zabbix分布式监控应用_第9张图片

Zabbix 客户端部署

1. 下载 Zabbix 客户端安装包

[root@zabbix-agent ~]# wget http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.3-1.el7.x86_64.rpm
--2023-12-13 00:41:21--  http://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-agent-4.0.3-1.el7.x86_64.rpm
Resolving repo.zabbix.com (repo.zabbix.com)... 178.128.6.101, 2604:a880:2:d0::2062:d001
Connecting to repo.zabbix.com (repo.zabbix.com)|178.128.6.101|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 394640 (385K) [application/x-redhat-package-manager]
Saving to: ‘zabbix-agent-4.0.3-1.el7.x86_64.rpm’

100%[=================================================================================================================================>] 394,640     12.9KB/s   in 14s    

2023-12-13 00:41:36 (26.8 KB/s) - ‘zabbix-agent-4.0.3-1.el7.x86_64.rpm’ saved [394640/394640]

[root@zabbix-agent ~]# rpm -ivh zabbix-agent-4.0.3-1.el7.x86_64.rpm 
warning: zabbix-agent-4.0.3-1.el7.x86_64.rpm: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:zabbix-agent-4.0.3-1.el7         ################################# [100%]
安装完后我们后面经常使用的文件如路径如下:
/etc/zabbix/zabbix_agentd.conf			### zabbix_agentd 配置文件
/etc/zabbix/zabbix_agentd.d					### zabbix_agentd 进程文件
/var/run/zabbix/zabbix_agentd.pid		###zabbix_agentd pid 文件路径 
/var/log/zabbix/zabbix_agentd.log		###zabbix_agentd 日志文件路径

修改配置文件

### 这个命令的作用是从/etc/zabbix/zabbix_agentd.conf.bak文件中筛选出不以空行或以#开头的行,并将结果输出到/etc/zabbix/zabbix_agentd.conf文件中

[root@zabbix-agent ~]# egrep -v '^$|#' /etc/zabbix/zabbix_agentd.conf.bak > /etc/zabbix/zabbix_agentd.conf

[root@zabbix-agent ~]# vim /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0								### 指定日志文件的大小,单位 MB。0 表示禁用日志轮转
ListenPort=10050						### 指定监听端口
StartAgents=3				### Zabbix 启动之后开启被动监控的进程数量,如果设置 为 0,那么 Zabbix 被动监控被禁用
ListenIP=0.0.0.0						### 监听 IP 地址,默认为所有接口
Server=192.168.169.204			### 允许哪个 Zabbix server 的 IP 地址监控本机
#ServerActive=127.0.0.1			### Zabbix 主动监控 server 的 ip 地址
Hostname=Zabbix server
Include=/etc/zabbix/zabbix_agentd.d/*.conf		### 自定义配置文件

主动模式和被动模式的区别:

● 主动模式:Zabbix 的 agent 端,也就是客户端,主动把自己监测到的数

据,上报给 Zabbix 服务器,Zabbix 服务器不需要向 agent 发起请求。

注意:ServerActive 是主动模式开关,注释掉主动模式关闭;与 Server 不能

同时配置。

●被动模式:Zabbix 服务器向 agent 发送请求,告诉 agnet 我需要哪些数据,

然后 agnet 执行相关操作,把执行结果返回给服务端。

注意:StartAgents=0 表示被动模式关闭。

### 重启生效配置
[root@zabbix-agent ~]# systemctl restart zabbix-agent
[root@zabbix-agent ~]# systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-12-13 00:52:23 CST; 5s ago
  Process: 1350 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 1352 (zabbix_agentd)
   CGroup: /system.slice/zabbix-agent.service
           ├─1352 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
           ├─1353 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─1354 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─1355 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
           └─1356 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]

Dec 13 00:52:23 zabbix-agent systemd[1]: Starting Zabbix Agent...
Dec 13 00:52:23 zabbix-agent systemd[1]: Can't open PID file /run/zabbix/zabbix_agentd.pid (yet?) after start: No such file or directory
Dec 13 00:52:23 zabbix-agent systemd[1]: Started Zabbix Agent.


### 查看日志
[root@zabbix-agent ~]# tail -f /var/log/zabbix/zabbix_agentd.log 
  1352:20231213:005223.526 **** Enabled features ****
  1352:20231213:005223.526 IPv6 support:          YES
  1352:20231213:005223.526 TLS support:           YES
  1352:20231213:005223.526 **************************
  1352:20231213:005223.526 using configuration file: /etc/zabbix/zabbix_agentd.conf
  1352:20231213:005223.526 agent #0 started [main process]
  1354:20231213:005223.527 agent #2 started [listener #1]
  1353:20231213:005223.527 agent #1 started [collector]
  1355:20231213:005223.529 agent #3 started [listener #2]
  1356:20231213:005223.530 agent #4 started [listener #3]
  
### 查看端口
[root@zabbix-agent ~]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      925/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1054/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1352/zabbix_agentd  
tcp6       0      0 :::22                   :::*                    LISTEN      925/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1054/master
Zabbix Web 界面的使用管理操作

1. 首先设置中文
Zabbix分布式监控应用_第10张图片

2. 解决中文乱码问题

从 Windows 控制面板找到字体,选择字体文件,拷贝到 Zabbix 服务器的

/data/nginx/zabbix/fonts 目录下

Zabbix分布式监控应用_第11张图片

[root@zabbix-server ~]# mv simhei.ttf /data/nginx/zabbix/fonts/
[root@zabbix-server ~]# cd /data/nginx/zabbix/fonts/
[root@zabbix-server fonts]# ll
total 10268
-rw-r--r-- 1 root root  756072 Dec 13 00:09 DejaVuSans.ttf
-rw-r--r-- 1 root root 9753388 Dec 13 01:02 simhei.ttf

[root@zabbix-server fonts]# cp DejaVuSans.ttf DejaVuSans.ttf.bak

[root@zabbix-server fonts]# cp simhei.ttf DejaVuSans.ttf

[root@zabbix-server fonts]# ll
total 19796
-rw-r--r-- 1 root root 9753388 Dec 13 01:02 DejaVuSans.ttf
-rw-r--r-- 1 root root  756072 Dec 13 00:09 DejaVuSans.ttf.bak
-rw-r--r-- 1 root root 9753388 Dec 13 01:04 simhei.ttf


### 重启服务
[root@zabbix-server fonts]# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl):                  [  OK  ]

重新刷新页面后中文乱码消失

Zabbix分布式监控应用_第12张图片

3. 创建客户端监控主机

创建群组

Zabbix分布式监控应用_第13张图片

找到配置→主机→创建主机

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传
Zabbix分布式监控应用_第14张图片

接着配置主机链接模板,选择 Template OS Linux,单击"添加"按钮

之后在最新数据中查看结果

Zabbix分布式监控应用_第15张图片

Zabbix分布式监控应用_第16张图片

4. 创建模板

选择配置→模板→创建模板→添加监控项

Zabbix分布式监控应用_第17张图片

创建监控项
Zabbix分布式监控应用_第18张图片

Zabbix 自定义监控项

创建自定义监控脚本,并部署自定义监控项,实现监测

### 配置自定义监控脚本,修改 agent 配置文件
[root@zabbix-agent ~]# vi /etc/zabbix/zabbix_agentd.d/users.conf
UnsafeUserParameters=1
UserParameter=users, /bin/bash /scripts/user.sh ##存放脚本位置

### 配置监控脚本
[root@zabbix-agent ~]# mkdir /scripts
[root@zabbix-agent ~]# vi /scripts/user.sh
#!/bin/bash
user=$(who | wc -l)
if [ $user -gt 3 ]; then
    echo '1'
else
    echo '2'
fi

### 添加权限
[root@zabbix-agent ~]# chown -R zabbix:zabbix /scripts/
[root@zabbix-agent ~]# chmod 777 /scripts/user.sh

### 测试脚本
[root@zabbix-agent scripts]# ./user.sh 
2
### 如果返回值为 1 说明在线用户超过 3 个,如果返回值为 2,说明在线用户不 超过 3 个

### 查看端口是否都启动了
[root@zabbix-agent scripts]# netstat -ntpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      925/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1054/master         
tcp        0      0 0.0.0.0:10050           0.0.0.0:*               LISTEN      1425/zabbix_agentd  
tcp6       0      0 :::22                   :::*                    LISTEN      925/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      1054/master       
### 生效配置
[root@zabbix-agent scripts]# systemctl restart zabbix-agent
[root@zabbix-agent scripts]# systemctl status zabbix-agent 
● zabbix-agent.service - Zabbix Agent
   Loaded: loaded (/usr/lib/systemd/system/zabbix-agent.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-12-13 01:46:42 CST; 4s ago
  Process: 1547 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS)
  Process: 1549 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS)
 Main PID: 1551 (zabbix_agentd)
   CGroup: /system.slice/zabbix-agent.service
           ├─1551 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd.conf
           ├─1552 /usr/sbin/zabbix_agentd: collector [idle 1 sec]
           ├─1553 /usr/sbin/zabbix_agentd: listener #1 [waiting for connection]
           ├─1554 /usr/sbin/zabbix_agentd: listener #2 [waiting for connection]
           └─1555 /usr/sbin/zabbix_agentd: listener #3 [waiting for connection]

Dec 13 01:46:42 zabbix-agent systemd[1]: Stopped Zabbix Agent.
Dec 13 01:46:42 zabbix-agent systemd[1]: Starting Zabbix Agent...
Dec 13 01:46:42 zabbix-agent systemd[1]: Can't open PID file /run/zabbix/zabbix_agentd.pid (yet?) after start: No such file or directory
Dec 13 01:46:42 zabbix-agent systemd[1]: Started Zabbix Agent.
服务端测试

zabbix_get 命令是在 server 端用来检查 agent 端的一个命令,在添加完主机

或者触发器后,不能正常获得数据,可以用 zabbix_get 来检查能否采集到数据,

以便判断问题症结所在。

Zabbix_get 参数说明:

● -s --host:指定客户端主机名或者 IP。

● -p --port:客户端端口,默认 10050。

● -I --source-address:指定源 IP,写上 Zabbix server 的 IP 地址即可,一般

留空,服务器如果有多 IP 的时候,指定一个。

● -k --key:用户想获取的 key

至于使用长参数还是短的,自己选择,我经常使用-s 而不是-host,

如果不知道 key 参数可以使用 zabbix_agentd -p 命令寻找自己想要找的参数

[root@zabbix-server ~]# zabbix_agentd -p | grep users
system.users.num                              [u|1]

进入 Zabbix Web 配置

流程:配置→主机→监控项→创建监控项;配置→主机→触发器→创建触发 器

创建监控项

Zabbix分布式监控应用_第19张图片

创建触发器
Zabbix分布式监控应用_第20张图片

验证

此处需要克隆会话

[root@zabbix-server ~]# who
root     pts/0        2023-12-12 23:32 (192.168.169.1)
root     pts/1        2023-12-13 02:39 (192.168.169.1)
root     pts/2        2023-12-13 02:39 (192.168.169.1)
root     pts/3        2023-12-13 02:39 (192.168.169.1)

Zabbix分布式监控应用_第21张图片

Zabbix 自定义报警媒介

1. 关于 Zabbix 报警媒介

所谓的报警媒介,就是要监控的设备出问题了,要通过邮件、短信等通知管

理员。那邮件短信等就是报警媒介了。作为一个监控工具的基本功能,如无法提

供报警功能,那就算不上是一个好的监控工具了。今天对 Zabbix 媒介做一个详

细介绍:

● E-mail 邮件。常用,目前多数都已经配置了这个功能,而且免费。

● Custom alertscripts 常用。自定义脚本,把信息传递给脚本,我们在脚本

里使用 sendEmail(不要和 sendmail 搞混了)、飞信发短信、调用短信接口发送

短信等等。

● SMS。 短信设备,了解就可以了,一些公司应该有购买使用的。

2. 创建自定义报警媒介

创建邮件报警需要执行以下步骤:

安装 sendMail→创建邮件发送脚本→创建报警媒介类型

[root@zabbix-server ~]# yum install -y sendmail mailx

[root@zabbix-server ~]# cat /etc/mail.rc
set from=自己的QQ邮箱@qq.com

set smtp=smtp.qq.com

set smtp-auth-user=自己的QQ邮箱@qq.com

set smtp-auth-password=QQ邮箱授权码

set smtp-auth=login

### 重启服务
[root@zabbix-server ~]# systemctl restart sendmail
[root@zabbix-server ~]# systemctl status sendmail                  
● sendmail.service - Sendmail Mail Transport Agent
   Loaded: loaded (/usr/lib/systemd/system/sendmail.service; enabled; vendor preset: disabled)
   Active: active (running) since Wed 2023-12-13 02:56:40 CST; 1min 10s ago
  Process: 21334 ExecStart=/usr/sbin/sendmail -bd $SENDMAIL_OPTS $SENDMAIL_OPTARG (code=exited, status=0/SUCCESS)
  Process: 21328 ExecStartPre=/etc/mail/make aliases (code=exited, status=0/SUCCESS)
  Process: 21327 ExecStartPre=/etc/mail/make (code=exited, status=0/SUCCESS)
 Main PID: 21399 (sendmail)
   CGroup: /system.slice/sendmail.service
           └─21399 sendmail: accepting connections

Dec 13 02:55:40 zabbix-server systemd[1]: Starting Sendmail Mail Transport Agent...
Dec 13 02:55:40 zabbix-server sendmail[21334]: My unqualified host name (zabbix-server) unknown; sleeping for retry
Dec 13 02:56:40 zabbix-server sendmail[21334]: unable to qualify my own domain name (zabbix-server) -- using short name
Dec 13 02:56:40 zabbix-server sendmail[21399]: starting daemon (8.14.7): SMTP+queueing@01:00:00
Dec 13 02:56:40 zabbix-server systemd[1]: Can't open PID file /run/sendmail.pid (yet?) after start: No such file or directory
Dec 13 02:56:40 zabbix-server systemd[1]: Started Sendmail Mail Transport Agent.
### 定义告警脚本存放路径
### 添加以下参数
[root@zabbix-server ~]# vi /usr/local/zabbix/etc/zabbix_server.conf
AlertScriptsPath=/scripts

### 创建存放目录
[root@zabbix-server ~]# mkdir /scripts
[root@zabbix-server ~]# touch /tmp/mailx.log

### 脚本以及日志输出文件授权
[root@zabbix-server ~]# chown -R zabbix:zabbix /tmp/mailx.log 
[root@zabbix-server ~]# chmod -x /scripts/mail.sh

### 编辑脚本
[root@zabbix-server ~]# cat /scripts/mail.sh 
#!/bin/bash
# 发送邮件
messages=$(echo "$3" | tr '\r\n' '\n') # 定义邮件内容变量
subject=$(echo "$2" | tr '\r\n' '\n') # 定义邮件主题变量
echo "${messages}" | mail -s "${subject}" "$1" >> /tmp/mailx.log 2>&1


### 执行脚本
[root@zabbix-server ~]# sh -x /scripts/mail.sh 自己的[email protected] "hello" "zabbix mail"
+ receiver=自己的[email protected]
+ subject=hello
+ messages='zabbix mail'
+ messages='zabbix mail'
+ echo 'zabbix mail'
+ mail -s hello 自己的[email protected]

Zabbix分布式监控应用_第22张图片

Zabbix Web 界面设置告警媒介

管理→报警媒介类型→创建报警媒介
Zabbix分布式监控应用_第23张图片

### 脚本参数
{ALERT.SENDTO}			### 收件人
{ALERT.SUBJECT}			### 邮件标题
{ALERT.MESSAGE}			### 邮件内容

Zabbix分布式监控应用_第24张图片

1. 配置告警接收用户,选择告警等级

步骤如下:管理→用户→编辑 Admin 用户→报警媒介→单击“添加”按钮

Zabbix分布式监控应用_第25张图片

2. 创建邮件发送动作:

步骤如下:配置→动作→创建动作→添加触发条件→添加操作内容→添加恢复操作内容

Zabbix分布式监控应用_第26张图片

Zabbix分布式监控应用_第27张图片

### 默认标题
Problem: {EVENT.NAME}故障{TRIGGER.STATUS},服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!

### 消息内容
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件 ID:{EVENT.ID}

Zabbix分布式监控应用_第28张图片

### 默认标题
恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!

### 消息内容
恢复告警设备: {HOSTNAME1}
触发名称: {TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
恢复详情: {ITEM.NAME}:{ITEM.VALUE}
恢复当前状态为:{TRIGGER.STATUS}
事件 ID:{EVENT.ID}
脚本存放位置
[root@zabbix-server alertscripts]# cp /scripts/mail.sh /usr/local/zabbix/share/zabbix/alertscripts/

### 重启服务
[root@zabbix-server ~]# /etc/init.d/
netconsole     network        zabbix_agentd  zabbix_server  
[root@zabbix-server ~]# /etc/init.d/zabbix_server restart
Restarting zabbix_server (via systemctl):                  [  OK  ]
[root@zabbix-server ~]# /etc/init.d/zabbix_agentd restart
Restarting zabbix_agentd (via systemctl):                  [  OK  ]

Cannot execute command "/scripts/mail.sh": [13] Permission denied
[root@zabbix-server ~]# cd /usr/local/zabbix/share/zabbix/alertscripts/
[root@zabbix-server alertscripts]# ll
total 4
-rw-r--r--. 1 root root 228 Dec 15 05:59 mail.sh
[root@zabbix-server alertscripts]# chmod 777 mail.sh 

邮箱验证
Zabbix分布式监控应用_第29张图片
Zabbix分布式监控应用_第30张图片

你可能感兴趣的:(zabbix,分布式,云计算,运维)