論語(yǔ)

由于zabbix服務(wù)是由lnmp搭建的,所以需要監(jiān)控nginx,mysql,zabbix以及服務(wù)器的性能監(jiān)控nginx首先開啟nginx的status狀態(tài)需要用到ngx_htt" />

国产成人精品无码青草_亚洲国产美女精品久久久久∴_欧美人与鲁交大毛片免费_国产果冻豆传媒麻婆精东

15158846557 在線咨詢 在線咨詢
15158846557 在線咨詢
所在位置: 首頁(yè) > 營(yíng)銷資訊 > 網(wǎng)站運(yùn)營(yíng) > Zabbix監(jiān)控lnmp(附模板)

Zabbix監(jiān)控lnmp(附模板)

時(shí)間:2023-06-11 17:45:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)

時(shí)間:2023-06-11 17:45:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)

Zabbix監(jiān)控lnmp(附模板):
君子不重則不威,學(xué)則不固,主忠信,無(wú)友不如己者,過(guò)則勿憚改。
論語(yǔ)

由于zabbix服務(wù)是由lnmp搭建的,所以需要監(jiān)控nginx,mysql,zabbix以及服務(wù)器的性能

監(jiān)控nginx

首先開啟nginx的status狀態(tài)

需要用到ngx_http_stub_status_module模塊,提供對(duì)基本狀態(tài)信息的訪問(wèn)默認(rèn)情況下不構(gòu)建此模塊,應(yīng)使用—with-http_stub_status_module 配置參數(shù)啟用它 。
修改nginx配置文件,在server下添加vim /etc/nginx/conf.d/zabbix.conf

location /status{ stub_status; }重載nginx配置
systemctl reload nginx配置完成后訪問(wèn)127.0.0.1/status可以查看nginx運(yùn)行狀態(tài)







參數(shù)解釋:

Active connections:當(dāng)前活動(dòng)客戶端連接數(shù),包括Waiting連接數(shù)。
accepts:已接受的客戶端連接總數(shù)。
handled:已處理連接的總數(shù)。
requests:客戶端請(qǐng)求的總數(shù)。
Reading:nginx正在讀取請(qǐng)求標(biāo)頭的當(dāng)前連接數(shù)。
Writing:nginx將響應(yīng)寫回客戶端的當(dāng)前連接數(shù)。Waiting:當(dāng)前等待請(qǐng)求的空閑客戶端連接數(shù)。

編寫腳本監(jiān)控nginx

#/bin/bashping() { /sbin/pidof nginx | wc -l}nginx_active(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Active/ {print $NF}'}reading(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Reading/ {print $2}'}writing(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Writing/ {print $4}' }waiting(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk '/Waiting/ {print $6}' }accepts(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $1}' }handled(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $2}' }requests(){ /usr/bin/curl -s "http://127.0.0.1/status/" |awk 'NR==3 {print $3}' }$1給腳本授予執(zhí)行權(quán)限

chmod +x /usr/lib/zabbix/alertscripts/monitor_nginx.sh

修改zabbix-agent配置文件

vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf
UserParameter=nginx.[*],/usr/lib/zabbix/alertscripts/monitor_nginx.sh $1

重啟zabbix-agent

systemctl restart zabbix-agent

使用zabbix-get測(cè)試一下

zabbix_get -s 192.168.179.132 -k nginx.[ping]

創(chuàng)建nginx監(jiān)控模板







配置模板













創(chuàng)建一個(gè)nginx的應(yīng)用集







添加監(jiān)控項(xiàng)

將nginx的status的內(nèi)容添加監(jiān)控項(xiàng),這里以nginx.ping為例







創(chuàng)建觸發(fā)器













創(chuàng)建圖形







將模板鏈接到監(jiān)控的主機(jī)







可以看到nginx的狀態(tài)信息已經(jīng)出現(xiàn)在了nginx上







監(jiān)控mysql

mysql的狀態(tài)信息可以通過(guò)以下命令獲取

