Skip to content

Commit 64398c1

Browse files
committedDec 1, 2016
update
1 parent 1474ec6 commit 64398c1

11 files changed

+2586
-0
lines changed
 

‎nagios/nagios_client/install_nrpe.sh

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/bin/bash
2+
yum install -y make gcc bc openssl-devel net-snmp net-snmp-devel net-snmp-utils
3+
4+
/usr/sbin/useradd -s /sbin/nologin nagios
5+
6+
tar zxvf nagios-plugins-2.1.1.tar.gz
7+
cd nagios-plugins-2.1.1
8+
./configure && make && make install
9+
cd ..
10+
chown nagios.nagios /usr/local/nagios
11+
chown -R nagios.nagios /usr/local/nagios/libexec
12+
13+
tar zxvf nrpe-2.15.tar.gz
14+
cd nrpe-2.15
15+
./configure && make all && make install-plugin && make install-daemon && make install-daemon-config
16+
cd ..
17+
18+
Network_Card1=`/sbin/ip a|grep -B1 221.111 |grep inet | awk -F ' ' '{print $7}'`
19+
Network_Card2=`/sbin/ip a|grep -B1 192.168 |grep inet | awk -F ' ' '{print $7}'`
20+
21+
22+
cat >/usr/local/nagios/etc/nrpe.cfg<<EOF
23+
log_facility=daemon
24+
pid_file=/var/run/nrpe.pid
25+
server_port=5666
26+
nrpe_user=nagios
27+
nrpe_group=nagios
28+
allowed_hosts=127.0.0.1,192.168.1.12
29+
dont_blame_nrpe=0
30+
allow_bash_command_substitution=0
31+
debug=0
32+
command_timeout=60
33+
connection_timeout=300
34+
command[check_users]=/usr/local/nagios/libexec/check_users -w 2 -c 6
35+
command[check_load]=/usr/local/nagios/libexec/check_load -w 19,15,12 -c 30,25,20
36+
command[check_/]=/usr/local/nagios/libexec/check_disk -w 12% -c 10% -p /
37+
command[check_/data]=/usr/local/nagios/libexec/check_disk -w 20% -c 10% -p /data
38+
command[check_/opt]=/usr/local/nagios/libexec/check_disk -w 12% -c 10% -p /opt
39+
command[check_ss]=/usr/local/nagios/libexec/check_ss.sh 1000 3000
40+
command[check_$Network_Card1]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C nagios_test@1128 -H 127.0.0.1 -N $Network_Card1 -w 5000,5000 -c 10000,10000 -K -B
41+
command[check_$Network_Card2]=/usr/local/nagios/libexec/check_traffic.sh -V 2c -C nagios_test@1128 -H 127.0.0.1 -N $Network_Card2 -w 15000,15000 -c 30000,30000 -K -B
42+
command[check_disk]=/usr/local/nagios/libexec/check_disk.sh 90 90 95
43+
command[check_secure]=/usr/local/nagios/libexec/check_secure.sh 15
44+
EOF
45+
46+
47+
###系统判断
48+
if [ -f /etc/redhat-release ];then
49+
OS=CentOS
50+
check_OS1=`cat /etc/redhat-release | awk -F '[ ]+' '{print $3}' | awk -F '.' '{print $1}'`
51+
check_OS2=`cat /etc/redhat-release | awk -F '[ ]+' '{print $4}' | awk -F '.' '{print $1}'`
52+
if [ "$check_OS1" == "6" ];then
53+
OS=CentOS6
54+
fi
55+
if [ "$check_OS2" == "7" ];then
56+
OS=CentOS7
57+
fi
58+
elif [ ! -z "`cat /etc/issue | grep bian`" ];then
59+
OS=Debian
60+
elif [ ! -z "`cat /etc/issue | grep Ubuntu`" ];then
61+
OS=Ubuntu
62+
else
63+
echo -e "\033[31mDoes not support this OS, Please contact the author! \033[0m"
64+
fi
65+
66+
if [ $OS == 'CentOS6' ];then
67+
cat >/etc/snmp/snmpd.conf<<EOF
68+
com2sec notConfigUser 127.0.0.1 nagios_test@1128
69+
com2sec notConfigUser 192.168.1.12 nagios_test@1128
70+
group notConfigGroup v1 notConfigUser
71+
group notConfigGroup v2c notConfigUser
72+
view systemview included .1.3.6.1.2.1.1
73+
view systemview included .1.3.6.1.2.1.2
74+
view systemview included .1.3.6.1.2.1.25.1.1
75+
view all included .1 80
76+
access notConfigGroup "" any noauth exact all none none
77+
dontLogTCPWrappersConnects yes
78+
EOF
79+
80+
81+
fi
82+
83+
84+
if [ $OS == 'CentOS7' ];then
85+
cat >/etc/snmp/snmpd.conf<<EOF
86+
com2sec notConfigUser 127.0.0.1 nagios_test@1128
87+
com2sec notConfigUser 192.168.1.12 nagios_test@1128
88+
group notConfigGroup v1 notConfigUser
89+
group notConfigGroup v2c notConfigUser
90+
view systemview included .1.3.6.1.2.1.1
91+
view systemview included .1.3.6.1.2.1.25.1.1
92+
access notConfigGroup "" any noauth exact all none none
93+
view all included .1 80
94+
syslocation Unknown (edit /etc/snmp/snmpd.conf)
95+
syscontact Root <root@localhost> (configure /etc/snmp/snmp.local.conf)
96+
dontLogTCPWrappersConnects yes
97+
smuxpeer .1.3.6.1.4.1.674.10892.1
98+
EOF
99+
chmod 755 /bin/which
100+
101+
102+
fi
103+
104+
#############snmp#####################
105+
##cp -rf snmpd.conf /etc/snmp/
106+
service snmpd restart
107+
chkconfig snmpd on
108+
##systemctl enable snmpd.service
109+
cp -rf sh/*.sh /usr/local/nagios/libexec/
110+
chown nagios.nagios /usr/local/nagios/libexec/*.sh
111+
chmod -R 777 /usr/local/nagios/libexec/*.sh
112+
########################nagios############################
113+
ps -ef | grep 'nagios' | grep 'nrpe' | grep -v 'grep' | awk '{print $2}' | xargs kill
114+
/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
115+
echo "/usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d" >> /etc/rc.local
116+
117+
mkdir -p /opt/sh/
118+
cat >/opt/sh/ssh_secure.sh<<EOF
119+
echo "Nov 10 18:07:37 test sshd[11079]: Accepted password for root from 192.168.1.1 port 59557 ssh2" > /tmp/ssh.log
120+
tail -n 99 /var/log/secure | grep sshd | grep ssh2 >> /tmp/ssh.log
121+
echo "\$(date)"
122+
EOF
123+
124+
cat >> /var/spool/cron/root << EOF
125+
*/3 * * * * /opt/sh/ssh_secure.sh
126+
EOF
127+
chmod 777 /opt/sh/ssh_secure.sh
128+
chmod 600 /var/spool/cron/root
129+
/sbin/service crond restart
130+
131+
132+

