Listing 2. cpu Graphing Script
1 #!/bin/bash
2 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
3 # $Header: /var/log/sar/RCS/cpu,v 1.4 1997/06/19 04:31:05 dgavin Exp dgavin $
5 # cpu - display cpu utilization
7 # input - /var/log/sar/data/cpu.Mondd.yy
8 # output- chart showing time spent in user/sys/nice states
10 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
11 #
12 trap '/bin/rm /tmp/gnuplot.cpu.* 2>/dev/null;exit' SIGHUP SIGINT SIGQUIT SIGTERM
13 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
15 # Function definitions
17 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
18 function checkForEmpty {
19 if [ ! -s ${1} ] ; then
20 echo "0 0" >${1}
21 fi
22 return
23 }
24 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
26 # Mainline code
28 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
32 # Variable initialization
33 #
35 HARDCOPY=""
36 GHOSTVIEW=""
37 CMDNAME=`basename $0`
39 HELP="${CMDNAME} generates X11 or hardcopy
graph of cpu utilization."
40 USAGE="${CMDNAME} [-DHLVhPp] [MonDD[.YY]]
41 -D turns on execution tracing
42 -H displays help info and exits
43 -L displays RCS log and exits
44 -V display Revision info and exits
45 -h displays help info and exits
46 -P Prints without display
47 -p displays and offers print option
49 MonDD.YY - day to work with, defaults to
today
50 i.e. Jun12.97 -the year will default to
this year
52 "
54 #-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#
56 # Parse options
59 #
60 while getopts DHLVhPp c ; do
61 case ${c} in
62 h|H) echo -e "\n${HELP}\n\n${USAGE}\n"
63 exit 1
64 ;;
65 p) HARDCOPY="YES"
66 GHOSTVIEW="YES"
67 ;;
68 P) HARDCOPY="YES"
69 GHOSTVIEW=""
70 ;;
71 D) echo "DEBUG ON"
72 set -x
73 DEBUG="yes"
74 trap '' SIGHUP SIGINT SIGQUIT SIGTERM
75 ;;
76 L) echo '
77 $Log: $
78 '
79 exit 1
80 ;;
81 V) echo '$Revision: $'
82 exit 1
83 ;;
84 ?) echo -e "\n${USAGE}\n"
85 exit 3
86 ;;
87 esac
88 done
89 # re-align the positional parameters as $1,
$2,...
90 shift `expr ${OPTIND} - 1`
92 cd /var/log/sar/data
93 if [ $# -eq 0 ] ; then
94 FN="cpu.`date +%b%d.%y`"
95 else
96 l=`echo ${1} | wc -c`
97 l=`expr ${l} + 0`
98 case ${l} in
99 6) FN="cpu.${1}.`date +%y`";;
100 9) FN="cpu.${1}";;
101 *) echo -e "\n${CMDNAME}: Invalid date -
${1}\n"
102 exit
103 ;;
104 esac
105 fi
106 if [ ! -f ${FN} ] ; then
107 echo "No such file: ${FN}"
108 exit
109 fi
110 if [ ! -r ${FN} ] ; then
111 echo "File not readable: ${FN}"
112 exit
113 fi
114 if [ ! -s ${FN} ] ; then
115 echo "File empty: ${FN}"
116 exit
117 fi
118 #
119 # user nice system idle
120 #0940 8065221 3719924 2017086 53289599
121 awk '
122 BEGIN{}
123 {
124 if (NR == 1) {
125 old_user = $2;
126 old_nice = $3;
127 old_sys = $4;
128 old_idle = $5;
129 } else {
130 mm=substr($1,3,2) * .016;
131 tim=substr($1,1,2)+mm;
132 if ( old_user > $2 ) {
133 user = 0;
134 nice = 0;
135 sys = 0;
136 idle = 0;
137 reboot = 100;
138 } else {
139 user = $2 - old_user;
140 nice = $3 - old_nice;
141 sys = $4 - old_sys;
142 idle = $5 - old_idle;
143 reboot = -1;
144 }
145 total = user + nice + sys + idle ;
146 if ( user == 0 ) { puser = 0; } else {
puser= (user / total) * 100;}
147 if ( sys == 0 ) { psys = 0; } else {
psys = (sys / total) * 100;}
148 if ( nice == 0 ) { pnice = 0; } else {
pnice= (nice / total) * 100;}
149 if ( idle == 0 ) { pidle = 0; } else {
pidle= (idle / total) * 100;}
150
151 printf("%5.2f %7.2f %7.2f %7.2f %7.2f
%d\n",
152 tim, pidle+puser+psys+pnice,
153 puser+psys+pnice, psys+pnice,
pnice,reboot) >"/tmp/gnuplot.cpu";
155 old_user = $2;
156 old_nice = $3;
157 old_sys = $4;
158 old_idle = $5;
159 }
160 }
161 END{}' ${FN}
162 #
163 # Get system name and date of report into
variables...
164 system=`hostname -s | /usr/bin/tr
"[A-Z]" "[a-z]"`
165 sysdate=`echo ${FN} | cut -c 5-`
166 /bin/echo "Generating plot for $system
$sysdate"
167 #
168 # Set gnuplot variables as needed
169 #
170 if [ -z ${HARDCOPY} ]; then
171 TERMINAL="set terminal X11"
172 PAUSE="pause 10"
173 YLABEL=""
174 else
175 TERMINAL='set terminal postscript
landscape monochrome dashed "Times-Roman" 18
176 set output "/tmp/gnuplot.cpu.ps"'
177 PAUSE=""
178 YLABEL='set ylabel "Nice|Sys|User|Idle"'
179 fi
180 #
181 # Generate the plots
182 #
183 gnuplot <<EOF
184 ${TERMINAL}
185 set data style lines
186 set nogrid
187 set key
188 set title "CPU Activity - $system On
$sysdate"
189 set xlabel "Time of Day"
190 set xrange [00:24]
191 set xtics 0,1,24
192 ${YLABEL}
193 set yrange [0:140]
194 set ytics 0,10,100
195 plot \
196 "/tmp/gnuplot.cpu" using 1:2 title "idle"
with line 1, \
197 "/tmp/gnuplot.cpu" using 1:3 title "user"
with line 3, \
198 "/tmp/gnuplot.cpu" using 1:4 title "sys"
with line 2, \
199 "/tmp/gnuplot.cpu" using 1:5 title "nice"
with line 4, \
200 "/tmp/gnuplot.cpu" using 1:6 title "reboot"
with points 2
201 ${PAUSE}
202 quit
203 EOF
204 #
205 if [ .${HARDCOPY} = ".YES" ]; then
206 if [ -z ${GHOSTVIEW} ] ; then
207 lpr /tmp/gnuplot.cpu.ps
208 else
209 ghostview -upsidedown -landscape
-magstep 0 /tmp/gnuplot.ps
210 echo "Print ?"
211 read ans
212 if [ ${ans} = "y" ] ; then
213 lpr /tmp/gnuplot.ps
214 fi
215 fi
216 rm /tmp/gnuplot.cpu.ps 2>/dev/null
217 fi
218 # clean up the junk
219 if [ ${DEBUG}. = "." ]; then
220 rm /tmp/gnuplot.cpu 2>/dev/null
221 fi