Make netstat human readable?

Is there any way to make netstat output the information in a more human readable format? even if it's not exact? I don't even care if it has to round up/down to the nearest Meg to make it work.

I wind up having to stare at netstat running for while and I wish I could get it to output things in a format like 45M instead of 47395923 or other rediculously long numbers that I can't read at a glance.

Why would netstat be showing this info? what options/flags are you using with it? By default netstat shows listening ports and hosts/clients.

Also, what OS are you running ?

Running freebsd with bash.

Mostly i'm using it to watch overall throughput through the interfaces.

So normally all i'm doing is.

netstat -i 1 1

So i'll leave netstat running for a very long time and then scroll back through history looking for spikes in traffic and such trying to determine if the system is pushing too much traffic. If I could get it to display in more human readable numbers it would make life easier.

You could pipe it through awk or something to get human readable, paste some of your output here and we can help you.

Typical output would be something like this.

[/root]$ netstat -i 8 1
            input        (Total)           output
   packets  errs      bytes    packets  errs      bytes colls
     25658     0   19898065      25886     0   20063331     0
     29173     0   22438813      29419     0   22575686     0
     40981     0   33243787      41257     0   33420057     0
     35487     0   29289359      35752     0   29563170     0
     29735     0   23562050      30018     0   23781997     0
     32260     0   25572250      32489     0   25792588     0
     30569     0   23045738      30844     0   23257035     0

---------- Post updated at 03:50 PM ---------- Previous update was at 02:15 PM ----------

Ok so I've figured out how to do some math using Awk and I can get it to do the math on the columns that I want... Question now is how to do the math to get the output to be a nice clean easy to read number.
So now I need to convert 41408832 bytes into megabytes with no decimal places.

---------- Post updated at 03:56 PM ---------- Previous update was at 03:50 PM ----------

netstat -i 1 1 | awk '{print $3/1024/1024}'

ok so now I just gotta figure out how to get rid of the decimal places.

Try printf("%d", thing_I_want_to_have_no_decimals);

The man page, on FreeBSD 8.x, references the -h flag:

netstat -i . . . If -h is also present, print all counters in human
readable form.

Afraid my version of freebsd is old and i don't have -h

FreeBSD 5.3-RELEASE #0

---------- Post updated 03-03-11 at 07:41 PM ---------- Previous update was 03-02-11 at 10:05 PM ----------

Ok I've gotten most everything working how i want but now i'm having difficulty getting my if/else working. Any suggestions on what i'm doing wrong here. I'm trying to get awk to print two different results depending on how big the number is.

netstat -i 1 1 | awk '($3 > 100000) {print "\033[1;31m" $3/1024/1024*8 } else {print "\033[6�;30m" $3/1024/1024*8}'

Here is my output when I try.

awk: syntax error at source line 1
 context is
    ($3 > 100000) {print "\033[1;31m" $3/1024/1024*8 } >>>  else <<<  {print "\033[6;30m" $3/1024/1024*8}
    missing ]
awk: bailing out at source line 1

You are missing an if statement.

netstat -i 1 1 | awk '{ if($3 > 100000) {print "\033[1;31m" $3/1024/1024*8 } else {print "\033[6�;30m" $3/1024/1024*8}}'

DOH! god how could I miss something that stupid???? do you know how long I stared at that and didn't see that????

---------- Post updated at 10:09 AM ---------- Previous update was at 08:39 AM ----------

Ok so my final command looks like this and I'm really liking it. Lets me set how much load I want to watch for and then changes the output color to red anytime it is a heavier load than what I set it for.

echo "How many Megs" ; read gload ; gload2=`echo $gload | awk '{print $1*1024*1024/8}'` ; netstat -i 1 1 | awk '{ if($3 < '$gload2' ) {print "\033[1;0m" $3/1024/1024*8, $6/1024/1024*8} else {print "\033[1;31m" $3/1024/1024*8, $6/1024/1024*8}}'

The last minor thing that I can't seem to get rid of is the stupid 0's that show up. Since its doing math and every 20 or so lines it prints the column heads again.... errs errs coll etc.. I can't seem to get those to not show up. So i'll get these kind of results.

45.4351 45.513
47.8142 47.8741
50.3284 50.3137
51.098 51.1384
0 0
0 0
50.4882 50.5795
43.9978 43.9685
43.9704 44.1034
44.715 45.1511

Only slightly annoying but ideally i'd like to get those zero's to not show up.

---------- Post updated at 10:26 AM ---------- Previous update was at 10:09 AM ----------

I've tried doing a simple grep -v errs yet for some reason that breaks the whole command and i'm not certain why.

---------- Post updated at 01:44 PM ---------- Previous update was at 10:26 AM ----------

HA! got rid of the stupid zero's by adding

/[1-9]/

to my awk statement. That way if the line only has zero's it wont print.

---------- Post updated at 01:53 PM ---------- Previous update was at 01:44 PM ----------

Is there any way to get awk to append a timestamp to an output line?

My command is coming along smashingly however if I leave this running for long periods of time unattended its going to spew out tons of data and with no time stamp if the box goes over the load I'll have no idea when that actually occurred and therefor no time frame to investigate.

---------- Post updated at 02:18 PM ---------- Previous update was at 01:53 PM ----------

added

 ; x="'"`date +%T`"'"; print  x 

---------- Post updated at 03:28 PM ---------- Previous update was at 02:18 PM ----------

Well I jumped the gun on my timestamp. It puts the timestamp in there but its not current. Its only the time when I started the command, it doesn't refresh.