Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Average CPU utilization

Status
Not open for further replies.
Jul 4, 2003
19
US
I have a file called 24HourSummary.txt. I'm trying to create a script to stop at the SVR CPU utilization and add up all the 15 minute averages to come up with a daily average. Any help would be greatly appreciated. Portion of file is below.


SVR CPU utilization:
Device: System
Date/Time Avg Used
-------------- --------
00:15 27%
00:30 41%
00:45 21%
01:00 36%
01:15 39%
 
nawk -f avg.awk file.txt

#--------------------- avg.awk
/%/ {
num++;
sum+=substr($2,1,index($2,"%"));
}

END {
printf("avg->[%d]\n", sum/num);

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Vlad,
Thanks for the help. My other problem with this is that I need to do this only for SVR CPU utilization but it is embedded in a file with many other CPU and Memory utilizations so just searching for a % will not work well. Thanks for the help.
 
Refining vlad's code
Code:
#--------------------- avg.awk
BEGIN { state = 0 }

state == 0 && /SVR CPU utilization/ { state = 1 } # found the start

state == 1 && /%/ {
     num++;
     sum+=substr($2,1,index($2,&quot;%&quot;));
}

state == 1 && /???/ { exit }        # found the end, now do END

END {
   printf(&quot;avg->[%d]\n&quot;, sum/num);
}
You need to replace the ??? with whatever unique pattern in the file marks the end of the text you're interested in.
 
yeah, something like Salem's suggestion.

I'd be curious to see a sample file with the 'SVR CPU utilization' block in it among others. There might be a pattern separating blocks of data in your data stream.

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks Vlad and Salem. You guys are a real lifesaver. The file in question looks like below. I have tried to set up a script to pull out utilizations for the SVR CPU, WKS CPU, SVR memory, etc.. So far I always get the value produced in the first search put into all the other utilization searches. Any ideas on how to do more than one search in the same script?

SVR CPU utilization:
Device: System
Date/Time Avg Used
-------------- --------
00:15 27%
00:30 41%
00:45 21%
01:00 36%
01:15 39%
01:30 27%
01:45 34%
02:00 36%
02:15 41%
etc....
23:30 42%
23:45 29%
00:00 33%


WKS1 CPU utilization:
Device: System
Date/Time Avg Used
-------------- --------
00:15 0%
00:30 0%
00:45 0%
01:00 0%
01:15 0%
01:30 0%
01:45 0%
 
something like this should get you going:

# this will get you ALL the averages
nawk -f avg.awk file.txt

# this will get you an average of a perticular type:
# the value of 'find' is the 'header' of a particular block # in your file.txt
nawk -v find='WKS1 CPU utilization:' -f avg.awk file.txt


#---------------------- avg.awk
BEGIN {
FS=RS=&quot;&quot;
}

{
num=0;
for(i=2; i <= NF; i++) {
if ( $i ~ &quot;%&quot;) {
split($i,valA, &quot;[ ][ ]*&quot;);
num++
arr[$1]+=substr(valA[3],1,index(valA[3],&quot;%&quot;));
}
}
arr[$1]/=num
}


END {
if ( find != &quot;&quot; && find in arr)
printf(&quot;stat->[%s] avg->[%.2f]\n&quot;, find, arr[find]);
else
for ( i in arr)
printf(&quot;stat->[%s] avg->[%.2f]\n&quot;, i, arr);
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
or

#----------------------- avg.awk
BEGIN {
FS=RS=&quot;&quot;
}

{
num=0;
for(i=2; i <= NF; i++) {
if ( $i ~ &quot;%&quot;) {
gsub(&quot;^[ ]*|[ ]*$&quot;, &quot;&quot;,$i);
split($i,valA, &quot;[ ][ ]*&quot;);
num++
arr[$1]+=substr(valA[2],1,index(valA[2],&quot;%&quot;));
}
}
arr[$1]/=num
}


END {
if ( find in arr)
printf(&quot;stat->[%s] avg->[%.2f]\n&quot;, find, arr[find]);
else
for ( i in arr)
printf(&quot;stat->[%s] avg->[%.2f]\n&quot;, i, arr);
}


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks a million Vlad. You've solved a problem I've been trying to fix for weeks. I used to have to compile all those stats by hand but not anymore. Thanks also to Salem for the input.

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top