Illegal division by zero at /usr/local/libdata/perl5/site_perl/DBD/Chart/Plot.pm line 3626. #### 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. #### #!/usr/bin/perl # # Hatchet chart grapher (hatchart) # v 0.6.2, 2004.02.18 # Jason Dixon # 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 $DBI::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 FLOAT)"; $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', 'yellow', 'marine', 'purple', 'orange', 'lblue', 'pink', 'cyan', 'lyellow') AND BACKGROUND='white' AND SIGNATURE='Hatchet v.0.6.2, Jason Dixon'"; 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 for writing: $!"; binmode BAR; print BAR $$result[0]; close(BAR); $dbh->do('DROP TABLE pie'); }