Listing 5: Top-level graphlogs.pl which calls other scripts
#!/usr/local/bin/perl -w
#
# graphlogs.pl - Top-level script for generating .gif files from log files
#
# Usage: graphlogs.pl
#
# Input: $StatsDir is the directory containing log files and subscripts
# $NamedFile contains log file from named daemon
# $NetstatsFile contains log file from netstat routine
# GnuPlot is installed at /usr/local/bin/gnuplot
#
# Output: Normal operation is silent
# Creates temporary X,Y data files named.dat and netstats.dat
# Generates .gif files in $StatsDir (see gnuplot below)
#
require 'ctime.pl'; # For date functions
# Set $StatsDir to the directory containing the log files and sub-scripts
$StatsDir = "/var/stats";
$NamedFile = "named.messages"; # The log from named
$NetstatsFile = "netstats.messages"; # The log from the netstat undef $FirstX; # collector
$LastX = $StartX = 0;
# Go to where the logs and graphs are
#
chdir($StatsDir);
# Use sub-scripts to parse log files into plottable data
#
unlink("named.dat") if (-e "named.dat");
`named_log_to_stats.pl $NamedFile > named.dat`;
unlink("netstats.dat") if (-e "netstats.dat");
`netstats_log_to_stats.pl $NetstatsFile > netstats.dat`;
# Determine what X-values will display just the last 10 days of data
#
open(NAMED_DAT, "named.dat") || die "graphlogs.pl: Can't open named.dat";
while (<NAMED_DAT>) {
if (!(defined $FirstX)) { ($FirstCtime, $FirstX, $rest) = split(); }
($rest, $LastX, $rest) = split();
}
close(NAMED_DAT);
if (($LastX - $FirstX) < 10) {
$StartX = $FirstX;
} else {
$StartX = $LastX - 10;
}
# Just for decorating the graph's X-axis
#
($wday,$mo,$mday,$time,$rest) = split(/ +/, &ctime($FirstCtime));
$FirstDate = join(' ',$wday,$mo,$mday,$time);
# Write out gnuplot commands to a temporary file
#
open(GNUPLOT_CMD, ">gnuplot.cmd") || die
"graphlogs.pl: Can't open gnuplot.cmd for writing\n";
print GNUPLOT_CMD <<"EOF";
set term gif
set size 0.75,0.75
set nokey
set xlabel "X values are the Nth day since $FirstDate"
set output "rq_per_min.gif"
plot [$StartX:$LastX] "named.dat" using 2:3 with lines
set output "rq_per_hour.gif"
plot [$StartX:$LastX] "named.dat" using 2:4 with lines
set output "rq_total.gif"
plot [$StartX:$LastX] "named.dat" using 2:5 with lines
set xlabel ""
set output "collisions.gif"
plot [$StartX:$LastX] "netstats.dat" using 2:3 with lines
EOF
close(GNUPLOT_CMD);
# Generate the graphs by invoking gnuplot with the command file
#
system("/usr/local/bin/gnuplot gnuplot.cmd");
# End of File
|