#! /usr/local/bin/perl

#
# Copyright (c) 1991,1992 The Ohio State University.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that: (1) source distributions retain this entire copyright
# notice and comment, and (2) distributions including binaries display
# the following acknowledgement:  ``This product includes software
# developed by The Ohio State University and its contributors''
# in the documentation or other materials provided with the distribution
# and in all advertising materials mentioning features or use of this
# software. Neither the name of the University nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#

$options = "p:v";
$usage = "usage: getdata [-p pastNdays] [-v] [startdate [enddate]]\n";

require 'global.defs';
require 'local.defs';
require $yagrip_perl;
require $backuplib_perl;
require $ctime_perl;
require $unctime_perl;

    #
    # Real program starts here...
    #
&getopt($options) ||
  die $usage;

    # 
    # Deal with args...we define *_string things to use as args for progs 
    # called from here so that we can pass on the right values (eg, -d
    # or "", etc). 
    #
$start_date = 0;
$end_date = time;

$count = 0;			# used to create names for the per-host arrays

$start_date = shift(@ARGV) if $#ARGV >= 0;
if (($start_date = &parse_date_into_timestamp($start_date)) == -1) {
    exit(1);
}
$end_date = shift(@ARGV) if $#ARGV >= 0;
if (($end_date = &parse_date_into_timestamp($end_date)) == -1) {
    exit(1);
}

if (defined($end_date) && defined($opt_p)) {
    $start_date = $end_date - ($opt_p * 24 * 60 * 60);
}

    #
    # Process the tape database first.  Collect all of the data, shove 
    # it into the various arrays.
    #
$now = time;

&open_db(*TAPEDB, $tape_db_file, $db_mode) ||
  die "bgraph: can't open the dbm file '$tape_db_file'\n";

foreach $tapeid (&keys_db(*TAPEDB)) {
    &read_db(*TAPEDB, $tapeid, *tapedb_value, *tapedb_array);

    $tape_uses{$tapeid} = $tapedb_array{"uses"};
    $tape_drive{$tapeid} = $tapedb_array{"drive"};
    $tape_run{$tapeid} = $tapedb_array{"run_id"};
    $tape_age{$tapeid} = $now - $tapedb_array{"add_date"};
    $tape_last{$tapeid} = $now - $tapedb_array{"used_date"};
}

&close_db(*TAPEDB, $tape_db_file);

    #
    # Process the run database.  Collect all of the data, shove 
    # it into the various arrays.
    #
$now = time;

&open_db(*RUNDB, $run_db_file, $db_mode) ||
  die "bgraph: can't open the dbm file '$run_db_file'\n";

foreach $runid (&keys_db(*RUNDB)) {
    &read_db(*RUNDB, $runid, *rundb_value, *rundb_array);

    $run_chain{$runid} = $rundb_array{"chain"};
    $run_level{$runid} = $rundb_array{"level"};
    $run_hosts{$runid} = $rundb_array{"hosts"};
    $run_start{$runid} = $rundb_array{"start_date"};
    $run_duration{$runid} = $rundb_array{"done_date"} - $rundb_array{"start_date"};
    if ($run_duration{$runid} < 0) {
	$run_duration{$runid} = 0;
    }
    $run_size{$runid} = 0;
}

&close_db(*RUNDB, $run_db_file);

    #
    # Now read through each of the per-host backup databases and 
    # gather the size information.  
    #
$pattern = "$backup_host_file*.$dbext";
foreach $budb_name (<${pattern}>) {
    #
    # Need to nuke the trailing .dir to get the base of the db name, 
    # and get the host name for other uses.
    #
    @tmp = split(/\./, $budb_name);
    pop(@tmp);			# nuke trailing .dir
    $host = $tmp[$#tmp];	# host name is last before .dir
    $budb_name = join('.', @tmp);

    &open_db(*BUDB, $budb_name, $db_mode) ||
       &sigh("getdata: can't open the dbm file '$budb_name'\n");

    foreach $buid (&keys_db(*BUDB)) {
	next if ! &read_db(*BUDB, $buid, *budb_value, *budb_array);

	$fsname = $budb_array{"fs"};
	$fsname =~ s|/|_|g;

	$budb_array{"todate"} += 0;
	if ($budb_array{"todate"} < $start_date || $end_date < $budb_array{"todate"}) {
	    next;
	}

	$hostfs = $host . "+" . $fsname;
	if (! defined($list{$hostfs})) {
	    $list{$hostfs} = "a$count";
	    $count++;
	}

	open(ERROR, "$error_dir/$host.$buid") ||
	  next;

      line:
	while (<ERROR>) {
	    if (m/[ \t]*(DUMP: )+([0-9]+) (tape )*blocks.+/) {
		$size = $2 / 2;
		last line;
	    }
	}

	close(ERROR);

	chop($date = &ctime($budb_array{"todate"}));

	$runid = $budb_array{"run"};
	$run_size{$runid} += $size;

	$todo = sprintf("\$%s{\"%s\"} = \"%d %d %d %d %d %d\\n\";",
			$list{$hostfs},
			$buid,
			$buid,
			$runid,
			$run_chain{$runid},
			$run_level{$runid},
			$size,
			$budb_array{"todate"});
	eval($todo);
    }

    &close_db(*BUDB, $budb_name);
}

    #
    # OK, now the fun - print all the data out.
    #
foreach $hostfs (keys %list) {
    print "Working on $hostfs...\n" if defined($opt_v);

    $name = $list{$hostfs};

    if (! open(OUT, ">$hostfs.host-raw")) {
	printf(stderr "cannot open $name.host-raw\n");
	next;
    }

    $todo = sprintf("\@keywords = keys(%%%s);", $name);
    eval($todo);

    foreach $buid (@keywords) {
	$todo = sprintf("\$%s{\"%s\"}", $name, $buid);
	printf(OUT eval($todo));
    }

    close(OUT);
}

open(OUT, ">run.raw") || die "cannot open run.raw\n";

foreach $runid (keys %run_chain) {
    printf(OUT "%d %d %d %d %d %d %s\n",
	   $runid,
	   $run_chain{$runid},
	   $run_level{$runid},
	   $run_start{$runid},
	   $run_duration{$runid},
	   $run_size{$runid},
	   $run_hosts{$runid});
}

close(OUT);

open(OUT, ">tape.raw") || die "cannot open tape.raw\n";

foreach $tapeid (keys %tape_uses) {
    printf(OUT "%s %d %s %d %d %d\n",
	   $tapeid,
	   $tape_uses{$tapeid},
	   $tape_drive{$tapeid},
	   $tape_run{$tapeid},
	   $tape_age{$tapeid},
	   $tape_last{$tapeid});
}

close(OUT);

