If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > Unix Script to get the CPU statistics

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-05-06, 11:04
pintu005 pintu005 is offline
Registered User
 
Join Date: Oct 2006
Posts: 7
Unix Script to get the CPU statistics

Hello Experts,

I have to give the statistics of CPU every week. I am using the following:
date
ps -eo "user, pid, stime, pcpu, pmem, etime,args"|sort -nr|egrep -e 'informat'|grep -v egrep;|cut -c1-75

This scripts runs every 15 minutes and appends the output in a log file. which looks something like:

Wed Oct 4 00:15:00 CUT 2006
informat 67808 A 0.0 0.0 16-23:16:33 pmserver pmserver_PMETACDT.cfg
informat 66606 A 0.0 0.0 16-23:16:53 pmserver pmserver_PMETASRC.cfg
informat 60330 A 0.0 0.0 16-23:16:10 pmserver pmserver_PMETASCOR.cfg
informat 60068 A 0.0 0.0 16-23:17:03 pmserver pmserver_PMETASHD.cfg
informat 59158 A 0.0 0.0 16-17:21:49 pmbwserver INFO_SPVC_CRP %PM_USER
informat 58328 A 0.0 0.0 16-23:18:48 pmrepagent Config/PMETASHD-es.cfg
informat 57932 A 0.0 0.0 00:00 /bin/ksh /informatica/pc/test.ksh
informat 54596 A 0.5 20.0 01:08:21 pmdtm -s BWLOAD:WF_SEDW_BW_VEND_A
informat 49590 A 0.0 0.0 16-23:17:15 pmserver pmserver_PMETAGLS.cfg
informat 39850 A 0.0 0.0 16-23:16:22 pmserver pmserver_PMETASPVC.cfg
informat 39260 A 0.0 0.0 16-23:19:04 pmrepagent Config/PMETAGLS-es.cfg
informat 36056 A 0.0 0.0 16-17:21:55 pmbwserver INFO_SEDW_CRP %PM_USER
informat 32896 A 0.0 0.0 16-23:21:55 pmrepserver pmrepserver.cfg
informat 31904 A 0.0 0.0 00:00 ps -eo user, pid, stime, pcpu, pm
informat 30580 A 0.0 0.0 16-23:19:22 pmrepagent Config/PMETACDT-es.cfg
informat 29596 A 0.0 0.0 16-17:22:00 pmbwserver BWINFORMAT_CRP %PM_USE
informat 26646 A 0.1 0.0 16-23:18:30 pmrepagent Config/PMETASRC-es.cfg
informat 7572 A 9.5 0.0 00:58 pmdtm -s ODS:WF_STG_MOSAP_SLS.s_m

Now I want the out put in an excel file with the addition of PCPU and PCMEM for that particular time.For example ;

Date process name Total_cpu_used Date process name Total memory used
oct 04 2006 00.15 Informat 10.1 oct 04 2006 00.15 Informat 20

Can any one please help me in doing this....

Thanks in advance..

~Himansu
Reply With Quote
  #2 (permalink)  
Old 10-06-06, 07:02
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
Code:
while : 
do
  ps -u informat -o "user,pid,stime,pcpu,pmem,etime,args" | tail +2 | cut -c1-75 | sort -nr |  while read line
  do
    printf '%s\n' "$(date '+%b %d %Y %H.%M') $line" >>logfile
  done
  sleep 900
done
Reply With Quote
  #3 (permalink)  
Old 10-06-06, 10:22
pintu005 pintu005 is offline
Registered User
 
Join Date: Oct 2006
Posts: 7
Red face I am still getting multiple values

Hi,

Thanks for looking into it. But still I am getting multiple values:

