添加nginx
参考url:http://john88wang.blog.51cto.com/2165294/1577269
Nginx自带监控模块ngx_http_stub_status_module提供Nginx的基本信息
在编译安装Nginx时加参数 –with-http_stub_status_module
安装好以后可以通过nginx -V|grep http_stub_status_module 查看状态模块是否已安装
PHP-FPM也自带监控,通过在php-fpm.conf中设置
pm.status_path = /php-fpm_status
就可以获取URL的方式获取PHP-FPM的状态
参考文章
https://rtcamp.com/tutorials/php/fpm-status-page/
-
添加nginx_status.conf
server {
listen 88 ;
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
# allow 10.4.1.125;
deny all;
}
location /php-fpm_status {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
$ curl 127.0.0.1:88/nginx_status
Active connections: 1
server accepts handled requests
788163 788163 788163
Reading: 0 Writing: 1 Waiting: 0
Active connections
The current number of active client connections including Waiting connections.
活跃客户端连接数,包括处于等待状态的连接数
accepts
The total number of accepted client connections.
接收到的客户端连接总数
handled
The total number of handled connections. Generally, the parameter value is the same as accepts unless some resource limits have been reached
处理请求的总数。通常情况下,这个值和accepts的值相同。除非达到了一些资源限制。例如设置worker_connections 1024; 设置一个worker进程能够打开的最大并发连接数。
requests
The total number of client requests.
客户端请求总数
Reading
The current number of connections where nginx is reading the request header.
当前Nginx正在读取请求头的连接数量
Writing
The current number of connections where nginx is writing the response back to the client.
当前Nginx正在将响应写回到客户端的连接数量
Waiting
The current number of idle client connections waiting for a request.
当前正在等待请求的闲置客户端连接数量
$ curl 127.0.0.1:88/php-fpm_status
pool: www
process manager: dynamic
start time: 16/Nov/2014:13:29:11 +0800
start since: 77844
accepted conn: 202788
listen queue: 0
max listen queue: 1
listen queue len: 128
idle processes: 6
active processes: 1
total processes: 7
max active processes: 4
max children reached: 0
slow requests: 0
pool pool名称
process manager static or dynamic
start time 启动时间
start since 启动了多长时间,以秒为单位
accepted conn pool接收到的请求数量
listen queue the number of request in the queue of pending connections.这个值如果不为0,最好增加PHP-FPM的进程数量
max listen queue the maximum number of requests in the queue of pending connections since FPM has started
listen queue len the size of the socket queue of pending connections
idle processes the number of idle processes
active processes the number of active processes
total processes the number of idle + active processes
max active processes the maximum number of active processes since FPM has started
max children reached number of times, the process limit has been reached, when pm tries to start more children. If that value is not zero, then you may need to increase max process limit for your PHP-FPM pool. Like this, you can find other useful information to tweak your pool better way.如果这个值不为0,最好增大最大进程的限制。
slow requests 如果这个值不为0,表示有处理慢的程序
$ curl 127.0.0.1:88/php-fpm_status?full
pool: www
process manager: dynamic
start time: 17/Nov/2014:16:09:17 +0800
start since: 1139
accepted conn: 3354
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 5
active processes: 1
total processes: 6
max active processes: 2
max children reached: 0
slow requests: 0
************************
pid: 19921
state: Idle
start time: 17/Nov/2014:16:09:17 +0800
start since: 1139
requests: 563
request duration: 223
request method: GET
request URI: /php-fpm_status
content length: 0
user: -
script: -
last request cpu: 0.00
last request memory: 262144
curl 127.0.0.1:88/php-fpm_status?json
curl 127.0.0.1:88/php-fpm_status?html
curl 127.0.0.1:88/php-fpm_status?xml
2.编写Nginx和PHP-FPM状态信息获取脚本
nginx_status.sh
#!/bin/bash
#check nginx status
#ip=$(ifconfig eth0|grep "inet addr"|sed 's/^.*addr://'|awk '{print $1}')
#echo $ip
ip=127.0.0.1
port=88
#echo $ip:$port
function active() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Active"|awk '{print $NF}'
}
function reading() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Reading"|awk '{print $2}'
}
function writing() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Writing"|awk '{print $4}'
}
function waiting() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|grep "Waiting"|awk '{print $6}'
}
function accepts() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $1}'
}
function handled() {
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $2}'
}
function requests(){
/usr/bin/curl http://$ip:$port/nginx_status 2>/dev/null|awk 'NR==3{print $3}'
}
case $1 in
active)
active
;;
reading)
reading
;;
writing)
writing
;;
waiting)
waiting
;;
accepts)
accepts
;;
handled)
handled
;;
requests)
requests
;;
*)
exit 1
;;
esac
php-fpm_status.sh
#!/bin/bash
#check php-fpm status
ip=127.0.0.1
port=88
function idle() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "idle processes"|awk '{print $3}'
}
function active() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "active processes"|awk '{print $3}'|grep -v "processes"
}
function total() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "total processes"|awk '{print $3}'|grep -v "processes"
}
function mactive() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max active processes"|awk '{print $4}'
}
function conn() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "accepted conn"|awk '{print $3}'
}
function since() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "start since"|awk '{print $3}'
}
function slow() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "slow requests"|awk '{print $3}'
}
function listenqueue() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue:"|grep -v "max"|awk '{print $3}'
}
function maxlistenqueue() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max listen queue:"|awk '{print $4}'
}
function listenqueuelen() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "listen queue len:"|awk '{print $4}'
}
function maxchildren() {
/usr/bin/curl http://$ip:$port/php-fpm_status 2>/dev/null|grep "max children reached:"|awk '{print $4}'
}
$1
3.添加zabbix的子配置文件
nginx_status_zabbix.conf
### Option: UserParameter
# User-defined parameter to monitor. There can be several user-defined parameters.
# Format: UserParameter=<key>,<shell command>
# See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
# /usr/local/zabbix/bin/nginx_status.sh
UserParameter=nginx.accepts,/usr/local/zabbix/bin/nginx_status.sh accepts
UserParameter=nginx.handled,/usr/local/zabbix/bin/nginx_status.sh handled
UserParameter=nginx.requests,/usr/local/zabbix/bin/nginx_status.sh requests
UserParameter=nginx.connections.active,/usr/local/zabbix/bin/nginx_status.sh active
UserParameter=nginx.connections.reading,/usr/local/zabbix/bin/nginx_status.sh reading
UserParameter=nginx.connections.writing,/usr/local/zabbix/bin/nginx_status.sh writing
UserParameter=nginx.connections.waiting,/usr/local/zabbix/bin/nginx_status.sh waiting
php-fpm_status.conf
UserParameter=php-fpm.idle.processes,/usr/local/zabbix/bin/php-fpm_status.sh idle
UserParameter=php-fpm.total.processes,/usr/local/zabbix/bin/php-fpm_status.sh total
UserParameter=php-fpm.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh active
UserParameter=php-fpm.max.active.processes,/usr/local/zabbix/bin/php-fpm_status.sh mactive
UserParameter=php-fpm.listen.queue.len,/usr/local/zabbix/bin/php-fpm_status.sh listenqueuelen
UserParameter=php-fpm.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh listenqueue
UserParameter=php-fpm.start.since,/usr/local/zabbix/bin/php-fpm_status.sh since
UserParameter=php-fpm.accepted.conn,/usr/local/zabbix/bin/php-fpm_status.sh conn
UserParameter=php-fpm.slow.requests,/usr/local/zabbix/bin/php-fpm_status.sh slow
UserParameter=php-fpm.max.listen.queue,/usr/local/zabbix/bin/php-fpm_status.sh maxlistenqueue
UserParameter=php-fpm.max.children,/usr/local/zabbix/bin/php-fpm_status.sh maxchildren
4.添加zabbix监控模板
模板参见附件
mysql 的模板 在https://github.com/zbal/zabbix/tree/master/server_template
以前的我记得是通过php获取的,找不到了这个直接通过mysql 客户端获取的
后来发现找不到 原来是因为官网 删除了网页了 我日啊
http://os.51cto.com/art/201104/253006_all.htm 原来的安装方法。
zabbix全面监控mysql实施起来还是比较给力的!
一、从网上下载相应脚本与XML定义文件。
下载地址:http://www.zabbix.com/wiki/doku.php?id=extensive_mysql_monitoring_including_replication
- Template_MySQL_Server.xml
- Template_MySQL_Replication_Master.xml
- Template_MySQL_Replication_Slave.xml
和执行脚本:mysql.php
二、把mysql.php上传至配置文件目录/etc/zabbix/
- chmod 755 mysql.php
三、修改mysql.php文件
根据本机环境。
第一行加入:#!/usr/bin/php
在最后一行加入:?>
关闭调试:define(‘DEBUG’,true); 为 define(‘DEBUG’,False);
修改日志、数据文件路径:
- define('LOG',"/tmp/zabbix_".SYSTEM.".log");
- define('DAT',"/tmp/zabbix_".SYSTEM.".dat");
- define('UTIME',"/tmp/.zabbix_".SYSTEM.".utime");
- define('DTIME',"/tmp/.zabbix_".SYSTEM.".dtime");
修改:define(‘SYSTEM’,’mysql’.(DEBUG ? “-debug” : “”)); 为:define(‘SYSTEM’,’mysql’);
打开系统日志功能://system(“zabbix_sender -z $server -i “.DAT.” >> “.LOG); 为:system(“zabbix_sender -z $server -i “.DAT.” >> “.LOG);
在$cmd = “zabbix_sender -z $server -p 10051 -s $host -k “.SYSTEM.”.$var -o $val”;
下面增加一行:exec($cmd);
四、在zabbix_agentd.conf配置文件中加入
UserParameter=mysql.daily,php /etc/zabbix/mysql.php daily 用户名 密码
UserParameter=mysql.live,php /etc/zabbix/mysql.php live 用户名 密码
daily:每天执行一次。
live:按指定时间执行一次。
php :执行php文件
/etc/zabbix/mysql.php:mysql.php文件所在的文件路径
用户名 密码:登录mysql数据库的账户与密码
重启zabbix_agentd
五、在zabbix web管理界面导入xml文件
配置-导入/导出-浏览
六、在主机内增加指定模板
配置-主机-具体主机名-Link with Template-新增-刚导入的模板。
模板分为三种:
template MYSQL_server:监控项最全
template MYSQL_Replication_Master:只监控主要参数,监控功能相对最少
template MYSQL_Replication_Slave:主要针对性能方面的监控。
七、测试监控是否成功
删除 定时文件
- rm /tmp/.zabbix_mysql-debug.dtime;
取的每天的监控数据
- php /etc/zabbix/mysql.php daily root *****
取即时数据
- php /etc/zabbix/mysql.php live root *****
查看日志
- cat /tmp/zabbix_mysql.log
- Info from server: "Processed 1 Failed 0 Total 1 Seconds spent 0.006261"
- sent: 1; skipped: 0; total: 1
- Info from server: "Processed 1 Failed 0 Total 1 Seconds spent 0.004047"
- sent: 1; skipped: 0; total: 1
- Info from server: "Processed 0 Failed 106 Total 106 Seconds spent 0.045790"
- sent: 106; skipped: 0; total: 106
看到上述为成功
查看DAT的文件。上传的数据
- cat /tmp/zabbix_mysql.dat
八、最后查看zabbix 的最新数据
这时会看到新增加的监控项。
接下去就是绘制图形。
你们邀按上面的一步一步走,我们就可以用zabbix进行全全面监控mysql!