Path: utzoo!hoptoad!uunet!snorkelwacker!apple!brutus.cs.uiuc.edu!rpi!rpi.edu!rodney From: rodney@sun.ipl.rpi.edu (Rodney Peck II) Newsgroups: alt.sources Subject: pie -- generates system usage postscript charts Keywords: postscript perl accounting Message-ID: Date: 9 Nov 89 03:42:44 GMT Distribution: alt Organization: Rensselaer Polytechnic Institute Image Processing Lab, Troy NY Lines: 364 As those of you who saw the last thing I posted here might have guess already, I'm gathering system statistics for a network of suns in this lab. They want some cute graphics so I'm producing them. I thought maybe some other people might like this idea and would be able to do something with it and have some fun. The best part of this script is the part that figures out what the pie slice size cutoff will be. It does a (very) small amount of math that took me about five minutes to think up. It reduces to a single division operation and works perfectly. The program is in perl and it produces postscript. I hope you like it. If you have updates or patches, as always, mail them to me (rodney@ipl.rpi.edu) and I'll incorporate them in the next version. I am maintaining this under RCS so if you use RCS, please make yourself a branch off from version 1.4 for your changes. RCS is really nice. Here's the code: ---- #! /bin/sh # This is a shell archive. Remove anything before this line, then unpack # it by saving it into a file and typing "sh file". To overwrite existing # files, type "sh file -c". You can also feed this as standard input via # unshar, or by typing "sh 'pie' <<'END_OF_FILE' X#!/usr/local/bin/perl X# $Id: pie,v 1.4 89/11/08 22:29:54 rodney Rel $ X X# setup constants X$piesize = 150; X$printheader = 1; X$xpos = 306; X$ypos = 396; X$label = '<<>>'; X X$field{'process'} = 1; X$units{'process'} = ''; X$field{'cpu'} = 2; X$units{'cpu'} = 'mins'; X$field{'io'} = 3; X$units{'io'} = 'k'; X$field{'memory'} = 4; X$units{'memory'} = 'k*sec'; X$type = 'cpu'; X X#parse up the arguments Xwhile ($_=$ARGV[0],/^-/) { X shift; X if (/^-l/) { $label = shift;} X elsif (/^-x/) { $xpos = shift;} X elsif (/^-y/) { $ypos = shift;} X elsif (/^-s/) { $piesize = shift;} X elsif (/^-p/) { $printheader = 0;} X elsif (/^-f/) { $type = shift;} X} X Xif ($label eq '<<>>') { X $label = sprintf ("%s %s",$type, $units{$type});} Xif ($field{$type} == 0) {die "$0: sa -m has no \"$type\" field.\n";} X X# copy the postscript program to the output X$pieprogram = "piechart.ps"; Xopen pieprogram; Xprint "%!\n"; Xif ($printheader) { X while () { X if (/^%/) {} else X {print;} X } X} X X# basically, we want at least 6 points on the edge of a slice. X# this number was found by guessing and looking at charts that were nice X# since the side is the percentage times the distance around the whole, X# which is pi times the diameter, we get the percentage to give 6 points X# as follows: 6 / pi / diameter = percentage. since this is just a X# rough six, we'll pretend that pi is three and come up with 2/diameter. X# isn't math fun?! X$minpercent = 2 / $piesize; X X# read in the data X$total = 0; Xwhile (<>) { X split; X $num = $_[$field{$type}]; X if ($num < 0) {$num = $num + 4294967294;} X $value{$_[0]} = $num; X $total = $total + $num; X} X#print the label, and font sizes Xprintf ("(%s %d %s) %.4f %.4f\n[\n",$label,$total,$units{$type}, X $piesize/3,$piesize/7); X X#print the data back out formatted X$other = 0.0; Xforeach $key (keys(value)) { X $percent = $value{$key}/$total; X if ($percent < $minpercent) { X $other = $other + $value{$key}; X } X else { X printf ("[(%s %d %s) %f ]\n",$key,$value{$key},$units{$type},$percent); X } X} X X# print an "other" if we used it Xif ($other != 0.0) { X printf ("[(other %d %s) %f ]\n",$other,$units{$type},$other/$total); X} X X# and finally, the postscript closing Xprint "] $xpos $ypos $piesize DrawPieChart\n"; Xif ($printheader) {print "showpage\n";} X X END_OF_FILE if test 2297 -ne `wc -c <'pie'`; then echo shar: \"'pie'\" unpacked with wrong size! fi chmod +x 'pie' # end of 'pie' fi if test -f 'pie.1' -a "${1}" != "-c" ; then echo shar: Will not clobber existing file \"'pie.1'\" else echo shar: Extracting \"'pie.1'\" \(1238 characters\) sed "s/^X//" >'pie.1' <<'END_OF_FILE' X.\" $Header: /home/rodney/ipl/acct/RCS/pie.1,v 1.1 89/11/08 22:18:22 rodney Rel $ X.TH pie l "November 8, 1989" X.SH NAME Xpie \- generate a postscript program to display a pie chart of system usage X.SH SYNOPSIS X.B pie X[ X.B \-p \-x \-y \-l