I wanted to illustrate how to use Fido with an example. Today we're going to use it to count software downloads on this site. Exciting! This will be straightforward since we have only one data source. A few years ago, I moved my software from an FTP repository onto this web server. To quantify software downloads, we can simply monitor the HTTP access log.
Here’s our fido configuration for the log file:
/var/log/httpd/access_log { rules = downloads.conf action = /usr/local/bin/tally log = syslog}
This tells Fido to monitor the access_log in real time. Its pattern match rules are in a file called downloads.conf When Fido finds a match, it will execute a program called tally. Finally, the last directive tells Fido to use syslog to log its activity.
To understand what we’re looking for, please review the software repository. It contains multiple versions and helpful links to the latest releases and betas. We want to match them all.
Let’s take a look at our downloads.conf file. Since we didn't specify a full path to the file, fido knows to look for it under $sysconfdir/etc/fido/rules. If you configured it to use /etc, the rules are configured in downloads.conf in the /etc/fido/rules directory. Here's the file:
## Track and count downloads from the websiteSIEGE: .*siege-.*tar.gz.*FIDO: .*fido-.*\([rpm]|[tar\.gz]\).*WACKY: .*wackyd-.*tar.gz.*DICK: .*dick.*tar.gz.*SPROXY: .*sproxy-.*tar.gz.*CONFIG: .*JoeDog-Config.*STATS: .*JoeDog-Stats.*GETOPT: .*php-getopt.*WACKY: .*JoeDog-Wacky.*PBAR: .*JoeDog-ProgressBar.*
Each line begins with an optional label. If a label is present, fido will pass it (minus the colon) to the action program. In the example above, if the JoeDog-Config perl module is downloaded, then fido will run /usr/local/bin/tally CONFIG. For more on labels, see the Fido user’s manual.
tally It is a Perl script that does our heavy lifting. It uses Tie::File to map an array with the contents of a file. When invoked, it searches the map for the label specified by $ARGV[0] and increments its value. Here’s the script:
#!/usr/bin/perluse strict;use warnings;use Tie::File;my $file = "/tmp/downloads.txt";my $type = $ARGV[0] or die "usage: $0 <identifier>";sub trim($);if (! -e $file) { open (FILE, ">$file"); close(FILE);}tie my @array, 'Tie::File', $file or die;my $found = 0;for my $line (@array) { my ($name,$downloads) = split(/\|/,$line); $downloads += 1; if ($name =~ /$ARGV[0]/) { $name = trim($name); $line = sprintf("%-7s | %5s \n", $name, $downloads); print "$line"; $found = 1; }}if (! $found) { push(@array, $ARGV[0] . " | 1")}untie @array;sub trim($) { my $thing = shift; $thing =~ s/#.*$//; # trim trailing comments $thing =~ s/^\s+//; # trim leading whitespace $thing =~ s/\s+$//; # trim trailing whitespace return $thing;}
I could just as easily have inserted those values into a database and used that information to publish on this site. It was designed to be flexible. In some cases, I have fido daemons that send traps to OpenView; in others, I have it send pages. What you do at the event of a match is limited only by your imagination.
So what was the result of this tabulation? I set this example up five months ago, and here's the result:
$ cat downloads.txtSIEGE | 20406 STATS | 151 GETOPT | 63 SPROXY | 2262 DICK | 146 CONFIG | 165 PBAR | 102 FIDO | 1177 WACKY | 160 PWACKY | 172
Siege is the clear “winner” followed by sproxy which is a siege utility that helps build a urls.txt file. I was disappointed that JoeDog::Config received so little love. I use it all the time.
Happy hacking.