Linux-zabbix部署监控-nginx-MySQL-邮件通知
centos7 10.0.0.17 zabbix服务器 zabbix-server 4.0 mysql5..7
centos7 10.0.0.27 nginx服务器 nginx zabbix-angel
centos7 10.0.0.57 MySQL服务器 MySQL5.7
yum部署zabbix服务器
10.0.0.17 zabbix服务器
安装zbbix存储库
[root@zabbix-server ~]#rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm Retrieving https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm warning: /var/tmp/rpm-tmp.7MdOqz: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY Preparing... ################################# [100%] Updating / installing... 1:zabbix-release-4.0-2.el7 ################################# [100%] [root@zabbix-server ~]#yum clean all Loaded plugins: fastestmirror Cleaning repos: BASE base extras updates zabbix zabbix-non-supported Cleaning up list of fastest mirrors [root@zabbix-server ~]#
安装zabbix server ,web前端,agent
[root@zabbix-server ~]#yum install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent zabbix-get
安装的文件和依赖关系
Installing: zabbix-agent x86_64 4.0.42-1.el7 zabbix 431 k zabbix-server-mysql x86_64 4.0.42-1.el7 zabbix 2.2 M zabbix-web-mysql noarch 4.0.42-1.el7 zabbix 12 k #依赖关系
Installing for dependencies: OpenIPMI x86_64 2.0.27-1.el7 BASE 243 k OpenIPMI-libs x86_64 2.0.27-1.el7 BASE 523 k OpenIPMI-modalias x86_64 2.0.27-1.el7 BASE 16 k apr x86_64 1.4.8-7.el7 BASE 104 k apr-util x86_64 1.5.2-6.el7 BASE 92 k fping x86_64 3.16-1.el7 zabbix-non-supported 44 k httpd x86_64 2.4.6-97.el7.centos.5 updates 2.7 M httpd-tools x86_64 2.4.6-97.el7.centos.5 updates 94 k iksemel x86_64 1.4-2.el7.centos zabbix-non-supported 49 k libXpm x86_64 3.5.12-1.el7 BASE 55 k libzip x86_64 0.10.1-8.el7 BASE 48 k mailcap noarch 2.1.41-2.el7 BASE 31 k net-snmp-libs x86_64 1:5.7.2-49.el7_9.2 updates 752 k php x86_64 5.4.16-48.el7 BASE 1.4 M php-bcmath x86_64 5.4.16-48.el7 BASE 58 k php-cli x86_64 5.4.16-48.el7 BASE 2.7 M php-common x86_64 5.4.16-48.el7 BASE 565 k php-gd x86_64 5.4.16-48.el7 BASE 128 k php-ldap x86_64 5.4.16-48.el7 BASE 53 k php-mbstring x86_64 5.4.16-48.el7 BASE 506 k php-mysql x86_64 5.4.16-48.el7 BASE 102 k php-pdo x86_64 5.4.16-48.el7 BASE 99 k php-xml x86_64 5.4.16-48.el7 BASE 126 k t1lib x86_64 5.1.2-14.el7 BASE 166 k unixODBC x86_64 2.3.1-14.el7 BASE 413 k zabbix-web noarch 4.0.42-1.el7 zabbix 3.0 M
安装数据库
zabbix需要连接数据库,需要数据库客户端
脚本安装mysql5.7
[root@centos7-liyj ~]#ls anaconda-ks.cfg install_mysql5.7or8.0_centos.sh mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
10.0.0.57 数据库服务器

