<br />
<b>Warning</b>:  Undefined variable $post in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  Attempt to read property "ID" on null in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  Undefined variable $ID in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  Undefined variable $post in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  Attempt to read property "ID" on null in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>19</b><br />
<br />
<b>Warning</b>:  Undefined variable $ID in <b>/home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php</b> on line <b>20</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php:19) in <b>/home/wolfdendesign/www/themadindian/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1930</b><br />
<br />
<b>Warning</b>:  Cannot modify header information - headers already sent by (output started at /home/wolfdendesign/www/themadindian/wp-content/plugins/facebook-google-twitter-share/facebook-googleplus-twitter-share.php:19) in <b>/home/wolfdendesign/www/themadindian/wp-includes/rest-api/class-wp-rest-server.php</b> on line <b>1930</b><br />
{"id":69,"date":"2013-11-27T20:12:43","date_gmt":"2013-11-28T01:12:43","guid":{"rendered":"http:\/\/themadindian.wolfdendesign.com\/?p=69"},"modified":"2013-11-27T20:12:43","modified_gmt":"2013-11-28T01:12:43","slug":"awk-who-is-connected-how-many-connections-do-they-have-and-what-is-their-connection-state","status":"publish","type":"post","link":"http:\/\/themadindian.wolfdendesign.com\/?p=69","title":{"rendered":"awk &#8211; Who is connected, how many connections do they have and what is their connection state"},"content":{"rendered":"<p>I&#8217;ve been busy tuning Varnish to prevent outages and that makes me take a much closer look at things and how to get through things quickly especially when it comes to working with farms of servers. I noticed in my cacti graphs a huge rise in FIN_WAIT_1 connection states on my FreeBSD servers to a degree of 25x the number of established connections so I decided to do some work to see what&#8217;s going. What I came to I think will provide some nice insight for many different scenarios.<\/p>\n<p>First how I made my determination of my ratios, I know what connection states I&#8217;m looking so I create an array with those values, I know that it&#8217;s the 6th field and I&#8217;d like a count of where each stands. This is the oneliner<\/p>\n<pre>connstates=( FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT ESTABLISHED SYN_RCVD LAST_ACK); for a in ${connstates[@]}; do netstat -n |awk '($6 = \/'\"${a}\"'\/){freq['\"${a}\"']++} END {for (x in freq) {print freq[x], \"'\"${a}\"'\", \"\\n\" }}' |sort -nr;done<\/pre>\n<p>I added the carriage return to clean up the output<\/p>\n<pre>767 FIN_WAIT_1 \r\n\r\n103 FIN_WAIT_2\r\n\r\n866 TIME_WAIT\r\n\r\n54 ESTABLISHED\r\n\r\n220 SYN_RCVD\r\n\r\n7 LAST_ACK<\/pre>\n<p>Now that I have that I want to know who&#8217;s connected, how many connections they have by connection state this oneliner takes care of that<\/p>\n<pre>connstates=( FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT ESTABLISHED SYN_RCVD LAST_ACK); for a in ${connstates[@]}; do echo -e \"\\n\" $a; netstat -n |awk '($6 = \/'\"${a}\"'\/){print $5}'|awk -F \\. '{$5 = \"\"; gsub(\/\\ \/, \".\"); gsub(\/.$\/,\"\"); print}' |awk '{freq[$0]++} END {for (x in freq) {print freq[x], x }}' |sort -nr;done<\/pre>\n<p>and that produces an output like this<\/p>\n<pre>FIN_WAIT_1\r\n627 67.198.71.114\r\n390 64.90.159.102\r\n264 89.241.213.178\r\n221 174.108.76.131\r\n71 186.32.54.114\r\n\r\nFIN_WAIT_2\r\n5 66.66.133.117\r\n5 65.0.139.23\r\n5 173.169.40.34\r\n5 166.147.120.16\r\n4 96.246.240.75\r\n\r\nTIME_WAIT\r\n98 10.10.10.20\r\n68 10.10.10.22\r\n60 10.10.10.23\r\n60 10.10.10.24\r\n49 10.10.10.25\r\n\r\nESTABLISHED\r\n13 186.32.54.114\r\n9 98.255.88.48\r\n7 64.90.159.102\r\n6 70.208.78.19\r\n5 98.204.42.34\r\n\r\nSYN_RCVD\r\n22 108.45.28.123\r\n2 12.160.201.226\r\n1 98.255.88.48\r\n\r\nLAST_ACK\r\n6 71.215.198.128\r\n2 66.104.42.194\r\n1 80.58.205.48\r\n1 41.79.120.19\r\n1 121.120.116.205<\/pre>\n<p>Some of these gave me a hard time so let&#8217;s take a closer look at some of these<br \/>\nI&#8217;ve covered the gotchas from using a bash array variable in an earlier blog so I won&#8217;t cover that again. What I did discover is awk unkowingly modifies the output. I didn&#8217;t understand why I was getting this strange frequency output until I broke down what was happening and that&#8217;s when I saw what awk was doing<\/p>\n<pre>\r\nnetstat -n |awk '($6 = \/FIN_WAIT_1\/)' |head -n 5\r\ntcp4 0 532 10.10.10.169.80 173.175.47.91.55257 1\r\ntcp4 0 532 10.10.10.169.80 173.175.47.91.55256 1\r\ntcp4 0 533 10.10.10.169.80 108.45.28.123.49944 1\r\ntcp4 0 0 10.10.10.169.80 113.197.8.162.41647 1\r\ntcp4 0 0 10.10.10.169.80 113.197.8.162.2879 1<\/pre>\n<p>UH? I thought maybe it was how netstat behaved and I just never noticed<\/p>\n<pre>\r\n netstat -n |grep FIN_WAIT_1 |head -n 5\r\ntcp4       0  24606 10.10.10.169.80      199.224.118.221.62975  FIN_WAIT_1\r\ntcp4       0   3579 10.10.10.169.80      74.12.184.206.1323     FIN_WAIT_1\r\ntcp4       0    533 10.10.10.169.80      173.175.47.91.57498    FIN_WAIT_1\r\ntcp4       0    533 10.10.10.169.80      173.175.47.91.57497    FIN_WAIT_1\r\ntcp4       0    533 10.10.10.169.80      173.175.47.91.57496    FIN_WAIT_1<\/pre>\n<p>Nope<\/p>\n<p>OK so now to account for this we&#8217;ve passed the bash variable in and accounted for it in the search, in the print section I&#8217;d normally have x as its my variable for the for loop but x was returning a blank field<\/p>\n<pre>connstates=( FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT ESTABLISHED SYN_RCVD LAST_ACK); for a in ${connstates[@]};  do netstat -n |awk '($6 = \/'\"${a}\"'\/){freq['\"${a}\"']++} END {for (x in freq) {print freq[x], x, \"\\n\" }}' |sort -nr;done\r\n2513  \r\n\r\n167  \r\n\r\n789  \r\n\r\n128  \r\n\r\n213  \r\n\r\n6<\/pre>\n<p>this is no good so I try passing the bash variable like I did in the search<\/p>\n<pre>connstates=( FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT ESTABLISHED SYN_RCVD LAST_ACK); for a in ${connstates[@]};  do netstat -n |awk '($6 = \/'\"${a}\"'\/){freq['\"${a}\"']++} END {for (x in freq) {print freq[x], '\"${a}\"', \"\\n\" }}' |sort -nr;done\r\n2495  \r\n\r\n230  \r\n\r\n892  \r\n\r\n149  \r\n\r\n124  \r\n\r\n6<\/pre>\n<p>Same thing, then it hit me maybe its not being passed as a string so I added double quotes<\/p>\n<pre>connstates=( FIN_WAIT_1 FIN_WAIT_2 TIME_WAIT ESTABLISHED SYN_RCVD LAST_ACK); for a in ${connstates[@]};  do netstat -n |awk '($6 = \/'\"${a}\"'\/){freq['\"${a}\"']++} END {for (x in freq) {print freq[x], \"'\"${a}\"'\", \"\\n\" }}' |sort -nr;done\r\n2563 FIN_WAIT_1 \r\n\r\n194 FIN_WAIT_2 \r\n\r\n901 TIME_WAIT \r\n\r\n108 ESTABLISHED \r\n\r\n158 SYN_RCVD \r\n\r\n5 LAST_ACK<\/pre>\n<p>Perfect<\/p>\n<p>The next one I hadn&#8217;t had to deal with previously on linux was FreeBSD doesn&#8217;t use : for the port associated with the IP address, it uses a .<br \/>\nI used gsub to clean up my formatting<\/p>\n<p>bash variable is looking for one of my states, I know I want the 5th field so I print and pipe the output and want to strip the port so I use gsub, now the 1st gsub adds an additional period at the end of the address so I add a 2nd gsub and I look for any period at the end of the line and remove it and then print the output<\/p>\n<pre>netstat -n |awk '($6 = \/'\"${a}\"'\/){print $5}'|awk -F \\. '{$5 = \"\"; gsub(\/\\ \/, \".\"); gsub(\/.$\/,\"\"); print}'<\/pre>\n<p>I hope you&#8217;ve found this useful and find ways to expand and modify it for your needs<\/p>\n<div class=\"sharelinks\" style=\"\">\n\t<a title=\"awk &#8211; Who is connected, how many connections do they have and what is their connection state\" rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/www.facebook.com\/sharer.php?u=http:\/\/themadindian.wolfdendesign.com\/?p=69\"><img loading=\"lazy\" decoding=\"async\" width=\"56\" height=\"20\" alt=\"facebook\" src=\"http:\/\/themadindian.wolfdendesign.com\/wp-content\/plugins\/facebook-google-twitter-share\/images\/facebook.png\"><\/a>\n\t<a title=\"awk &#8211; Who is connected, how many connections do they have and what is their connection state\" rel=\"nofollow\" target=\"_blank\" href=\"https:\/\/plus.google.com\/share?url=http:\/\/themadindian.wolfdendesign.com\/?p=69\"><img loading=\"lazy\" decoding=\"async\" width=\"60\" height=\"20\" alt=\"google plus\" src=\"http:\/\/themadindian.wolfdendesign.com\/wp-content\/plugins\/facebook-google-twitter-share\/images\/googleplus.png\"><\/a>\n\t<a title=\"awk &#8211; Who is connected, how many connections do they have and what is their connection state\" Nagar\" rel=\"nofollow\" target=\"_blank\" href=\"http:\/\/twitter.com\/home?status=awk &#8211; Who is connected, how many connections do they have and what is their connection state http:\/\/themadindian.wolfdendesign.com\/?p=69\"><img loading=\"lazy\" decoding=\"async\" width=\"70\" height=\"20\" alt=\"twitter\" src=\"http:\/\/themadindian.wolfdendesign.com\/wp-content\/plugins\/facebook-google-twitter-share\/images\/twitter.png\"><\/a>\n\t<\/div>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been busy tuning Varnish to prevent outages and that makes me take a much closer look at things and how to get through things quickly especially when it comes to working with farms of servers. I noticed in my &hellip; <a href=\"http:\/\/themadindian.wolfdendesign.com\/?p=69\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,5],"tags":[],"class_list":["post-69","post","type-post","status-publish","format-standard","hentry","category-awk","category-freebsd"],"_links":{"self":[{"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/posts\/69","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=69"}],"version-history":[{"count":5,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions"}],"predecessor-version":[{"id":74,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=\/wp\/v2\/posts\/69\/revisions\/74"}],"wp:attachment":[{"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=69"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=69"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/themadindian.wolfdendesign.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=69"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}