Shell script to calculate the max cpu usage from the main script


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell script to calculate the max cpu usage from the main script
# 1  
Linux Shell script to calculate the max cpu usage from the main script

Hi All,
I have a script which does report the cpu usuage, there are few output parameter/fields displayed from the script. My problem is I have monitor the output and decide
which cpu number (column 2) has maximum value (column 6).
Since the output is displayed/updated every seconds, it's very difficult to capture the result.
Is there a way, we can write a seperate script, which read the output and display the cpu number and it's cpu usuage value(max) and output that data ?
script need to calculate/report the cpu and cpu value for every 1 sec set data
Main script :
Code:
PATH=/usr/bin:/usr/sbin:$PATH
export PATH
if [ "$1" != "" ] ;then
[ $1 -gt 0 ] && TIME=$1
else
TIME=1
fi
LOG_BASE=${LOG_BASE:-/usr/logs/}
HOST=`hostname`
[ -d $LOG_BASE/cap ] || mkdir -p $LOG_BASE/cap
if [ ! -f $LOG_BASE/cap/mpstat_${HOST}_`date '+%Y%m%d'`.csv ];then
echo "Date Time,CPU ID,User,System,Wait,CPU_Use" \
> $LOG_BASE/cap/mpstat_${HOST}_`date '+%Y%m%d'`.csv
fi
mpstat $TIME | while read r
do
echo "`date '+%Y/%m/%d %H:%M:%S'` $r" |nawk '$3 !~ /CPU/ \
{printf \
"%s %s,%d,%d,%d,%d,%d\n", \
$1,$2,$3,$15,$16,$17,100-$18 \
}' >> $LOG_BASE/cap/mpstat_${HOST}_`date '+%Y%m%d'`.csv
done

Main script output :
Code:
2013/08/09 09:44:09,0,1,2,0,3
2013/08/09 09:44:09,1,0,0,0,0
2013/08/09 09:44:09,2,0,2,0,2
2013/08/09 09:44:09,3,0,1,0,1
2013/08/09 09:44:09,4,0,1,0,1
2013/08/09 09:44:09,5,1,1,0,2
2013/08/09 09:44:09,6,0,1,0,1
2013/08/09 09:44:09,7,0,1,0,1
2013/08/09 09:44:10,0,1,1,0,2
2013/08/09 09:44:10,1,0,0,0,0
2013/08/09 09:44:10,2,1,1,0,2
2013/08/09 09:44:10,3,1,2,0,3
2013/08/09 09:44:10,4,0,0,0,0
2013/08/09 09:44:10,5,0,1,0,1
2013/08/09 09:44:10,6,0,0,0,0
2013/08/09 09:44:10,7,0,0,0,0
2013/08/09 09:44:11,0,1,1,0,2
2013/08/09 09:44:11,1,0,0,0,0
2013/08/09 09:44:11,2,0,2,0,2
2013/08/09 09:44:11,3,1,1,0,2
2013/08/09 09:44:11,4,0,0,0,0
2013/08/09 09:44:11,5,0,1,0,1
2013/08/09 09:44:11,6,0,0,0,0
2013/08/09 09:44:11,7,1,1,0,2

In the above output, there are 6 columns ie,
Date Time,CPU ID,User,System,Wait,CPU_Use
2013/08/09 09:44:09,0,1,2,0,3

from the above output, need to monitor which cpu(column 2 : 0-7 cpu) has utilized more say,
for 2013/08/09 09:44:09
cpu 0 has max cpu usuage value = 3
cpu 2 has max cpu usuage value = 2
for 2013/08/09 09:44:10
cpu 3 has max cpu usuage value = 3
cpu 2 has max cpu usuage value = 2
for 2013/08/09 09:44:11
cpu 2 has max cpu usuage value = 2
cpu 2 has max cpu usuage value = 2
Can any help me to sort this out please ?

Many Thanks
# 2  
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #869
Difficulty: Medium
Fortran introduced the concept of automatic garbage collection.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with speeding up my working script to take less time - how to use more CPU usage for a script

