Article Figure 1 Listing 1 Listing 2 Listing 3 mar2004.tar

Listing 3 mkimage_backperf.pl

#!/usr/bin/perl -w

# purpose: make graphs for backperf table

use DBI;
use Data::Dumper;
use GD::Graph::bars;
use CGI qw/:standard *table/;
use strict;

my $policy_hunt = $ARGV[0];
if ($policy_hunt) {
 $policy_hunt = qq{
                      AND policy = '$policy_hunt'
                  };
}

my $success = 0;
my $failure = 0;
my @failures;

# Connect to DB
my $database = 'nbperf';
my $hostname = 'databaseserver';
my $dsn = "DBI:mysql:database=$database;host=$hostname";

my $user = 'database_user';
my $password = 'password';
my $dbh = DBI->connect($dsn, $user, $password); 

my @y;
my @x;
# get data from the last 1 days
my $timespan = " 24 hours ";
my $query = qq {
                     SELECT backdate, client, kbpersec, policy,
                      (to_days(now()) - to_days(backdate)) as age
                      FROM backperf WHERE 
                      (to_days(now()) - to_days(backdate)) < 2
                      $policy_hunt
                      ORDER BY policy DESC
                   };
my $sth = $dbh->prepare($query);
$sth->execute;
my $rows = $sth->rows;
unless ($rows > 0) {
   $query = qq{
                     SELECT backdate, client, kbpersec, policy,
                      (to_days(now()) - to_days(backdate)) as age
                      FROM backperf WHERE 
                      (to_days(now()) - to_days(backdate)) < 4
                      $policy_hunt
                      ORDER BY policy DESC
   };
   $sth = $dbh->prepare($query);
   $sth->execute;
   $timespan = " 3 days";
}
while (my $hash_ref = $sth->fetchrow_hashref) {
  my $backdate = $hash_ref->{backdate};
  my $client = $hash_ref->{client};
  my $kbpersec = $hash_ref->{kbpersec};
  my $policy = $hash_ref->{policy};
  push(@x, "$client - $policy");
  push(@y, $kbpersec);
}

# make and save graph
my $graph = GD::Graph::bars->new(800,400);
my $x_ref = \@x;
my $y_ref = \@y;
my @data = (
   $x_ref, $y_ref
   );

$graph->set(
      x_label           => 'host - policy',
      y_label           => 'kb/sec',
      x_labels_vertical => 1,
      title             => "Backup Speeds in Last $timespan",
      y_max_value       => 25000,
      y_tick_number     => 100,
      y_label_skip      => 30
);

my $format = $graph->export_format;
open(IMG,">img/backperf.png");
binmode IMG;
my $pic = $graph->plot(\@data);
print IMG $pic->png;