How to report CPU usage using Glance
The following article describes how to use Glance to create a report of the CPU utilization by process ID.
Contents
Create report configuration file
# cat > /tmp/cpu-report.cfg << EOF DATA TYPE PROCESS DATE TIME PROC_PROC_ID PROC_PROC_NAME PROC_CPU_TOTAL_UTIL PROC_CPU_TOTAL_UTIL_CUM PROC_CPU_TOTAL_TIME_CUM CPU_TOTAL_UTIL EOF
Create extract report
Create a report between know and the previous day
# extract -p -xp -r /tmp/cpu-report.cfg -f /tmp/cpu-report.out,purge -b TODAY-1 -e LAST
Writing PROCESS data to file /tmp/cpu-report.out
Create a report between a time range
extract -p -xp -r /tmp/cpu-report.cfg -f /tmp/cpu-report.out,purge \
-b "08/24/09 06:00" -e "08/24/09 08:00"
Example Report
# cat /tmp/cpu-report.out
Process CPU CPU
Date Time PID Name CPU % % Cum Time Cum
...
04/28/2009 17:37:00 24860 gzip 58.35 60.49 36.01
04/28/2009 17:37:00 1457 mxdomainmgr 0.06 0.99 1032.23
04/28/2009 17:37:00 24859 pax 1.10 2.03 1.21
04/28/2009 17:37:00 24528 list_expander 9.06 6.41 7.68
04/28/2009 17:38:00 24860 gzip 80.57 70.97 84.85
04/28/2009 17:38:00 1457 mxdomainmgr 0.00 0.99 1032.60
04/28/2009 17:38:00 24859 pax 2.17 2.32 2.78
04/28/2009 17:39:00 24860 gzip 91.49 78.58 140.55
04/28/2009 17:39:00 1457 mxdomainmgr 0.00 0.99 1032.90
04/28/2009 17:39:00 24859 pax 1.10 2.20 3.95
04/28/2009 17:40:00 24860 gzip 90.37 81.82 195.76
04/28/2009 17:40:00 1457 mxdomainmgr 0.00 0.99 1033.21
04/28/2009 17:40:00 24859 pax 1.08 2.10 5.04
04/28/2009 17:41:00 66 vxfsd 0.00 0.01 59.19
04/28/2009 17:41:00 24860 gzip 82.80 82.34 247.05
04/28/2009 17:41:00 1457 mxdomainmgr 0.00 0.99 1033.53
04/28/2009 17:41:00 24859 pax 3.17 2.42 7.27
04/28/2009 17:42:00 66 vxfsd 0.00 0.01 59.24
04/28/2009 17:42:00 24860 gzip 56.52 78.23 281.29
04/28/2009 17:42:00 24862 sh 24.90 5.87 21.12
04/28/2009 17:42:00 1457 mxdomainmgr 0.00 0.99 1033.88
...
Note that the report have one minute time frame for the processes. The colums shows the average CPU usage, the Cumulative CPU usage and the Cumulative Time of the running process.
Sorting Report
To quickly check what processes are taking the most cpu time, the report can be order by the key colum, for example, for the CPU % column (5), you can use:
( head -n 2 cpu-report.out ; tail -n $(expr $(cat cpu-report.out | wc -l) - 2) cpu-report.out | \
sort -nrk 5,5 ) | head -n 100
For example:
# ( head -n 2 cpu-report.out ; \
> tail -n $(expr $(cat cpu-report.out | wc -l) - 2) cpu-report.out | \
> sort -nrk 5,5 ) | head -n 100
Process CPU CPU
Date Time PID Name CPU % % Cum Time Cum
06/04/2009 00:21:00 20538 oraclegpopa 72.18 60.74 437.56
06/04/2009 02:34:01 22688 oraclegpopc 71.68 59.19 178.10
06/04/2009 00:29:00 20538 oraclegpopa 71.19 63.20 758.71
06/04/2009 00:23:00 20538 oraclegpopa 71.15 61.96 520.67
06/04/2009 00:31:00 20538 oraclegpopa 70.18 63.46 837.46
06/04/2009 00:34:00 20538 oraclegpopa 70.17 64.12 962.30
06/04/2009 00:18:00 20538 oraclegpopa 69.18 59.07 318.82
06/05/2009 00:44:00 29812 oraclegpopa 69.17 62.56 1051.41
06/05/2009 02:38:00 6194 oraclegpopc 69.15 50.19 60.25
06/05/2009 02:34:00 23592 oraclegpopc 68.39 57.73 174.05
06/04/2009 00:36:00 20538 oraclegpopa 68.18 64.27 1041.23
06/04/2009 00:37:00 20538 oraclegpopa 68.17 64.45 1082.82
06/04/2009 00:33:00 20538 oraclegpopa 68.17 63.83 919.49
06/05/2009 00:24:00 29812 oraclegpopa 68.15 61.27 294.54
06/04/2009 02:16:00 14544 gzip 67.21 48.95 58.53
06/04/2009 02:14:00 27748 oraclegpop1 67.21 58.52 175.50
06/05/2009 00:46:00 29812 oraclegpopa 67.19 62.25 1120.61
06/05/2009 00:34:00 29812 oraclegpopa 67.18 62.00 670.04
06/04/2009 00:27:00 20538 oraclegpopa 67.17 62.56 675.97
06/04/2009 00:22:00 20538 oraclegpopa 66.28 61.24 477.87
06/04/2009 00:02:00 12548 oraclegpopa 66.19 45.91 908.74
06/05/2009 00:32:00 29812 oraclegpopa 66.18 61.68 591.85
06/05/2009 02:41:00 6194 oraclegpopc 66.17 56.82 170.42
06/04/2009 00:32:00 20538 oraclegpopa 66.17 63.62 877.61
06/04/2009 00:38:00 20538 oraclegpopa 66.16 64.53 1122.71
06/04/2009 00:26:00 20538 oraclegpopa 66.15 62.23 634.56
06/05/2009 00:27:00 29812 oraclegpopa 66.14 61.85 408.26
06/05/2009 00:26:00 29812 oraclegpopa 66.14 61.34 368.12
06/04/2009 22:53:00 24603 symparser 65.70 0.79 564.02
06/05/2009 02:33:00 23592 oraclegpopc 65.50 54.68 131.31
06/04/2009 00:24:00 20538 oraclegpopa 65.18 62.20 559.80
06/04/2009 00:13:00 20538 oraclegpopa 65.17 56.56 136.06
06/05/2009 00:37:00 29812 oraclegpopa 65.15 62.04 781.88
06/05/2009 00:23:00 29812 oraclegpopa 65.15 60.18 252.51
06/04/2009 00:28:00 20538 oraclegpopa 65.15 62.74 715.38
06/04/2009 02:13:00 27748 oraclegpop1 64.22 56.16 134.73
06/05/2009 00:42:00 29812 oraclegpopa 64.19 62.28 972.09
06/05/2009 02:14:00 28008 oraclegpop1 64.18 56.17 168.38
06/05/2009 00:14:00 20972 oraclegpopa 64.18 46.03 1104.91
06/04/2009 00:04:00 12548 oraclegpopa 64.18 46.70 981.02
06/04/2009 02:31:01 22688 oraclegpopc 64.17 50.87 61.79
06/04/2009 00:11:00 20538 oraclegpopa 64.17 49.73 59.94
06/05/2009 00:19:00 29812 oraclegpopa 64.14 56.97 102.72
06/04/2009 00:19:00 20538 oraclegpopa 64.14 59.75 358.09
06/05/2009 00:41:00 29812 oraclegpopa 63.19 62.17 933.07
06/05/2009 00:38:00 29812 oraclegpopa 63.17 62.14 820.59
06/04/2009 00:16:00 20538 oraclegpopa 63.17 57.24 240.59
06/05/2009 00:22:00 29812 oraclegpopa 63.16 59.19 212.99
06/05/2009 00:21:00 29812 oraclegpopa 63.16 58.21 174.68
06/05/2009 00:29:00 29812 oraclegpopa 63.14 61.95 483.28
Report CPU process Hogs
Once you have identified the major CPU process hogs, the next step consist in extracting from the consolidated report those samples that contains information of the process. The following script to report only the oraclegp* processes. The awk code snip on the for loop extract only process matching the pattern and remove duplicated names before creating the process reports:
#! /usr/bin/bash
printf "Process name pattern: "
read PATTERN
for process in `grep ${PATTERN} cpu-report.out | sort -k 4,4 | \
awk 'BEGIN {PROC="foo"} {if ($4 != PROC) {printf "%s\n", $4; PROC = $4 }}'`
do
# Extract process reports
echo "Extracting $process..."
/usr/xpg4/bin/tail -n $(expr $(cat cpu-report.out | wc -l) - 2) cpu-report.out | \
grep $process | \
awk 'BEGIN {LINE=1; print "#Spl Date Time PID Process Name CPU % CPU % Cum CPU Time Cum"}
{ if (NF == 7) {
printf "%-6d %10s %8s %5d %-16s %5.2f %9.2f %13.2f\n", \
LINE, $1, $2, $3, $4, $5, $6, $7
LINE++
}
}' > $process.out
Chart process hoags
#! /usr/bin/bash
printf "Process name pattern: "
read PATTERN
for process in `grep ${PATTERN} cpu-report.out | sort -k 4,4 | \
awk 'BEGIN {PROC="foo"} {if ($4 != PROC) {printf "%s\n", $4; PROC = $4 }}'`
do
# Extract process reports
echo "Extracting $process..."
/usr/xpg4/bin/tail -n $(expr $(cat cpu-report.out | wc -l) - 2) cpu-report.out | \
grep $process | \
awk 'BEGIN {LINE=1; print "#Spl Date Time PID Process Name CPU % CPU % Cum CPU Time Cum"}
{ if (NF == 7) {
printf "%-6d %10s %8s %5d %-16s %5.2f %9.2f %13.2f\n", \
LINE, $1, $2, $3, $4, $5, $6, $7
LINE++
}
}' > $process.out
# Gnuplot chart creation (only CPU %)
cat > process.plt<<EOF
set terminal png
set size 1,1
set xlabel "Samples"
set ylabel "% CPU"
set yrange [0:120]
set output "$process.png"
plot "$process.out" using 1:6 title "$process CPU usage" with lines
EOF
echo "Plotting $process.png."
/usr/sfw/bin/gnuplot process.plt 2> /dev/null
mv $process.out $process.png glance
done
rm *.plt
Creating CSV files
for process in `grep oraclegp cpu-report.out | sort -k 4,4 | \
awk 'BEGIN {PROC="foo"} {if ($4 != PROC) {printf "%s\n", $4; PROC = $4 }}'`
do
/usr/xpg4/bin/tail -n $(expr $(cat cpu-report.out | wc -l) - 2) cpu-report.out | \
grep $process | \
awk 'BEGIN {print "\"Date and Time\",\"PID\",\"Process Name\",\"CPU %\",\"CPU % Cum\",\"CPU Time Cum\""}
{printf "\"%s %s\",\"%6d\",\"%s\",\"%4.2f\",\"%4.2f\",\"%4.2f\"\n", \
$1, $2, $3, $4, $5, $6, $7}' | unix2dos > csv/$process.csv
done
More Scripts
for time in `sort -rnk 3,3 sarc.avg | head -n 21 | sort -nk 2,2 |
awk '{print substr($2, 1, 5)}'`
do
grep "$time" cpu-report.out
done | sort -rnk 5,5 | head -n 21