How to output the prstat into table and send through email?

Hi how to send below prstat script output into table format and send through email?

prstat|awk '{ if ($9 >= 5) print $0 }'

What exactly do you mean by "table format"?

Mean like this prstat output tranform into table looks having borders.

This is absolutely vague. A bit more care in specifying would NOT harm. Try

prstat |
awk     'NR==1  {COLS=NF
                 for (i=1; i<=COLS; i++) printf "+-------"
                 printf "+\n"
                }
                {gsub(/[         ]+/, "\t|"); print "|"$0"\t|"
                }
         END    {for (i=1; i<=COLS; i++) printf "+-------"
                 printf "+\n"
                }
        '
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+
|PID    |USERNA |USR    |SYS    |TRP    |TFL    |DFL    |LCK    |SLP    |LAT    |VCX    |ICX    |SCL    |SIG    |PROCESS/LWP    |
|1      |root   |0.0    |0.0    |-      |-      |-      |-      |100    |-      |0      |0      |0      |0      |init/1 |
|102    |root   |0.0    |0.0    |-      |-      |-      |-      |100    |-      |0      |0      |3      |0      |in.rdisc/1     |
|250    |root   |0.0    |0.0    |-      |-      |-      |-      |100    |-      |0      |0      |0      |0      |utmpd/1        |
|1185   |john   |0.0    |0.0    |-      |-      |-      |-      |100    |-      |0      |0      |0      |0      |csh/1  |
|240    |root   |0.0    |0.0    |-      |-      |-      |-      |100    |-      |0      |0      |0      |0      |powerd/4       |
|TOTAL: |71,    |load   |averages:      |0.02,  |0.04,  |0.08   |
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

Hi RUdic,
Thanks for the reply. Sorry for less details. but return output like above for below filter where I only want to capture the process which use more than 5% CPU usage.

 prstat|awk '{ if ($9 >= 5) print $0 }'

You say that you want to capture processes that use more than 5% of your system's available CPU power and then you show us code that captures processes that are sleeping (not using any CPU) 5% or more of the time.

Do you not understand that sleeping at least 5% of the time does not mean that that process is using 5% of your CPU. And, in particular, if the value in field 9 (sleeping) is 100 (which is greater than 5), it isn't using any CPU at all?

Are we also to assume that you want the headings removed from the table that RudiC's code provided. "SLP" when converted to a number in awk is 0 (which is less than 5)?