[root@centos7-liyj ~]#cat install_mysql5.7or8.0_centos.sh #!/bin/bash # # #liyanjin . /etc/init.d/functions SRC_DIR=`pwd` MYSQL='mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz' COLOR='echo -e \E[01:31m' END='\E[0m' MYSQL_ROOT_PASSWORD='123456' check () { if [ $UID -ne 0 ];then action "当前用户不是root,安装失败" false exit 1 fi cd $SRC_DIR if [ ! -e $MYSQL ];then $COLOR"缺少${MYSQL}文件"$END $COLOR"请将相关软件放在${SRC_DIR}目录下"$END exit elif [ -e /usr/local/mysql ];then action "数据库已存在,安装失败" false exit else return fi } install_mysql(){ $COLOR"开始安装MySQL数据库..."$END yum -y -q install libaio numactl-libs libaio &> /dev/null cd $SRC_DIR tar xf $MYSQL -C /usr/local/ MYSQL_DIR=`echo $MYSQL| sed -nr 's/^(.*[0-9]).*/\1/p'` ln -s /usr/local/$MYSQL_DIR /usr/local/mysql chown -R root.root /usr/local/mysql/ id mysql &> /dev/full || { useradd -s /sbin/nologin -r mysql ; action "创建myslq用户"; } echo 'PATH=/usr/local/mysql/bin/:$PATH' > /etc/profile.d/mysql.sh . /etc/profile.d/mysql.sh ln -s /usr/local/mysql/bin/* /usr/bin/ cat > /etc/my.cnf <<-EOF [mysqld] server-id=`hostname -I|cut -d. -f4` log-bin datadir=/data/mysql socket=/data/mysql/mysql.sock log-error=/data/mysql/mysql.log pid-file=/data/mysql/mysql.pid [client] socket=/data/mysql/mysql.sock EOF mysqld --initialize --user=mysql --datadir=/data/mysql cp /usr/local/mysql/support-files/mysql.server /etc/init.d/mysqld chkconfig --add mysqld chkconfig mysqld on systemctl start mysqld [ $? -ne 0 ] && { $COLOR"数据库启动失败,退出!"$END;exit; } sleep 5; MYSQL_OLDPASSWORD=`awk '/A temporary password/{print $NF}' /data/mysql/mysql.log` mysqladmin -uroot -p$MYSQL_OLDPASSWORD password $MYSQL_ROOT_PASSWORD &> /dev/null action "数据库安装完成" } check install_mysql
[root@centos7-liyj ~]#ls anaconda-ks.cfg install_mysql5.7or8.0_centos.sh mysql-5.7.33-linux-glibc2.12-x86_64.tar.gz
创建zabbix数据库并授权
[root@centos7-liyj ~]#mysql -uroot -p Enter password: Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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_server character set utf8 collate utf8_bin; Query OK, 1 row affected (0.02 sec) mysql> grant all privileges on zabbix_server.* to zabbix@"10.0.0.%" identified by "123456"; Query OK, 0 rows affected, 1 warning (0.02 sec)
10.0.0.17 zabbix服务器
#数据库连接验证
[root@centos7-liyj ~]#mysql -uzabbix -p123456 -h10.0.0.57 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.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | zabbix_server | +--------------------+ 2 rows in set (0.02 sec) mysql> exit Bye
#创建zabbix初始数据 [root@zabbix-server ~]#zcat /usr/share/doc/zabbix-server-mysql-4.0.42/create.sql.gz | mysql -uzabbix -p123456 -h10.0.0.57 zabbix_server mysql: [Warning] Using a password on the command line interface can be insecure.
#登录验证 [root@zabbix-server ~]#mysql -uzabbix -p123456 -h10.0.0.57 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 6 Server version: 5.7.33-log MySQL Community Server (GPL) Copyright (c) 2000, 2021, Oracle and/or its affiliates. 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> use zabbix_server; Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed
#检查初始创建的数据库表 mysql> show tables; +----------------------------+ | Tables_in_zabbix_server | +----------------------------+ | 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)
配置文件,配置数据库参数
[root@centos7-liyj ~]#grep "^[a-Z]" /etc/zabbix/zabbix_server.conf ListenPort=10051 LogFile=/var/log/zabbix/zabbix_server.log LogFileSize=0 PidFile=/var/run/zabbix/zabbix_server.pid SocketDir=/var/run/zabbix DBHost=10.0.0.57 #数据库连接地址 DBName=zabbix_server #连接的数据库名称 DBUser=zabbix #连接数据库的用户名 DBPassword=123456 #连接数据库的密码 DBPort=3306 #数据库的端口 SNMPTrapperFile=/var/log/snmptrap/snmptrap.log Timeout=4 AlertScriptsPath=/usr/lib/zabbix/alertscripts ExternalScripts=/usr/lib/zabbix/externalscripts LogSlowQueries=3000
为Zabbix前端配置PHP
编辑配置文件 /etc/httpd/conf.d/zabbix.conf 更改时区为中国上海 Asia/shanghai
[root@zabbix-server ~]#vim /etc/httpd/conf.d/zabbix.conf # # Zabbix monitoring system php web frontend # Alias /zabbix /usr/share/zabbix <Directory "/usr/share/zabbix"> Options FollowSymLinks AllowOverride None Require all granted <IfModule mod_php5.c> php_value max_execution_time 300 php_value memory_limit 128M php_value post_max_size 16M php_value upload_max_filesize 2M php_value max_input_time 300 php_value max_input_vars 10000 php_value always_populate_raw_post_data -1 php_value date.timezone Asia/shanghai </IfModule>
监听端口,zabbix-agent 10050 zabbix-server 10051 httpd 80 端口
[root@zabbix-server ~]#ss -tnl State Recv-Q Send-Q Local Address:Port LISTEN 0 128 *:22 LISTEN 0 100 127.0.0.1:25 LISTEN 0 128 127.0.0.1:6010 LISTEN 0 128 127.0.0.1:6011 LISTEN 0 128 *:10050 LISTEN 0 128 *:10051 LISTEN 0 128 [::]:80 LISTEN 0 128 [::]:22 LISTEN 0 100 [::1]:25 LISTEN 0 128 [::1]:6010 LISTEN 0 128 [::1]:6011 LISTEN 0 128 [::]:10050 LISTEN 0 128 [::]:10051
启动httpd服务,配置web界面
访问:http://10.0.0.17/zabbix/setup.php
报错行,修改配置文件对应的行
[root@centos7-liyj ~]#vim /etc/php.ini
修改时区,中国上海
date.timezone =Asia/shanghai
登录账号,Admin
登录密码,zabbix
修改界面为中文界面
选中 中文 ,点击update
选用中文后,web界面显示为中文但是系统没有相关的字体支持,下图监控数值指标地方,显示不规则乱码
解决方法
在windows上找到 控制面板\所有控制面板项\字体 (选择一种中文字体),然后把字体拷贝到windows系统的其他目录,比如桌面等
上传到zabbix web目录
将windows 字体文件上传至zabbix web目录,具体路径为/usr/share/zabbix/assets/fonts/
[root@zabbix-server ~]#cd /usr/share/zabbix/assets/fonts/ [root@zabbix-server /usr/share/zabbix/assets/fonts]#ll total 0 lrwxrwxrwx 1 root root 33 Jul 14 17:06 graphfont.ttf -> /etc/alternatives/zabbix-web-font [root@zabbix-server /usr/share/zabbix/assets/fonts]#rz -E rz waiting to receive. [root@zabbix-server /usr/share/zabbix/assets/fonts]#ll total 11512 lrwxrwxrwx 1 root root 33 Jul 14 17:06 graphfont.ttf -> /etc/alternatives/zabbix-web-font -rw-r--r-- 1 root root 11787328 Oct 15 2019 simkai.ttf
修改字体权限,为zabbix用户和可以使用 [root@zabbix-server /usr/share/zabbix/assets/fonts]#chown zabbix.zabbix simkai.ttf [root@zabbix-server /usr/share/zabbix/assets/fonts]#ll total 11512 lrwxrwxrwx 1 root root 33 Jul 14 17:06 graphfont.ttf -> /etc/alternatives/zabbix-web-font -rw-r--r-- 1 zabbix zabbix 11787328 Oct 15 2019 simkai.ttf
#修改zabbix文件调用新字体 [root@zabbix-server /usr/share/zabbix/assets/fonts]#vim /usr/share/zabbix/include/defines.inc.php [root@zabbix-server /usr/share/zabbix/assets/fonts]#grep "define('ZBX_GRAPH_FONT_NAME" /usr/share/zabbix/include/defines.inc.php
#注销原先的行,复制修改 #define('ZBX_GRAPH_FONT_NAME', 'graphfont'); // font file name define('ZBX_GRAPH_FONT_NAME', 'simkai'); // font file name [root@zabbix-server /usr/share/zabbix/assets/fonts]#grep "ZBX_FONT_NAME" /usr/share/zabbix/include/defines.inc.php
#注销原先的行,复制修改
#define('ZBX_FONT_NAME', 'graphfont'); define('ZBX_FONT_NAME', 'simkai');
直接刷新界面,一般不用重启服务
nginx监控
10.0.0.27 nginx服务器
手动编译安装参照https://www.cnblogs.com/lyj1023/p/16328748.html
脚本自动化