mysqladmin -uroot -proot extended-statusmysqladmin -uroot -proot status但是使用明文密碼會(huì)有如下警告信息,zabbix也會(huì)取到這個(gè)報(bào)錯(cuò),導(dǎo)致監(jiān)控項(xiàng)錯(cuò)誤,解決方法可以將用戶名密碼寫入到mysql配置文件的mysqladmin中,然后在運(yùn)行命令時(shí)指定配置文件就可以了,命令如下:

mysqladmin --defaults-extra-file=/etc/my.cnf status但是修改完配置文件需要重啟mysql,這在生產(chǎn)環(huán)境中顯然不太現(xiàn)實(shí),這里我有兩種方法,兩種方法都是在系統(tǒng)上配置腳本的不同,web頁(yè)面配置相同,個(gè)人推薦使用第一種方法,簡(jiǎn)單方便

1.直接將錯(cuò)誤信息重定向?yàn)榭?.將取到的值輸出到特定文件里






系統(tǒng)配置

第一種方法:
編寫監(jiān)控腳本vim /usr/lib/zabbix/alertscripts/monitor_mysql.sh

#/bin/bashMYSQL_USER='root'# 密碼MYSQL_PWD='root'# 主機(jī)地址/IPMYSQL_HOST='192.168.179.132'# 端口MYSQL_PORT='3306'# 數(shù)據(jù)連接MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"if [ $# -ne "1" ];then echo "arg error!" exit 1fi case $1 in Uptime) result=`${MYSQL_CONN} status 2>/dev/null|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status 2>/dev/null|cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status 2>/dev/null|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status 2>/dev/null|grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status 2>/dev/mull|grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; Open_tables) result=`${MYSQL_CONN} extended-status 2>/dev/mull|grep -w "Open_tables"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin|Open_tables)" ;;esac給腳本授予可執(zhí)行權(quán)限

chmod +x /usr/lib/zabbix/alertscripts/monitor_mysql.sh

修改zabbix-agent配置文件

vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

UserParameter=mysql.status[*],/usr/lib/zabbix/alertscripts/monitor_mysql.sh $1UserParameter=mysql.ping,/usr/local/mysql/bin/mysqladmin -uroot -proot ping 2>/dev/null |grep -c aliveUserParameter=mysql.slave,mysql -uroot -proot -e 'show slave status/G' 2>/dev/null|grep -E "Slave_IO_Running|Slave_SQL_Running"|awk '{print $2}'|grep -c Yes重啟zabbix-agent

systemctl restart zabbix-agent

使用zabbix_get測(cè)試是否成功獲取值

zabbix_get -s 192.168.179.132 -k mysql.pingzabbix_get -s 192.168.179.132 -k mysql.[Uptime]





第二種方法:
編寫監(jiān)控腳本vim /usr/lib/zabbix/alertscripts/monitor_mysql.sh

#!/bin/bashMYSQL_USER='root'# 密碼MYSQL_PWD='root'# 主機(jī)地址/IPMYSQL_HOST='192.168.179.132'# 端口MYSQL_PORT='3306'# 數(shù)據(jù)連接MYSQL_CONN="/usr/local/mysql/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"#MYSQL_CONN="/usr/local/mysql/bin/mysqladmin "# 參數(shù)是否正確if [ $# -ne "1" ];then echo "arg error!" fi # 獲取數(shù)據(jù)case $1 in Uptime) result=`${MYSQL_CONN} status|cut -f2 -d":"|cut -f1 -d"T"` echo $result ;; Com_update) result=`${MYSQL_CONN} extended-status |grep -w "Com_update"|cut -d"|" -f3` echo $result ;; Slow_queries) result=`${MYSQL_CONN} status |cut -f5 -d":"|cut -f1 -d"O"` echo $result ;; Com_select) result=`${MYSQL_CONN} extended-status |grep -w "Com_select"|cut -d"|" -f3` echo $result ;; Com_rollback) result=`${MYSQL_CONN} extended-status |grep -w "Com_rollback"|cut -d"|" -f3` echo $result ;; Questions) result=`${MYSQL_CONN} status|cut -f4 -d":"|cut -f1 -d"S"` echo $result ;; Com_insert) result=`${MYSQL_CONN} extended-status |grep -w "Com_insert"|cut -d"|" -f3` echo $result ;; Com_delete) result=`${MYSQL_CONN} extended-status |grep -w "Com_delete"|cut -d"|" -f3` echo $result ;; Com_commit) result=`${MYSQL_CONN} extended-status |grep -w "Com_commit"|cut -d"|" -f3` echo $result ;; Bytes_sent) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_sent" |cut -d"|" -f3` echo $result ;; Bytes_received) result=`${MYSQL_CONN} extended-status |grep -w "Bytes_received" |cut -d"|" -f3` echo $result ;; Com_begin) result=`${MYSQL_CONN} extended-status |grep -w "Com_begin"|cut -d"|" -f3` echo $result ;; *) echo "Usage:$0(Uptime|Com_update|Slow_queries|Com_select|Com_rollback|Questions|Com_insert|Com_delete|Com_commit|Bytes_sent|Bytes_received|Com_begin)" ;; esac給腳本授予執(zhí)行權(quán)限
chmod +x /usr/lib/zabbix/alertscripts/monitor_mysql.sh

