論語(yǔ)
由于zabbix服務(wù)是由lnmp搭建的,所以需要監(jiān)控nginx,mysql,zabbix以及服務(wù)器的性能監(jiān)控nginx首先開啟nginx的status狀態(tài)需要用到ngx_htt" />
時(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ù)器的性能
location /status{ stub_status; }
重載nginx配置systemctl reload nginx
配置完成后訪問(wèn)127.0.0.1/status可以查看nginx運(yùn)行狀態(tài)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ù)。
#/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
UserParameter=nginx.[*],/usr/lib/zabbix/alertscripts/monitor_nginx.sh $1
systemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k nginx.[ping]
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)榭?.將取到的值輸出到特定文件里
#/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
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-agentsystemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k mysql.pingzabbix_get -s 192.168.179.132 -k mysql.[Uptime]
#!/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
nohup /usr/lib/zabbix/alertscripts/monitor_mysql.sh &
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-agentsystemctl restart zabbix-agent
zabbix_get -s 192.168.179.132 -k mysql.Com_beginzabbix_get -s 192.168.179.132 -k mysql.Questions
pm.status_path = /php_status
pid = run/php-fpm.pid
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
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;}
重載nginxsystemctl reload nginx
curl 127.0.0.1/php_status
curl 127.0.0.1/php_status?full
pool:php-fpm池名稱,大多數(shù)為wwwfull詳解:
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ù)量
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)存
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-agentsystemctl restart zabbix-agent
https://github.com/zhouhua-amei/zabbix/歡迎各位一起交流學(xué)習(xí)
本文使用 文章同步助手 同步
關(guān)鍵詞:模板
客戶&案例
營(yíng)銷資訊
關(guān)于我們
客戶&案例
營(yíng)銷資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。