Oct 06 2006 13.57 informat 7987428 A 0.0 0.0 00:00 sh test.sh
Oct 06 2006 13.57 informat 7864382 A 0.0 0.0 30-10:42:26 pmrepagent Config/DMETAGLS-es.c
Oct 06 2006 13.57 informat 7651448 A 0.0 0.0 00:00 ps -u informat -o user,pid,stim
Oct 06 2006 13.57 informat 7553272 A 0.0 0.0 23:23:52 pmdtm -s CET_MAIN:wf_CET_LISTEN
Oct 06 2006 13.57 informat 7192660 A 0.0 0.0 30-10:41:04 pmrepagent Config/DMETASRC-es.c
Oct 06 2006 13.57 informat 7151840 A 0.0 0.0 22:24:17 pmdtm -s CET_MAIN:wf_CET_LISTEN
Oct 06 2006 13.57 informat 7127090 A 0.0 0.0 13-22:11:53 pmbwserver INFO_SEDW_CRD %PM_US
Oct 06 2006 13.57 informat 6930604 A 0.0 0.0 30-10:44:03 pmrepserver pmrepserver_ETD.cfg
Oct 06 2006 13.57 informat 6848626 A 0.1 1.0 30-10:42:42 pmrepagent Config/DMETACDT-es.c
Oct 06 2006 13.57 informat 6824064 A 0.0 0.0 25-01:28:02 pmserver pmserver_SMETASND.cfg
Oct 06 2006 13.57 informat 6709374 A 0.0 0.0 30-10:37:45 pmserver pmserver_DMETAGLS.cfg
Oct 06 2006 13.57 informat 6692892 A 0.0 0.0 06:04 ftpd
Oct 06 2006 13.57 informat 6504612 A 0.0 0.0 00:00 cut -c1-75
Oct 06 2006 13.57 informat 6471746 A 0.0 0.0 00:00 sort -nr
Oct 06 2006 13.57 informat 6348844 A 0.0 0.0 01:28 -csh
Oct 06 2006 13.57 informat 6308048 A 0.0 0.0 00:00 tail +2
Oct 06 2006 13.57 informat 6045948 A 0.0 0.0 16-19:38:25 pmserver pmserver_DMETASPVC.cfg
Oct 06 2006 13.57 informat 5603582 A 0.0 0.0 13-22:12:36 pmserver pmserver_DMETACDT.cfg
Oct 06 2006 13.57 informat 5349574 A 0.0 0.0 90-09:28:01 pmrepagent Config/SMETASND-es.c
Oct 06 2006 13.57 informat 5128366 A 0.0 0.0 90-10:16:29 pmrepserver pmrepserver_ETS.cfg
Oct 06 2006 13.57 informat 4784162 A 0.0 0.0 13-22:11:42 pmbwserver INFO_SPVC_CRD %PM_US
Oct 06 2006 13.57 informat 4743194 A 0.0 0.0 30-09:54:06 pmserver pmserver_DMETASCOR.cfg
Oct 06 2006 13.57 informat 4137018 A 0.0 0.0 30-10:37:22 pmserver pmserver_DMETASHD.cfg
Oct 06 2006 13.57 informat 3883256 A 0.0 0.0 30-10:36:26 pmserver pmserver_DMETASRC.cfg
Oct 06 2006 13.57 informat 3768504 A 0.0 0.0 13-22:11:47 pmbwserver BWINFORMAT_CRD %PM_U
Oct 06 2006 13.57 informat 3211382 A 0.0 0.0 30-10:42:04 pmrepagent Config/DMETASHD-es.c
Oct 06 2006 13.57 informat 1261588 A 0.0 0.0 13-22:11:37 pmbwserver INFO_SCOR_CRD %PM_US

I need the out put as:

Oct 06 2006 13.57 informat 0.1 1.0

Thanks
Himansu
Reply With Quote
  #4 (permalink)  
Old 10-09-06, 02:35
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
OK so you want to sum the usage of all the user's processes
Note however that ps gives you a snapshot at the time of running the command and not the average since the last run.
i.e. if you take a snapshot at 10:00 when cpu usage is 0%
and between 10:01 and 10:13 you had a process using 100% cpu
your snapshot at 10:15 will again report 0% usage.
$ pcpu=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
$ pmem=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
$ echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"
Reply With Quote
  #5 (permalink)  
Old 11-02-06, 11:20
pintu005 pintu005 is offline
Registered User
 
Join Date: Oct 2006
Posts: 7
Hi

Thanks! But I am still facing some problems.I am executing the following in AIX:
ps -u informat -o "user,pid,stime,pcpu,pmem,etime,args" | tail +2 | cut -c1-75 | sort -nr |
$pcpu=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l),$pmem=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
printf '%s\n' "$(date '+%b %d %Y %H.%M') $line" >>logfile
$echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"

It is doing the summation but the $pcpu $pmem are not getting appended into the logfile.The following message is getting displayed on my screen:
[288] % sh test1.sh
test1.sh[2]: =0.1,=0: not found.
test1.sh[4]: Nov 02 2006 16.18 informat : not found.

Thanks
Himansu






;














;
Reply With Quote
  #6 (permalink)  
Old 11-03-06, 01:17
pdreyer pdreyer is offline
Registered User
 
