Zabbix-企业级监控工具
企业级监控工具应用实战-zabbix
环境软件包
节点名称 |
ip地址 |
组件 |
controller |
192.168.174.11 |
mysql,php,nginx,zabbix-server,zabbix-agent |
compute |
192.168.174.12 |
mysql,php,nginx,zabbix-agent |
2台服务器,1台zabbix_server,1台zabbix_agent
2台服务器配置50G硬盘,内存2G以上,cpu2个
软件包zabbix-4.0.3.tar, zabbix-agent-4.0.3-1.el7.x86_64.rpm, lnmp1.6-full.tar
Zabbix server机器装载mysql,php,nginx,zabbix-server,zabbix-agent
Zabbix agent机器装载mysql,php,nginx,zabbix-agent
1.lnmp环境配置
1. 安装MySQL
(1)卸载mariadb
[root@controller ~]# rpm -qa |grep mariadb //查看有无安装mariadb mariadb-libs-5.5.44-2.el7.centos.x86_64 [root@controller ~]# rpm -e --nodeps mariadb-libs-5.5.44-2.el7.centos.x86_64 //卸载mariadb
(2)下载
[root@controller ~]# cd /opt/ //软件包都放在这里方便管理 [root@controller opt]# wget https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz --2021-06-18 11:26:07-- https://cdn.mysql.com//Downloads/MySQL-5.7/mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz Resolving cdn.mysql.com (cdn.mysql.com)... 104.75.165.42 Connecting to cdn.mysql.com (cdn.mysql.com)|104.75.165.42|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 661214270 (631M) [application/x-tar-gz] Saving to: ‘mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz’ 100%[========================>] 661,214,270 24.0MB/s in 28s 2021-06-18 11:26:36 (22.3 MB/s) - ‘mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz’ saved [661214270/661214270]
(3)解压
[root@controller opt]# tar zxvf mysql-5.7.32-linux-glibc2.12-x86_64.tar.gz
(4)安装和配置
安装依赖
[root@controller opt]# yum -y install ncurses-devel autoconf [root@controller opt]# groupadd mysql [root@controller opt]# useradd -g mysql -s /sbin/nologin -d /usr/local/mysql -MN mysql [root@controller opt]# mkdir /data/mysql/data/ -p [root@controller opt]# chown -R mysql:mysql /data/mysql/ [root@controller opt]# mv mysql-5.7.32-linux-glibc2.12-x86_64 /usr/local/ [root@controller opt]# ln -s /usr/local/mysql-5.7.32-linux-glibc2.12-x86_64/ /usr/local/mysql [root@controller opt]# chown -R mysql.mysql /usr/local/mysql [root@controller opt]# cd /usr/local/mysql
[root@controller mysql]# vi /etc/my.cnf [client] port = 3306 socket = /tmp/mysql.sock [mysql] no-auto-rehash [mysqld] user = mysql basedir = /usr/local/mysql datadir = /data/mysql/data port = 3306 socket = /tmp/mysql.sock log-error = error.log slow_query_log_file = slow.log character-set-server = utf8 open_files_limit = 65535 max_connections = 100 max_connect_errors = 100000 lower_case_table_names =1
初始化mysql
[root@controller mysql]# /usr/local/mysql/bin/mysqld --initialize [root@controller mysql]# grep 'temporary password' /data/mysql/data/error.log 2021-06-18T15:38:46.762382Z 1 [Note] A temporary password is generated for root@localhost: aHNOneeh:2#z // 查看mysql密码
(5)复制启动脚本文件
[root@controller mysql]# cp support-files/mysql.server /etc/init.d/mysql
(6)添加环境变量
[root@controller mysql]# echo "export PATH=/usr/local/mysql/bin:$PATH">>/etc/profile [root@controller mysql]# source /etc/profile
(7)启动mysql
[root@controller mysql]# service mysql start
Starting MySQL. SUCCESS!
(8)查看你mysql进程
[root@controller mysql]# ps -ef | grep mysql root 3201 1 0 11:43 pts/0 00:00:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql/data --pid-file=/data/mysql/data/controller.pid mysql 3443 3201 0 11:43 pts/0 00:00:00 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql/data --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=error.log --open-files-limit=65535 --pid-file=/data/mysql/data/controller.pid --socket=/tmp/mysql.sock --port=3306 root 3473 3032 0 11:44 pts/0 00:00:00 grep --color=auto mysql
(9)登录mysql
[root@controller mysql]# mysql -uroot -p Enter password: (密码是上面红色字体标识) Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.32 Copyright (c) 2000, 2020, 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>
(10)修改mysql密码
mysql> alter user user() identified by 'test'; Query OK, 0 rows affected (0.00 sec)
(11)登录mysql
[root@controller mysql]# mysql -uroot -ptest mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.32 MySQL Community Server (GPL) Copyright (c) 2000, 2020, 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>
2. 安装Nginx
(1)使⽤用Nginx官⽅方提供的rpm包。
[root@controller mysql]# vi /etc/yum.repos.d/nginx.repo [nginx] name=nginx repo baseurl=http://nginx.org/packages/centos/7/$basearch/ gpgcheck=0 enabled=1
(2)执行yum安装
[root@controller mysql]# yum -y install nginx [root@controller mysql]# systemctl start nginx [root@controller mysql]# systemctl enable nginx Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
3. 安装PHP
(1)使用第三方扩展epel源安装php7.2
[root@controller mysql]# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm Retrieving https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm warning: /var/tmp/rpm-tmp.TH9bgW: Header V4 RSA/SHA256 Signature, key ID 352c64e5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:epel-release-7-13 ################################# [100%] [root@controller mysql]# rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm Retrieving https://mirror.webtatic.com/yum/el7/webtatic-release.rpm warning: /var/tmp/rpm-tmp.PK9B4K: Header V4 RSA/SHA1 Signature, key ID 62e74ca5: NOKEY Preparing... ################################# [100%] Updating / installing... 1:webtatic-release-7-3 ################################# [100%]
(2)清除版本
[root@controller mysql]# yum -y remove php* Loaded plugins: fastestmirror No Match for argument: php* No Packages marked for removal
(3)安装php
[root@controller mysql]# yum -y install php72w php72w-cli php72w-fpm php72w-common php72w-devel php72w-embedded php72w-gd php72w-mbstring php72w-mysqlnd php72w-opcache php72w-pdo php72w-xml php72w-redis
(4)启动php-fpm服务
[root@controller mysql]# systemctl enable php-fpm.service Created symlink from /etc/systemd/system/multi-user.target.wants/php-fpm.service to /usr/lib/systemd/system/php-fpm.service. [root@controller mysql]# systemctl start php-fpm.service
(7)启动php-fpm
[root@controller mysql]# service php-fpm start
Redirecting to /bin/systemctl start php-fpm.service
4. 配置nginx支持PHP
[root@controller mysql]# vi /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /soft/code; index index.php index.html index.htm; location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php fastcgi_param SCRIPT_FILENAME /soft/code$fastcgi_script_name; include fastcgi_params; } }
新建目录
[root@controller mysql]# mkdir -p /soft/code [root@controller mysql]# vi /soft/code/info.php <?php phpinfo(); ?>
重启nginx
[root@controller mysql]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@controller mysql]# nginx -s reload
浏览网页
http://192.168.174.11/info.php
(1)lnmp 环境配置
导入 lnmp1.8-full.tar,并解压出来,里面存放的是 lnmp 环境一键部署脚本;
[root@zabbix-server local]# tar zxvf lnmp1.8-full.tar.gz -C /usr/local/
[root@zabbix-server local]# cd /usr/local/lnmp1.8-full [root@zabbix-server lnmp1.8-full]#
修改脚本环境变量配置文件:
[root@zabbix-server lnmp1.8-full]# vi lnmp.conf Download_Mirror='https://soft.vpser.net' Nginx_Modules_Options='' PHP_Modules_Options='' ##MySQL/MariaDB database directory## MySQL_Data_Dir='/data/mysql/' MariaDB_Data_Dir='/usr/local/mariadb/var' ##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@zabbix-server lnmp1.8-full]# ./install.sh lnmp +------------------------------------------------------------------------+ | LNMP V1.8 for CentOS Linux Server, Written by Licess | +------------------------------------------------------------------------+ | A tool to auto-compile & install LNMP/LNMPA/LAMP on Linux | +------------------------------------------------------------------------+ | For more information please visit https://lnmp.org | +------------------------------------------------------------------------+ You have 11 options for your DataBase install. 1: Install MySQL 5.1.73 2: Install MySQL 5.5.62 (Default) 3: Install MySQL 5.6.51 4: Install MySQL 5.7.34 5: Install MySQL 8.0.25 6: Install MariaDB 5.5.68 7: Install MariaDB 10.1.48 8: Install MariaDB 10.2.38 9: Install MariaDB 10.3.29 10: Install MariaDB 10.4.19 0: DO NOT Install MySQL/MariaDB Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10 or 0):4
目前提供了较多的 MySQL、MariaDB 版本和不安装数据库的选项,需要注意的是 MySQL 5.6,5.7 及 MariaDB 10 必须在 1G 以上内存的更高配置上才能选择! 输入对应 MySQL 或 MariaDB 版本前面的序号,回车进入下一步
Please setup root password of MySQL.
Please enter:000000
设置 MySQL 的 root 密码,输入后回车进入下一步,如下图所示:
Do you want to enable or disable the InnoDB Storage Engine?
Default enable,Enter your choice [Y/n]: y
询问是否需要启用 MySQL InnoDB,InnoDB 引擎默认为开启,一般建议开启!直接回车或 输入 y ,输入完成,回车进入下一步。
1: Install PHP 5.2.17 2: Install PHP 5.3.29 3: Install PHP 5.4.45 4: Install PHP 5.5.38 5: Install PHP 5.6.40 (Default) 6: Install PHP 7.0.33 7: Install PHP 7.1.33 8: Install PHP 7.2.34 9: Install PHP 7.3.28 10: Install PHP 7.4.19 11: Install PHP 8.0.6 Enter your choice (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11): 6
注意:选择 PHP 7+版本时需要自行确认 PHP 版本是否与自己的程序兼容。 输入要选择的 PHP 版本的序号,回车进入下一步,选择是否安装内存优化:
1: Don't install Memory Allocator. (Default) 2: Install Jemalloc 3: Install TCMalloc Enter your choice (1, 2 or 3): 1
安装完成
如果显示 Nginx: OK,MySQL: OK,PHP: OK
============================== Check install ============================== Checking ... Nginx: OK MySQL: OK PHP: OK PHP-FPM: OK Clean Web Server src directory... +------------------------------------------------------------------------+ | LNMP V1.8 for CentOS Linux Server, Written by Licess | +------------------------------------------------------------------------+ | For more information please visit https://lnmp.org | +------------------------------------------------------------------------+ | lnmp status manage: lnmp {start|stop|reload|restart|kill|status} | +------------------------------------------------------------------------+ | phpMyAdmin: http://IP/phpmyadmin/ | | phpinfo: http://IP/phpinfo.php | | Prober: http://IP/p.php | +------------------------------------------------------------------------+ | Add VirtualHost: lnmp vhost add | +------------------------------------------------------------------------+ | Default directory: /home/wwwroot/default | +------------------------------------------------------------------------+ | MySQL/MariaDB root password: 000000 | +------------------------------------------------------------------------+ +-------------------------------------------+ | Manager for LNMP, Written by Licess | +-------------------------------------------+ | https://lnmp.org | +-------------------------------------------+ nginx (pid 101508) is running... php-fpm is stop! SUCCESS! MySQL running (102079) State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:80 *:* LISTEN 0 128 *:22 *:* LISTEN 0 128 :::3306 :::* LISTEN 0 128 :::22 :::* Install lnmp takes 81 minutes. Install lnmp V1.8 completed! enjoy it.
5.zabbix安装部署
Zabbix Server编译安装安装依赖
[root@controller ~]# yum install -y libevent-devel wget tar gcc gcc-c++ make net-snmp-devel libxml2-devel libcurl-devel
创建zabbix用户
[root@controller ~]# useradd -s /sbin/nologin zabbix
下载zabbix源码包
[root@controller src]# ls zabbix-4.4.10.tar.gz
解压编译
[root@controller src]# tar zxvf zabbix-4.4.10.tar.gz
[root@controller local]# mkdir zabbix [root@controller ~]# cd /usr/local/src/ [root@controller src]# mv /usr/local/src/zabbix-4.4.10/* /usr/local/zabbix
[root@controller src]# cd /usr/local/zabbix/
[root@controller 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
选项说明1) --prefix指定安装目录
2) --enable-server安装zabbix server
3) --enable-agent安装zabbix agent
4) --with-mysql用mysql来存储
[root@controller zabbix]# make && make install
环境变量设置:
[root@controller zabbix]# vi /etc/profile .... PATH=$PATH:/usr/local/mysql/bin:/usr/local/apache2.4/bin:/usr/local/zabbix/bin
[root@controller zabbix]# source /etc/profile
登录数据库创建zabbix用户和导入zabbix库Mysql数据库
[root@controller zabbix]# mysql -uroot -p000000 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 4 Server version: 5.7.32 MySQL Community Server (GPL) Copyright (c) 2000, 2020, 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; Query OK, 1 row affected (0.36 sec) ##创建zabbix库和设置格式
mysql> grant all privileges on zabbix.* to zabbix@'127.0.0.1' identified by 'zabbix'; Query OK, 0 rows affected, 1 warning (0.03 sec) mysql> grant all privileges on zabbix.* to zabbix@'%' identified by 'zabbix'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; ##刷新权限 Query OK, 0 rows affected (0.05 sec)
mysql> set names utf8; ##设置字符集
Query OK, 0 rows affected (0.00 sec)
mysql> use zabbix;
Database changed
切换到zabbix库中把源码/usr/local/zabbix/database/mysql/安装路径下的三个sql文件导入数据库: 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
编辑zabbix server的配置(记得先备份配置文件)
[root@controller ~]# vi /usr/local/zabbix/etc/zabbix_server.conf
LogFile=/tmp/zabbix_server.log DBHost=127.0.0.1 DBName=zabbix DBUser=zabbix DBPassword=zabbix DBPort=3306
Timeout=30 LogSlowQueries=3000 DBSocket=/tmp/mysql.sock AlertScriptsPath=/scripts ##定义告警脚本存放路径
[root@controller ~]# mkdir /scripts
[root@controller ~]# chown zabbix:zabbix -R /usr/local/zabbix/
[root@zabbix-server bin]# cp /usr/local/zabbix/misc/init.d/fedora/core/zabbix_server /etc/init.d/
[root@zabbix-server bin]# cp /usr/local/zabbix/misc/init.d/fedora/core/zabbix_agentd /etc/init.d/
[root@zabbix-server bin]# vi /etc/init.d/zabbix_server #!/bin/bash # # chkconfig: - 90 10 # description: Starts and stops Zabbix Server using chkconfig # Tested on Fedora Core 2 - 5 # Should work on all Fedora Core versions # # @name: zabbix_server # @author: Alexander Hagenah <hagenah@topconcepts.com> # @created: 18.04.2006 # # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # # Source function library. . /etc/init.d/functions # Variables # Edit these to match your system settings # Zabbix-Directory BASEDIR=/usr/local/zabbix #zabbix 安装目录 # Binary File BINARY_NAME=zabbix_server
[root@zabbix-server bin]# vi /etc/init.d/zabbix_agentd #!/bin/bash # # chkconfig: - 90 10 # description: Starts and stops Zabbix Agent using chkconfig # Tested on Fedora Core 2 - 5 # Should work on all Fedora Core versions # # @name: zabbix_agentd # @author: Alexander Hagenah <hagenah@topconcepts.com> # @created: 18.04.2006 # # Modified for Zabbix 2.0.0 # May 2012, Zabbix SIA # # Source function library. . /etc/init.d/functions # Variables # Edit these to match your system settings # Zabbix-Directory BASEDIR=/usr/local/zabbix #zabbix 安装目录 # Binary File BINARY_NAME=zabbix_agentd
添加权限:
[root@zabbix-server bin]# chmod 755 /etc/init.d/zabbix_*
启动zabbix:
[root@zabbix-server bin]# /etc/init.d/zabbix_server start
Reloading systemd: [ OK ]
Restarting zabbix_server (via systemctl): [ OK ]
[root@zabbix-server bin]# ps -ef | grep zabbix ###查看zabbix服务是否启动
Zabbix Web 的安装
配置 nginx 虚拟主机:
[root@zabbix-server bin]# vi /usr/local/nginx/conf/vhost/zabbix.com.conf server { listen 81; server_name zabbix.com www.zabbix.com; index index.html index.htm index.php; root /data/nginx/zabbix; location ~ [^/]\.php(/|$) { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /data/nginx/zabbix$fastcgi_sc ript_name; include fastcgi_params; } }
[root@zabbix-server bin]# mkdir -p /data/nginx/zabbix ##创建 zabbix web 的文件目录 [root@zabbix-server bin]# /etc/init.d/nginx reload Reload nginx... done [root@zabbix-server bin]# /etc/init.d/nginx restart Stoping nginx... done Starting nginx... done [root@zabbix-server bin]# cp -rf /usr/local/zabbix/frontends/php/* /data/nginx/zabbix/
##把源码安装包路径下的文件 cp 到 zabbix web 文件目录当中
到浏览器通过 http://192.168.174.13/setup.php 配置 zabbix 的初始化设置;
注:这里 zabbix 初始化设置会有报错,需要提前修改 php.ini 的配置文件;
[root@zabbix-server bin]# vi /usr/local/php/etc/php.ini
post_max_size = 32M max_execution_time = 350 max_input_time = 350 date.timezone = Asia/Shanghai
修改php-fpm.conf文件:
[root@zabbix-server bin]# vi /usr/local/php/etc/php-fpm.conf [global] pid = /usr/local/php/var/run/php-fpm.pid error_log = /usr/local/php/var/log/php-fpm.log log_level = notice [www] listen = 127.0.0.1:9000
重启 php-fpm 服务 /etc/init.d/php-fpm restart
[root@zabbix-server bin]# /etc/init.d/php-fpm restart Gracefully shutting down php-fpm . done Starting php-fpm done
Zabbix web 界面部署:
将下载文件导入/data/nginx/zabbix/conf
测试登录: 登陆账户是 Admin 密码是 zabbix
设置中文:
监控报警提示:Zabbix agent on Zabbix server is unreachable for 5 minutes
此提示为 zabbix agent 未启动导致的!
查看端口 10051 是否有启动:
[root@zabbix-server bin]# netstat -lntp 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:9000 0.0.0.0:* LISTEN 112821/php-fpm: mas tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 112840/nginx: maste tcp 0 0 0.0.0.0:81 0.0.0.0:* LISTEN 112840/nginx: maste tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1453/sshd tcp 0 0 0.0.0.0:10051 0.0.0.0:* LISTEN 112458/zabbix_serve tcp6 0 0 :::3306 :::* LISTEN 102079/mysqld tcp6 0 0 :::22 :::* LISTEN 1453/sshd
3.zabbix agent 客户端安装部署
下载源码包:
[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 rpm -ivh zabbix-agent-4.0.3-1.el7.x86_64.rpm--2021-06-21 21:36:30-- 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 478KB/s in 0.8s 2021-06-21 21:36:31 (478 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 日志文件路径
修改配置文件
[root@zabbix-agent ~]#cp /etc/zabbix/zabbix_agentd.conf{,.bak} ##备份配置
[root@zabbix-agent ~]# egrep -v '^$|#' /etc/zabbix/zabbix_agentd.conf.bak > /etc/zabbix/zabbix_agentd.conf ##过滤一下空白 行和注释 [root@zabbix-agent ~]# cat /etc/zabbix/zabbix_agentd.conf
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
#Server=127.0.0.1
ListenPort=10050
StartAgents=3
ListenIP=0.0.0.0
#ServerActive=127.0.0.1
Server=192.168.174.13
Hostname=zabbix agent
Include=/etc/zabbix/zabbix_agentd.d/*.conf
###Server 和 ServerActive 上配置的是 zabbix server 的 ip Hostname 建议为客户端主机名
[root@zabbix-agent zabbix]# systemctl start zabbix-agent [root@zabbix-agent zabbix]# 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 Mon 2021-06-21 22:32:47 CST; 4min 12s ago Process: 103009 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS) Main PID: 103011 (zabbix_agentd) CGroup: /system.slice/zabbix-agent.service ├─103011 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabb... ├─103012 /usr/sbin/zabbix_agentd: collector [idle 1 ... ├─103013 /usr/sbin/zabbix_agentd: listener #1 [waiti... ├─103014 /usr/sbin/zabbix_agentd: listener #2 [waiti... └─103015 /usr/sbin/zabbix_agentd: listener #3 [waiti... Jun 21 22:32:47 zabbix-agent systemd[1]: zabbix-agent.service h... Jun 21 22:32:47 zabbix-agent systemd[1]: Stopped Zabbix Agent. Jun 21 22:32:47 zabbix-agent systemd[1]: Starting Zabbix Agent... Jun 21 22:32:47 zabbix-agent systemd[1]: Can't open PID file /r... Jun 21 22:32:47 zabbix-agent systemd[1]: Started Zabbix Agent. Hint: Some lines were ellipsized, use -l to show in full. [root@zabbix-agent zabbix]# systemctl enable zabbix-agent Created symlink from /etc/systemd/system/multi-user.target.wants/zabbix-agent.service to /usr/lib/systemd/system/zabbix-agent.service.
查看日志:
[root@zabbix-agent zabbix]# tail -f /var/log/zabbix/zabbix_agentd.log 103011:20210621:223247.645 **** Enabled features **** 103011:20210621:223247.645 IPv6 support: YES 103011:20210621:223247.645 TLS support: YES 103011:20210621:223247.645 ************************** 103011:20210621:223247.645 using configuration file: /etc/zabbix/zabbix_agentd.conf 103011:20210621:223247.646 agent #0 started [main process] 103012:20210621:223247.647 agent #1 started [collector] 103013:20210621:223247.648 agent #2 started [listener #1] 103014:20210621:223247.649 agent #3 started [listener #2] 103015:20210621:223247.650 agent #4 started [listener #3]
查看端口 10050 是否启动:
[root@zabbix-agent zabbix]# netstat -lntp | grep zabbix tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 103011/zabbix_agent
创建客户端监控主机:
乱码
解决中文乱码无法显示问题:
windows控制面板找到字体,选择黑体 常规拷贝到桌面,上传至/data/nginx/zabbix/fonts/上
[root@zabbix-server fonts]# ll total 9528 -rw-r--r-- 1 root root 9753388 Oct 15 2019 simhei.ttf
4.自定义监控脚本
[root@zabbix-agent ~]# cd /etc/zabbix/zabbix_agentd.d/ [root@zabbix-agent zabbix_agentd.d]# vi users.conf UnsafeUserParameters=1 UserParameter=users,/bin/bash /scripts/user.sh
配置脚本:
[root@zabbix-agent ~]# mkdir /scripts [root@zabbix-agent ~]# cd !$ cd /scripts [root@zabbix-agent scripts]# touch user.sh [root@zabbix-agent scripts]# vi user.sh #!/bin/bash user=$(who |wc -l) if [ $user -gt 3 ];then echo '1' else echo '2' fi
添加权限:
[root@zabbix-agent scripts]# chmod 755 user.sh [root@zabbix-agent scripts]# ./user.sh 2
### 测试脚本: ./user.sh 如果返回值为 1 说明在线用户超过 3 个,如果返回值为 2,说明在线用户不超过 3 个;
生效配置:
[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; enabled; vendor preset: disabled) Active: active (running) since Tue 2021-06-22 00:13:35 CST; 10s ago Process: 103187 ExecStop=/bin/kill -SIGTERM $MAINPID (code=exited, status=0/SUCCESS) Process: 103189 ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0/SUCCESS) Main PID: 103191 (zabbix_agentd) CGroup: /system.slice/zabbix-agent.service ├─103191 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabb... ├─103192 /usr/sbin/zabbix_agentd: collector [idle 1 ... ├─103193 /usr/sbin/zabbix_agentd: listener #1 [waiti... ├─103194 /usr/sbin/zabbix_agentd: listener #2 [waiti... └─103195 /usr/sbin/zabbix_agentd: listener #3 [waiti... Jun 22 00:13:35 zabbix-agent systemd[1]: Stopped Zabbix Agent. Jun 22 00:13:35 zabbix-agent systemd[1]: Starting Zabbix Agent... Jun 22 00:13:35 zabbix-agent systemd[1]: Can't open PID file /r... Jun 22 00:13:35 zabbix-agent systemd[1]: Started Zabbix Agent. Hint: Some lines were ellipsized, use -l to show in full. [root@zabbix-agent scripts]# netstat -lntp |grep zabbix tcp 0 0 0.0.0.0:10050 0.0.0.0:* LISTEN 103191/zabbix_agent
##查看端口是否都启动了
服务端测试:
[root@zabbix-server ~]# zabbix_get -s 192.168.174.14 -k users 2
通过 zabbix_get 去测试获取脚本数据
进入 ZABBIX WEB 配置
流程:配置—>主机->监控项->创建监控项
配置—>主机->触发器>创建触发器
验证: 客户端主机开多于三个用户,看是否会触发报警!
5.报警警邮件通知
安装 mail 服务
[root@zabbix-server ~]# yum -y install sendmail mailx
邮件发送配置/etc/mail.rc
[root@zabbix-server ~]# vi /etc/mail.rc set from=xxxxxxxxx@qq.com set smtp=smtp.qq.com set smtp-auth-user=xxxxxxx@qq.com set smtp-auth-password=xxxx 授权码 QQ-邮箱-账户-打开smtp服务 set smtp-auth=login
set smtp-use-starttls
set ssl-verify=ignore
set nss-config-dir=/etc/pki/nssdb/
重启服务
[root@zabbix-server ~]# systemctl restart sendmail
编写邮件发送脚本
[root@zabbix-server ~]# vi /scripts/mail.sh #!/bin/bash #send mail 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 ~]# touch /tmp/mailx.log [root@zabbix-server ~]# chown -R zabbix.zabbix /tmp/mailx.log [root@zabbix-server ~]# chown -R zabbix.zabbix /scripts/ [root@zabbix-server ~]# chmod +x /scripts/mail.sh
测试发送邮件是否成功:
[root@zabbix-server ~]# /scripts/mail.sh 1205098712@qq.com "hello" "safsaf"
配置告警脚本所在位置
[root@zabbix-server ~]# vi /usr/local/zabbix/etc/zabbix_server.conf
AlertScriptsPath=/scripts
到 zabbix web 界面设置告警媒介:管理-报警媒介类型-创建报警媒介
脚本参数:
{ALERT.SENDTO}收件人
{ALERT.SUBJECT}邮件标题
{ALERT.MESSAGE}邮件内容
配置告警接收用户,选择告警等级
管理-用户-创建用户-报警媒介
创建邮件发送动作
告警标题
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}
在操作中勾选 Pause operations for suppressed problems-->新的 添加一下内容:选择添加!
恢复操作与操作那边一样:
恢复标题
恢复{TRIGGER.STATUS}, 服务器:{HOSTNAME1}: {TRIGGER.NAME}已恢复!
恢复内容
恢复告警设备: {HOSTNAME1}
触发名称: {TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
恢复详情: {ITEM.NAME}:{ITEM.VALUE}
恢复当前状态为:{TRIGGER.STATUS}
事件 ID:{EVENT.ID}
测试停掉 nginx 服务:
[root@zabbix-agent scripts]# /etc/init.d/nginx stop
Stoping nginx... done
[root@zabbix-agent scripts]# /etc/init.d/nginx restart
Stoping nginx... done
Starting nginx... done