A機(jī)器:zabbix服務(wù)端(192.168.234.128) B機(jī)器:zabbix客戶端(192.168.234.125)
創(chuàng)新互聯(lián)公司從2013年成立,先為北海等服務(wù)建站,北海等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為北海企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
在B機(jī)器(zabbix客戶端)操作:
編輯nginx虛擬主機(jī)配置文件:
[root@centos ~]# vi /etc/nginx/conf.d/default.conf
在server{}中添加以下內(nèi)容:
location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; }
重載nginx配置:
[root@centos ~]# nginx -s reload
測(cè)試:
[root@centos ~]# curl http://127.0.0.1/nginx_status Active connections: 1 server accepts handled requests 3 3 3 Reading: 0 Writing: 1 Waiting: 0
#nginx狀態(tài)信息已顯示
狀態(tài)說(shuō)明:
添加監(jiān)控腳本:
vi /usr/local/sbin/ngx_status.sh
添加以下內(nèi)容:
#!/bin/bash url="http://127.0.0.1/nginx_status" curl=/usr/bin/curl # 檢測(cè)nginx進(jìn)程是否存在 function ping { /sbin/pidof nginx | wc -l } # 檢測(cè)nginx性能 function active { $curl $url 2>/dev/null| grep 'Active' | awk '{print $NF}' } function reading { $curl $url 2>/dev/null| grep 'Reading' | awk '{print $2}' } function writing { $curl $url 2>/dev/null| grep 'Writing' | awk '{print $4}' } function waiting { $curl $url 2>/dev/null| grep 'Waiting' | awk '{print $6}' } function accepts { $curl $url 2>/dev/null| awk NR==3 | awk '{print $1}' } function handled { $curl $url 2>/dev/null| awk NR==3 | awk '{print $2}' } function requests { $curl $url 2>/dev/null| awk NR==3 | awk '{print $3}' } $1
添加權(quán)限:
[root@centos ~]# chmod 755 /usr/local/sbin/ngx_status.sh
編輯zabbix_agent配置文件:
[root@centos ~]# vi /etc/zabbix/zabbix_agentd.conf
在Option:UserParameter處添加:UserParameter=nginx.status[*],/usr/local/sbin/ngx_status.sh $1
重啟服務(wù):
[root@centos ~]# systemctl restart zabbix-agent
在A機(jī)器(服務(wù)端)測(cè)試:
[root@zabbix ~]# zabbix_get -s 192.168.234.125 -k 'nginx.status[accepts]' 6
在本機(jī)創(chuàng)建.xml文件并添加以下內(nèi)容:(文件名稱(chēng)自定義)
<?xml version="1.0" encoding="UTF-8"?>4.0 2019-02-11T07:29:29Z Templates Template App NGINX Template App NGINX Templates nginx nginx status server accepts 0 nginx.status[accepts] 60 90d 365d 0 3 0 0 0 0 accepts 0 nginx 10 3s 200 1 0 0 0 0 0 0 0 nginx status connections active 0 nginx.status[active] 60 90d 365d 0 3 0 0 0 0 active 0 nginx 3s 200 1 0 0 0 0 0 0 0 nginx status server handled 0 nginx.status[handled] 60 90d 365d 0 3 0 0 0 0 handled 0 nginx 10 3s 200 1 0 0 0 0 0 0 0 nginx status PING 0 nginx.status[ping] 60 30d 365d 0 3 0 0 0 0 is live 0 nginx Service state 3s 200 1 0 0 0 0 0 0 0 nginx status connections reading 0 nginx.status[reading] 60 90d 365d 0 3 0 0 0 0 reading 0 nginx 3s 200 1 0 0 0 0 0 0 0 nginx status server requests 0 nginx.status[requests] 60 90d 365d 0 3 0 0 0 0 requests 0 nginx 10 3s 200 1 0 0 0 0 0 0 0 nginx status connections waiting 0 nginx.status[waiting] 60 90d 365d 0 3 0 0 0 0 waiting 0 nginx 3s 200 1 0 0 0 0 0 0 0 nginx status connections writing 0 nginx.status[writing] 60 90d 365d 0 3 0 0 0 0 writing 0 nginx 3s 200 1 0 0 0 0 0 0 0 {Template App NGINX:nginx.status[ping].last()}=0 0 nginx was down! 0 0 4 NGINX進(jìn)程數(shù):0,請(qǐng)注意 0 0 nginx status connections 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 00C800 0 2 0 Template App NGINX nginx.status[active] 1 0 C80000 0 2 0 Template App NGINX nginx.status[reading] 2 0 0000C8 0 2 0 Template App NGINX nginx.status[waiting] 3 0 C800C8 0 2 0 Template App NGINX nginx.status[writing] nginx status server 900 200 0.0000 100.0000 1 1 0 1 0 0.0000 0.0000 0 0 0 0 0 0 00C800 0 2 0 Template App NGINX nginx.status[accepts] 1 0 C80000 0 2 0 Template App NGINX nginx.status[handled] 2 0 0000C8 0 2 0 Template App NGINX nginx.status[requests] Service state 0 Down 1 Up
在zabbix前端頁(yè)面點(diǎn)擊配置→模板→導(dǎo)入:
#導(dǎo)入剛才創(chuàng)建的xml文件即可
導(dǎo)入成功后在主機(jī)列表頁(yè)面選擇被監(jiān)控的機(jī)器(B機(jī)器)鏈接該模板即可開(kāi)始監(jiān)控nginx
A機(jī)器:zabbix服務(wù)端(192.168.234.128) B機(jī)器:zabbix客戶端(192.168.234.125)
在A機(jī)器安裝zabbix-java-gateway:
[root@zabbix ~]# wget https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-java-gateway-4.0.14-1.el7.x86_64.rpm [root@zabbix ~]# yum -y localinstall zabbix-java-gateway-4.0.14-1.el7.x86_64.rpm
編輯gateway配置文件:
[root@zabbix ~]# vi /etc/zabbix/zabbix_java_gateway.conf
去掉LISTEN_IP、LISTEN_PORT(監(jiān)聽(tīng)端口)、START_POLLERS(進(jìn)程數(shù))的注釋符號(hào)#:
#LISTEN_IP定義被監(jiān)控機(jī)器的地址,不指定默認(rèn)監(jiān)聽(tīng)全部機(jī)器
編輯server配置文件:
[root@zabbix ~]# vi /etc/zabbix/zabbix_server.conf
定義以下3個(gè)配置參數(shù):
啟動(dòng)zabbix-java-gateway服務(wù):
[root@zabbix ~]# systemctl start zabbix-java-gateway
重啟zabbix-server服務(wù):
[root@zabbix ~]# systemctl restart zabbix-server
查看監(jiān)聽(tīng)端口:
[root@zabbix ~]# netstat -lntp |grep java tcp6 0 0 :::10052 :::* LISTEN 8706/java
開(kāi)啟JMX
在B機(jī)器編輯tomcat配置文件:
[root@centos ~]# vi /usr/local/tomcat/bin/catalina.sh
添加以下內(nèi)容:
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Djava.rmi.server.hostname=192.168.234.125 -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
#hostname定義B機(jī)器地址,端口9999(默認(rèn)12345)
重啟tomcat:
[root@centos ~]# /usr/local/tomcat/bin/shutdown.sh [root@centos ~]# /usr/local/tomcat/bin/startup.sh
查看監(jiān)聽(tīng)端口9999:
[root@centos ~]# netstat -lntp |grep 9999 tcp6 0 0 :::9999 :::* LISTEN 25861/java
進(jìn)入zabbix前端頁(yè)面添加主機(jī):
#與普通情況添加監(jiān)控主機(jī)不同,監(jiān)控B機(jī)器的tomcat需要配置JMX接口
模板鏈接JMX相關(guān)的兩個(gè)模板:
當(dāng)JMX圖標(biāo)變綠即表示配置成功:
A機(jī)器:zabbix服務(wù)端(192.168.234.128) B機(jī)器:zabbix客戶端(192.168.234.125)
zabbix_agent定義mysql監(jiān)控的配置文件:/etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
在B機(jī)器登錄mysql創(chuàng)建用戶:
mysql> grant usage,process,replication client on *.* to zamysql@'localhost' identified by 'zamysql';
創(chuàng)建配置文件指定的目錄:
[root@centos ~]# mkdir /var/lib/zabbix
創(chuàng)建my.cnf文件:
[root@centos ~]# vi /var/lib/zabbix/.my.cnf
#需要注意該文件是隱藏文件
添加以下內(nèi)容:
[mysql] host=localhost user=zamysql password='zamysql' socket=/tmp/mysql.sock [mysqladmin] host=localhost user=zamysql password='zamysql' socket=/tmp/mysql.sock
在A機(jī)器測(cè)試:
[root@zabbix ~]# zabbix_get -s 192.168.234.125 -p 10050 -k mysql.ping 1 [root@zabbix ~]# zabbix_get -s 192.168.234.125 -p 10050 -k mysql.version mysql Ver 14.14 Distrib 5.6.43, for linux-glibc2.12 (x86_64) using EditLine wrapper [root@zabbix ~]# zabbix_get -s 192.168.234.125 -p 10050 -k mysql.size 10240
#已成功獲取數(shù)據(jù)
在zabbix前端頁(yè)面添加主機(jī):
監(jiān)控模板選擇DB mysql:
ZBX標(biāo)準(zhǔn)變?yōu)榫G色表示成功:
在監(jiān)測(cè)→最新數(shù)據(jù)頁(yè)面即可查看mysql監(jiān)控?cái)?shù)據(jù):
總結(jié)
以上所述是小編給大家介紹的zabbix監(jiān)控Nginx/Tomcat/MySQL的詳細(xì)教程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!