一、下载客户端按照包,这里用rpm包安装
1
|
[root@localhost ~] # rpm -ivh http://repo.zabbix.com/zabbix/3.0/rhel/7/x86_64/zabbix-agent-3.0.4-1.el7.x86_64.rpm
|
修改客户端配置文件
1
2
|
Server=127.0.0.1 ServerActive=127.0.0.1 |
改为zabbix server实际的IP地址(比如zabbix server的IP地址为10.0.0.100)
1
2
|
Server=10.0.0.100 ServerActive=10.0.0.100 |
重启zabbix 客户端
1
|
[root@zabbix] # systemctl restart zabbix-agent
|
二、自定义脚本监控nginx连接状态:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
#!/bin/bash HOST=localhost PORT= "9999"
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}'
}
$1 |
将脚本存放与脚本目录,注意该目录根据自己情况定义,我这里用rpm 按照zabbix-agent客户端,所以直接放在/etc/zabbix/script目录下
1
2
3
|
[root@localhost script] # ll nginx-status.sh
-rwx------ 1 root root 947 Mar 22 10:49 nginx-status.sh [root@localhost script] #
|
不要忘了给脚本执行权限
1
2
3
4
5
|
[root@localhost script] # chmod +x nginx-status.sh
[root@localhost script] #
[root@localhost script] # ll nginx-status.sh
-rwxr-xr-x 1 root root 947 Mar 22 10:49 nginx-status.sh [root@localhost script] #
|
修改配置文件,在末尾添加如下内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[root@localhost zabbix] # vim zabbix_agentd.conf
...................................省略部分...................................................... ### DISK I/O UserParameter=custom.vfs.dev. read .ops[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$4}' UserParameter=custom.vfs.dev. read .ms[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$7}' UserParameter=custom.vfs.dev.write.ops[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$8}' UserParameter=custom.vfs.dev.write.ms[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$11}' UserParameter=custom.vfs.dev.io.active[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$12}' UserParameter=custom.vfs.dev.io.ms[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$13}' UserParameter=custom.vfs.dev. read .sectors[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}' UserParameter=custom.vfs.dev.write.sectors[*], cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
######## TCP connections total UnsafeUserParameters=1 UserParameter=synrecv, /etc/zabbix/script/tcp_connection_status .sh SYNRECV
UserParameter=estab, /etc/zabbix/script/tcp_connection_status .sh ESTAB
UserParameter=timewait, /etc/zabbix/script/tcp_connection_status .sh TIMEWAIT
UserParameter=last_ack, /etc/zabbix/script/tcp_connection_status .sh LAST_ACK
UserParameter=total, /etc/zabbix/script/tcp_connection_status .sh TOTAL
#####Nginx Status UserParameter=nginx.accepts, /etc/zabbix/script/nginx-status .sh accepts
UserParameter=nginx.handled, /etc/zabbix/script/nginx-status .sh handled
UserParameter=nginx.requests, /etc/zabbix/script/nginx-status .sh requests
UserParameter=nginx.connections.active, /etc/zabbix/script/nginx-status .sh active
UserParameter=nginx.connections.reading, /etc/zabbix/script/nginx-status .sh reading
UserParameter=nginx.connections.writing, /etc/zabbix/script/nginx-status .sh writing
UserParameter=nginx.connections.waiting, /etc/zabbix/script/nginx-status .sh waiting
EnableRemoteCommands=1 |
然后重启zabbix客户端
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@zabbix] # systemctl restart zabbix-agent
[root@localhost zabbix] # systemctl status zabbix-agent
● zabbix-agent.service - Zabbix Agent Loaded: loaded ( /usr/lib/systemd/system/zabbix-agent .service; disabled; vendor preset: disabled)
Active: active (running) since Wed 2017-03-22 11:03:05 CST; 6min ago
Process: 13613 ExecStop= /bin/kill -SIGTERM $MAINPID (code=exited, status=0 /SUCCESS )
Process: 13615 ExecStart= /usr/sbin/zabbix_agentd -c $CONFFILE (code=exited, status=0 /SUCCESS )
Main PID: 13617 (zabbix_agentd)
CGroup: /system .slice /zabbix-agent .service
├─13617 /usr/sbin/zabbix_agentd -c /etc/zabbix/zabbix_agentd .conf
├─13618 /usr/sbin/zabbix_agentd : collector [idle 1 sec]
├─13619 /usr/sbin/zabbix_agentd : listener #1 [waiting for connection]
├─13620 /usr/sbin/zabbix_agentd : listener #2 [waiting for connection]
└─13621 /usr/sbin/zabbix_agentd : listener #3 [waiting for connection]
Mar 22 11:03:05 localhost systemd[1]: Starting Zabbix Agent... Mar 22 11:03:05 localhost systemd[1]: PID file /run/zabbix/zabbix_agentd .pid not readable (yet?) after start.
Mar 22 11:03:05 localhost systemd[1]: Started Zabbix Agent. [root@localhost zabbix] #
|
最后在服务端添加主机对应的监控项,键值为如下字段:
1
2
3
4
5
6
7
|
nginx.accepts nginx.handled nginx.requests nginx.connections.active, nginx.connections.reading nginx.connections.writing nginx.connections.waiting |
网上看到另外一篇关于tcp连接数的监控转载下来:
http://www.codes51.com/article/detail_341554.html
Tcp的连接状态对于我们web服务器来说是至关重要的,尤其是并发量ESTAB;或者是syn_recv值,假如这个值比较大的话我们可以认为是不是受到了攻击,或是是time_wait值比较高的话,我们要考虑看我们内核是否需要调优,太高的time_wait值的话会占用太多端口,要是端口少的话后果不堪设想:
监控脚本如下:
#!/bin/bash
#Linux sir
#scripts for tcp status
function SYNRECV {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'SYN-RECV' | awk '{print $2}'
}
function ESTAB {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'ESTAB' | awk '{print $2}'
}
function FINWAIT1 {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-1' | awk '{print $2}'
}
function FINWAIT2 {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'FIN-WAIT-2' | awk '{print $2}'
}
function TIMEWAIT {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'TIME-WAIT' | awk '{print $2}'
}
function LASTACK {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LAST-ACK' | awk '{print $2}'
}
function LISTEN {
/usr/sbin/ss -ant | awk '{++s[$1]} END {for(k in s) print k,s[k]}' | grep 'LISTEN' | awk '{print $2}'
}
$1
二、填写key值:当然大家在加入key值之后最好再服务器上面去执行看有没有返回值:
[root@WEB_007 scripts]# cat /usr/local/zabbix/etc/zabbix_agentd.conf.d/tcp
#monitor tcp
UserParameter=tcp[*],/usr/local/zabbix/scripts/tcp_status $1
三、导入模板(在我的附件上,然后触发器根据自己的情况调整:)
四、查看效果:
到此结束:
在写这篇的时候刚好也写完了监控lvs的活动连接。下面也给出模板、大家自己下载,我这里给出脚本和key:
一、脚本:
#!/bin/bash
#xiaoluo
#status about lvs forward
function first {
/sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.234:http' | sed '2d' | awk '{print $5}'
}
function second {
/sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.235:http' | sed '2d' | awk '{print $5}'
}
function third {
/sbin/ipvsadm -L 2>/dev/null | grep '192.168.10.236:http' | sed '2d' | awk '{print $5}'
}
$1
二、key:
#monitor lvs
UserParameter=lvs[*],/usr/local/zabbix/scripts/ipvsadm_status $1
效果展示:
模板在下面自己下载:
------------------------------------------分割线------------------------------------------
免费下载地址在 http://linux.linuxidc.com/
用户名与密码都是www.linuxidc.com
具体下载目录在 /2015年资料/5月/14日/Zabbix 监控tcp连接的状态/
下载方法见 http://www.linuxidc.com/Linux/2013-07/87684.htm
以上就介绍了Zabbix监控TCP连接数,包括了方面的内容,希望对系统运维有兴趣的朋友有所帮助。
本文转自027ryan 51CTO博客,原文链接:http://blog.51cto.com/ucode/1909146,如需转载请自行联系原作者