#!/bin/bash # # # SRC_DIR=/usr/local/src NGINX_URL=http://nginx.org/download/ NGINX_FILE=nginx-1.18.0 TAR=.tar.gz NGINX_INSTALL_DIR=/apps/nginx CPUS=`lscpu |awk '/^CPU\(s\)/{print $2}'` color () { RES_COL=60 MOVE_TO_COL="echo -en \\033[${RES_COL}G" SETCOLOR_SUCCESS="echo -en \\033[1;32m" SETCOLOR_FAILURE="echo -en \\033[1;31m" SETCOLOR_WARNING="echo -en \\033[1;33m" SETCOLOR_NORMAL="echo -en \E[0m" echo -n "$1" && $MOVE_TO_COL echo -n "[" if [ $2 = "success" -o $2 = "0" ] ;then ${SETCOLOR_SUCCESS} echo -n $" OK " elif [ $2 = "failure" -o $2 = "1" ] ;then ${SETCOLOR_FAILURE} echo -n $"FAILED" else ${SETCOLOR_WARNING} echo -n $"WARNING" fi ${SETCOLOR_NORMAL} echo -n "]" echo } os_type () { awk -F'[ "]' '/^NAME/{print $2}' /etc/os-release } os_version () { awk -F'"' '/^VERSION_ID/{print $2}' /etc/os-release } check () { [ -e ${NGINX_INSTALL_DIR} ] && { color "nginx 已安装,请卸载后再安装" 1; exit; } cd ${SRC_DIR} if [ `os_type` == "CentOS" -a `os_version` == '8' ] ;then yum -y -q install wget elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then yum -y -q install wget else apt update &> /dev/null apt -y install wget &> /dev/null fi if [ -e ${NGINX_FILE}${TAR} ];then color "相关文件已准备好" 0 else color '开始下载 nginx 源码包' 0 wget ${NGINX_URL}${NGINX_FILE}${TAR} [ $? -ne 0 ] && { color "下载 ${NGINX_FILE}${TAR}文件失败" 1; exit; } fi } install () { color "开始安装 nginx" 0 if id nginx &> /dev/null;then color "nginx 用户已存在" 1 else useradd -s /sbin/nologin -r nginx color "创建 nginx 用户" 0 fi color "开始安装 nginx 依赖包" 0 if [ `os_type` == "CentOS" -a `os_version` == '8' ] ;then yum -y -q install make gcc-c++ libtool pcre pcre-devel zlib zlib-devel openssl openssl-devel perl-ExtUtils-Embed elif [ `os_type` == "CentOS" -a `os_version` == '7' ];then yum -y -q install make gcc pcre-devel openssl-devel zlib-devel perl-ExtUtils-Embed else apt update &> /dev/null apt -y install make gcc libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev &> /dev/null fi cd $SRC_DIR tar xf ${NGINX_FILE}${TAR} NGINX_DIR=`echo ${NGINX_FILE}${TAR}| sed -nr 's/^(.*[0-9]).*/\1/p'` cd ${NGINX_DIR} ./configure --prefix=${NGINX_INSTALL_DIR} --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module make -j $CPUS && make install [ $? -eq 0 ] && color "nginx 编译安装成功" 0 || { color "nginx 编译安装失败,退出!" 1 ;exit; } echo "PATH=${NGINX_INSTALL_DIR}/sbin:${PATH}" > /etc/profile.d/nginx.sh cat > /lib/systemd/system/nginx.service <<EOF [Unit] Description=The nginx HTTP and reverse proxy server After=network.target remote-fs.target nss-lookup.target [Service] Type=forking PIDFile=${NGINX_INSTALL_DIR}/logs/nginx.pid ExecStartPre=/bin/rm -f ${NGINX_INSTALL_DIR}/logs/nginx.pid ExecStartPre=${NGINX_INSTALL_DIR}/sbin/nginx -t ExecStart=${NGINX_INSTALL_DIR}/sbin/nginx ExecReload=/bin/kill -s HUP \$MAINPID KillSignal=SIGQUIT TimeoutStopSec=5 KillMode=process PrivateTmp=true [Install] WantedBy=multi-user.target EOF systemctl daemon-reload systemctl enable --now nginx &> /dev/null systemctl is-active nginx &> /dev/null || { color "nginx 启动失败,退出!" 1 ; exit; } color "nginx 安装完成" 0 } check install
监控nginx服务
需要安装zabbix-agent
[root@nginx ~]#rpm -Uvh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm Retrieving https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-2.el7.noarch.rpm warning: /var/tmp/rpm-tmp.tiaO42: Header V4 RSA/SHA512 Signature, key ID a14fe591: NOKEY Preparing... ################################# [100%] Updating / installing... 1:zabbix-release-4.0-2.el7 ################################# [100%] [root@nginx ~]#yum clean all Loaded plugins: fastestmirror Cleaning repos: BASE base extras updates zabbix zabbix-non-supported Cleaning up list of fastest mirrors [root@nginx ~]#yum install -y zabbix-agent
设置nginx状态页
配置文件添加以下信息
[root@nginx ~]#vim /apps/nginx/conf/nginx.conf
location /nginx_status { stub_status; allow 10.0.0.0/24; allow 192.168.1.0/24; allow 127.0.0.0; deny all; }
加载配置文件
[root@nginx ~]#nginx -s reload
页面测试
[root@zabbix-server ~]#curl 10.0.0.27/nginx_status Active connections: 1 server accepts handled requests 7 7 5 Reading: 0 Writing: 1 Waiting: 0
Active connections: #当前处于活动状态的客户端连接数,包括连接等待空闲连接数 =reading+writing+waiting accepts:#统计总值,Nginx自启动后已经接受的客户端请求连接的总数。 handled:#统计总值,Nginx自启动后已经处理完成的客户端请求连接总数,通常等于accepts,除非有因 worker_connections限制等被拒绝的连接 requests:#统计总值,Nginx自启动后客户端发来的总的请求数。 Reading:#当前状态,正在读取客户端请求报文首部的连接的连接数,数值越大,说明排队现象严重,性能不足 Writing:#当前状态,正在向客户端发送响应报文过程中的连接数,数值越大,说明访问量很大 Waiting:#当前状态,正在等待客户端发出请求的空闲连接数,开启 keep-alive的情况下,这个值等于 active – (reading+writing)
nginx监控脚本
[root@nginx ~]#cat /etc/zabbix/zabbix_agentd.d/nginx_status.sh #!/bin/bash # # nginx_status_fun(){ NGINX_PORT=$1 #端口,函数的第一个参数是脚本的第二个参数,即脚本的第二个参数是端口号 NGINX_COMMAND=$2 #命令,函数的第二个参数是脚本的第三个参数,即脚本的第三个参数是命令 nginx_active(){ #获取nginx_active数量,以下相同,这是开启了nginx状态但是只能从本机看到 /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | grep 'Active' | awk '{printf $NF}' } nginx_reading(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | grep 'Reading'| awk '{printf $2}' } nginx_writing(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | grep 'Writing'| awk '{print $4}' } nginx_waiting(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | grep 'Waiting'| awk '{print $6}' } nginx_accepts(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | awk NR==3 | awk '{print $1}' } nginx_handled(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | awk NR==3 | awk '{print $2}' } nginx_requests(){ /usr/bin/curl "http://10.0.0.27:"$NGINX_PORT"/nginx_status/" 2>/dev/null | awk NR==3 | awk '{print $3}' } case $NGINX_COMMAND in active) nginx_active; ;; reading) nginx_reading; ;; writing) nginx_writing; ;; waiting) nginx_waiting; ;; accepts) nginx_accepts; ;; handled) nginx_handled; ;; requests) nginx_requests; esac } main(){ case $1 in #分支结构,用于判断用户的输入而进行响应的操作 nginx_status) #当输入nginx_status就用nginx_status_fun,并传递第二和第三个参数 nginx_status_fun $2 $3; ;; *) #其他的输入打印帮助信息 echo $"usage: $0 {nginx_status key}" esac } main $1 $2 $3
加执行权限
[root@nginx ~]#chmod +x /etc/zabbix/zabbix_agentd.d/nginx_status.sh
zabbix agent添加自定义监控项:
[root@nginx ~]#vim /etc/zabbix/zabbix_agentd.conf UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"
zabbix agent配置文件信息
[root@nginx ~]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.0.0.17,10.0.0.27 ListenPort=10050 ListenIP=0.0.0.0 ServerActive=127.0.0.1 Hostname=Zabbix server Include=/etc/zabbix/zabbix_agentd.d/*.conf UserParameter=nginx_status[*],/etc/zabbix/zabbix_agentd.d/nginx_status.sh "$1" "$2" "$3"
zabbix agent获取监控想数据测试
[root@nginx ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"active"]" 1
zabbix server 测试监控项数据
[root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"active"]" 1
zabbix-server WEB界面配置监控模板
1、添加主机
点击添加
2、创建模板
创建应用集
创建监控项
填写 * 号标记必填处
应用集选择创建好的ngxin_status
点击更新即可
键值数据 必须保证在 zabbix-server服务器上可以监控倒数据
以此创建以下红色标记的 nginx监控项
[root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"active"]" 1[root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"reading"]" 0 [root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"writing"]" 1 [root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"waiting"]" 2 [root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"accepts"]" 8[root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"handled"]" 9 [root@zabbix-server ~]#zabbix_get -s 10.0.0.27 -p 10050 -k "nginx_status["nginx_status",80,"requests"]" 11
创建图形
1.填写图形名车给
2.监控项 点击添加
3.弹框全选,所有的监控项在一个图形显示
监控颜色自定义可调节
添加
主机,模板,选择创建好的LIJ-Linux-nginx-status模板 添加
nginx监控基本监控就创建好了
MySQL监控
实验中是 zabbix监控服务器的数据库(生产中监控的是 业务数据库)
搭建数据主从关系实验环境
ubuntu 10.0.0.100 MySQL服务器 MySQL5.7 zabbix-angel 主服务器
ubuntu 10.0.0.101 MySQL服务器 MySQL5.7 从服务器
Ubuntu系统zabbix Web界面中文设置
[21:24:15 root@ubuntu-lyj ~]#apt-get install language-pack-zh* [21:39:36 root@ubuntu-lyj ~]#cat /etc/environment PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games" LANG="zh_CN.UTF-8" #添加此行 [21:27:15 root@ubuntu-lyj ~]#dpkg-reconfigure locales
[21:27:15 root@ubuntu-lyj ~]#dpkg-reconfigure locales Generating locales (this might take a while)... en_AG.UTF-8... done en_AU.UTF-8... done en_BW.UTF-8... done en_CA.UTF-8... done en_DK.UTF-8... done en_GB.UTF-8... done en_HK.UTF-8... done en_IE.UTF-8... done en_IL.UTF-8... done en_IN.UTF-8... done en_NG.UTF-8... done en_NZ.UTF-8... done en_PH.UTF-8... done en_SG.UTF-8... done en_US.UTF-8... done en_ZA.UTF-8... done en_ZM.UTF-8... done en_ZW.UTF-8... done zh_CN.UTF-8... done zh_HK.UTF-8... done zh_SG.UTF-8... done zh_TW.UTF-8... done Generation complete. *** update-locale: Warning: LANGUAGE ("en_HK:en") is not compatible with LANG (zh_CN.UTF-8). Disabling it.
[21:29:27 root@ubuntu-lyj ~]#systemctl restart apache2
搭建主从关系
主数据库MySQL 10.0.0.101
创建授权账号
mysql> create user repluser@"10.0.0.%" identified by "123456"; Query OK, 0 rows affected (0.02 sec) mysql> grant replication slave on *.* to repluser@"10.0.0.%"; Query OK, 0 rows affected (0.00 sec)
停止业务,数据备份,做主从复制
10.0.0.100 zabbix-server服务器,停止业务
[21:39:41 root@ubuntu-lyj ~]#systemctl stop zabbix-server zabbix-agent
10.0.0.101MySQL服务器,做数据备份,增加从节点
[23:46:10 root@zabbix-mysql ~]#mysqldump -A -F --single-transaction --master-data=1 > /data/zabbix_backup_`date +%F`.sql [23:46:51 root@zabbix-mysql ~]#ll /data/zabbix_backup_2022-07-23.sql -rw-r--r-- 1 root root 3910387 Jul 23 23:46 /data/zabbix_backup_2022-07-23.sql [23:46:58 root@zabbix-mysql ~]#scp /data/zabbix_backup_2022-07-23.sql 10.0.0.102:/data/ root@10.0.0.102's password: zabbix_backup_2022-07-23.sql 100% 3819KB 95.7MB/s 00:00
10.0.0.67 MySQL从节点做数据还原
[root@slave-mysql ~]#vim /data/zabbix_backup_2022-07-17.sql
-- Position to start replication or point-in-time recovery from
--
CHANGE MASTER TO MASTER_LOG_FILE='matster-mysql-bin.000003', MASTER_LOG_POS=154;
--
-- Current Database: `mysql`
修改CHANGE MASTER TO 行
添加以下内容,保存退出
MASTER_HOST='10.0.0.101', 主节点IP
MASTER_USER='repluser', 用户
MASTER_PASSWORD='123456', 密码
MASTER_PORT=3306, 端口
CHANGE MASTER TO
MASTER_HOST='10.0.0.101',
MASTER_USER='repluser',
MASTER_PASSWORD='123456',
MASTER_PORT=3306,
MASTER_LOG_FILE='master-log.000002', MASTER_LOG_POS=2044;
还原数据库,启动主从复制
mysql> source /data/zabbix_backup_2022-07-17.sql mysql> start slave;

mysql> show slave status\G; *************************** 1. row *************************** Slave_IO_State: Waiting for master to send event Master_Host: 10.0.0.101 Master_User: repluser Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000003 Read_Master_Log_Pos: 1666 Relay_Log_File: relay-log.000002 Relay_Log_Pos: 1833 Relay_Master_Log_File: master-log.000003 Slave_IO_Running: Yes Slave_SQL_Running: Yes Replicate_Do_DB: Replicate_Ignore_DB: Replicate_Do_Table: Replicate_Ignore_Table: Replicate_Wild_Do_Table: Replicate_Wild_Ignore_Table: Last_Errno: 0 Last_Error: Skip_Counter: 0 Exec_Master_Log_Pos: 1666 Relay_Log_Space: 2034 Until_Condition: None Until_Log_File: Until_Log_Pos: 0 Master_SSL_Allowed: No Master_SSL_CA_File: Master_SSL_CA_Path: Master_SSL_Cert: Master_SSL_Cipher: Master_SSL_Key: Seconds_Behind_Master: 0 Master_SSL_Verify_Server_Cert: No Last_IO_Errno: 0 Last_IO_Error: Last_SQL_Errno: 0 Last_SQL_Error: Replicate_Ignore_Server_Ids: Master_Server_Id: 101 Master_UUID: 70af4c7d-0a81-11ed-98ce-000c29f8aada Master_Info_File: /var/lib/mysql/master.info SQL_Delay: 0 SQL_Remaining_Delay: NULL Slave_SQL_Running_State: Slave has read all relay log; waiting for more updates Master_Retry_Count: 86400 Master_Bind: Last_IO_Error_Timestamp: Last_SQL_Error_Timestamp: Master_SSL_Crl: Master_SSL_Crlpath: Retrieved_Gtid_Set: Executed_Gtid_Set: Auto_Position: 0 Replicate_Rewrite_DB: Channel_Name: Master_TLS_Version: 1 row in set (0.00 sec) ERROR: No query specified
10.0.0.17 zabbix-server服务器 启动业务,正常访问,数据写入数据库
[root@zabbix-server ~]#systemctl start zabbix-server
MySQL监控配置
安装监控服务 zabbix-agent
# wget https://repo.zabbix.com/zabbix/4.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.0-3+bionic_all.deb # dpkg -i zabbix-release_4.0-3+bionic_all.deb # apt update
apt install zabbix-agent
配置文件修改
[09:55:38 root@zabbix-mysql /tmp]#grep "^[a-Z]" /etc/zabbix/zabbix_agentd.conf PidFile=/var/run/zabbix/zabbix_agentd.pid LogFile=/var/log/zabbix/zabbix_agentd.log LogFileSize=0 Server=10.0.0.100 ListenPort=10050 StartAgents=5 ServerActive=10.0.0.100 Hostname=10.0.0.101 AllowRoot=1 User=root #以root用户登录 Include=/etc/zabbix/zabbix_agentd.d/*.conf
安装
https://www.percona.com/downloads/percona-monitoring-plugins/
下载yum安装
[00:09:43 root@zabbix-mysql ~]#dpkg -i percona-zabbix-templates_1.1.8-1.artful_all.deb
安装percona监控依赖插件 php环境
Ubuntu自带php7.2不兼容,安装php5.6版本
[00:15:16 root@zabbix-mysql ~]#add-apt-repository ppa:ondrej/php [00:18:11 root@zabbix-mysql ~]#apt-get -y update Hit:1 http://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic InRelease Hit:2 http://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-updates InRelease Hit:3 http://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-backports InRelease Hit:4 http://mirrors.tuna.tsinghua.edu.cn/ubuntu bionic-security InRelease Hit:5 http://repo.zabbix.com/zabbix/4.0/ubuntu bionic InRelease Hit:6 http://ppa.launchpad.net/ondrej/php/ubuntu bionic InRelease Reading package lists... Done [00:19:06 root@zabbix-mysql ~]#apt install -y php5.6 php5.6-mysql
[00:10:56 root@zabbix-mysql ~]#dpkg -L percona-zabbix-templates
/.
/usr
/usr/share
/usr/share/doc
/usr/share/doc/percona-zabbix-templates
/usr/share/doc/percona-zabbix-templates/changelog.Debian.gz
/usr/share/doc/percona-zabbix-templates/copyright
/var
/var/lib
/var/lib/zabbix
/var/lib/zabbix/percona
/var/lib/zabbix/percona/scripts
/var/lib/zabbix/percona/scripts/get_mysql_stats_wrapper.sh
/var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
/var/lib/zabbix/percona/templates
/var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf
/var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
复制配置文件到/etc/zabbix/zabbix_agentd.d/目录下
userparameter_percona_mysql.conf 文件中配置mysql都是监控项
[00:12:00 root@zabbix-mysql ~]#cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/
[root@matster-mysql ~]#vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php $mysql_user = 'root'; #root用户进行校验 $mysql_pass = ''; $mysql_port = 3306; $mysql_socket = NULL; $mysql_flags = 0; $mysql_ssl = FALSE; # Whether to use SSL to connect to MySQL. 创建 监控用户校验文件,记录登录用户和密码 [00:32:32 root@zabbix-mysql ~]#cat /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php.cnf <?php $mysql_user = 'root'; #root用户进行校验
$mysql_pass = '';
?>
启动文件修改,root登录
以下设置会把zabbix所有执行权限修改为root,普通用户获取不到数据
[10:00:51 root@zabbix-mysql /tmp]#vim /lib/systemd/system/zabbix-agent.service [Unit] Description=Zabbix Agent After=syslog.target After=network.target [Service] Environment="CONFFILE=/etc/zabbix/zabbix_agentd.conf" EnvironmentFile=-/etc/default/zabbix-agent Type=forking Restart=on-failure PIDFile=/run/zabbix/zabbix_agentd.pid KillMode=control-group ExecStart=/usr/sbin/zabbix_agentd -c $CONFFILE ExecStop=/bin/kill -SIGTERM $MAINPID RestartSec=10s User=root Group=root [Install] WantedBy=multi-user.target
加载配置文件,重启zabbix服务
[root@matster-mysql ~]#systemctl daemon-reload
[root@matster-mysql ~]#systemctl restart zabbix-agent
获取数据测试
[09:50:59 root@ubuntu-lyj ~]#zabbix_get -s 10.0.0.101 -p 10050 -k "MySQL.Key-read-requests" 6
percona,自带的模板文件
/var/lib/zabbix/percona/templates/zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
导入会报错,格式不兼容
以下连接修改后的模板内容
https://files.cnblogs.com/files/blogs/740428/Percona-MySQL-Server.xml?t=1658628538
上传模板zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.8.xml
报错以下界面
模板导入
正确模板格式
内容参照
https://files.cnblogs.com/files/blogs/740428/Percona-MySQL-Server.xml?t=1658628538
创建主机
主机添加模板
产看监控数据图像
实现故障和恢复的邮件通知
管理--报警媒介类型--创建媒介--填写邮箱信息
阿里邮箱信息参数 对照:https://help.aliyun.com/document_detail/36576.html
用户 选择 Admin
报警媒介,添加邮箱
创建触发器nginx 端口监控
添加动作
端口被杀死,服务停止,发送通知邮件
服务恢复,发送通知邮件
收到的邮件通知
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)