將腳本放入后臺(tái)運(yùn)行
nohup /usr/lib/zabbix/alertscripts/monitor_mysql.sh &

修改zabbix-agent配置文件vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.conf

UserParameter=mysql.version,/usr/local/mysql/bin/mysql -VUserParameter=mysql.Uptime,cat /monitor_mysql/Uptime.txtUserParameter=mysql.Com_update,cat /monitor_mysql/Com_update.txtUserParameter=mysql.Slow_queries,cat /monitor_mysql/Slow_queries.txtUserParameter=mysql.Com_select,cat /monitor_mysql/Com_select.txtUserParameter=mysql.Com_rollback,cat /monitor_mysql/Com_rollback.txtUserParameter=mysql.Questions,cat /monitor_mysql/Questions.txtUserParameter=mysql.Com_insert,cat /monitor_mysql/Com_insert.txtUserParameter=mysql.Com_delete,cat /monitor_mysql/Com_delete.txtUserParameter=mysql.Com_commit,cat /monitor_mysql/Com_commit.txtUserParameter=mysql.Bytes_sent,cat /monitor_mysql/Bytes_sent.txtUserParameter=mysql.Bytes_received,cat /monitor_mysql/Bytes_received.txtUserParameter=mysql.Com_begin,cat /monitor_mysql/Com_begin.txtUserParameter=mysql.ping,cat /monitor_mysql/ping.txt重啟zabbix-agent

systemctl restart zabbix-agent

使用zabbix_get測(cè)試鍵值是否可用

zabbix_get -s 192.168.179.132 -k mysql.Com_beginzabbix_get -s 192.168.179.132 -k mysql.Questions





添加模板







zabbix自帶了一個(gè)mysql的模板,我們只需要在原有模板上修改一下就可以了

修改監(jiān)控項(xiàng),與配置文件的相同







修改完的監(jiān)控項(xiàng)如下所示







將數(shù)據(jù)庫(kù)模板鏈接到lnmp模板上







監(jiān)控php-fpm

修改PHP配置文件,啟用php-fpm的狀態(tài)功能

vim /usr/local/php/etc/php-fpm.d/www.conf,添加以下字段,以啟用php-fpm的狀態(tài)功能
pm.status_path = /php_status

vim /usr/local/php/etc/php-fpm.conf,將下面字段取消注釋,這樣就可以使用pid文件進(jìn)行重啟php-fpm
pid = run/php-fpm.pid

重啟php-fpm

kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`

修改nginx配置文件

在nginx中開啟對(duì)php-fpm的狀態(tài)訪問(wèn)

location ~ ^/(php_status|ping)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name; include fastcgi_params;}重載nginx

systemctl reload nginx

測(cè)試配置是否成功

curl 127.0.0.1/php_status







訪問(wèn)帶參數(shù)的頁(yè)面

curl 127.0.0.1/php_status?full







php-fpm狀態(tài)參數(shù)解釋:

pool:php-fpm池名稱,大多數(shù)為www
process manager:進(jìn)程管理方式,值:static, dynamic or ondemand. dynamic
start time:?jiǎn)?dòng)日期,如果reload了php-fpm,時(shí)間會(huì)更新
start since:運(yùn)行時(shí)長(zhǎng)
accepted conn:當(dāng)前池接受的請(qǐng)求數(shù)
listen queue:請(qǐng)求等待隊(duì)列,如果這個(gè)值不為0,那么要增加FPM的進(jìn)程數(shù)量
max listen queue:請(qǐng)求等待隊(duì)列最高的數(shù)量
listen queue len:socket等待隊(duì)列長(zhǎng)度
idle processes:空閑進(jìn)程數(shù)量
active processes:活躍進(jìn)程數(shù)量
total processes:總進(jìn)程數(shù)量
max active processes:最大的活躍進(jìn)程數(shù)量(FPM啟動(dòng)開始算)
max children reached:進(jìn)程最大數(shù)量限制的次數(shù),如果這個(gè)數(shù)量不為0,那說(shuō)明你的最大進(jìn)程數(shù)量太小了,請(qǐng)改大一點(diǎn)。slow requests:?jiǎn)⒂昧藀hp-fpm slow-log,緩慢請(qǐng)求的數(shù)量
full詳解:

pid – 進(jìn)程PID,可以單獨(dú)kill這個(gè)進(jìn)程.
state – 當(dāng)前進(jìn)程的狀態(tài) (Idle, Running, …)
start time – 進(jìn)程啟動(dòng)的日期
start since – 當(dāng)前進(jìn)程運(yùn)行時(shí)長(zhǎng)
requests – 當(dāng)前進(jìn)程處理了多少個(gè)請(qǐng)求
request duration – 請(qǐng)求時(shí)長(zhǎng)(微妙)
request method – 請(qǐng)求方法 (GET, POST, …)
request URI – 請(qǐng)求URI
content length – 請(qǐng)求內(nèi)容長(zhǎng)度 (僅用于 POST)
user – 用戶 (PHP_AUTH_USER) (or ‘-’ 如果沒設(shè)置)
script – PHP腳本 (or ‘-’ if not set)
last request cpu – 最后一個(gè)請(qǐng)求CPU使用率。last request memorythe - 上一個(gè)請(qǐng)求使用的內(nèi)存

配置zabbix-agent文件,添加php-fpm監(jiān)控

vim /etc/zabbix/zabbix_agentd.d/userparameter_mysql.confUserParameter=php-fpm.status[*],/usr/bin/curl -s "http://127.0.0.1/php_status?xml"| grep "<$1>"|/usr/bin/cut -d '>' -f2 |cut -d '<' -f1 UserParameter=php-fpm.ping,/sbin/pidof php-fpm | wc -l UserParameter=php-fpm.version,/usr/local/php/sbin/php-fpm -v | awk 'NR==1{print $1,$2}'重啟zabbix-agent

systemctl restart zabbix-agent

使用zabbix_get測(cè)試







添加監(jiān)控項(xiàng)到模板







以添加php-fpm啟停狀態(tài)監(jiān)控為例







php-fpm所有監(jiān)控如下所示







創(chuàng)建觸發(fā)器







創(chuàng)建圖形







監(jiān)控zabbix-server

zabbix自帶了監(jiān)控自身的模板,因此只要在模板上鏈接zabbix-server的模板就可以了,zabbix-agent的模板已經(jīng)鏈接到os的模板上了,因此無(wú)需添加zabbix-agent模板







文中的模板以及所用到的腳本已經(jīng)上傳到GitHub,訪問(wèn)鏈接獲取

https://github.com/zhouhua-amei/zabbix/
歡迎各位一起交流學(xué)習(xí)







本文使用 文章同步助手 同步

關(guān)鍵詞:模板

74
73
25
news

版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。

為了最佳展示效果,本站不支持IE9及以下版本的瀏覽器,建議您使用谷歌Chrome瀏覽器。 點(diǎn)擊下載Chrome瀏覽器
關(guān)閉