Nagios a script to monitor the number of TCP state connections

Source: Internet
Author: User

This is a shell-developed Nagios plug-in, based on the Nagios Plugin Development guidelines and Nagios API, supplemented on the basis of predecessors, supporting the official standard-V, Plugin-t option. The main idea of the plug-in is to get TCP's various state connections through the netstat command, to count each state number, and finally to output the detection information and performance information according to the standard Nagios plug-in format. Monitoring that State, how many alerts are connected, can be specified by scripting parameters.

Nagios monitors the server, through the Nrpe in the monitored side executes the detection script, and the detection script runs the status return value and the output information, returns to the Nagios monitoring server. Where the state return value 0 for normal, 1 for warning, 2 for emergency, 3 for unknown.
Description: Script supports these several parameters

-v|–version Display script version information
-c|–critical threshold Specifies the valve value of the critical, you must specify
-w|–warning threshold Specifies the valve value of the warning, you must specify
-s|–status TCP status Specifies the TCP connection state, which must be specified
-h|–help Get use Help
-t|–timeout time Specifies the script run timeout

Threshold format
~ Represents the negative infinity

Start can be omitted from 0:0:end
Preceded by Start: no end value indicated; infinity
The alarm will exceed the starting and ending values, including the two values
The start value is @, the alarm will be between the starting and ending values, including the two values
10 less than 0 and greater than 10 alert
10:10 to positive infinity, i.e. negative infinity to 10 alarm
~:10 in negative infinity to 10, i.e. 10 to positive infinity alarm
10:20 10 to 20, i.e. less than 10 and greater than 20
@10:20 between 10 and 20 alarms
@10 between 0 and 10 alarms

This script may still have bugs, and you are welcome to fix it.

Code:

