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

**URL:** <https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807>\
**Category:** Solaris\
**Created:** [March 1, 2015, 8:16am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807 "2015-03-01T08:16:55Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 1, 2015, 8:16am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/1 "2015-03-01T08:16:55Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2015, 8:41am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/2 "2015-03-01T08:41:03Z")

</div>

What exactly do you mean by "table format"?

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 1, 2015, 9:14am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/3 "2015-03-01T09:14:08Z")

</div>

> [@rudic](#):
>
> What exactly do you mean by "table format"?

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2015, 11:55am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/4 "2015-03-01T11:55:05Z")

</div>

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

```nohighlight
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 |
+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+-------+

```

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 1, 2015, 8:19pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/5 "2015-03-01T20:19:58Z")

</div>

> [@rudic](#):
>
> This is absolutely vague. A bit more care in specifying would NOT harm. Try
> 
> ```plaintext
> 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.

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

```

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 1, 2015, 10:19pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/6 "2015-03-01T22:19:35Z")

</div>

> [@tharmendran](#):
>
> 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.
> 
> ```plaintext
> 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:

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

```

in RudiC's code to:

```plaintext
         ($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.

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 2, 2015, 1:17am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/7 "2015-03-02T01:17:31Z")

</div>

> [@don cragun](#):
>
> 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:
> 
> ```plaintext
> {gsub(/[]+/, "\t|"); print "|"$0"\t|"
> 
> ```
> 
> in RudiC's code to:
> 
> ```plaintext
> ($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.

```plaintext
 
 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?

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 2, 2015, 1:54am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/8 "2015-03-02T01:54:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [March 2, 2015, 2:31am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/9 "2015-03-02T02:31:20Z")

</div>

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

```plaintext
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:

```plaintext
+--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+
| 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](http://www.unix.com/302934556-post9.html)

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 7, 2015, 10:24am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/10 "2015-03-07T10:24:18Z")

</div>

> [@jlliagre](#):
>
> Here is a slightly modified version of RudiC's script:
> 
> ```plaintext
> 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:
> 
> ```plaintext
> +--------+--------+--------+--------+--------+--------+--------+--------+--------+--------------------+
> | 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](http://www.unix.com/302934556-post9.html)

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?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 7, 2015, 1:34pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/11 "2015-03-07T13:34:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 7, 2015, 10:21pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/12 "2015-03-07T22:21:56Z")

</div>

> [@rudic](#):
>
> 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?

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [March 9, 2015, 4:29pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/13 "2015-03-09T16:29:24Z")

</div>

> [@tharmendran](#):
>
> When i send the output to the email, the border is not consistent as below.

How do you send the output to the email?

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 10, 2015, 3:36am UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/14 "2015-03-10T03:36:01Z")

</div>

> [@jlliagre](#):
>
> How do you send the output to the email?

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

```plaintext
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

```

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [March 15, 2015, 12:49pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/15 "2015-03-15T12:49:24Z")

</div>

This should work:

```nohighlight
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%

```

---

<div class="post-metadata">

**Author:** ![tharmendran](https://community.unix.com/letter_avatar/tharmendran/32/5_5575768a8748004e209b776fc1b2916d.png) [@tharmendran](https://community.unix.com/u/tharmendran)\
**Post date:** [March 15, 2015, 1:46pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/16 "2015-03-15T13:46:32Z")

</div>

> [@jlliagre](#):
>
> This should work:
> 
> ```plaintext
> 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..

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 15, 2015, 2:34pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/17 "2015-03-15T14:34:23Z")

</div>

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

For your table, you might want to try

```nohighlight
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"}
        ' 

```

---

<div class="post-metadata">

**Author:** ![jlliagre](https://community.unix.com/letter_avatar/jlliagre/32/5_5575768a8748004e209b776fc1b2916d.png) [@jlliagre](https://community.unix.com/u/jlliagre)\
**Post date:** [March 15, 2015, 3:04pm UTC](https://community.unix.com/t/how-to-output-the-prstat-into-table-and-send-through-email/350807/18 "2015-03-15T15:04:43Z")

</div>

> [@tharmendran](#):
>
> I try use your above code but get Variable syntax error.

You should run this under ksh or bash.