‎nagios/nagios_client/sh/check_disk.sh

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
percent=`df -k | grep -v Filesystem| awk '{print int($5)}' | sort -nr | head -1`
3+
4+
for NUM in $percent
5+
do
6+
7+
if [ $NUM -gt $1 ]
8+
then
9+
echo "Critical -Disk usage $NUM | Current=$NUM;Warning=$2;Critical=$3"
10+
exit 2
11+
fi
12+
if [ $NUM -lt $1 ]
13+
then
14+
echo "OK -Disk usage $NUM | Current=$NUM;Warning=$2;Critical=$3"
15+
exit 0
16+
fi
17+
if [ $NUM -eq $1 ]
18+
then
19+
echo "Warning -Disk usage $NUM | Current=$NUM;Warning=$2;Critical=$3"
20+
exit 1
21+
fi
22+
23+
done
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/bin/bash
2+
3+
#set nagios status
4+
STATE_OK=0
5+
STATE_WARNING=1
6+
STATE_CRITICAL=2
7+
STATE_UNKNOWN=3
8+
9+
usage (){
10+
echo -en "Usage: $0 -d [ eth|bond ]\nFor example:\t$0 -d bond0 -w 100[B|K|M|G] -c 200[B|K|M|G]\n" 1>&2
11+
exit ${STATE_WARNING}
12+
}
13+
14+
check_input () {
15+
local str="$1"
16+
echo "${str}"|grep -E '[0-9]+[b|B|k|K|m|M|g|G]$' >/dev/null 2>&1 ||\
17+
eval "echo ${str} is wrong!;usage"
18+
}
19+
20+
replace_str () {
21+
local str="$1"
22+
output=`echo "${str}"|sed -r 's/[k|K]/*1024/;s/[m|M]/*1024*1024/;s/[g|G]/*1024*1024*1024/'`
23+
echo ${output}
24+
}
25+
26+
while getopts w:c:d: opt
27+
do
28+
case "$opt" in
29+
w)
30+
check_input "$OPTARG"
31+
warning_str=`replace_str "$OPTARG"`
32+
warning=`echo "${warning_str}"|bc`
33+
;;
34+
c)
35+
check_input "$OPTARG"
36+
critical_str=`replace_str "$OPTARG"`
37+
critical=`echo "${critical_str}"|bc`
38+
;;
39+
d)
40+
dev_id="$OPTARG"
41+
;;
42+
*)
43+
usage
44+
;;
45+
esac
46+
done
47+
48+
shift $[ $OPTIND - 1 ]
49+
50+
if [ -z "${dev_id}" -o -z "${warning}" -o -z "${critical}" ];then
51+
usage
52+
fi
53+
54+
source_file='/proc/net/dev'
55+
if [ ! -f "${source_file}" ];then
56+
echo "${source_file} not exsit!" 1>&2
57+
exit ${STATE_WARNING}
58+
fi
59+
60+
grep "${dev_id}" ${source_file} >/dev/null 2>&1 || dev_stat='not found'
61+
if [ "${dev_stat}" = 'not found' ];then
62+
echo "${dev_id} ${dev_stat}!" 1>&2
63+
usage
64+
fi
65+
66+
time_now=`date -d now +"%F %T"`
67+
nagios_path='/usr/local/nagios/libexec'
68+
test -d ${nagios_path} || mkdir -p ${nagios_path} && mark="${nagios_path}/net_traffic.${dev_id}"
69+
search_dev=`awk -F':' '/'${dev_id}'/{print $2}' /proc/net/dev|sed -r 's/^[ ]+//'`
70+
info=`echo "${search_dev}"|awk -v date="${time_now}" 'BEGIN{OFS=";"}{print "TIME=\""date"\"","RX="$1,"TX="$9,"DEV='${dev_id}'"}'`
71+
72+
#debug
73+
#eval "${info}"
74+
#echo $info
75+
#echo $TIME $RX $TX && exit
76+
77+
marking () {
78+
echo "$info" > ${mark} || exit ${STATE_WARNING}
79+
chown nagios.nagios ${mark}
80+
}
81+
82+
if [ ! -f "${mark}" ];then
83+
marking
84+
echo "This script is First run! ${info}"
85+
exit ${STATE_OK}
86+
else
87+
old_info=`cat ${mark}`
88+
eval "${old_info}"
89+
OLD_TIME="${TIME}";OLD_RX=${RX};OLD_TX=${TX}
90+
if [ -z "${OLD_RX}" -o -z "${OLD_TX}" ];then
91+
echo "Data Error: ${old_info}" 1>&2
92+
marking
93+
exit ${STATE_WARNING}
94+
fi
95+
fi
96+
97+
if [ -n "${info}" ];then
98+
eval ${info}
99+
sec_now=`date -d "${TIME}" +"%s"`
100+
sec_old=`date -d "${OLD_TIME}" +"%s"`
101+
sec=`echo "${sec_now}-${sec_old}"|bc|sed 's/-//'`
102+
rx=`echo "(${RX}-${OLD_RX})/${sec}"|bc|sed 's/-//'`
103+
tx=`echo "(${TX}-${OLD_TX})/${sec}"|bc|sed 's/-//'`
104+
marking
105+
#debug
106+
# echo $sec $rx $tx
107+
else
108+
echo "Can not read ${source_file}" 1>&2
109+
exit ${STATE_WARNING}
110+
fi
111+
112+
human_read () {
113+
local number="$1"
114+
if [ `echo "(${number}-1073741824) > 0"|bc` -eq 1 ];then
115+
output="`echo "scale=2;${number}/1024/1024/1024"|bc` GB/s"
116+
elif [ `echo "(${number}-1048576) > 0"|bc` -eq 1 ];then
117+
output="`echo "scale=2;${number}/1024/1024"|bc` MB/s"
118+
elif [ `echo "(${number}-1024) > 0"|bc` -eq 1 ];then
119+
output="`echo "scale=2;${number}/1024"|bc` KB/s"
120+
else
121+
output="${number} B/s"
122+
fi
123+
echo "${output}"
124+
}
125+
126+
rx_human_read=`human_read "${rx}"`
127+
tx_human_read=`human_read "${tx}"`
128+
129+
message () {
130+
local stat="$1"
131+
echo "${DEV} Traffic is ${stat} - In: ${rx_human_read} Out: ${tx_human_read} interval: ${sec}s |in=${rx};${warning};${critical};${min};${max} out=${tx};${warning};${critical};${min};${max}"
132+
}
133+
134+
#pnp4nagios setting
135+
min=0
136+
max=1073741824
137+
138+
total_int=`echo "${rx}+${tx}"|bc`
139+
140+
[ `echo "(${total_int}-${warning}) < 0"|bc` -eq 1 ] && message "OK" && exit ${STATE_OK}
141+
[ `echo "(${total_int}-${critical}) >= 0"|bc` -eq 1 ] && message "Critical" && exit ${STATE_CRITICAL}
142+
[ `echo "(${total_int}-${warning}) >= 0"|bc` -eq 1 ] && message "Warning" && exit ${STATE_WARNING}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
NUM=`curl http://192.168.10.9:8081/status | grep Active | awk '{print $3}'`
3+
4+
if [ $NUM -lt $1 ]
5+
then
6+
echo "OK -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
7+
8+
exit 0
9+
fi
10+
if [ $NUM -gt $1 -a $NUM -lt $2 ]
11+
then
12+
echo "Warning -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
13+
exit 1
14+
fi
15+
if [ $NUM -gt $2 ]
16+
then
17+
echo "Critical -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
18+
exit 2
19+
fi
+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env python
2+
3+
import commands
4+
5+
class redis_monitor:
6+
redis_bin='/opt/redis/bin/redis-cli'
7+
8+
def __init__(self,password,port,item):
9+
self.Password = password
10+
self.Port = int(port)
11+
self.items = item
12+
13+
def Monitor_items(self):
14+
try:
15+
code,res = commands.getstatusoutput("%s -a %s -p %d info | grep %s | awk -F: '{print$2}'" %(redis_monitor.redis_bin,self.Password,self.Port,self.items))
16+
except ValueError,e:
17+
print e
18+
print 'error'
19+
else:
20+
res_m = int(res)/1024/1024
21+
return res_m
22+
23+
if __name__ == '__main__':
24+
obj=redis_monitor('quB1BY3njv0e1212b7BFw92',6779,'used_memory_rss')
25+
Redis_use_Mem=obj.Monitor_items()
26+
print Redis_use_Mem
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
##检查redis使用内存是否占满
3+
NORMAL_MSG="redis_memory is OK!"
4+
WARONG_MSG="redis_memory is WARONG!!"
5+
CRITICAL_MSG="redis_memory is CRITICAL!!!"
6+
ERROR_MSG="max_redis_memory is 0"
7+
PORT=$1
8+
WARONG_NUM=$2
9+
CRITICAL_NUM=$3
10+
Redisclient=`find /opt/redis*|grep redis-cli$|head -1`
11+
12+
max_redis_memory=`echo 'config get maxmemory' |$Redisclient -h 127.0.0.1 -p $PORT | grep -v 'maxmemory'`
13+
used_redis_memory=`$Redisclient -h 127.0.0.1 -p $PORT info | grep 'used_memory' | egrep -v 'used_memory_' | awk -F ":" '{print $2}'`
14+
if [[ $max_redis_memory -ne 0 ]];then
15+
percent_redis_memory=`awk 'BEGIN{printf "%d\n",'$used_redis_memory'/'$max_redis_memory'*100}'`
16+
17+
if [ $percent_redis_memory -ge $CRITICAL_NUM ];then
18+
echo "CRITICAL!!! ${CRITICAL_MSG} value is ${percent_redis_memory}"
19+
exit 2
20+
elif [[ $percent_redis_memory -ge $WARONG_NUM ]];then
21+
echo "WARONG!!! ${WARONG_MSG} value is ${percent_redis_memory}"
22+
exit 1
23+
else
24+
echo "NORMAL ${NORMAL_MSG} value is ${percent_redis_memory}"
25+
exit 0
26+
fi
27+
else
28+
echo "ERROR!!! ${ERROR_MSG}"
29+
exit 2
30+
fi
31+
32+
#nrpe配置文件添加
33+
#command[check_redis_7710_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7710 90 95
34+
#command[check_redis_7711_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7711 90 95
35+
#command[check_redis_7712_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7712 90 95
36+
#command[check_redis_7713_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7713 90 95
37+
#command[check_redis_7714_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7714 90 95
38+
#command[check_redis_7715_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7715 90 95
39+
#command[check_redis_7716_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7716 90 95
40+
#command[check_redis_7717_rate_memory]=/usr/local/nagios/libexec/check_redis_rate_memory.sh 7717 90 95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
from check_redis import redis_monitor
3+
import sys
4+
5+
Total_mem=10240
6+
Warn_mem = 6000
7+
Circtl_mem = 9000
8+
9+
class redis_use_memory(redis_monitor):
10+
11+
pass
12+
13+
mem_val=redis_use_memory('quB1BY3njv0edffdfHldfBFw92',6779,'used_memory_rss')
14+
15+
Current_use_Mem=mem_val.Monitor_items()
16+
17+
18+
if Current_use_Mem < int(sys.argv[1]):
19+
print "OK -connect counts is %s | Current=$NUM;Warning=$Warn;Critical=$Crit" % Current_use_Mem
20+
sys.exit(0)
21+
elif Current_use_Mem > int(sys.argv[1]) and Current_use_Mem < int(sys.argv[2]):
22+
print "Warning -connect counts is %s | Current=$NUM;Warning=$Warn;Critical=$Crit" % Current_use_Mem
23+
sys.exit(1)
24+
else:
25+
#Current_use_Mem > sys.argv[2]:
26+
print "Critical -connect counts is %s | Current=$NUM;Warning=$Warn;Critical=$Crit" % Current_use_Mem
27+
sys.exit(2)
28+
29+
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
IP=`tail -n 99 /var/log/secure | grep sshd | grep ssh2 | awk -F ' ' '{print $11}' | tail -n 1`
3+
4+
strA="192.168.1.12 192.168.1.21"
5+
strB=`cat /tmp/ssh.log | awk -F ' ' '{print $11}' | tail -n 1 | awk -F'.' '{print $1 "." $2}'`
6+
if [[ $strA =~ $strB ]]
7+
then
8+
NUM=10
9+
else
10+
NUM=20
11+
fi
12+
13+
if [ $NUM -gt 15 ]
14+
then
15+
echo "Critical -secure $CHECK| Current=$NUM;Warning=15;Critical=15"
16+
exit 2
17+
fi
18+
if [ $NUM -lt 15 ]
19+
then
20+
echo "OK -secure $CHECK| Current=$NUM;Warning=15;Critical=15"
21+
exit 0
22+
fi
23+
if [ $NUM -eq 15 ]
24+
then
25+
echo "Warning -secure $CHECK | Current=$NUM;Warning=15;Critical=15"
26+
exit 1
27+
fi

‎nagios/nagios_client/sh/check_ss.sh

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/bin/bash
2+
NUM=`ss -s | grep estab | awk '{print$4}' | cut -d, -f1`
3+
4+
if [ $NUM -lt $1 ]
5+
then
6+
echo "OK -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
7+
8+
exit 0
9+
fi
10+
if [ $NUM -gt $1 -a $NUM -lt $2 ]
11+
then
12+
echo "Warning -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
13+
exit 1
14+
fi
15+
if [ $NUM -gt $2 ]
16+
then
17+
echo "Critical -connect counts is $NUM | Current=$NUM;Warning=$Warn;Critical=$Crit"
18+
exit 2
19+
fi
+180
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
#!/bin/bash
2+
3+
#nagios exit code
4+
STATE_OK=0
5+
STATE_WARNING=1
6+
STATE_CRITICAL=2
7+
STATE_UNKNOWN=3
8+
9+
#help
10+
help () {
11+
local command=`basename $0`
12+
echo "NAME
13+
${command} -- check network status
14+
SYNOPSIS
15+
${command} [OPTION]
16+
DESCRIPTION
17+
-H IP ADDRESS
18+
-p LOCAL PORT
19+
-S [TIME_WAIT|FIN_WAIT|ESTABLISHED|CLOSING|SYN_SEND|TIMED_WAIT|LISTEN]
20+
-w warning
21+
-c critical
22+
USAGE:
23+
Total connections:
24+
$0 -w 100 -c 200
25+
Port:
26+
$0 -p 8819 -w 100 -c 200
27+
Host and Port:
28+
$0 -H 192.168.0.6 -p 8819 -w 100 -c 200
29+
Status:
30+
$0 -H 192.168.0.6 -p 8819 -S ESTABLISHED -w 100 -c 200" 1>&2
31+
exit ${STATE_WARNING}
32+
}
33+
34+
check_num () {
35+
local num_str="$1"
36+
echo ${num_str}|grep -E '^[0-9]+$' >/dev/null 2>&1 || local stat='not a positive integers!'
37+
if [ "${stat}" = 'not a positive integers!' ];then
38+
echo "${num_str} ${stat}" 1>&2
39+
exit ${STATE_WARNING}
40+
else
41+
local num_int=`echo ${num_str}*1|bc`
42+
if [ ${num_int} -lt 0 ];then
43+
echo "${num_int} must be greater than 0!" 1>&2
44+
exit ${STATE_WARNING}
45+
fi
46+
fi
47+
}
48+
49+
check_ip () {
50+
local ip_str="$1"
51+
echo "${ip_str}"|grep -P '^\d{1,3}(\.\d{1,3}){3}$' >/dev/null 2>&1 || local stat='not a ip!'
52+
if [ "${ip_stat}" = 'not a ip!' ];then
53+
echo "${ip_str} ${stat}" 1>&2
54+
exit ${STATE_WARNING}
55+
fi
56+
}
57+
58+
check_state () {
59+
local stat_str="$1"
60+
if [ -n "${stat_str}" ];then
61+
case "${stat_str}" in
62+
TIME_WAIT|FIN_WAIT|ESTABLISHED|CLOSING|SYN_SEND|TIMED_WAIT)
63+
cmd="netstat -nt|grep ${stat_str}"
64+
;;
65+
LISTEN)
66+
cmd="netstat -ntl"
67+
;;
68+
*)
69+
echo "This script only support [TIME_WAIT|FIN_WAIT|ESTABLISHED|CLOSING|SYN_SEND|TIMED_WAIT]" 1>&2
70+
exit ${STATE_WARNING}
71+
;;
72+
esac
73+
fi
74+
}
75+
76+
logging () {
77+
local now_date=`date -d now +"%F %T"`
78+
local log_path='/var/log/tcp'
79+
local log_name=`date -d "now" +"%F"`
80+
81+
local uid=`id -u`
82+
if [ "${uid}" == '0' ];then
83+
test -d ${log_path} || mkdir -p ${log_path}/
84+
chown nagios.nagios -R ${log_path}
85+
fi
86+
87+
log="${log_path}/tcp_stat_${log_name}.log"
88+
echo "${now_date} ${info}"|sed 's/;//g' >> ${log}
89+
test -f ${log} && chown nagios.nagios ${log}
90+
}
91+
92+
message () {
93+
local stat="$1"
94+
echo "TCP status is ${stat} - ${info}|Total_connections=${total_connections_int};${warning};${critical};${min};${max}"
95+
}
96+
97+
#input
98+
while getopts w:c:p:H:S:l opt
99+
do
100+
case "$opt" in
101+
w)
102+
warning=$OPTARG
103+
check_num "${warning}"
104+
;;
105+
c)
106+
critical=$OPTARG
107+
check_num "${critical}"
108+
;;
109+
p)
110+
port="$OPTARG"
111+
check_num "${port}"
112+
;;
113+
H)
114+
ip="$OPTARG"
115+
check_ip "${ip}"
116+
;;
117+
S)
118+
state="$OPTARG"
119+
check_state "${state}"
120+
;;
121+
l)
122+
log_status='on'
123+
;;
124+
*) help;;
125+
esac
126+
done
127+
shift $[ $OPTIND - 1 ]
128+
129+
#[ $# -gt 0 -o -z "${warning}" -o -z "${critical}" ] && help
130+
[ $# -gt 0 -o -z "${warning}" ] && help
131+
132+
if [ -n "${warning}" -a -n "${critical}" ];then
133+
if [ ${warning} -ge ${critical} ];then
134+
echo "-w ${warning} must lower than -c ${critical}!" 1>&2
135+
exit ${STATE_UNKNOWN}
136+
fi
137+
fi
138+
139+
if [ -n "${warning}" -a -z "${critical}" ];then
140+
if [ "${warning}" == "0" ];then
141+
critical="${warning}"
142+
else
143+
echo "Critical can not be empty!" 1>&2
144+
exit ${STATE_UNKNOWN}
145+
fi
146+
fi
147+
148+
[ -z "${state}" ] && netstat_cmd="netstat -nt" || netstat_cmd="${cmd}"
149+
[ -z "${ip}" -a -z "${port}" ] && run_cmd="${netstat_cmd}"
150+
[ -n "${ip}" -a -z "${port}" ] && run_cmd="${netstat_cmd}|grep \"${ip}:\""
151+
[ -n "${port}" -a -z "${ip}" ] && run_cmd="${netstat_cmd}|grep -P \":${port}\s\""
152+
[ -n "${port}" -a -n "${ip}" ] && run_cmd="${netstat_cmd}|grep -P \"${ip}:${port}\s\""
153+
154+
info=`eval "${run_cmd}"|\
155+
awk 'BEGIN{OFS=":";ORS="; "}/^tcp/{stats[$(NF)]+=1;sum++}END{print "Total",sum;for (stat in stats) {print stat,stats[stat]}}'`
156+
157+
echo "${info}"|grep -E '[0-9]' >/dev/null 2>&1 || info="Total:0"
158+
159+
min=0
160+
max=4096
161+
total_connections_str=`echo "${info}"|grep -oP "Total:\d+"|awk -F':' '{print $2}'`
162+
total_connections_int=`echo "${total_connections_str}*1"|bc`
163+
echo "${total_connections_int}"|grep -E '^[0-9]+$' >/dev/null 2>&1 ||\
164+
eval "echo ${total_connections_int} not a number!;exit ${STATE_UNKNOWN}"
165+
166+
[ "${log_status}" == 'on' ] && logging
167+
168+
if [ "${warning}" == "0" ];then
169+
if [ ${total_connections_int} -eq 0 ];then
170+
message "Warning"
171+
exit ${STATE_WARNING}
172+
else
173+
message "OK"
174+
exit ${STATE_OK}
175+
fi
176+
fi
177+
178+
[ ${total_connections_int} -lt ${warning} ] && message "OK" && exit ${STATE_OK}
179+
[ ${total_connections_int} -ge ${critical} ] && message "Critical" && exit ${STATE_CRITICAL}
180+
[ ${total_connections_int} -ge ${warning} ] && message "Warning" && exit ${STATE_WARNING}

‎nagios/nagios_client/sh/check_traffic.sh

+1,949
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.