您的位置:首页 > 运维架构 > Nginx

zabbix密钥监控mysal、nginx、php

2017-12-06 15:47 435 查看
一、zabbix监控mysql

在客户机上配置:

[root@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_mysql.sh

#!/bin/bash
# -------------------------------------------------------------------------------
# FileName:    check_mysql.sh
# Revision:    1.0
# Date:        2016/06/20
# Author:      Liuguangshun
# Email:       lgs6666@yeah.net
# Website:     liuguangshun.github.io
# Description:
# Notes:       ~
# -------------------------------------------------------------------------------
# Copyright:   2016 (c) Liuguangshun
# License:
# 用户名
MYSQL_USER='root'
# 密码
MYSQL_PWD='YyIrpFNhIofsd'
# 主机地址/IP
MYSQL_HOST='127.0.0.1'
# 端口
MYSQL_PORT='3306'
# 数据连接
MYSQL_CONN="/usr/local/bin/mysqladmin -u${MYSQL_USER} -p${MYSQL_PWD} -h${MYSQL_HOST} -P${MYSQL_PORT}"
# 参数是否正确
if [ $# -ne "1" ];then
echo "arg error!"
fi
# 获取数据
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


[root@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

在后面添加:

# 获取mysql版本
UserParameter=mysql.version,mysql -V
#  获取mysql性能指标,这个是上面定义好的脚本
UserParameter=mysql.status[*],/usr/local/zabbix/etc/chk_mysql.sh $1
#  获取mysql运行状态
UserParameter=mysql.ping,mysqladmin -uroot -pYyIrpFNhIofsd -P3306 -h127.0.0.1  ping | grep -c alive


服务端取值:

[root@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p10050 -k mysql.status[Com_update]

297

二、监控php-fpm

[root@iZ25j76vlxnZ ~]# vim /usr/local/php/etc/php-fpm.conf

pm.status_path = /status    //把;好去掉,启用监控fpm状态


[root@iZ25j76vlxnZ ~]# vim /usr/local/nginx/conf/nginx.conf

location ~ ^/(status|ping)$
{
fastcgi_pass   127.0.0.1:9000;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
allow 127.0.0.1;
allow 10.170.236.125;
deny all;
}


[root@iZ25j76vlxnZ ~]# service nginx restart

[root@iZ25j76vlxnZ etc]# curl -s http://localhost/status //测试是否可以看到php-fpm的状态,有信息即可

创建监控脚本:

[root@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_php-fpm.sh

#!/bin/bash
#monitor php-fpm status from zabbix
#lincense:GPL
#mail:lgs6666@yeah.net
#date:2016.06.20
source /etc/bashrc >/dev/null 2>&1
source /etc/profile >/dev/null 2>&1
LOG=/usr/local/zabbix/etc/phpfpm_status.log
curl -s http://localhost/status >$LOG
pool(){
awk '/pool/ {print $NF}' $LOG
}
process_manager(){
awk '/process manager/ {print $NF}' $LOG
}
start_since(){
awk '/start since:/ {print $NF}' $LOG
}
accepted_conn(){
awk '/accepted conn:/ {print $NF}' $LOG
}
listen_queue(){
awk '/^(listen queue:)/ {print $NF}' $LOG
}
max_listen_queue(){
awk '/max listen queue:/ {print $NF}' $LOG
}
listen_queue_len(){
awk '/listen queue len:/ {print $NF}' $LOG
}
idle_processes(){
awk '/idle processes:/ {print $NF}' $LOG
}
active_processes(){
awk '/^(active processes:)/ {print $NF}' $LOG
}
total_processes(){
awk '/total processes:/ {print $NF}' $LOG
}
max_active_processes(){
awk '/max active processes:/ {print $NF}' $LOG
}
max_children_reached(){
awk '/max children reached:/ {print $NF}' $LOG
}
slow_requests(){
awk '/slow requests:/ {print $NF}' $LOG
}
case "$1" in
pool)
pool
;;
process_manager)
process_manager
;;
start_since)
start_since
;;
accepted_conn)
accepted_conn
;;
listen_queue)
listen_queue
;;
max_listen_queue)
max_listen_queue
;;
listen_queue_len)
listen_queue_len
;;
idle_processes)
idle_processes
;;
active_processes)
active_processes
;;
total_processes)
total_processes
;;
max_active_processes)
max_active_processes
;;
max_children_reached)
max_children_reached
;;
slow_requests)
slow_requests
;;
*)
echo "Usage: $0 {pool|process_manager|start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached|slow_requests}"
esac


引用脚本文件

[root@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/zabbix_agentd.conf

#UserParameter=php-fpm[*],/usr/local/zabbix/etc/chk_php-fpm.sh "$1"
UserParameter=phpfpm.status.pool,/usr/local/zabbix/etc/chk_php-fpm.sh pool
UserParameter=phpfpm.status.process.manager,/usr/local/zabbix/etc/chk_php-fpm.sh process_manager
UserParameter=phpfpm.status.start.since,/usr/local/zabbix/etc/chk_php-fpm.sh start_since
UserParameter=phpfpm.status.accepted.conn,/usr/local/zabbix/etc/chk_php-fpm.sh accepted_conn
UserParameter=phpfpm.status.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queue
UserParameter=phpfpm.status.max.listen.queue,/usr/local/zabbix/etc/chk_php-fpm.sh max_listen_queue
UserParameter=phpfpm.status.listen.queue.len,/usr/local/zabbix/etc/chk_php-fpm.sh listen_queue_len
UserParameter=phpfpm.status.idle.processes,/usr/local/zabbix/etc/chk_php-fpm.sh idle_processes
UserParameter=phpfpm.status.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh active_processes
UserParameter=phpfpm.status.total.processes,/usr/local/zabbix/etc/chk_php-fpm.sh total_processes
UserParameter=phpfpm.status.max.active.processes,/usr/local/zabbix/etc/chk_php-fpm.sh max_active_processes
UserParameter=phpfpm.status.max.children.reached,/usr/local/zabbix/etc/chk_php-fpm.sh max_children_reached
UserParameter=phpfpm.status.slow.requests,/usr/local/zabbix/etc/chk_php-fpm.sh slow_requests


[root@iZ25j76vlxnZ ~]# /etc/init.d/zabbix_agentd restart

[root@iZ25wrxqp21Z etc]# curl http://127.0.0.1/status
pool: www

process manager: dynamic

start time: 27/Apr/2016:14:21:33 +0800

start since: 85601

accepted conn: 37078

listen queue: 0

max listen queue: 13

listen queue len: 128

idle processes: 2

active processes: 1

total processes: 3

max active processes: 5

max children reached: 4

slow requests: 0

下面介绍每个参数的作用:

pool:php-fpm池的名称,一般都是应该是www

process manage:进程的管理方法,php-fpm支持三种管理方法,分别是static,dynamic和ondemand,一般情况下都是dynamic

start time:php-fpm启动时候的时间,不管是restart或者reload都会更新这里的时间

start since:php-fpm自启动起来经过的时间,默认为秒

accepted conn:当前接收的连接数

listen queue:在队列中等待连接的请求个数,如果这个数字为非0,那么最好增加进程的fpm个数

max listen queue:从fpm启动以来,在队列中等待连接请求的最大值

listen queue len:等待连接的套接字队列大小

idle processes:空闲的进程个数

active processes:活动的进程个数

total processes:总共的进程个数

max active processes:从fpm启动以来,活动进程的最大个数,如果这个值小于当前的max_children,可以调小此值

max children reached:当pm尝试启动更多的进程,却因为max_children的限制,没有启动更多进程的次数。如果这个值非0,那么可以适当增加fpm的进程数

slow requests:慢请求的次数,一般如果这个值未非0,那么可能会有慢的php进程,一般一个不好的mysql查询是最大的祸首。

服务器验证:

[root@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p 10050 -k phpfpm.status.idle.processes

127 //获取到数据说明配置没问题了,接下来在web页面上添加模板

三、监控nginx

[root@iZ25j76vlxnZ ~]# vim /usr/local/nginx/conf/nginx.conf

在php-pfm下增加:

location /nginx-status {
stub_status on;
access_log on;
allow 127.0.0.1;
allow 10.170.236.125;
deny all;
}


[root@iZ25j76vlxnZ ~]# vim /usr/local/zabbix/etc/chk_nginx.sh

#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: lgs6666@yeah.net
# License: GPLv2
# Set Variables
BKUP_DATE=`/bin/date +%Y%m%d`
LOG="/data/log/zabbix/webstatus.log"
HOST=127.0.0.1
PORT="80"
# Functions to return nginx stats
function active {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Active' | awk '{print $NF}'
}
function reading {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Reading' | awk '{print $2}'
}
function writing {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Writing' | awk '{print $4}'
}
function waiting {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| grep 'Waiting' | awk '{print $6}'
}
function accepts {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $1}'
}
function handled {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $2}'
}
function requests {
/usr/bin/curl "http://$HOST:$PORT/nginx-status" 2>/dev/null| awk NR==3 | awk '{print $3}'
}
# Run the requested function
$1


在被监控端配置:

[liuguangshun@iZ25j76vlxnZ ~]$ vim /usr/local/zabbix/etc/zabbix_agentd.conf

在最后添加:

UserParameter=nginx.accepts,/usr/local/zabbix/etc/chk_nginx.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix/etc/chk_nginx.sh handled
UserParameter=nginx.requests,/usr/local/zabbix/etc/chk_nginx.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix/etc/chk_nginx.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix/etc/chk_nginx.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix/etc/chk_nginx.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix/etc/chk_nginx.sh waiting


在被监控查看nginx状态:

[root@iZ25wrxqp21Z etc]# curl http://127.0.0.1/nginx-status
Active connections: 1

server accepts handled requests

67561 67561 159860

Reading: 0 Writing: 1 Waiting: 0

Activeconnections:对后端发起的活动连接数;

server accepts 67561:nginx 总共处理了67561个连接;

handled:成功创建了67561次握手;

requests:总共处理了159860请求。

Reading:nginx读取客户端的header数;

Writing: nginx 返回给客户端的header数;

Waiting: nginx 请求处理完成,正在等待下一请求指令的连接。

服务器上测试

[root@iZ25t2tlsnqZ ~]# zabbix_get -s 10.44.37.221 -p 10050 -k "nginx.accepts"

3151

注意:在web添加模板时,键值(密钥,web是通过键值来链接监控服务器的)要和监控端UserParameter后面的值一样,例如nginx.accepts
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zabbix监控LNMP