zabbix自定义监控
zabbix的监控流程
服务端:配置模板 ---> 创建监控项 --->创建监控项,监控 ---->主机添加模板
客户端:配置key
用户数量的监控
客户端配置
# 1.了解监控需求,获取监控数据
## 零宽断言
[root@web02 ~]# ifconfig eth0|grep -Po '[1-9][0-9]*\.\d+\.\d+\.\d+(?= netmask)'
[root@web02 ~]# uptime|grep -Po '\d+(?= user)'
# 2.修改zabbix-agent的配置文件(使用zabbix-agent配置文件中的 UserParameter= 定义监控项)
语法:
Format: UserParameter=<key>,<shell command>
Format: UserParameter=监控项名字,shell命令
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.conf
UserParameter=user_count,uptime|grep -Po '\d+(?= user)'
# 3.重启zabbix-agent
[root@web01 ~]# systemctl restart zabbix-agent
# 4.在zabbix-server上(zabbix-get)测试,是否可以接收到zabbix-agent获取的数据
[root@zabbix5-server ~]# rpm -ivh
https://mirrors.tuna.tsinghua.edu.cn/zabbix/zabbix/5.0/rhel/7/x86_64/zabbix-get-5.0.42-1.el7.x86_64.rpm
[root@zabbix5-server ~]# zabbix_get -s 172.16.1.7 -k user_count
3
[root@zabbix5-server ~]# zabbix_get -s 172.16.1.7 -k user_count
5
# 5.优化监控项的保存配置
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/user.conf
[root@web01 ~]# cat /etc/zabbix/zabbix_agentd.d/user.conf
UserParameter=user_count,uptime|grep -Po '\d+(?= user)'
服务端配置
创建一个用户检测模板


在模板中添加监控项



为主机添加模板



为监控项添加图形




为模板中的监控添加触发器




参数化定义监控项
### 传入参数 调取参数
vim /etc/zabbix/zabbix_agentd.d/port.conf
UserParameter=tcp.port[*],ss -lnt|grep -wc "$1"
[root@web01 ~]# systemctl restart zabbix-agent


监控TCP的十一种状态 ---(传递参数)
LISTEN - 侦听来自远方TCP端口的连接请求;
SYN-SENT -在发送连接请求后等待匹配的连接请求;
SYN-RECEIVED - 在收到和发送一个连接请求后等待对连接请求的确认;
ESTABLISHED- 代表一个打开的连接,数据可以传送给用户;
FIN-WAIT-1 - 等待远程TCP的连接中断请求,或先前的连接中断请求的确认;
FIN-WAIT-2 - 从远程TCP等待连接中断请求;
CLOSE-WAIT - 等待从本地用户发来的连接中断请求;
CLOSING -等待远程TCP对连接中断的确认;
LAST-ACK - 等待原来发向远程TCP的连接中断请求的确认;
TIME-WAIT -等待足够的时间以确保远程TCP接收到连接中断请求的确认;
CLOSED - 没有任何连接状态;
客户端独有的:(1)SYN_SENT (2)FIN_WAIT1 (3)FIN_WAIT2 (4)CLOSING (5)TIME_WAIT
。
服务端独有的:(1)LISTEN (2)SYN_RCVD (3)CLOSE_WAIT (4)LAST_ACK 。
共有的:(1)CLOSED (2)ESTABLISHED 。
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/tcp_status.conf
UserParameter=tcp.status[*],ss -lnt|grep -ic "$1"

监控nginx - php 状态页监控



#### 状态页面对应解释 #####
# nginx
Active connections – 活跃的连接数量
server accepts handled requests — 总共处理了7个连接 , 成功创建7次握手, 总共处理了36个请求。
reading — 读取客户端的连接数。
writing — 响应数据到客户端的数量。
waiting — 开启 keep-alive 的情况下,这个值等于 active – (reading+writing), 意思就是 Nginx 已经处理完正在等候下一次请求指令的驻留连接。
#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,那说明你的最大进程数量过小,可以适当调整。
vim /etc/nginx/conf.d/webapp.conf
server{
listen 80;
server_name _;
location / {
root /code/webapp;
index index.html;
}
location /status {
stub_status on;
}
location ~ ^/(php_status)$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
}
http://10.0.0.7/status
##### nginx 监控脚本呢 #####
[root@web01 ~]# vim /etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh
#!/bin/bash
NGINX_PORT=80 #如果端口不同仅需要修改脚本即可,否则修改xml很麻烦
NGINX_URL=http://172.16.1.7/status
nginx_active(){
/usr/bin/curl -s "${NGINX_URL}" |awk '/Active/ {print $NF}'
}
nginx_reading(){
/usr/bin/curl -s "${NGINX_URL}" |awk '/Reading/ {print $2}'
}
nginx_writing(){
/usr/bin/curl -s "${NGINX_URL}" |awk '/Writing/ {print $4}'
}
nginx_waiting(){
/usr/bin/curl -s "${NGINX_URL}" |awk '/Waiting/ {print $6}'
}
nginx_accepts(){
/usr/bin/curl -s "${NGINX_URL}" |awk 'NR==3 {print $1}'
}
nginx_handled(){
/usr/bin/curl -s "${NGINX_URL}" |awk 'NR==3 {print $2}'
}
nginx_requests(){
/usr/bin/curl -s "${NGINX_URL}" |awk 'NR==3 {print $3}'
}
case $1 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
# 授权
[root@web01 ~]# chmod +x /etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh
####### php 监控脚本
[root@web01 scripts]# cat php_status.sh
#!/bin/bash
PHPFPM_COMMAND=$1
PHPFPM_URL="http://172.16.1.7/php_status"
start_since(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^start since:/ {print $NF}'
}
accepted_conn(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^accepted conn:/ {print $NF}'
}
listen_queue(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^listen queue:/ {print $NF}'
}
max_listen_queue(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max listen queue:/ {print $NF}'
}
listen_queue_len(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^listen queue len:/ {print $NF}'
}
idle_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^idle processes:/ {print $NF}'
}
active_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^active processes:/ {print $NF}'
}
total_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^total processes:/ {print $NF}'
}
max_active_processes(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max active processes:/ {print $NF}'
}
max_children_reached(){
/usr/bin/curl -s "${PHPFPM_URL}" |awk '/^max children reached:/ {print $NF}'
}
slow_requests(){
/usr/bin/curl -s "${PHPFPM_URL}" |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
# 修改客户端配置文件
[root@web01 zabbix_agentd.d]# vim php_status.conf
UserParameter=php.status[*],/etc/zabbix/zabbix_agentd.d/scripts/php_status.sh $1
#客户端--修改zabbix-agent配置文件
UserParameter=nginx.status[*],/etc/zabbix/zabbix_agentd.d/scripts/nginx_status.sh $1
[root@web01 ~]# systemctl restart zabbix-agent


