AIX

AIX

Connect with fellow AIX users and experts to gain knowledge, share insights, and solve problems.


#Power
#Power
 View Only
  • 1.  Output to console differs from output to file (> filename)

    Posted Mon November 01, 2010 01:01 PM

    Originally posted by: SystemAdmin


    I am having a strange issue. I have an awk script that analyzes netstat output. When I run the script it formats it perfectly to the screen. However when I > it into a file some of the lines are reorganized.

    The following is the section of code that does the printing. I have simplified the script to make it easier to see and prevent from having to publish IP addresses in the full output. I have also attached the full script as an attachment if that would help, however even the shortened script has the issue:
    
    
    { print 
    "Local port totals"; printf 
    "%8s %8s %10s %10s\n", 
    "Port", 
    "Count", 
    "Recv-Q", 
    "Send-Q"; sort = 
    "sort -rn"; 
    
    for (i in local_count) 
    { 
    
    if (local_count[i] > 1) 
    { split(i,g,
    " "); printf 
    "%8s %8s %10s %10s\n", g[1], local_count[i], local_recvq[i], local_sendq[i] | sort; 
    } 
    } close(sort);   print 
    "\nState totals"; printf 
    "%16s %8s %10s %10s\n", 
    "State", 
    "Count", 
    "Recv-Q", 
    "Send-Q"; sort = 
    "sort"; 
    
    for (i in state_count) 
    { 
    
    if (state_count[i] > 1) 
    { split(i,g,
    " "); printf 
    "%16s %8s %10s %10s\n", g[3], state_count[i], state_recvq[i], state_sendq[i] | sort; 
    } 
    } close(sort);   print 
    "\nLocal port by state"; printf 
    "%8s %16s %8s %10s %10s\n", 
    "Port", 
    "State", 
    "Count", 
    "Recv-Q", 
    "Send-Q"; sort = 
    "sort -rn"; 
    
    for (i in local_state_count) 
    { 
    
    if (local_state_count[i] > 1) 
    { split(i,g,
    " "); printf 
    "%8s %16s %8s %10s %10s\n", g[1], g[3], local_state_count[i], local_state_recvq[i], local_state_sendq[i] | sort; 
    } 
    } close(sort); 
    }
    


    Here is the output on the screen when i run netstat -an | netstat_analyze.awk
    
    Local port totals Port    Count     Recv-Q     Send-Q 50000       99          0          0 47001        2          0          0 22        4          0         52   State totals State    Count     Recv-Q     Send-Q LISTEN       32          0          0 ESTABLISHED      108          0         52   Local port by state Port            State    Count     Recv-Q     Send-Q 50000      ESTABLISHED       99          0          0 47001      ESTABLISHED        2          0          0 22      ESTABLISHED        4          0         52
    


    Here is the output in the netstat_report file when i run netstat -an | netstat_analyze.awk > netstat_report:
    
    50000       99          0          0 47001        2          0          0 22        4          0         52 LISTEN       32          0          0 ESTABLISHED      108          0         52 50000      ESTABLISHED       99          0          0 47001      ESTABLISHED        2          0          0 22      ESTABLISHED        4          0         52 Local port totals Port    Count     Recv-Q     Send-Q   State totals State    Count     Recv-Q     Send-Q   Local port by state Port            State    Count     Recv-Q     Send-Q
    


    If I change the code and pipe and close each individual header line through sort it outputs to a file correctly but that is a hack and extremely inefficient. I have tried embedding the netstat -an and the awk command into the same script and I see the same thing. As a sanity check of the script I tried it on a linux machine and it works just fine, although obviously that doesn't solve anything.

    I thought > just redirected the output and didn't do anything that would change it but I am guessing that is not the case.

    Any help would be appreciated.
    #AIX-Forum


  • 2.  Re: Output to console differs from output to file (> filename)

    Posted Mon November 01, 2010 01:58 PM

    Originally posted by: SystemAdmin


    When I pipe to tee both the console output and the file have the lines moved around.
    #AIX-Forum


  • 3.  Re: Output to console differs from output to file (> filename)

    Posted Mon November 01, 2010 06:40 PM

    Originally posted by: SystemAdmin


    Hi,

    There are some interesting ideas in your script. I have never seen sort used that way in an awk program, so you have taught me something new. I like the way you close the open sort file handles after use.

    I think that you have been caught out by awk trying to outguess the programmer. In many cases UNIX utilities work differently when they are sending the output to a terminal than when they are sending to a file. I think that this is an example of awk attempting to improve the efficiency of your sorts; and getting it wrong.

    Perl would have been a better option.

    Regards,
    George
    #AIX-Forum


  • 4.  Re: Output to console differs from output to file (> filename)

    Posted Tue November 02, 2010 10:16 AM

    Originally posted by: SystemAdmin


    Perl would certainly handle this but this is an ideal use of awk, except the sorts i guess. I have done this before with other things without issue. I may just do the sorts after populating all the arrays. Then I can just print everything at the end.

    I tried sorting the individual lines above in the table headers and that works. However, I can't get myself to write a script that calls sort 12 times just to sort a single line and avoid the issue.
    #AIX-Forum


  • 5.  Re: Output to console differs from output to file (> filename)

    Posted Tue November 02, 2010 12:38 PM

    Originally posted by: SystemAdmin


    It looks like it is buffering output. This is a hack but by issuing: system(""); after my non sorted print lines it now outputs correctly to a file.

    
    print 
    "Local port totals"; printf 
    "%8s %8s %10s %10s\n", 
    "Port", 
    "Count", 
    "Recv-Q", 
    "Send-Q"; system(
    "") sort = 
    "sort -rn"; 
    
    for (i in local_count) 
    { 
    
    if (local_count[i] > 1) 
    { split(i,g,
    " "); printf 
    "%8s %8s %10s %10s\n", g[1], local_count[i], local_recvq[i], local_sendq[i] | sort; 
    } 
    } close(sort);
    

    #AIX-Forum