There are 3 parts to this article:
Part 1 – Part 2 – Part 3
In this article we introduced this graph and showed how it looks live, and in this article we created the scale. Finally, we will get to the part that creates the blue bars that comprise the graph. Here is the Perl script:
open (HTTPDFIG, ">> /path/to/httpd.fig"); open (COUNTHTTPD, "ps -C httpd h | wc -l |"); ($sec,$min,$hour,$mday,$mon,$year,$wday, $yday,$isdst)=localtime(time); while (){ print HTTPDFIG " 2 1 0 5 1 7 50 -1 -1 0.000 0 0 -1 0 0 2\n"; print HTTPDFIG ($min+$hour*60)*45; print HTTPDFIG " "; print HTTPDFIG 22500-$_*450; print HTTPDFIG " "; print HTTPDFIG ($min+$hour*60)*45; print HTTPDFIG " "; print HTTPDFIG " 22500\n"; } close COUNTHTTPD; close HTTPDFIG; system "/usr/bin/fig2dev -L png -m .2 /path/to/httpd.fig /path/to/httpd.png"; system "/usr/bin/fig2dev -L png -m .1 /path/to/httpd.fig /path/to/httpdsm.png"; |
First off, we open the fig file to write. We use the >> operator so that the new line is written at the end of the file. We use a pipe command and loop to read the number of httpd processes. For the blue bar/line, we use the time of the day in minutes since midnight to determine the x coordinates, and we simply multiply the number of processes ($_) times 450 to locate the end of the y coordinate on the graph. The fig2dev commands are the ones that convert the fig file to a png file. We have two so that we can display different sizes. The size is determined by the -m option. For more details on Xfig and the fig2dev command, see Xfig.org. Stick this command in your crontab to run every minute:
* * * * * /usr/bin/perl /path/to/hcount.pl > /dev/null 2>&1 |
One cool thing about this is that there is no extra process running all of the time to create this graph. Cron is always running (at least on our Red Hat system by default), so there really isn’t much overhead here. There is a bit of a spike in usage when fig2dev renders the graphic, but it really isn’t that noticeable to us.