forked from rohit01/nrpe-plugins
-
Notifications
You must be signed in to change notification settings - Fork 2
/
check_ports.sh
executable file
·132 lines (121 loc) · 3.33 KB
/
check_ports.sh
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/bin/bash
#
# Nagios NRPE plugin to check status weather the given ports are
# listening to TCP/UDP connections
#
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
AUTHOR="Rohit Gupta - @rohit01"
PROGNAME=`basename $0`
VERSION="Version 1.0,"
print_version() {
echo "$PROGNAME: $VERSION $AUTHOR"
}
print_help() {
print_version
echo ""
echo "$PROGNAME is a custom Nagios plugin to check port status"
echo "using netstat command."
echo ""
echo "Usage: $PROGNAME -p <port numbers separated by comma>"
echo ""
echo "Options:"
echo " -p/--ports)"
echo " Check if the server is listening to given port numbers"
echo " Multiple port numbers can be given separated by comma"
echo " -t/--connectiontype)"
echo " Port type on which service listens. Possible values: tcp, udp"
echo " Default: check both"
echo " -h/--help)"
echo " Print this help message & exit"
echo " -v/--version)"
echo " Print version of this script & exit"
echo ""
echo "Examples:"
echo " $PROGNAME -p 80,8080,8880"
}
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version
exit $ST_UK
;;
--ports|-p)
ports=$2
shift
;;
--connectiontype|-t)
connectiontype=$2
shift
;;
*)
echo "Unknown argument: $1"
echo ""
print_help
exit $ST_UK
;;
esac
shift
done
######## Validate arguments passed ########
if echo $ports | grep "^$" >/dev/null; then
echo "Mandatory option -p/--ports not specified"
echo "Use -h/--help option to get more details"
exit $ST_UK
fi
if echo ${ports} | grep -v -e "^$" -e "^[0-9,]*$" | grep -v grep \
| grep -v ${PROGNAME} >/dev/null; then
echo "Invalid value: '${ports}' for option -p/--ports. Possible values:" \
"integer separated by comma "
exit $ST_UK
fi
if echo ${connectiontype} | grep -v -e "^tcp,udp$" -e "^tcp$" -e "^udp$" \
>/dev/null; then
echo "Invalid value: '${connectiontype}' for option -t/--connectiontype." \
"Possible values: tcp, udp, {tcp,udp}. Default: tcp,udp"
exit $ST_UK
fi
###########################################
## Set port type filter ##
if [ "X${connectiontype}" = 'Xtcp' ]; then
type_filter='t'
elif [ "X${connectiontype}" = 'Xudp' ]; then
type_filter='u'
elif [ "X${connectiontype}" = 'Xtcp,udp' ]; then
type_filter='tu'
else
type_filter='tu'
fi
exit_status="${ST_OK}"
message=''
for port_no in $(echo $ports | tr ',' ' ') ; do
if echo ${message} | grep -v "^$" >/dev/null; then
message="${message}; "
fi
netstat -ln${type_filter} | tr -s " " | cut -d " " -f 4 | grep ":$port_no$" >/dev/null
if [ $? -ne 0 ]; then
message="${message}Port: $port_no - NOT LISTENING"
exit_status="${ST_CR}"
else
message="${message}Port: $port_no - LISTEN"
fi
done
if [ $exit_status -eq $ST_OK ]; then
echo "OK - ${message}"
exit $ST_OK
elif [ $exit_status -eq $ST_WR ]; then
echo "WARNING - ${message}"
exit $ST_WR
elif [ $exit_status -eq $ST_CR ]; then
echo "CRITICAL - ${message}"
exit $ST_CR
else
echo "UNKNOWN - $message"
exit $ST_UK
fi