Hi monks-
I'm trying to debug a script I wrote which uses the DBD::Chart module for chart creation. Everything works great, but I get the following warning when it runs via cron:
Illegal division by zero at
/usr/local/libdata/perl5/site_perl/DBD/Chart/Plot.pm line 3626.
Obviously, this doesn't tell me much about what part of my code might be inserting the zero value. The funny thing is, if I run the script manually, I don't get that warning anymore. But I do get this when I enable warnings:
v-string in use/require non-portable at /usr/local/libdata/perl5/site_
+perl/DBD/Chart/Plot.pm line 97.
v-string in use/require non-portable at /usr/local/libdata/perl5/site_
+perl/DBD/Chart.pm line 98.
Any ideas why I'm only seeing the divide-by-zero warning from cron? Both are run as root. Thanks in advance. Here's the code...
#!/usr/bin/perl
#
# Hatchet chart grapher (hatchart)
# v 0.6.2, 2004.02.18
# Jason Dixon <jason@dixongroup.net>
# http://www.dixongroup.net/hatchet/
#
use strict;
use DBI;
use DBD::SQLite;
use DBD::Chart;
##################################################
# Configuration Section
##################################################
my $db_file = "/var/db/pflog.db";
my $max_wedges = 5;
my $graphs_dir = "/var/www/htdocs/hatchet/graphs";
my $width = 500;
my $height = 400;
##################################################
my $dbh = DBI->connect("DBI:SQLite:dbname=$db_file", "", "") || die $D
+BI::errstr;
my $date = get_date();
print_graph(get_top_sources(), $graphs_dir, 'hosts_all.png');
print_graph(get_top_services(), $graphs_dir, 'ports_all.png');
print_graph(get_top_sources($$date), $graphs_dir, 'hosts_today.png');
print_graph(get_top_services($$date), $graphs_dir, 'ports_today.png');
sub get_date {
my $day;
if ([split(/ /,localtime)]->[2] =~ /^[\d]{1}$/) {
$day = '0' . [split(/ /,localtime)]->[2];
} else {
$day = [split(/ /,localtime)]->[2];
}
my $date = [split(/ /,localtime)]->[1] . ' ' . $day;
return \$date;
}
sub get_top_services {
my $date = shift || undef;
my $select_query = "select dst_port from logs";
$select_query .= " where date like '%$date%'" if ($date);
my $sth = $dbh->prepare($select_query);
$sth->execute;
my %events;
my $total = 0;
while (my $result = $sth->fetchrow_hashref) {
$events{$result->{'dst_port'}}{'count'}++;
$total++;
}
my @sorted = sort {
$events{$b}{'count'} <=> $events{$a}{'count'}
} keys %events;
my @top_entries;
for (my $i=0; $i<$max_wedges; $i++) {
my %hash = ($sorted[$i] => $events{$sorted[$i]}{'count
+'});
push(@top_entries, \%hash);
}
push(@top_entries, { 'other' => $total });
return \@top_entries;
}
sub get_top_sources {
my $date = shift || undef;
my $select_query = "select src_host from logs";
$select_query .= " where date like '%$date%'" if ($date);
my $sth = $dbh->prepare($select_query);
$sth->execute;
my %events;
my $total = 0;
while (my $result = $sth->fetchrow_hashref) {
$events{$result->{'src_host'}}{'count'}++;
$total++;
}
my @sorted = sort {
$events{$b}{'count'} <=> $events{$a}{'count'}
} keys %events;
my @top_entries;
for (my $i=0; $i<$max_wedges; $i++) {
my %hash = ($sorted[$i] => $events{$sorted[$i]}{'count
+'});
push(@top_entries, \%hash);
}
push(@top_entries, { 'other' => $total });
return \@top_entries;
}
sub print_graph {
my ($data, $dir, $filename) = @_;
my $dbh = DBI->connect('dbi:Chart:') || die $DBI::errstr;
my $create_query = "CREATE TABLE pie (source CHAR(15), count F
+LOAT)";
$dbh->do($create_query);
my $sth = $dbh->prepare("INSERT INTO pie VALUES (?,?)");
foreach (@$data) {
my ($key, $value) = each(%$_);
$sth->execute($key, $value);
}
my $select_query = "SELECT PIECHART FROM pie WHERE WIDTH=? AND
+ HEIGHT=? AND
COLOR IN ('lred', 'lgreen', 'blue', 'y
+ellow', 'marine', 'purple',
'orange', 'lblue', 'pi
+nk', 'cyan', 'lyellow') AND
BACKGROUND='white' AND
SIGNATURE='Hatchet v.0.6.2, Jason Dixo
+n'";
my $rsth = $dbh->prepare($select_query);
$rsth->execute($width, $height);
my $result = $rsth->fetchrow_arrayref;
open(BAR, ">$graphs_dir/$filename") || die "Can't open file fo
+r writing: $!";
binmode BAR;
print BAR $$result[0];
close(BAR);
$dbh->do('DROP TABLE pie');
}
BazB added readmore tags