简介
在linux的监控中, 在TCP方面,有一个很重要的文件,叫/proc/net/sockstat,里面的内容如下:
sockets: used 150
TCP: inuse 10 orphan 0 tw 48175 alloc 12 mem 2
UDP: inuse 8 mem 2
UDPLITE: inuse 0
RAW: inuse 0
FRAG: inuse 0 memory 0
一般来说,主要关心TCP的相关数量,于是想办法把图形画出来.
框架
10.180.92.195里面的操作
data input methods
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a tcpInuse
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a socketUsed
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a tcpOrphan
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a tcpTw
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a tcpAlloc
<path_cacti>/scripts/check_nrpe -H <server_addr> -c cacti_sockstat -a tcpMem
data template
graph template
cli deploy command
#!/bin/bash
ip=$1
# 判断是否存在,如果存在就退出
hostfind=`php add_graphs.php --list-hosts | grep $ip$ | wc -l`
[ $hostfind -eq 1 ] && ( echo "host $ip 已经存在 ";exit 0 )
# 添加hosts
php add_device.php --description=$ip --ip=$ip --template=9
hostid=`php add_graphs.php --list-hosts | grep $ip$ | awk '{print $1}'`
echo $hostid
#exit 0
#[[ $hostid -gt 70 ]] || exit 0
# 开始画vmstat
php add_graphs.php --graph-type=cg --graph-template-id=76 --host-id=$hostid --input-fields="server_addr=$ip"
php add_graphs.php --graph-type=cg --graph-template-id=80 --host-id=$hostid --input-fields="server_addr=$ip"
php add_graphs.php --graph-type=cg --graph-template-id=83 --host-id=$hostid --input-fields="server_addr=$ip"
php add_graphs.php --graph-type=cg --graph-template-id=84 --host-id=$hostid --input-fields="server_addr=$ip"
php add_graphs.php --graph-type=cg --graph-template-id=81 --host-id=$hostid --input-fields="server_addr=$ip"
php add_graphs.php --graph-type=cg --graph-template-id=82 --host-id=$hostid --input-fields="server_addr=$ip"
10.125.89.135里面的操作
define a command cacti_sockstat
command[cacti_sockstat]=/usr/local/nagios/libexec/check_sockstat $ARG1$
/usr/local/nagios/libexec/check_sockstat
#!/bin/bash
#
# socketsUsed
# tcpInuse
# tcpOrphan
# tcpTw
# tcpAlloc
# tcpMem
#
# sockets: used 6121
# TCP: inuse 6142 orphan 0 tw 63905 alloc 6145 mem 4206
# UDP: inuse 6 mem 3
# UDPLITE: inuse 0
# RAW: inuse 0
# FRAG: inuse 0 memory 0
# [root@10-125-89-135 ~]#
#
#
#echo "OK"
#echo $1
if [ $1 = "socketsUsed" ];then
name="socketsUsed"
result=`cat /proc/net/sockstat | grep sockets | awk '{print $3}'`
fi
if [ $1 = "tcpInuse" ];then
name="tcpInuse"
result=`cat /proc/net/sockstat | grep TCP | awk '{print $3}'`
fi
if [ $1 = "tcpOrphan" ];then
name="tcpOrphan"
result=`cat /proc/net/sockstat | grep TCP | awk '{print $5}'`
fi
if [ $1 = "tcpTw" ];then
name="tcpTw"
result=`cat /proc/net/sockstat | grep TCP | awk '{print $7}'`
fi
if [ $1 = "tcpAlloc" ];then
name="tcpAlloc"
result=`cat /proc/net/sockstat | grep TCP | awk '{print $9}'`
fi
if [ $1 = "tcpMem" ];then
name="tcpMem"
result=`cat /proc/net/sockstat | grep TCP | awk '{print $11}'`
fi
echo "$name:$result"