Join Date: May 2005
Location: South Africa
Posts: 1,268
OK lets analyze this command
Quote:
Originally Posted by pintu005
$echo "$(date '+%b %d %Y %H.%M') informat $pcpu $pmem"
The shell substitute all the variables
Assuming the variable $echo is not set it will translate to ''
next the date command $(....) will return a date 'Nov 02...' , etc,etc
Now the shell try and execute the line that start with Nov
i.e. trying to run the command Nov... that doesn't exist
Hence the error: not found
Remove the $ in front of echo (or replace with the more portable printf)

Quote:
Originally Posted by pintu005
$pcpu $pmem are not getting appended into the logfile
You did not include them in your 'printf....>>logfile' command. Change to
printf '%s\n' "$(date '+%b %d %Y %H.%M') $pcpu $pmem" >>logfile

You need to drop the initial $ in front of the commands that I previously posted (post #4). I copied and pasted from a terminal session where the $ is the command prompt. Sorry for the confusion.

PS. I did not test on AIX but it should work there.

Last edited by pdreyer; 11-03-06 at 01:22.
Reply With Quote
  #7 (permalink)  
Old 11-03-06, 09:10
pintu005 pintu005 is offline
Registered User
 
Join Date: Oct 2006
Posts: 7
Hi

Thanks! So much. It's working now.
Reply With Quote
  #8 (permalink)  
Old 12-20-06, 16:02
pintu005 pintu005 is offline
Registered User
 
Join Date: Oct 2006
Posts: 7
Sorry for troubling again. I have modified the script as no I have to give stats for some more users. The script is
***********
#!/bin/sh
pcpuinformat=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmeminformat=$((ps -u informat -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpudb2eod=$((ps -u db2eod -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemdb2eod=$((ps -u db2eod -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpudb2emd=$((ps -u db2emd -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemdb2emd=$((ps -u db2emd -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcputrillium=$((ps -u trillium -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemtrillium=$((ps -u trillium -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpuinfouser=$((ps -u infouser -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmeminfouser=$((ps -u infouser -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcputriluser=$((ps -u triluser -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemtriluser=$((ps -u triluser -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

pcpuroot=$((ps -u root -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)
pmemroot=$((ps -u root -o pmem | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l)

echo "$(date '+%b %d %Y %H.%M') informat $pcpuinformat $pmeminformat db2eod $pcpudb2eod $pmemdb2eod db2emd $pcpudb2emd $pmemdb2emd trillium $pcputrillium $pmemtrillium infouser $pcpuinfouser $pmeminfouser triluser $pcputriluser $pmemtriluser root $pcpuroot $pmemroot" ## >> /informatica/pc/logfile1

***********
i have scheduled this script in crontab.Now I am getting syntax error in the output file.Below is the output file.

**********************************
Dec 20 2006 20.26 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.27 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.28 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.29 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
Dec 20 2006 20.30 informat 0.2 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.31 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.32 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.33 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.34 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.35 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
Dec 20 2006 20.36 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 1.0
Dec 20 2006 20.37 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.38 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.39 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
syntax error on line 1 stdin
Dec 20 2006 20.40 informat 0.2 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 1.0
Dec 20 2006 20.41 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.42 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.43 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
Dec 20 2006 20.44 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
syntax error on line 1 stdin
Dec 20 2006 20.45 informat 0.2 0 db2eod 0 db2emd 0.1 trillium 0 0 infouser 0 0 triluser 0 0 root
syntax error on line 1 stdin
Dec 20 2006 20.46 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0
Dec 20 2006 20.47 informat 0.2 0 db2eod 0.1 0 db2emd 0.1 0 trillium 0 0 infouser 0 0 triluser 0 0 root 81.0 1.0

**********************************
If we analyze the output, we will see whenever it is not finding any value for any user like for example it's not finding a value for pcpuformat or pmeminformat or pcpuroot or pmemroot.....it's throwing a syntax error.

Please advice,how can I avoid this.

Thanks.
Waiting for your sugestion.
Reply With Quote
  #9 (permalink)  
Old 12-21-06, 08:28
Tyveleyn Tyveleyn is offline
Registered User
 
Join Date: Aug 2006
Location: The Netherlands
Posts: 248
Try:
Code:
pcpuinformat=$((ps -u informat -o pcpu | tail +2 | tr "\n" "+" ; echo 0 ) | bc -l 2>/dev/null) etc...

Last edited by Tyveleyn; 12-21-06 at 08:42.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On