]> git.pld-linux.org Git - packages/nagios-plugin-check_iptables.git/blame - check_iptables.sh
version 0.2; unhardcode tool paths
[packages/nagios-plugin-check_iptables.git] / check_iptables.sh
CommitLineData
ac1ff40f
ER
1#!/bin/sh
2PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
3
f3708dd1 4PROGNAME=${0##*/}
ac1ff40f 5PROGPATH=`echo $0 | sed -e 's,[\\/][^\\/][^\\/]*$,,'`
53b26018 6VERSION=0.2
66c92905 7ARGS="$*"
ac1ff40f
ER
8
9. $PROGPATH/utils.sh
10
164127df
ER
11iptables=iptables
12sudo=sudo
ac1ff40f
ER
13chain=INPUT
14table=filter
15verbose=0
16warning=1
17critical=1
045c1338 18setup_sudo=0
ac1ff40f
ER
19
20print_usage() {
21 echo "Usage: $PROGNAME -C CHAIN -t TABLE"
22 echo "Usage: $PROGNAME --help"
23 echo "Usage: $PROGNAME --version"
24}
25
26print_help() {
53b26018 27 print_revision $PROGNAME $VERSION
ac1ff40f
ER
28 echo ""
29 print_usage
30 echo ""
f3708dd1 31 echo "This plugin tests if iptables has needed amount of rules loaded"
ac1ff40f
ER
32 echo ""
33
34 echo "-C CHAIN"
35 echo " Chain to list. Default: $chain"
36 echo "-t TABLE"
37 echo " Table to list. Default: $table"
38 echo "-S"
39 echo " Install sudo rules"
40 echo "-v"
41 echo " Enable verbose run"
42 echo "--help"
43 echo " Print this help screen"
44 echo "--version"
45 echo " Print version and license information"
46 echo ""
47
48 support
49 exit 0
50}
51
52setup_sudoers() {
53 new=/etc/sudoers.$$.new
54 umask 0227
55 cat /etc/sudoers > $new
56 cat >> $new <<-EOF
57
66c92905 58 # Lines matching CHECK_IPTABLES added by $0 $ARGS on $(date)
ac1ff40f 59 User_Alias CHECK_IPTABLES=nagios
045c1338 60 CHECK_IPTABLES ALL=(root) NOPASSWD: $list_iptables
ac1ff40f
ER
61 EOF
62
63 if visudo -c -f $new; then
64 mv -f $new /etc/sudoers
65 exit 0
66 fi
f519d7e9 67 rm -f $new
ac1ff40f
ER
68 exit 1
69}
70
71list_iptables() {
045c1338
ER
72 # if running as root, skip sudo
73 [ "$(id -u)" != 0 ] || sudo=
74
0fefbbed 75 $sudo $list_iptables | grep -Fc /
ac1ff40f
ER
76}
77
78while [ $# -gt 0 ]; do
79 case "$1" in
80 --help)
81 print_help
82 exit 0
83 ;;
84
85 -h)
86 print_help
87 exit 0
88 ;;
89
90 --version)
53b26018 91 print_revision $PROGNAME $VERSION
ac1ff40f
ER
92 exit 0
93 ;;
94
95 -V)
53b26018 96 print_revision $PROGNAME $VERSION
ac1ff40f
ER
97 exit 0
98 ;;
99
100 -v)
101 verbose=1
102 ;;
103
104 -S)
045c1338 105 setup_sudo=1
ac1ff40f
ER
106 ;;
107
108 -C)
109 chain=$2; shift
110 ;;
111
112 -t)
113 table=$2; shift
114 ;;
115
116 -w)
117 warning=$2; shift
118 ;;
119
120 -c)
121 critical=$2; shift
122 ;;
123
124 *)
125 echo >&2 "Unknown argument: $1"
126 print_usage
127 exit $STATE_UNKNOWN
128 ;;
129 esac
130 shift
131done
132
ac1ff40f
ER
133rc=$STATE_UNKNOWN
134
0fefbbed 135list_iptables="$iptables -n -t $table -L $chain"
045c1338
ER
136
137if [ "$setup_sudo" = 1 ]; then
138 setup_sudoers
139fi
ac1ff40f
ER
140
141count=$(list_iptables)
142if [ "$count" -lt "$critical" ]; then
143 rc=$STATE_CRITICAL
144 state=CRITICAL
145elif [ "$count" -lt "$warning" ]; then
146 rc=$STATE_WARNING
147 state=WARNING
148else
149 rc=$STATE_OK
150 state=OK
151fi
152
153echo "$state: $count iptables rules in $chain chain of $table table"
154
155exit $rc
This page took 0.103789 seconds and 4 git commands to generate.