[CentOS] Total Number of conecctions

Fri Dec 3 21:46:30 UTC 2010
Alexander Dalloz <ad+lists at uni-x.org>

Am 03.12.2010 22:27, schrieb m.roth at 5-cent.us:
> Robert Heller wrote:
>> At Fri, 3 Dec 2010 12:57:59 -0800 (PST) CentOS mailing list
>> <centos at centos.org> wrote:
>>>
>>> I have the need to know how many connection the server has, i run this
>>> command but i don't know how to sum all the results and get a final
>>> number. any ideas?
>>>
>>> netstat -an | grep -E 'tcp|udp' | awk '{print $6}' | sort | uniq -c |
>>> sort -n
>>>
>>> Â Â  1 CLOSE_WAIT
>>> Â Â  1 FIN_WAIT_2
>>> Â Â  1 LAST_ACK
>>> Â Â  1 TIME_WAIT
>>> Â Â  4 SYN_SENT
>>> Â  15
>>> Â  37 LISTEN
>>> Â  44 ESTABLISHED
>>
>>  <the above script> | awk '{print $1;}' | tr '\n' '+'|sed 's/\+$//g'|bc
>>
>> The awk prints just the number, the tr replaces the newlines with +'s,
>> the sed strips off the trailing + (from the last newline), and bc does
>> the math.
> 
> Why do people use awk without using it?
> netstat -an | awk '{if ($0 ~ /tcp|udp/){ print $6;}END { print NF;}' gets
> you the number of lines at the end. Or, to be more elegant,
> netstat -an | \
>   awk '{if ($0 ~ /tcp|udp/) {
>            array[$6] += 1;
>        }
>        END {
>           for ( i in array ) {
>               print i;
>               sum += array[i];
>           }
>           print sum;
>       }'
> 
>      mark "me? like awk? yell at Larry Wall in '94 for proselytizing
>               perl in comp.language.awk?"

Pretty correct. I hate those pipe orgies and awk usage where people just
use it to print out a specific field. Why piping grep output into awk
when awk itself can grep?

Though Mark, I feel you miss something in your awk script. It does not
print the value of each state.

LANG=C netstat -an | awk '/tcp|udp/ { array[$6] += 1; sum += 1 } END {
for ( i in array ) printf "%3s %s\n", array[i],i; print sum }'

Alexander