The check_antp provided by nagios is too simple. In addition to the status statistics output, no parameters are provided. When faced with different application servers, alarm becomes a big problem.
So I decided to write a check script myself. When running a script, it is different from the command operation, that is, to consider the efficiency issue. Running the netstat-ant command regularly on high-concurrency machines for statistics is obviously not suitable. You can retrieve data directly from the proc system, which is much faster.
First, we will introduce the/proc/net/tcp file, which records all tcp connections under ipv4, including the following values:
Sl local_address rem_address st tx_queue rx_queue tr tm-> when retrnsmt uid timeout inode
0: 00000000:3241 000000000000:0000 0A 000000000000:00000000 00000000 00000000 0 22714864 1 ffff88004f918740 750 0 0 2-1
Most importantly, local_address local address: Port, rem_address remote address: Port, and st connection status.
Note 1: All files are in hexadecimal format, so the HTTP port 80 record is 0050.
Note 2: The Status Code is as follows:
00 "ERROR_STATUS ",
01 "TCP_ESTABLISHED ",
02 "TCP_SYN_SENT ",
03 "TCP_SYN_RECV ",
04 "TCP_FIN_WAIT1 ",
05 "TCP_FIN_WAIT2 ",
06 "TCP_TIME_WAIT ",
07 "TCP_CLOSE ",
08 "TCP_CLOSE_WAIT ",
09 "TCP_LAST_ACK ",
0A "TCP_LISTEN ",
0B "TCP_CLOSING ",
Next, we will introduce the nrpe check script. The script can be written no matter how it is written. For nagios server, in addition to accepting the output result of the script, it only recognizes the exit value of the script running (echo $ can be used after testing? See), including OK exit 0, WARNING exit 1, CRITICAL exit 2, and unknown exit 3.
The last simple script to check the number of http port connections is as follows:
#! /Bin/bash # Written by Gemmy. rao # Version 0.2 # CHANGES # by www.jbxue.com # Add-p option for checking other service's port # InitPORT = 80 WARNING = 5000 CRITICAL = 20000 # get optionswhile getopts "w: c: p: hs "OPT; do case $ OPT in w) WARNING =$ {OPTARG}; c) CRITICAL =$ {OPTARG}; p) PORT =$ {OPTARG} # convert each PORT to hexadecimal PORT_16 = 'echo $ {PORT} | awk-F, '{for (I = 1; I <= NF; I ++) printf "| %. 4X ", $ I} '| sed's/| //''; h) echo "Usage: $0-w 500-c 2000-p 80, 8081-s "exit 0; s) SILENT = 1; *) echo" Usage: $0-w 500-c 2000-p 80, 8081 "exit 0; esacdone # tested by time, set the value to netstat> awk '// {a ++} END {print a}'> cat | grep | wc> cat | awk | wc. In a 2 W connection, netstat takes 20 s, and the fastest way is less than 5 s (generally, from nagios to 10 s, it should directly report timeout) PORT_CONN = 'cat/proc/net/tcp * | awk '$2 ~ /:( '$ PORT_16') $/'| wc-l' if [["$ SILENT" = 1]; then [[-d/usr/local/nagios] | mkdir-p/usr/local/nagios echo "Silent log write OK | Port $ {PORT} =$ {PORT_CONN }; $ {WARNING };$ {CRITICAL}; 0; 0 "echo-en" 'date' t $ PORT_CONNn ">>/ usr/local/nagios/conn. log exit 0 elif [["$ PORT_CONN"-lt "$ WARNING"]; then echo "Port $ PORT connection OK for $ PORT_CONN. | Port $ {PORT }=$ {PORT_CONN}; $ {WARNING };$ {CRITICAL}; 0; 0 "exit 0 elif [[" $ PORT_CONN "-gt" $ CRITICAL "]; then echo" Port $ PORT connection critical for $ PORT_CONN !! | Port $ {PORT }=$ {PORT_CONN}; $ {WARNING };$ {CRITICAL}; 0; 0 "exit 2 else echo" Port $ PORT connection warning for $ PORT_CONN! | Port $ {PORT }=$ {PORT_CONN}; $ {WARNING };$ {CRITICAL}; 0; 0 "exit 1fi