Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

URGENT - CPU usage string help

Original Message
Name: damiike
Date: May 6, 2007 at 21:56:03 Pacific
Subject: URGENT - CPU usage string help
OS: Unix
CPU/Ram: 1g
Model/Manufacturer: 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.


Report Offensive Message For Removal


Response Number 1
Name: Razor2.3
Date: May 6, 2007 at 22:56:09 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
Try editing your AWK to this:

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


Report Offensive Follow Up For Removal

Response Number 2
Name: damiike
Date: May 7, 2007 at 00:38:17 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Razor2.3
Date: May 7, 2007 at 03:05:05 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
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.



Report Offensive Follow Up For Removal

Response Number 4
Name: damiike
Date: May 7, 2007 at 05:42:21 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 5
Name: nails
Date: May 7, 2007 at 13:44:31 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
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...



Report Offensive Follow Up For Removal


Response Number 6
Name: damiike
Date: May 7, 2007 at 18:53:32 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
That fixed my issue, thanks Guys

Report Offensive Follow Up For Removal

Response Number 7
Name: ghostdog
Date: May 7, 2007 at 19:30:53 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
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]

Report Offensive Follow Up For Removal

Response Number 8
Name: damiike
Date: May 7, 2007 at 20:45:42 Pacific
Subject: URGENT - CPU usage string help
Reply: (edit)
thanks ghostdog, much easier than the bc code.

Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: URGENT - CPU usage string help

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




how to setup call of duty to joytok

WindowsME / HotMail Problem

Corrupt memory

Convert fat32 to Ntfs

Best WinMo phone of 2008


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC