Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1734187
  • 博文数量: 150
  • 博客积分: 660
  • 博客等级: 上士
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 11:39
文章分类

全部博文(150)

文章存档

2019年(4)

2018年(36)

2017年(53)

2016年(7)

2015年(3)

2014年(3)

2013年(27)

2012年(2)

2011年(1)

2006年(1)

2005年(13)

分类: 系统运维

2017-04-07 15:16:59

关于系统网络连接数的监测

以运行nginx的主机为例说明(2012年的老文,稍微修改)

1、查看系统所有打开的文件句柄

lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|awk '{c=$1+c}END{print c}'

2、查看每个进程打开的文件句柄

lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|more

从大到小排序

lsof -n |awk '{ command[$1]=$1 ;cnt[$1]=cnt[$1]+1} END{for (i in command) print command[i],cnt[i]}' |sort -k 2 -nr

3、连接总数的监控脚本

测试计算总数

[root@b11 ~]# lsof -n|awk '{print $2}'|sort|uniq -c|sort -nr|awk '{c=$1+c}END{print c}'
5734

脚本内容

向[作者]致敬

[root@b11 ~]# more /home/zabbix/nginx-connection.sh 
#!/bin/bash
# Script to fetch nginx statuses for tribily monitoring systems
# Author: krish@toonheart.com
# License: GPLv2

# Set Variables
HOST=`ip addr | grep bond0|sed -n '/inet /p'|grep /32 |awk '{print $2}' |awk -F/ '{print $1}'`
PORT=80
ip_port=`echo $HOST:$PORT`
#netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'$ip_port'"))  c=c+1; a=a+1}} END{print a,c,a-c}'
# Functions to return nginx stats

function all {
        netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'$ip_port'"))  c=c+1; a=a+1}} END{print a}'
        }      

function client {
        netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'$ip_port'"))  c=c+1; a=a+1}} END{print c}'
        }      

function server {
        netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'$ip_port'"))  c=c+1; a=a+1}} END{print a-c}'
        }      

function acs {
        netstat -ant | sed -n '1,2!p'|awk '{ if($6!="LISTEN") {if(match($4,"'$ip_port'"))  c=c+1; a=a+1}} END{print a,c,a-c}'
        }

# Run the requested function
$1

4、增加zabbix的监控项目显示nginx的连接总数

4.1zabbix ui上增加一个模板

模板增加三个项目分别为

nginx.all.connections
nginx.client.connections
nginx.server.connections

增加一个graph,添加上述三个项目的显示

4.2 为需要的host增加上述模板

4.3 添加一个screen,选择上述增加的graphic 4.4 添加自定义脚本到zabbix_agentd.conf 

UserParameter=nginx.all.connections,/home/zabbix/nginx-connection.sh all
UserParameter=nginx.client.connections,/home/zabbix/nginx-connection.sh client
UserParameter=nginx.server.connections,/home/zabbix/nginx-connection.sh server

4.5 把3的脚本放到/home/zabbix/下面

4.6 重启zabbix_agentd



阅读(3567) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~