#!/bin/bash
State_ok=0
State_warning=1
state_critical=2
State_unknown=3
State_dependent=4
Path=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
Print_revision () {
echo "(Nagios-plugins-netstat 0.5)"
}
Usage () {
Echo-e "Usage: $ [-v|--version] [-h|--help] <-w|–warning warning threshold>\n<-c|–critical critical threshold > <-s|–status status>\n<-t|–timeout time> "
}
Check_range () {
Mflag=0
If echo $ | GREP-E-Q "^@? ((~:[0-9]*$) | ([0-9]+:? [0-9]*$)] "; then
If echo $ |grep-e-Q "^@"; then
range=${1#@}
Mflag=1
Else
Range=$1
Fi
If Echo $range |grep-e-Q ":"; then
start=${range%%:*}
If ["$start" = "~"];then
start=-999999
Fi
If ["$start"-lt 0];then
Return 2
Fi
end=${range#*:}
if [[Z $end]];then
end=65535
Fi
Else
Start=0
End= $range
Fi
If ["$start"!= "~"] && ["$end"!= ""];then
If [$start-gt $end];then
Return 2
Fi
Fi
Else
echo "Invalid range"
Return 2
Fi
return 0
}
Select_arg () {
If [$#-eq 0];then
Return 1
Fi
Wcount=0
Ccount=0
Scount=0
Until [$#-eq 0];d o
Case is in
-v|–version)
Versionflag=1
Shift 1
;;
-H|–HELP)
Helpflag=1
Shift 1
;;
-w|–warning)
[$#-lt 2] && return 1
Check_range $
If [$?-ne 0];then
Return 1
Else
Warn_start= $start
Warn_end= $end
warn_mflag= $mflag
Fi
Shift 2
Let wcount++
;;
-c|–critical)
[$#-lt 2] && return 1
Check_range $
If [$?-ne 0];then
Return 1
Else
Critical_start= $start
Critical_end= $end
critical_mflag= $mflag
Fi
Shift 2
Let ccount++
;;
-s|–status)
[$#-lt 2] && return 1
Case $ in
established| Established)
Status=established
;;
Time_wait| TIME_WAIT)
Status=time_wait
;;
syn_recv| SYN_RECV)
Status=syn_recv
;;
fin_wait1| FIN_WAIT1)
Status=fin_wait1
;;
fin_wait1| FIN_WAIT2)
Status=fin_wait2
;;
last_ack| Last_ack)
Status=last_ack
;;
Close_wait| close_wait)
Status=close_wait
;;
*)
Return 1
;;
Esac
Shift 2
Let scount++
;;
-t|–timeout)
[$#-lt 2] && return 1
if! echo $ |grep-e-Q "^[1-9][0-9]*$"; then
Return 1
Fi
Timeout=$2
;;
*)
Return 1
;;
Esac
Done
return 0
}
Alarm () {
connect= ' Netstat-ant | awk '/^tcp/&&!/listen/{s[$NF]++}end{for (i in S) print I,s[i]} '
Established= ' echo $connect |awk '/established/{print $} '
[Z $established] && established=0
Time_wait= ' echo $connect |awk '/time_wait/{print $} '
[Z $time _wait] && time_wait=0
Syn_recv= ' echo $connect |awk '/syn_recv/{print $} '
[Z $syn _recv] && syn_recv=0
Fin_wait1= ' echo $connect |awk '/fin_wait1/{print $} '
[Z $fin _wait1] && fin_wait1=0
Fin_wait2= ' echo $connect |awk '/fin_wait2/{print $} '
[Z $fin _wait2] && fin_wait2=0
Last_ack= ' echo $connect |awk '/last_ack/{print $} '
[Z $last _ack] && last_ack=0
Close_wait= ' echo $connect |awk '/close_wait/{print $} '
[Z $close _wait] && close_wait=0
If [$warn _mflag-eq 0-a $critical _mflag-eq 0];then
W1=-ge;w2=-le;c1=-ge;c2=-le;wboole=-a;cboole=-a
elif [$warn _mflag-eq 1-a $critical _mflag-eq 0];then
W1=-le;w2=-ge;c1=-ge;c2=-le;wboole=-o;cboole=-a
elif [$warn _mflag-eq 0-a $critical _mflag-eq 1];then
W1=-ge;w2=-le;c1=-le;c2=-ge;wboole=-a;cboole=-o
elif [$warn _mflag-eq 1-a $critical _mflag-eq 1];then
W1=-le;w2=-ge;c1=-le;c2=-ge;wboole=-o;cboole=-o
Fi
If [${!status} $w 1 $warn _start $wboole ${!status} $w 2 $warn _end] && [${!status} $c 1 $critical _start $cboole ${! Status} $c 2 $critical _end];then
Exitcode=0
Else
if! [${!status} $w 1 $warn _start $wboole ${!status} $w 2 $warn _end];then
Exitcode=1
Fi
if! [${!status} $c 1 $critical _start $cboole ${!status} $c 2 $critical _end];then
exitcode=2
Fi
If [${!status}-le 0];then
Exitcode=3
Fi
Fi
If [$exitcode-eq 0];then
serviceoutput= "$status Ok-total:${!status}"
elif [$exitcode-eq 1];then
serviceoutput= "$status Warning-total:${!status}"
elif [$exitcode-eq 2];then
serviceoutput= "$status Critical-total:${!status}"
elif [$exitcode-eq 3];then
serviceoutput= "$status Unknown-total:${!status}"
Fi
Echo-e "$serviceoutput; \ntime_wait $time _wait;\nsyn_recv $syn _recv;\nfin_wait1 $fin _wait1, established $established; \nfin_wait2 $fin _wait2;\nlast_ack $last _ack;\nclose_wait $close _wait "
Exit $exitcode
}
Select_arg $@
[$?-ne 0] && usage && exit 3
if [[-N $versionflag]];then
If [$versionflag-eq 1];then
Print_revision && Exit 0
Fi
Else
[[-N $helpflag]] && [$helpflag-eq 1] && usage && exit 0
Fi
[$ccount-ne 1] | | [$wcount-ne 1] | | [$scount-ne 1] && usage && exit 3
[Z $timeout] && timeout=10
Alarm &
commandpid=$!
(Sleep $timeout; commandchild=$ (Ps-eo pid,ppid | awk "\$2== $commandpid {print \$1}"), for B in $commandchild;d o kill-9 $b &>/dev/null;d one;kill-9 $commandpid &>/dev/null) &
watchdog=$!
Wait $commandpid &>/dev/null
Pexitcode=$?
[$pexitcode-gt 3] && pexitcode=3
Watchdogchild= ' Ps-eo pid,ppid | awk "\\$2== $watchdog {print \\$1}"
For a in $watchdogchild;d o
Kill-9 $a &>/dev/null
Done
Kill-9 $watchdog &>/dev/null
Exit $pexitcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.