#
#  Make a System Activity Graph
#
#  This will be a little confusing to read...
#  it contains quotes of 'lout' code embedded within an 'awk' script;
#  they both use lots of braces.
#

#
#  Write the 'lout' statements to setup the graph
#
( echo "@SysInclude {graph}
@SysInclude {doc}
@Document
  @InitialFont {Helvetica Base 10p}
  @PageHeaders {None}
//
@Text @Begin
@Display  90d @Rotate  @Graph
  width  {8 in}
  height {5.5 in}
  abovecaption {System Activity Graph: User, System, and Idle Percentages}
  belowcaption {Ten minute period starting on `date`}
  leftcaption {90d @Rotate {Percent of System Used}}
  hidecaptions {no}
  xticks { 0@ 1@ 2@ 3@ 4@ 5@ 6@ 7@ 8@ 9@ 10@ }
  yextra {0}
  xextra {0}"

#
#  run "vmstat" to get system data
#  read the data lines; store selected data in arrays
#  at end of the data, generate the remainder of the 'lout' statements
#

vmstat -n 10 60 | awk '

BEGIN {
   n = 0          # initialize data-point counter
}

{
   if ( $0 ~ /[a-zA-Z]/ )     # skip over header lines
      next

   # save selected data values
   user[n] = $13        # user %
   syst[n] = $14        # system %
   idle[n] = $15        # idle %
   n++
}


#
#  we have all the values
#  generate data statements for lout
#

function gen_data(array, dot, line) {

   printf("@Data ")          # start a data group
   if ( dot != "" )
      printf("points { %s } ", dot)      # set data-point style
   printf("pairs { %s } {\n", line)      # x/y pairs, line-style
   for ( i = 0; i < n; i++ ) {         # write the data values
      printf("%f   %d\n", i/6.0, array[i])
   }
   print "}"              # end this data group
}

END {
   # print the identifier at the end of each plot
   print "objects {"
   printf(" @E at {%d %d} {User}\n",   (n+2)/6.0, user[n-1])
   printf(" @E at {%d %d} {System}\n", (n+2)/6.0, syst[n-1])
   printf(" @E at {%d %d} {Idle}\n",   (n+2)/6.0, idle[n-1])
   print "}"

   # now generate the data body
   print "{ "
   gen_data(user, "filledcircle", "solid")
   gen_data(syst, "", "dashed")
   gen_data(idle, "", "dotted");
   print "}"

   # and finish-up the document
   print "@End @Text"
}' ) | lout -s -