If you were to change the line:

                {gsub(/[         ]+/, "\t|"); print "|"$0"\t|"

in RudiC's code to:

         ($9 + 0) <= 95 {
                        gsub(/[         ]+/, "\t|"); print "|"$0"\t|"
                }

would that give you something you could use?

Of course, if you are running this on a Solaris/SunOS system, you need to use /usr/xpg4/bin/awk or nawk instead of awk (unless /usr/xpg4/bin comes before /usr/bin in $PATH ).

And, as was noted in the other thread you started, basing any decision on one sample of prstat output is dangerous.

Hi Don Cragun,
Sorry for my mistake. When I do prstat in my SUN server the output as below:
PID|Username|Size|RSS|State|PRI|NICE|TIME||CPU|Process/NLWP

So when I need to capture CPU(field 9) usage more than 5%. I do below query.

 
 prstat 5 1|awk '{ if ($9 >= 1) print $0}
 

So how can I produce the output of above code in table format(with border) as what shown by Rudic and send it through email?

Please show us the output you got from RudiC's script when you ran it on your system (using CODE tags).

Here is a slightly modified version of RudiC's script:

prstat 5 1|nawk 'NR==1  {COLS=NF; for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }
$9 >= 0.1 { for (i=1; i<COLS; i++) printf("|%8s",$i);printf("|%20s|\n",$i)}
END {for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }'

and its output:

+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+
|     PID|USERNAME|    SIZE|     RSS|   STATE|     PRI|    NICE|    TIME|     CPU|        PROCESS/NLWP|
|       5|    root|      0K|      0K|   sleep|      99|     -20| 2:10:13|    0.3%|     zpool-rpool1/37|
|   10204|jlliagre|   9184K|   3128K|    cpu0|      49|       0| 0:00:00|    0.2%|            prstat/1|
|    9978|jlliagre|     10M|   3088K|   sleep|      49|       0| 0:00:00|    0.1%|             ksh93/1|
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+

Note that I set the threshold to 0.1 percent to test it.

Note also that this command is unlikely to help identifying what processes are using 75% of the swap space. You should monitor different columns and commands as stated in Capture PRSTAT Post: 302934556

Hi Jiliagre,
Thanks for the reply.
When i send the output to the email, the border is not consistent as below.
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+
| PID|USERNAME| SIZE| RSS| STATE| PRI| NICE| TIME| CPU| PROCESS/NLWP|
| 13434| oracle| 12G| 10G| sleep| 9| 0| 0:00:06| 2.6%| oracle/1|
| 13452| oracle| 12G| 10G| sleep| 1| 0| 0:00:05| 2.4%| oracle/1|
| 466| oracle| 3279M| 94M| sleep| 24| 0|1072:17:4| 2.2%| tnslsnr/1|
| 5529| oracle| 2104K| 1472K| sleep| 59| 2| 0:03:59| 1.8%| tar/1|
| 5789| oracle| 12G| 10G| sleep| 54| 0| 1:22:10| 0.9%| oracle/17|
| 13422| oracle| 12G| 10G| sleep| 60| 0| 0:00:02| 0.9%| oracle/1|
| 13412|sprdcron| 12G| 10G| sleep| 60| 19| 0:00:01| 0.5%| oracle/1|
| 13216| oracle| 12G| 10G| sleep| 52| 0| 0:00:06| 0.4%| oracle/1|
| 4699| oracle| 12G| 2745M| sleep| 1| 0|373:25:52| 0.4%| oracle/18|
| 1114| root| 98M| 91M| sleep| 59| 0|592:35:53| 0.3%| esd/4|
| 13467| oracle| 12G| 10G| sleep| 60| 0| 0:00:00| 0.3%| oracle/1|
| 13471| oracle| 12G| 10G| sleep| 52| 0| 0:00:00| 0.3%| oracle/1|
| 4697| oracle| 12G| 2738M| sleep| 58| 0|291:53:50| 0.3%| oracle/258|
| 14908| oracle| 12G| 10G| sleep| 52| 0| 0:12:57| 0.3%| oracle/1|
| 1121| oracle| 12G| 10G| sleep| 54| 0|15:31:42| 0.2%| oracle/11|
| Total:| 449|processes,| 1774| lwps,| load|averages:| 2.29,| 2.17,| 2.13|
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+

Is that possible to get nicer output Jiliagre?

Is that an HTML mail? Then use HMTL table format.
Is that a text mail? Then use a monospace font to print it.

Hi RudiC,
Thanks for your reply. I thinkthat is HTML email. So kindly explain how send output to email using HTML table format?

How do you send the output to the email?

Hi Jlliagre,
Thanks for the reply. I send the output to email like below

prstat 5 1|nawk 'NR==1  {COLS=NF; for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }
$9 >= 0.1 { for (i=1; i<COLS; i++) printf("|%8s",$i);printf("|%20s|\n",$i)}
END {for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }'| mailx -s "Testing" xxxxxx@yyyy.com.my

This should work:

cat <<%EOF% | /usr/lib/sendmail -t
MIME-Version: 1.0
From: root@domain.com
To: xxxxxx@yyyy.com.my
Subject: Testing
Content-Type: multipart/mixed; boundary=Boundary_00000
Content-Transfer-Encoding: 7bit
--Boundary_00000
Content-Type: text/html

Status:
<pre>
$(
prstat 5 1|nawk 'NR==1  {COLS=NF; for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }
$9 >= 0.1 { for (i=1; i<COLS; i++) printf("|%8s",$i);printf("|%20s|\n",$i)}
END {for (i=1; i<=COLS; i++) printf "+--------"; printf "------------+\n" }'
)
</pre>
%EOF%

Hi Jilliagre,
Thanks for the reply. I try use your above code but get Variable syntax error. Kindly need your help..

If we knew where that error occurs, we might be able to help.

For your table, you might want to try

prstat 5 1 |
awk     'BEGIN  {printf "<html><body><table width=\"900\" border=\"3\" align=\"center\">\n"}
                {printf "<tr>"; for (i=1; i<=NF; i++) printf "<td>%s</td>", $i; printf "</tr>\n" }
         END    {printf "</table></body></html>\n"}
        ' 
1 Like

You should run this under ksh or bash.