03.Zabbix应用服务监控
回到顶部(go to top)
一、Zabbix监控Nginx
1.1 实验环境
服务器系统 | 角色 | IP |
---|---|---|
CentOS 7.4 x86_64 | Zabbix-Server | 192.168.90.10 |
CentOS 7.4 x86_64 | Zabbix-Agent | 192.168.90.11 |
1.2.在nginx.conf
的Server
标签下添加如下内容
location /nginx_status {
stub_status on;
access_log off;
allow 127.0.0.1;
deny all;
}
1.3.本地访问Nginx Status
[root@linux-node1 ~]# curl http://127.0.0.1/nginx_status
Active connections: 1
server accepts handled requests
1 1 1
Reading: 0 Writing: 1 Waiting: 0
1.4.编写Nginx的Shell脚本(如果端口不一致,只需要修改脚本端口即可)
[root@Agent ~]# mkdir -p /etc/zabbix/scripts
[root@linux-node1 scripts]# vim /etc/zabbix/scripts/nginx_status.sh
#!/bin/bash
############################################################
# $Name: nginx_status.sh
# $Version: v1.0
# $Function: Nginx Status
# $Author: xuliangwei
# $organization: www.xuliangwei.com,www,bjstack.com
# $Create Date: 2016-06-23
# $Description: Monitor Nginx Service Status
############################################################
NGINX_PORT=80 #如果端口不同仅需要修改脚本即可,否则修改xml很麻烦
NGINX_COMMAND=$1
nginx_active(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Active/ {print $NF}'
}
nginx_reading(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Reading/ {print $2}'
}
nginx_writing(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Writing/ {print $4}'
}
nginx_waiting(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk '/Waiting/ {print $6}'
}
nginx_accepts(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $1}'
}
nginx_handled(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $2}'
}
nginx_requests(){
/usr/bin/curl -s "http://127.0.0.1:"$NGINX_PORT"/nginx_status/" |awk 'NR==3 {print $3}'
}
case $NGINX_COMMAND in
active)
nginx_active;
;;
reading)
nginx_reading;
;;
writing)
nginx_writing;
;;
waiting)
nginx_waiting;
;;
accepts)
nginx_accepts;
;;
handled)
nginx_handled;
;;
requests)
nginx_requests;
;;
*)
echo $"USAGE:$0 {active|reading|writing|waiting|accepts|handled|requests}"
esac
1.5.给脚本添加执行权限
[root@Agent]# chmod +x /etc/zabbix/scripts/nginx_status.sh
1.6.监控项nginx_status.conf
的配置文件如下:
[root@Agent ~]# cat /etc/zabbix/zabbix_agentd.d/nginx_status.conf
UserParameter=nginx_status[*],/bin/bash /etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh "$1"
1.7.重启zabbix-agent
[root@Agent ~]# systemctl restart zabbix-agent
1.8.使用Zabbix_get
来获取值
[root@linux-node1 ~]# zabbix_get -s 192.168.90.11 -k nginx_status[writing]
1
1.9.添加所有监控项, 如图4-3, 记得关联到指定的主机
二、Zabbix监控PHP
2.1.实践环境
服务器系统 | 角色 | IP |
---|---|---|
CentOS 7.4 x86_64 | Zabbix-Server | 192.168.90.10 |
CentOS 7.4 x86_64 | Zabbix-Agent | 192.168.90.11 |
2.2.PHP-FPM
工作模式通常与Nginx
结合使用,修改php-fpm.conf
[root@Agent ~]# vim /etc/php-fpm.d/www.conf
pm.status_path = /phpfpm_status
2.3.修改nginx.conf
的配置文件,增加如下location
访问PHP-FPM
状态信息。
location ~ ^/(phpfpm_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
2.4.访问测试phpfpm_status
[root@Agent ~]# curl http://127.0.0.1/phpfpm_status
pool: www
process manager: dynamic
start time: 05/Jul/2016:15:30:56 +0800
start since: 409
accepted conn: 22
listen queue: 0
max listen queue: 0
listen queue len: 128
idle processes: 4
active processes: 1
total processes: 5
max active processes: 2
max children reached: 0
#PHP-FPM状态解释:
pool #fpm池名称,大多数为www
process manager #进程管理方式dynamic或者static
start time #启动日志,如果reload了fpm,时间会更新
start since #运行时间
accepted conn #当前池接受的请求数
listen queue #请求等待队列,如果这个值不为0,那么需要增加FPM的进程数量
max listen queue #请求等待队列最高的数量
listen queue len #socket等待队列长度
idle processes #空闲进程数量
active processes #活跃进程数量
total processes #总进程数量
max active processes #最大的活跃进程数量(FPM启动开始计算)
max children reached #程最大数量限制的次数,如果这个数量不为0,那说明你的最大进程数量过小,可以适当调整。
2.5.编写php-fpm
的Shell
脚本(如果端口不一致,只需要修改脚本端口即可)
[root@Agent ~]# cd /etc/zabbix/scripts
[root@Agent scripts]# vim phpfpm_status.sh
#!/bin/bash
############################################################
# $Name: phpfpm_status.sh
# $Version: v1.0
# $Function: Nginx Status
# $Author: xuliangwei
# $organization: www.xuliangwei.com
# $Create Date: 2016-06-23
# $Description: Monitor Nginx Service Status
############################################################
PHPFPM_COMMAND=$1
PHPFPM_PORT=80 #根据监听不同端口进行调整
start_since(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^start since:/ {print $NF}'
}
accepted_conn(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^accepted conn:/ {print $NF}'
}
listen_queue(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue:/ {print $NF}'
}
max_listen_queue(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max listen queue:/ {print $NF}'
}
listen_queue_len(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^listen queue len:/ {print $NF}'
}
idle_processes(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^idle processes:/ {print $NF}'
}
active_processes(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^active processes:/ {print $NF}'
}
total_processes(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^total processes:/ {print $NF}'
}
max_active_processes(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max active processes:/ {print $NF}'
}
max_children_reached(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^max children reached:/ {print $NF}'
}
slow_requests(){
/usr/bin/curl -s "http://127.0.0.1:"$PHPFPM_PORT"/phpfpm_status" |awk '/^slow requests:/ {print $NF}'
}
case $PHPFPM_COMMAND in
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 {start_since|accepted_conn|listen_queue|max_listen_queue|listen_queue_len|idle_processes|active_processes|total_processes|max_active_processes|max_children_reached}"
esac
2.6.给脚本添加执行权限
[root@Agent scripts]# chmod +x phpfpm_status.sh
2.7.监控项的phpfpm_status.conf
配置文件如下:
[root@Agent ~]# cat /etc/zabbix/zabbix_agentd.d/phpfpm_status.conf
UserParameter=phpfpm_status[*],/bin/bash /etc/zabbix/phpfpm_status.sh "$1"
2.8.重启zabbix-agent
[root@Agent ~]# systemctl restart zabbix-agent
2.9.Server
使用zabbix_get
命令来获取Agent
端的值
[root@ZabbixServer]# zabbix_get -s 192.168.90.11 -k phpfpm_status[accepted_conn]
45
2.10.添加所有监控项, 如下图4-5, 最后记得关联至对应主机
2.11.查看图形,如图4-4(图形自定义)
三、Zabbix监控MySQL
percona Monitoring Plugins
是一个高质量的组件,为MySQL
数据库添加企业级的监控和图表功能。但其脚本使用PHP
实现,故而Zabbix-Agent
需要安装PHP
环境。
实践环境
服务器系统 | 角色 | IP |
---|---|---|
CentOS 7.4 x86_64 | Zabbix-Server | 192.168.90.10 |
CentOS 7.4 x86_64 | Zabbix-Agent | 192.168.90.11 |
3.1.在Zabbix-Agent
端安装percona Monitoring Plugins
[root@Agent ~]# yum install -y http://www.percona.com/downloads/percona-release/redhat/0.1-3/percona-release-0.1-3.noarch.rpm
[root@Agent ~]# yum install percona-zabbix-templates -y
3.2.查看percona
安装后的目录结构
[root@Agent percona]# tree /var/lib/zabbix/percona
/var/lib/zabbix/percona
├── scripts #脚本文件路径
│ ├── get_mysql_stats_wrapper.sh
│ └── ss_get_mysql_stats.php
└── templates
├── userparameter_percona_mysql.conf #key文件位置
└── zabbix_agent_template_percona_mysql_server_ht_2.0.9-sver1.1.6.xml #模板文件位置
3.4.将自定义监控项配置文件复制至/etc/zabbix_agentd.conf.d
目录下
[root@Agent ~]# cp /var/lib/zabbix/percona/templates/userparameter_percona_mysql.conf /etc/zabbix/zabbix_agentd.d/percona_mysql.conf
3.5.重启zabbix-agent
[root@Agent ~]# systemctl restart zabbix-agent
3.6.修改脚本中的MySQL
用户名和密码
[root@Agent scripts]# vim /var/lib/zabbix/percona/scripts/ss_get_mysql_stats.php
$mysql_user = 'root';
$mysql_pass = 'xuliangwei.com';
$mysql_port = 3306;
3.7.在Zabbix-Server
端上使用Zabbix_get
获取值(否则会失败)
[root@Server ~]# zabbix_get -s 192.168.90.11 -k MySQL.pool-read-requests
223003813
//如果获取不到值常见问题
1.看是否是MySQL密码错误
2.不要直接执行脚本来获取
3.删除/tmp/localhost-mysql_cacti_stats.txt文件
4.权限问题导致
3.8.在Zabbix页面模板选项中导入Percona模板, 模板存放在/var/lib/zabbix/percona/templates
, 最后关联主机即可。
四、Zabbix监控Redis
Redis
使用自带的INFO
命令,进行状态监控。以一种易于解释且易于阅读的格式,返回关于Redis
服务器的各种信息和统计数值。
实践环境
服务器系统 | 角色 | IP |
---|---|---|
CentOS 7.4 x86_64 | Zabbix-Server | 192.168.90.10 |
CentOS 7.4 x86_64 | Zabbix-Agent | 192.168.90.11 |
4.1.编写Shell脚本
- 脚本端口、连接redis服务地址根据具体情况进行修改
- AUTH认证没有开启,将PASSWD修改为空即可。
[root@Agent ~]# mkdir -p /etc/zabbix/scripts
[root@Agent ~]# vim /etc/zabbix/scripts/redis_status.sh
#!/bin/bash
############################################################
# $Name: redis_status.sh
# $Version: v1.0
# $Function: Redis Status
# $Author: xuliangwei
# $organization: www.xuliangwei.com
# $Create Date: 2016-06-23
# $Description: Monitor Redis Service Status
############################################################
R_COMMAND="$1"
R_PORT="6379" #根据实际情况调整端口
R_SERVER="127.0.0.1" #根据具体情况调整IP地址
PASSWD="" #如果没有设置Redis密码,为空即可
redis_status(){
(echo -en "AUTH $PASSWD
INFO
";sleep 1;) | /usr/bin/nc "$R_SERVER" "$R_PORT" > /tmp/redis_"$R_PORT".tmp
REDIS_STAT_VALUE=$(grep "$R_COMMAND:" /tmp/redis_"$R_PORT".tmp | cut -d ':' -f2)
echo "$REDIS_STAT_VALUE"
}
case $R_COMMAND in
used_cpu_user_children)
redis_status "$R_PORT" "$R_COMMAND"
;;
used_cpu_sys)
redis_status "$R_PORT" "$R_COMMAND"
;;
total_commands_processed)
redis_status "$R_PORT" "$R_COMMAND"
;;
role)
redis_status "$R_PORT" "$R_COMMAND"
;;
lru_clock)
redis_status "$R_PORT" "$R_COMMAND"
;;
latest_fork_usec)
redis_status "$R_PORT" "$R_COMMAND"
;;
keyspace_misses)
redis_status "$R_PORT" "$R_COMMAND"
;;
keyspace_hits)
redis_status "$R_PORT" "$R_COMMAND"
;;
keys)
redis_status "$R_PORT" "$R_COMMAND"
;;
expires)
redis_status "$R_PORT" "$R_COMMAND"
;;
expired_keys)
redis_status "$R_PORT" "$R_COMMAND"
;;
evicted_keys)
redis_status "$R_PORT" "$R_COMMAND"
;;
connected_clients)
redis_status "$R_PORT" "$R_COMMAND"
;;
changes_since_last_save)
redis_status "$R_PORT" "$R_COMMAND"
;;
blocked_clients)
redis_status "$R_PORT"