Hello experts, we have input files with 700K lines each (one generated for every hour). and we need to convert them as below and move them to another directory once. Sample INPUT:- # cat test1 1559205600000,8474,NormalizedPortInfo,PctDiscards,0.0,Interface,BG-CTA-AX1.test.com,Vl111... (7 Replies)
Discussion started by: prvnrk
7 Replies

2. Shell Programming and Scripting

Help with Shell script that monitors CPU Usage

I'm a newbie to shell scripting, I was given this script to modify. This script that monitors when CPU Usage is too high based off the top command. The comparison is not working as it should. Its comparing a decimal to a regualar interger. When it send me an email, it send an email and ignores the... (21 Replies)
Discussion started by: mhannor
21 Replies

3. Shell Programming and Scripting

Script to record max memory usage of program

Hello, I am working on an application that uses allot of memory depending on the input. I am also working on more than one processing algorithm. The program has a long processing time (hours) so it would be nice to be able to monitor the maximum memory footprint of the application during runs... (1 Reply)
Discussion started by: LMHmedchem
1 Replies

4. Shell Programming and Scripting

Script for CPU usage -Linux

Hi all I was wondering if its possible to write a script to keep CPU usage at 90%-95%? for a single cpu linux server? I have a perl script I run on servers with multple cpu's and all I do is max all but one cpu to get into the 90'% utilised area. I now need a script that raises the CPU to... (4 Replies)
Discussion started by: sudobash
4 Replies

5. Shell Programming and Scripting

CPU usage script

Hello Friends, I am trying to create a shell script which will check the CPU utilization. I use command top to check the %CPU usage. It give s me below output Cpu states: CPU LOAD USER NICE SYS IDLE BLOCK SWAIT INTR SSYS 0 0.31 9.6% 0.0% 6.1% 84.3% 0.0% 0.0%... (3 Replies)
Discussion started by: Nakul_sh
3 Replies

6. Shell Programming and Scripting

Shell script for logging cpu and memory usage of a Linux process

I am looking for a way to log and graphically display cpu and RAM usage of linux processes over time. Since I couldn't find a simple tool to so (I tried zabbix and munin but installation failed) I started writing a shell script to do so The script file parses the output of top command through... (2 Replies)
Discussion started by: andy_dufresne
2 Replies

7. Linux

How to calculate total CPU usage from SAR report?

Hi, I want to calculate the total cpu usage from the sar report. Say for example, Linux 2.6.24-21-generic (blade10) 09/10/2012 04:54:36 PM CPU %user %nice %system %iowait %steal %idle 04:54:37 PM all 0.00 0.00 0.00 0.00 ... (1 Reply)
Discussion started by: rohitmd
1 Replies

8. Shell Programming and Scripting

shell script to alert cpu memory and disk usage help please

Hi all can any one help me to script monitoring CPU load avg when reaches threshold value and disk usage if it exceeds some % tried using awk but when df -h out put is in two different lines awk doesnt work for the particular output in two different line ( output for df -h is in two... (7 Replies)
Discussion started by: robo
7 Replies

9. Shell Programming and Scripting

Help with bash script - Need to get CPU usage as a percentage

I'm writing a bash script to log some selections from a sensors output (core temp, mb temp, etc.) and I would also like to have the current cpu usage as a percentage. I have no idea how to go about getting it in a form that a bash script can use. For example, I would simply look in the output of... (3 Replies)
Discussion started by: graysky
3 Replies

10. Shell Programming and Scripting

How to calculate Memory and CPU Usage on AIX

I have written a script that will calculate CPU usage and Memory usage of a particular process on AIX Unix. I know the PID and I am using the command -- # for CPU Usage MSG_CPU_USG=`ps uax | grep ${PID} | awk {'print $3'}` #for Memory Usage MSG_MEM_USG=`ps uax | grep ${PID} | awk {'print... (0 Replies)
Discussion started by: asutoshch
0 Replies

Featured Tech Videos