libwanetmondb.pm 3.66 KB
Newer Older
1 2 3
#!/usr/bin/perl -w
#
# Copyright (c) 2006 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
23 24
#

25 26 27 28 29 30 31
#
# 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...
#

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
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
93
#    print "t0=$t0\nt1=$t1\n$query\n";
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

    #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 ){
109
#        print "USING OPS\n";
Dan Gebhardt's avatar
Dan Gebhardt committed
110
        TBDBConnect($OPSDBNAME, $DBUSER, $dbpwd,"") == 0
111 112 113
            or die("Could not connect to ops/pelab database!\n");
        my $query_result = DBQueryFatal($query);
        if (! $query_result->numrows) {
114
#            warn("No results from OpsDB with query:\n$query\n");
115 116 117 118 119 120
            return @rows;
        }
        while( my $hrow = $query_result->fetchrow_hashref() ){
            push @rows, $hrow;
        }
    }else{
Dan Gebhardt's avatar
Dan Gebhardt committed
121
#        print "USING DP: $query\n";
122 123 124 125
        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) {
126
#            warn("No results from DataPository DB with query:\n$query\n");
127 128 129 130 131
            return @rows;
        }
        while( my $hrow = $query_result->fetchrow_hashref() ){
            push @rows, $hrow;
        }
Dan Gebhardt's avatar
Dan Gebhardt committed
132
#        print "finished DP\n";
133 134 135 136 137 138 139 140 141
    }

    return @rows;
}




1;  # DON'T REMOVE THIS
142 143