Computing.Net > Forums > Unix > URGENT - CPU usage string help

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

URGENT - CPU usage string help

Reply to Message Icon

Name: damiike
Date: May 6, 2007 at 21:56:03 Pacific
OS: Unix
CPU/Ram: 1g
Product: custom
Comment:

Hi,

I am having an issue with a sctipt i am creating for monitoring of CPU usage.

To retreive the usage for a certain PID i am using the following:

getCPU=`prstat -p $PID 1 1 | awk '{print $9}' | cut -d'%' -f1 | cut -d',' -f1`

the output from the command is the following:

CPU
0.0
0.09

is there a way that i can select the 0.0 value(the second row) as this is the CPU percentage used.

or is there a better command i can use to isolate this value, as i need the value to be an integer so that i can use a compare statement.

All help is greatly appreciated.



Sponsored Link
Ads by Google

Response Number 1
Name: Razor2.3
Date: May 6, 2007 at 22:56:09 Pacific
Reply:

Try editing your AWK to this:

awk 'NR == 2 {print $9}'


0

Response Number 2
Name: damiike
Date: May 7, 2007 at 00:38:17 Pacific
Reply:

Hi,

This resolved the isse of retreiving the cpu usage number,

however with the if statements i am trying to use i get the following issue,

my updated statement is the following:
CPU=`/usr/bin/prstat -p $PID 1 1 | /usr/bin/awk 'NR == 2 {print $9}' | /usr/bin/cut -d'%' -f1`

1. this if statement is ignored and the script runs through regardsless of the if statement (ie with a value of cpu usage of 0.4):

if [ $CPU > 40 ]

2. this if statement gives the following error message and does not get into the if statement.

if [ $CPU -gt 40 ]

./monit.sh: [: 0.0: integer expression expected

all help is greatly appreciated on this issue,

regards,

miike


0

Response Number 3
Name: Razor2.3
Date: May 7, 2007 at 03:05:05 Pacific
Reply:

Disclaimer: I can't test this reply because my version of UNIX doesn't have prstat, so I don't even know what its output looks like.

Try this pipe:
prstat -p $PID 1 1 | awk '{print $9}' | cut -d'.' -f1 | cut -d',' -f1

Edit: Spelling.



0

Response Number 4
Name: damiike
Date: May 7, 2007 at 05:42:21 Pacific
Reply:

Hi,

The output is:

0.4

the problem is the comparison, i've tried -gt and >, however when i use -gt it comes up with the error, and when i use > it goes into the if statement regardless of it being smaller in the comparison,

is there any way to compare the output (0.4) to make sure it only goes into the if statement when greater than 40,

cheers


0

Response Number 5
Name: nails
Date: May 7, 2007 at 13:44:31 Pacific
Reply:

Your problem is that the bourne shell, sh, and the '88 version of korn shell does not do decimal type arithmetic. The bc command, arbitrary precision command, is one way of solving this problem.

Go to this link for a bc example using an if statement:

http://daresler.net/info/shell-scri...



0

Related Posts

See More



Response Number 6
Name: damiike
Date: May 7, 2007 at 18:53:32 Pacific
Reply:

That fixed my issue, thanks Guys


0

Response Number 7
Name: ghostdog
Date: May 7, 2007 at 19:30:53 Pacific
Reply:

you can do everything in awk, no need for bc
[code]
#!/bin/sh
/usr/bin/prstat -p 13633 1 1 | nawk '
NR == 2 {
sub("%", "",$9)
print $9
if ($9 <= 40) {
print "lower"
}
else if ( $9 > 40 ) {
print "higher"
}
}'
[/code]


0

Response Number 8
Name: damiike
Date: May 7, 2007 at 20:45:42 Pacific
Reply:

thanks ghostdog, much easier than the bc code.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: URGENT - CPU usage string help

unix process with recent cpu usage www.computing.net/answers/unix/unix-process-with-recent-cpu-usage/4892.html

CPU usage calculation www.computing.net/answers/unix/cpu-usage-calculation/3545.html

CPU Usage : vmstat, prstat... www.computing.net/answers/unix/cpu-usage-vmstat-prstat/3848.html