libwanetmondb.pm 2.97 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2006 University of Utah and the Flux Group.
# All rights reserved.
#

8 9 10 11 12 13 14
#
# TODO:
#  - Don't connect to DB every query. Leave connection open util
#    another DB needs to be connected to (DG 1/3/07), unless the 
#    "connect" subroutine already does this...
#

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
package libwanetmondb;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);
use lib '/usr/testbed/lib';
use libtbdb;
require Exporter;

@ISA    = "Exporter";
our @EXPORT = qw ( 
                   getRows
              );
our @EXPORT_OK = qw(
                    );

my $PWDFILE = "/usr/testbed/etc/pelabdb.pwd";
my $OPSDBNAME  = "pelab";
my  $DPDBNAME  = "nodesamples";
my $DPDBHOST = "nfs.emulab.net";
my $DBUSER  = "flexlabdata";
my $OpsDataDuration = 24; #hours that data persists on the ops DB
my $dbpwd;
my $dppwd;
if (`cat $PWDFILE` =~ /^([\w]*)\s([\w]*)$/) {
    $dbpwd = $1;
    $dppwd = $2;
}
else {
    fatal("Bad characters in password!");
}
$dbpwd = '';

#
# Return an array of result row hashes based on given query string.
#   If a unixstamp range is not given, data from ops is used
#   If given unixstamp range falls within the past hour, and goes back over
#   ($OpsDataDuration-1) in time, then the query only uses the datapository
#   and WILL NOT LOOK AT data within the last hour
#
sub getRows($)
{
    my ($query) = @_;
    my ($tablename, $t0,$t1);
    my $useOps = 1;
    my @rows = ();

    if( $query =~ /from\s+(\w+)/ ){
        $tablename = $1;
    }
    if( $query =~ /unixstamp\s*>=?\s*(\d+)/ ){
        $t0 = $1;
    }
    if( $query =~ /unixstamp\s*<=?\s*(\d+)/ ){
        $t1 = $1;
    }

    if( !defined $t0 || !defined $t1 ){
        #use Ops
        $useOps = 1;
    }
Dan Gebhardt's avatar
Dan Gebhardt committed
76
#    print "t0=$t0\nt1=$t1\n$query\n";
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    #Determine whether to go to DPos or Ops databases
    if( defined $t0 &&
        $t0 < time()-($OpsDataDuration-0.5)*60*60 )
    {
        #use DataPository
        $useOps = 0;
    }else{
        #use Ops DB
        $useOps = 1;
    }



    if( $useOps ){
92
#        print "USING OPS\n";
Dan Gebhardt's avatar
Dan Gebhardt committed
93
        TBDBConnect($OPSDBNAME, $DBUSER, $dbpwd,"") == 0
94 95 96
            or die("Could not connect to ops/pelab database!\n");
        my $query_result = DBQueryFatal($query);
        if (! $query_result->numrows) {
97
#            warn("No results from OpsDB with query:\n$query\n");
98 99 100 101 102 103
            return @rows;
        }
        while( my $hrow = $query_result->fetchrow_hashref() ){
            push @rows, $hrow;
        }
    }else{
Dan Gebhardt's avatar
Dan Gebhardt committed
104
#        print "USING DP: $query\n";
105 106 107 108
        TBDBConnect($DPDBNAME, $DBUSER, $dbpwd,$DPDBHOST) == 0
            or die("Could not connect to nfs/pelab database!\n");
        my $query_result = DBQueryFatal($query);
        if (! $query_result->numrows) {
109
#            warn("No results from DataPository DB with query:\n$query\n");
110 111 112 113 114
            return @rows;
        }
        while( my $hrow = $query_result->fetchrow_hashref() ){
            push @rows, $hrow;
        }
Dan Gebhardt's avatar
Dan Gebhardt committed
115
#        print "finished DP\n";
116 117 118 119 120 121 122 123 124
    }

    return @rows;
}




1;  # DON'T REMOVE THIS
125 126