flexdatasrv.in 38.7 KB
Newer Older
1
#!/usr/local/bin/python
2 3
#
# Copyright (c) 2007 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 32 33 34 35 36 37 38

#
# A simple little xmlrpc server that provides an interface to flexlab data.
# At present, basically just exports goodnodes.pl functionality, but could
# do more if desired.
#

import os, os.path
import pwd
import SimpleXMLRPCServer
import sys
import types
import exceptions
import traceback
import threading
39
import time
David Johnson's avatar
David Johnson committed
40
import xmlrpclib
41 42
import string
import socket
43 44 45 46 47 48 49 50 51 52

debug = True

TBPATH = os.path.join("@prefix@", "lib")
if not TBPATH in sys.path:
    sys.path.append(TBPATH)
    pass

import libdb
import BasicXMLRPCServers
53 54
import networkx as NX
from networkx import *
55 56


David Johnson's avatar
David Johnson committed
57
class FlexlabFault(xmlrpclib.Fault):
58 59
    pass

David Johnson's avatar
David Johnson committed
60
class FlexlabInvalidArgumentFault(FlexlabFault):
61 62
    pass

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
# a few helper regexps to interpret node_ids
#VNODE_REG = re.compile("^plabvm(\d+)\-(\d+)$")
#PNODE_REG = re.compile("^plab(\d+)$")
#IP_REG = re.compile("^(\d+)\.(\d+)\.(\d+)\.(\d+)$")
#HOSTNAME_REG = re.compile("^([\d\w\-\.]+)$")

#
# Setup some db constants, and read passwds
#
OPSDB = 'pelab'
OPSUSER = 'flexlabdata'
DPDB = 'nodesamples'
DPDBHOST = 'nfs.emulab.net'
DPDBUSER = 'flexlabpush'
DPDBPASSWD = None

def readDPDBPasswd():
    f = open('/usr/testbed/etc/pelabdb-dp.pwd')
    passwd = f.readline().rstrip('\n')
    f.close()
    return passwd

DPDBPASSWD = readDPDBPasswd()
86 87 88

class FlexlabDataServer:
    """
89
    FlexlabDataServer keeps a cache of the last N seconds of flexmon data.
90
    """
91
    def __init__(self,autorefresh=60,cachetime=86400):
92 93 94 95 96 97 98 99 100 101 102 103 104
        """
        Set autorefresh to -1 to disable; else, set interval between refreshes
        in seconds.
        """
        # the "cache" is a big map:
        #   'lat' => srcsiteidx => dstsiteidx => (value,unixtime)
        #   'bw'  => srcsiteidx => dstsiteidx => (value,unixtime)
        self.cache = dict({})
        self._cacheinit = False
        self._lastdataidx = 0

        self.nodeidToSiteIdx = dict({})
        self.siteIdxToSiteName = dict({})
105 106
        self.nodeidToNodeIdx = dict({})
        self.nodeidToHostIPInfo = dict({})
107 108

        self.dbs = dict({})
109 110 111
        self.dbs['opsdb'] = libdb.TBDBConnect(dbnum=1,db=OPSDB,user=OPSUSER)
        self.dbs['dpdb'] = libdb.TBDBConnect(dbnum=2,db=DPDB,user=DPDBUSER,
                                             host=DPDBHOST,passwd=DPDBPASSWD)
112

113 114 115 116
        self.cachetime = cachetime
        self.autorefresh = None
        if autorefresh != None and autorefresh > 0:
            self.autorefresh = autorefresh
117
            # fire off a refresh/age thread:
118
            at = threading.Thread(target=self._foreverRefreshAndAge,args=())
119 120 121 122 123 124 125 126
            # it really IS a "daemon thread" but we want it to die when the
            # main thread is killed.
            at.setDaemon(0)
            at.start()
            pass
        
        return

127 128 129 130
    def queryDB(self,dbname,query,args=()):
        qres = libdb.DBQueryFatal(query,querySub=args,dbnum=self.dbs[dbname])
        return qres

131 132 133 134 135 136 137 138 139 140 141 142
    def populatePairDataCache(self):
        if len(self.cache.keys()) > 0:
            self.cache = dict({})
            pass

        # We find the least greatest index of the two queries so that we can
        # easily update later.
        mingidx = 0

        # Grab a summary of the last 24 hours worth of pair_data from the
        # ops pelab db.  Note that we only remember the most recent measurement
        # that satisfies our criteria, for each site pair.
143 144 145 146 147
        q = "select idx,srcsite_idx,dstsite_idx,latency,max(unixstamp) as ts" \
            " from pair_data where (unix_timestamp()-unixstamp) < %d" \
            "  and latency is not NULL and latency > 0" \
            " group by srcsite_idx,dstsite_idx " \
            " order by srcsite_idx,dstsite_idx;" % self.cachetime
148 149 150 151
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])

        for (idx,sidx,didx,latency,ts) in qres:
            if idx > mingidx:
152
                mingidx = idx
153 154 155
            self._cacheMeasurement('lat',sidx,didx,latency,ts)
            pass
        
156 157 158 159 160
        q = "select idx,srcsite_idx,dstsite_idx,bw,max(unixstamp) as ts" \
            " from pair_data where (unix_timestamp()-unixstamp) < %d" \
            "  and bw > 0 " \
            " group by srcsite_idx,dstsite_idx " \
            " order by srcsite_idx,dstsite_idx;" % self.cachetime
161 162 163 164 165
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])

        tidx = 0
        for (idx,sidx,didx,bw,ts) in qres:
            if idx > tidx:
166
                tidx = idx
167 168 169 170 171 172 173
            self._cacheMeasurement('bw',sidx,didx,bw,ts)
            pass

        if tidx < mingidx:
            mingidx = tidx

        self._lastdataidx = mingidx
174
        print "Updated lastdataidx to %d" % self._lastdataidx
175

176 177 178 179 180 181
        q = "select node_id,hostname,IP from node_mapping order by hostname"
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])
        for (node_id,hostname,IP) in qres:
            self.nodeidToHostIPInfo[node_id] = [hostname,IP]
            pass

182
        q = "select site_name,site_idx,node_id,node_idx" \
183 184 185 186 187 188
            " from site_mapping order by site_idx"
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])

        for (sname,sidx,node_id,nidx) in qres:
            self.nodeidToSiteIdx[node_id] = sidx
            self.siteIdxToSiteName[sidx] = sname
189
            self.nodeidToNodeIdx[node_id] = nidx
190 191 192 193 194
            pass

        self._cacheinit = True
        pass

195 196
    def getNodeInfo(self,node_id):
        retval = [self.nodeidToNodeIdx[node_id],self.nodeidToSiteIdx[node_id],
David Johnson's avatar
David Johnson committed
197 198 199 200 201 202 203 204 205
                  node_id]
        if self.nodeidToHostIPInfo.has_key(node_id) \
               and not self.nodeidToHostIPInfo[node_id] == None:
            retval.append(self.nodeidToHostIPInfo[node_id][0])
            retval.append(self.nodeidToHostIPInfo[node_id][1])
            pass
        else:
            retval.append(None)
            retval.append(None)
206
            pass
David Johnson's avatar
David Johnson committed
207

208 209
        return retval

210 211 212 213 214 215 216 217 218 219 220 221
    def getNodeSiteMap(self):
        retval = []

        for (k,v) in self.nodeidToNodeIdx.iteritems():
            if not v == None and self.nodeidToSiteIdx.has_key(k):
                si = self.nodeidToSiteIdx[k]
                retval.append([self.siteIdxToSiteName[si],si,k,v])
                pass
            pass

        return retval

222 223 224 225 226 227 228 229 230 231 232 233 234
    def _cacheMeasurement(self,type,sidx,didx,mval,ts):
        if not self.cache.has_key(type):
            self.cache[type] = dict({})
        if not self.cache[type].has_key(sidx):
            self.cache[type][sidx] = dict({})

        self.cache[type][sidx][didx] = (mval,ts)
        pass

    def _refreshPairDataCache(self):
        if not self._cacheinit:
            return

235 236 237 238 239
        q = "select idx,srcsite_idx,dstsite_idx,latency,bw,unixstamp as ts" \
            " from pair_data where idx > %d" \
            "  and ((latency is not NULL and latency > 0) or bw > 0)" \
            " group by srcsite_idx,dstsite_idx " \
            " order by unixstamp" % self._lastdataidx
240

241
        count = 0
242 243 244 245 246 247 248 249 250
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])
        for (idx,sidx,didx,lat,bw,ts) in qres:
            # figure out which it is, bw or lat:
            if lat == None or lat == '':
                self._cacheMeasurement('bw',sidx,didx,bw,ts)
            else:
                self._cacheMeasurement('lat',sidx,didx,lat,ts)
            if idx > self._lastdataidx:
                self._lastdataidx = idx
251 252 253 254
            count += 1

        print "Added %d new measurements." % count
        print "Updated lastdataidx to %d" % self._lastdataidx
255 256
        pass

257 258 259 260
    def _refreshNodeSiteMap(self):
        if not self._cacheinit:
            return

261 262 263 264 265 266
        q = "select node_id,hostname,IP from node_mapping order by hostname"
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])
        for (node_id,hostname,IP) in qres:
            self.nodeidToHostIPInfo[node_id] = [hostname,IP]
            pass

267 268 269 270 271 272 273 274 275 276 277
        q = "select site_name,site_idx,node_id,node_idx" \
            " from site_mapping order by site_idx"
        qres = libdb.DBQueryFatal(q,dbnum=self.dbs['opsdb'])

        # for now, we only update new nodes, not flush "old" ones.
        newnodes = []
        for (sname,sidx,node_id,nidx) in qres:
            if not self.nodeidToSiteIdx.has_key(node_id):
                newnodes.append(node_id)
                self.nodeidToSiteIdx[node_id] = sidx
                self.siteIdxToSiteName[sidx] = sname
278
                self.nodeidToNodeIdx[node_id] = nidx
279 280 281 282 283 284 285 286
                pass
            pass

        print "Added %d new nodes (%s)." % (len(newnodes),
                                            str.join(',',newnodes))
        
        pass

287 288 289 290 291
    def _ageCache(self):
        if not self._cacheinit:
            return

        now = time.time()
292
        count = 0
293 294 295 296 297

        for mtype in self.cache.keys():
            sdict = self.cache[mtype]
            for srcsite in sdict.keys():
                ddict = sdict[srcsite]
298 299 300 301 302 303 304
                for dstsite in ddict.keys():
                    (val,ts) = ddict[dstsite]
                    if (now - ts) > self.cachetime:
                        del ddict[dstsite]
                        count += 1

        print "Aged out %d measurements." % count
305 306 307 308 309

        pass

    def refreshAndAge(self):
        self._refreshPairDataCache()
310
        self._refreshNodeSiteMap()
311 312 313 314 315 316 317 318 319 320 321
        self._ageCache()
        pass

    def _foreverRefreshAndAge(self):
        while True:
            try:
                self.refreshAndAge()
            except:
                traceback.print_exc()
                pass
            try:
322
                time.sleep(self.autorefresh)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
            except:
                traceback.print_exc()
                pass
            pass
        pass
        
    def getMeasurement(self,type,srcsiteidx,dstsiteidx,t0=0,t1=0):
        """
        Retrieves a measurement from flexlab data.
        """
        if self.cache.has_key(type) \
           and self.cache[type].has_key(srcsiteidx) \
           and self.cache[type][srcsiteidx].has_key(dstsiteidx):
            (val,ts) = self.cache[type][srcsiteidx][dstsiteidx]
        else:
            val = None
        
        return val

342
    def getSiteIdxByNode(self,nodeid):
343 344 345 346
        if self.nodeidToSiteIdx.has_key(nodeid):
            return self.nodeidToSiteIdx[nodeid]
        return None

347 348 349 350 351 352 353
    def getSiteIdxBySite(self,sitename):
        for (k,v) in self.siteIdxToSiteName.iteritems():
            if sitename == v:
                return k
            pass
        return None

354 355 356 357 358 359 360 361 362 363 364 365 366
    def getAvailNodes(self):
        return self.nodeidToSiteIdx.keys()

    def getAvailSiteIdxs(self):
        return self.siteIdxToSiteName.keys()

    pass


class flexlab:
    def __init__(self,fds):
        self.fds = fds
        self.debug = True
367

David Johnson's avatar
David Johnson committed
368
        self.MAX_QUERY_INTERVAL = 60*60*24
369
        
370
        return
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

    def getMeasurements(self,argdict={}):
        """
        Returns a list of measurements.  Each measurement is a list like:
          [ src_node_idx,src_site_idx,
            dst_node_idx,dst_site_idx,
            gmt_timestamp,measurement_value ].

        If you don't specify a datasource explicitly, the source database
        will be chosen by the time interval you specify: if it's within the
        last 24 hours GMT, we'll query the fast database; if older, we query
        the slow archive database!

        We will not, under any circumstances, allow you to query larger than
        a 24-hour window; even that will get you around 150000 measurements.
        (the problem is that we must buffer the results, and then convert them
        into XML -- huge!)

        NOTE: We strongly advise you to test queries on a slice of the past
        24 hours, or better yet, on the hot, latest, in-memory cache.

        Arguments:
          t0 (int)      Find measurements after this unix timestamp (in GMT).
          t1 (int)      Find measurements before this unix timestamp (in GMT).
          mtype (str)   Measurement type ('bw','lat').

        Optional Arguments:
          src_filter (list(filter_elm)), where filter_elm looks like one of:
                      [ 'site','short-site-name' ];
                      [ 'node','hostname_or_emulab_id_or_IP' ].
                        Only select measurements where the source is the
                        site or node specified.  ((Site names are taken from
                        PlanetLab's sites.xml
                        (https://www.planet-lab.org/xml/sites.xml), and we use
                        the SHORT_SITE_NAME attribute).
          dst_filter (list(filter_elm))
                        Same as src_filter, except filter by destination.
          value_constraints (list like [['<',<value>],['>=',<value>],...])
                        A list of constraints on the value of the measurement
                        type, all of which must be true for each measurement
                        returned.  Recognized comparison operators are
                        <  <=  >  >= .  Valid '<value>' params are integers or
                        floats.
          datasrc (str) Force queries to go to one of three sources:
                        'latest' (only query the cache of latest per-pair
                                  measurements -- fast!)
                        'recent' (only query the 24-hr db cache -- reasonable!)
                        'archive'(only query the Datapository, the giant,
                                  all-encompassing archive -- slow)
        """
        retval = []
        (t0,t1,mtype,sfnodes,sfsites,dfnodes,dfsites,vconst,datasource) = \
            (0,0,'bw',None,None,None,None,None,None)
        in_cache_interval = False

        # check args
        if argdict == None or type(argdict) != types.DictType:
            return FlexlabInvalidArgumentFault(10,"No arguments!")
        
        if argdict.has_key("t0"):
            if type(argdict["t0"]) == types.StringType:
                try:
                    t0 = time.strptime(argdict["t0"],"%Y-%m-%d %H:%M:%S %Z")
                    t0 = self.__timeToUTC(ts)
                except:
                    errstr = "Argument 't0' must be int or str of form " \
                             "%Y-%m-%d %H:%M:%S %Z"
                    return FlexlabInvalidArgumentFault(10,errstr)
                pass
            elif type(argdict["t0"]) == types.IntType:
                t0 = argdict["t0"]
                pass
            else:
                errstr = "Argument 't0' must have int/str type!"
                return FlexlabInvalidArgumentFault(11,errstr)
            pass
        else:
            return FlexlabInvalidArgumentFault(10,"Must supply 't0' argument!")

        if argdict.has_key("t1"):
            if type(argdict["t1"]) == types.StringType:
                try:
                    t1 = time.strptime(argdict["t1"],"%Y-%m-%d %H:%M:%S %Z")
                    t1 = self.__timeToUTC(ts)
                except:
                    errstr = "Argument 't1' must be int or str of form " \
                             "%Y-%m-%d %H:%M:%S %Z"
                    return FlexlabInvalidArgumentFault(10,errstr)
                pass
            elif type(argdict["t1"]) == types.IntType:
                t1 = argdict["t1"]
                pass
            else:
                errstr = "Argument 't1' must have int/str type!"
                return FlexlabInvalidArgumentFault(11,errstr)
            pass
        else:
            return FlexlabInvalidArgumentFault(10,"Must supply 't1' argument!")

        # other interval checks
        if t1 <= t0:
            errstr = "'t0' must be less than 't1'!"
            return FlexlabInvalidArgumentFault(10,errstr)

        if (t1 - t0) > self.MAX_QUERY_INTERVAL:
            errstr = "You have specified an interval greater than %d!" % \
                     self.MAX_QUERY_INTERVAL
            return FlexlabInvalidArgumentFault(10,errstr)

        # figure some stuff out about the interval:
        now = time.time()
        cache_interval = self.fds.cachetime
        cache_end = now - cache_interval

        # last checks
        if t1 > now and t0 > now:
            errstr = "You have specified an interval entirely in the future!"
            return FlexlabInvalidArgumentFault(10,errstr)

        # figure out where around the cache boundary the interval is, so
        # we can figure out which datasource to query later:
        if (t1 <= now and t1 >= cache_end and t0 <= now and t0 >= cache_end) \
           or t1 > cache_end:
            in_cache_interval = True
            pass

        if in_cache_interval:
            datasource = 'recent'
            pass
        else:
            datasource = 'archive'
            pass

        valid_mtypes = ('bw','latency')  # ,'loss','jitter')
        if argdict.has_key("mtype"):
            if type(argdict["mtype"]) == types.StringType \
               and argdict["mtype"] in valid_mtypes:
                mtype = argdict["mtype"]
                pass
            else:
                errstr = "Argument 'mtype' must have int/str type!"
                return FlexlabInvalidArgumentFault(11,errstr)
            pass
        else:
            return FlexlabInvalidArgumentFault(10,
                                               "Must supply 'mtype' argument!")

        # check optional args
        if argdict.has_key("src_filter"):
            # a dict of site idxs to lists of node idxs
            sfnodes = dict({})
            # a list of sites, as the idxs we expect to see in the DB
            sfsites = []
            # make an enclosing list, if one doesn't exist (i.e., if they
            # pass a single element
            if not type(argdict["src_filter"]) == types.ListType:
                argdict["src_filter"] = [argdict["src_filter"],]
                pass

            for li in argdict["src_filter"]:
                # per-element arg checks:
                if not type(li) == types.ListType or \
                   not len(li) == 2 or \
                   not type(li[0]) == types.StringType or \
                   not li[0] in ("site","node") or \
                   not type(li[1]) == types.StringType:
                    errstr = "Malformed argument 'src_filter'"
                    return FlexlabInvalidArgumentFault(10,errstr)

                if li[0] == "node":
                    ni = self.fds.getNodeInfo(li[1])
                    if ni == None:
                        errstr = "Unknown node '%s' in src_filter" % li[1]
                        return FlexlabInvalidArgumentFault(10,errstr)
                    if not sfnodes.has_key(ni[0]):
                        sfnodes[ni[0]] = []
                        pass
                    sfnodes[ni[0]].append("%s" % str(ni[1]))
                    pass
                elif li[0] == "site":
                    si = self.fds.getSiteIdxBySite(li[1])
                    if si == None:
                        errstr = "Unknown site '%s' in src_filter" % li[1]
                        return FlexlabInvalidArgumentFault(10,errstr)
                    sfsites.append(str(si))
                    pass
                else:
                    # this should not happen, due to checks at the top of
                    # this loop
                    pass
                pass
            pass

        if argdict.has_key("dst_filter"):
            # a dict of site ids to node ids
            dfnodes = dict({})
            # a list of sites, as the idxs we expect to see in the DB
            dfsites = []
            # make an enclosing list, if one doesn't exist (i.e., if they
            # pass a single element
            if not type(argdict["dst_filter"]) == types.ListType:
                argdict["dst_filter"] = [argdict["dst_filter"],]
                pass

            for li in argdict["dst_filter"]:
                # per-element arg checks:
                if not type(li) == types.ListType or \
                   not len(li) == 2 or \
                   not type(li[0]) == types.StringType or \
                   not li[0] in ("site","node") or \
                   not type(li[1]) == types.StringType:
                    errstr = "Malformed argument 'dst_filter'"
                    return FlexlabInvalidArgumentFault(10,errstr)

                if li[0] == "node":
                    ni = self.fds.getNodeInfo(li[1])
                    if ni == None:
                        errstr = "Unknown node '%s' in dst_filter" % li[1]
                        return FlexlabInvalidArgumentFault(10,errstr)
                    if not dfnodes.has_key(ni[0]):
                        dfnodes[ni[0]] = []
                        pass
                    dfnodes[ni[0]].append("%s" % str(ni[1]))
                    pass
                elif li[0] == "site":
                    si = self.fds.getSiteIdxBySite(li[1])
                    if si == None:
                        errstr = "Unknown site '%s' in dst_filter" % li[1]
                        return FlexlabInvalidArgumentFault(10,errstr)
                    dfsites.append(str(si))
                    pass
                else:
                    # this should not happen, due to checks at the top of
                    # this loop
                    pass
                pass
            pass

        valid_compare_ops = ('<','<=','>=','>')
        if argdict.has_key("value_constraints"):
            vconst = []
            if not type(argdict["value_constraints"]) == types.ListType:
                argdict["value_constraints"] = [argdict["value_constraints"],]
                pass

            for li in argdict["value_constraints"]:
                # per-element arg checks:
                if not type(li) == types.ListType or \
                   not len(li) == 2 or \
                   not type(li[0]) == types.StringType or \
                   not li[0] in valid_compare_ops or \
                   not (type(li[1]) == types.IntType or \
                        type(li[1]) == types.FloatType):
                    errstr = "Malformed argument 'value_constraints'"
                    return FlexlabInvalidArgumentFault(10,errstr)

                vconst.append(li)
                pass
            pass

        valid_datasources = ('latest','recent','archive')
        if argdict.has_key("datasrc"):
            # Depending on the interval arguments, we allow them to override
            # here.  We always allow them to override with 'latest' if the
            # interval touches the last N seconds.  Otherwise, if the interval
            # touches the last N seconds, we push the query to the opsdb.
            # Otherwise, we force them to the dpdb.
            if in_cache_interval and argdict["datasrc"] == 'latest':
                datasource = argdict["datasrc"]
                pass
            pass

        # whew!  now, compose the query if we're not fetching out of mem...
        query = "select srcsite_idx,srcnode_idx,dstsite_idx,dstnode_idx," \
                "   unixstamp,%s" \
                " from pair_data force index (unixstamp)" \
                " where unixstamp >= %d and unixstamp <= %d" % \
                (mtype,t0,t1)

        # first, site filters (probably they're faster):
        if not sfsites == None and len(sfsites) > 0:
            query += " and srcsite_idx in (" + string.join(sfsites,',') + ")"
            pass
        if not dfsites == None and len(dfsites) > 0:
            query += " and dstsite_idx in (" + string.join(dfsites,',') + ")"
            pass
        if not sfnodes == None and len(sfnodes.keys()) > 0:
            query += " and ("
            count = 0
            for (k,v) in sfnodes.iteritems():
                if count > 0:
                    query += " or "
                    pass
                query += "(srcsite_idx in (" + str(k) + ") and " \
                         " srcnode_idx in (" + string.join(v,',') + "))"
                count += 1
                pass
            query += ")"
            pass
        if not dfnodes == None and len(dfnodes.keys()) > 0:
            query += " and ("
            count = 0
            for (k,v) in dfnodes.iteritems():
                if count > 0:
                    query += " or "
                    pass
                query += "(dstsite_idx in (" + str(k) + ") and " \
                         " dstnode_idx in (" + string.join(v,',') + "))"
                count += 1
                pass
            query += ")"
            pass

        # now add the value constraints:
        if not vconst == None and len(vconst) > 0:
            for vc in vconst:
                query += " and %s %s %d" % (mtype,vc[0],vc[1])
                pass
            pass

        # setup the db source
        src = ""
        if datasource == "recent":
            src = "opsdb"
            pass
        elif datasource == "archive":
            src = "dpdb"
            pass

        if self.debug:
            print "getMeasurements db query (src: %s): %s" % (datasource,query)
            pass

        try:
            qres = self.fds.queryDB(src,query)
        except:
            traceback.print_exc()
            return FlexlabFault(22,"Error in query execution!")

        # dump out
        for elm in qres:
            retval.append(elm)
            pass

        return retval

    def __timeToUTC(self,ts):
        t = time.mktime(ts)
        if time.daylight:
            t = t + time.altzone
            pass
        else:
            t = t + time.timezone
            pass
        return t

    def getNodeInfo(self,argdict={}):
        """
        Returns a 5-element list:
          [node_idx,site_idx,emulab_node_id,hostname,IP address]

        Arguments:
          node (str)        An Emulab node_id, hostname, or IP address.
        """
        retval = []

        # check args
        if argdict == None or type(argdict) != types.DictType:
            return FlexlabInvalidArgumentFault(10,"No arguments!")
        
        if argdict.has_key("node"):
            if type(argdict["node"]) == types.StringType:

                pass
            else:
                return FlexlabInvalidArgumentFault(11,"Must supply a str for node!")
            pass
        else:
            return FlexlabInvalidArgumentFault(10,"Must supply node argument!")

        retval = self.fds.getNodeInfo(argdict["node"])

        return retval
754 755 756 757 758 759 760 761 762

    def getNodeSiteMap(self,argdict={}):
        """
        Returns a list of 4-element lists:
          [site_name,site_idx,emulab_node_id,node_idx]

        Arguments: none
        """
        return self.fds.getNodeSiteMap()
763 764 765 766 767 768 769 770
    
    def getFullyConnectedSet(self,argdict={}):
        """
        Returns a set of PlanetLab nodes that are fully connected based
        on flexmon data.

        Arguments:
          size (int)        Size of the fully connected set to find.
771
                            A size of 0 means give maximal clique size.
772 773 774 775 776 777
          nodefilter (list) A list of Emulab node_ids or PlanetLab hostnames
                            with which to restrict subset selection.
          filtertype (bool) If true, a fully connected set is chosen based on
                            only the nodes in the nodefilter list.  If false,
                            a subset is chosen from all nodes except the nodes
                            in the nodefilter list.
778 779 780 781 782 783 784
          searchtype (str)  Type of search to run.  'fastfallible' will search
                            linearly through known nodes and form a set this
                            way (it's highly dependent on which nodes are
                            selected initially into the set).  'maxclique' will
                            run a max clique heuristic over the set of nodes
                            that are supplied (or all known nodes, if none are
                            supplied), and will select the k-best nodes.
Sachin Goyal's avatar
Sachin Goyal committed
785
          meastype (str)    Measurements to be considered - 'lat','bw','both'
786 787 788 789 790 791 792
        Returns:
          A list of Emulab node_ids.
        """
        retval = ''

        # check args
        if argdict == None or type(argdict) != types.DictType:
David Johnson's avatar
David Johnson committed
793
            return FlexlabInvalidArgumentFault(10,"No arguments!")
794
        
795 796 797 798
        if argdict.has_key("size"):
            try:
                size = int(argdict["size"])
            except:
David Johnson's avatar
David Johnson committed
799
                return FlexlabInvalidArgumentFault(11,"Must supply a positive set size!")
Sachin Goyal's avatar
Sachin Goyal committed
800
            if size < 0:
David Johnson's avatar
David Johnson committed
801
                return FlexlabInvalidArgumentFault(11,"Must supply a positive set size!")
802 803
            pass
        else:
David Johnson's avatar
David Johnson committed
804
            return FlexlabInvalidArgumentFault(11,"Must supply a positive set size!")
805 806 807
        
        nodefilterlist = []
        filtertype = True
808
	meastype = ''
Sachin Goyal's avatar
Sachin Goyal committed
809
	searchtype = ''
810 811 812 813
        
        if argdict.has_key("nodefilter"):
            nodefilterlist = argdict["nodefilter"]
            if type(nodefilterlist) != types.ListType:
David Johnson's avatar
David Johnson committed
814
                return FlexlabInvalidArgumentFault(12,"nodefilterlist must be a list!")
815 816 817 818 819
            pass
        else:
            filtertype = False
            pass

820 821
        if argdict.has_key("meastype"):
            meastype = argdict["meastype"]
Sachin Goyal's avatar
Sachin Goyal committed
822 823
            if meastype != 'lat' and meastype != 'bw' and meastype != 'both':
                return FlexlabInvalidArgumentFault(12,"meastype can be only lat, bw or both!")
824 825 826 827 828
            pass
        else:
            meastype = 'lat'
            pass

Sachin Goyal's avatar
Sachin Goyal committed
829 830 831 832 833 834 835 836 837
        if argdict.has_key("searchtype"):
            searchtype = argdict["searchtype"]
            if searchtype != 'fastfallible' and searchtype != 'maxclique':
                return FlexlabInvalidArgumentFault(12,"searchtype can be only fastfallible or maxclique!")
            pass
        else:
            searchtype = 'maxclique'
            pass

838 839
        if argdict.has_key("filtertype"):
            filtertype = argdict["filtertype"]
840 841 842 843 844 845
            if type(filtertype) == types.IntType:
                if filtertype == 0:
                    filtertype = False
                else:
                    filtertype = True
                pass
846
            if type(filtertype) != types.BooleanType:
David Johnson's avatar
David Johnson committed
847
                return FlexlabInvalidArgumentFault(13,"filtertype must be a bool!")
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
            pass

        # grab our final node list:
        availnodes = self.fds.getAvailNodes()
        finalnodes = []
        if filtertype:
            finalnodes = nodefilterlist
            pass
        else:
            for n in availnodes:
                if not n in nodefilterlist:
                    finalnodes.append(n)
                    pass
                pass
            pass

        try:
865 866 867
	    if searchtype == 'oldbad':
            	retval = self._simpleGoodnodes(size,finalnodes)
            else:
Sachin Goyal's avatar
Sachin Goyal committed
868
            	retval = self._GraphGoodnodes(meastype,size,finalnodes)
869
        except:
870 871
            retval = FlexlabFault(99,"%s:%s" % (sys.exc_type,sys.exc_value))
            traceback.print_exc()
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976

        return retval

    def _simpleGoodnodes(self,size,nlist):
        retval = []

        if self.debug:
            print "size=%d,nlist=%s" % (size,str(nlist))

        tpool = list(nlist)
        cpool = []
        usedsites = dict({})

        foundit = False
        sitematrix = dict({})

        while not foundit:
            siterank = dict({})
            
            while len(cpool) != size and len(tpool) > 0:
                elm = tpool.pop(0)
                elmsite = self.fds.nodeidToSiteIdx[elm]
                if not usedsites.has_key(elmsite):
                    usedsites[elmsite] = elm
                    cpool.append(elm)
                pass

            if self.debug:
                print "cpool=%s" % str(cpool)
            
            # can't find a set
            if len(cpool) != size and len(tpool) == 0:
                break

            # test current site pool connectivity:
            sites = usedsites.keys()
            for i in range(0,len(sites) - 1):
                ss = sites[i]
                for j in range(i+1,len(sites)):
                    ds = sites[j]

                    if not sitematrix.has_key(ss):
                        sitematrix[ss] = dict({})
                    if not sitematrix.has_key(ds):
                        sitematrix[ds] = dict({})

                    if not sitematrix[ss].has_key(ds) \
                        or not sitematrix[ds].has_key(ss):
                        lat = self.fds.getMeasurement('lat',ss,ds)
                        if lat == None:
                            lat = self.fds.getMeasurement('lat',ds,ss)
                        fbw = self.fds.getMeasurement('bw',ss,ds)
                        bbw = self.fds.getMeasurement('bw',ds,ss)

                        if not lat == None:
                            lat = 1
                        else:
                            lat = 0
                        if not fbw == None:
                            fbw = 1
                        else:
                            fbw = 0
                        if not bbw == None:
                            bbw = 1
                        else:
                            bbw = 0
                        
                        sitematrix[ss][ds] = lat + fbw
                        sitematrix[ds][ss] = lat + bbw

                        pass
                    if not siterank.has_key(ss):
                        siterank[ss] = 0
                    if not siterank.has_key(ds):
                        siterank[ds] = 0
                    
                    siterank[ds] += sitematrix[ss][ds]
                    siterank[ss] += sitematrix[ds][ss]
                    pass
                pass

            # see if the pool is fully connected; if not, evict the worst node:
            lowv = (size - 1) * 2
            lows = -1
            for i in range(0,len(sites)):
                if siterank.has_key(sites[i]) and siterank[sites[i]] < lowv:
                    lowv = siterank[sites[i]]
                    lows = sites[i]
                pass

            if not lowv == (size - 1) * 2:
                # must evict a node
                nid = usedsites[lows]
                del usedsites[lows]
                cpool.remove(nid)
                if self.debug:
                    print "removing nid %s" % nid
            else:
                foundit = True
            pass

        # return the nodelist
        if foundit:
            retval = usedsites.values()
        else:
David Johnson's avatar
David Johnson committed
977 978 979
            retval = FlexlabFault(20,
                                  "Could not find %d fully-connected sites!" \
                                  % size)
980 981 982 983
        return retval
    
    pass

984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046
    def _GraphGoodnodes(self,meastype,size,nlist):
        retval = []

        if self.debug:
            print "size=%d,nlist=%s" % (size,str(nlist))
	
	G=Graph()

        tpool = list(nlist)
        cpool = []
        usedsites = dict({})

        foundit = False
        sitematrix = dict({})

	while len(tpool) > 0:
		elm	 = tpool.pop(0)
		elmsite = self.fds.nodeidToSiteIdx[elm]
                if not usedsites.has_key(elmsite):
                    usedsites[elmsite] = elm
                    cpool.append(elm)
                pass

	if self.debug:
                print "cpool=%s" % str(cpool)
            
	# test current site pool connectivity:
       	sites = usedsites.keys()
       	for i in range(0,len(sites) - 1):
                ss = sites[i]
                for j in range(i+1,len(sites)):
                    ds = sites[j]

                    lat = self.fds.getMeasurement('lat',ss,ds)
                    if lat == None:
                            lat = self.fds.getMeasurement('lat',ds,ss)
                    fbw = self.fds.getMeasurement('bw',ss,ds)
                    bbw = self.fds.getMeasurement('bw',ds,ss)

                    if not lat == None:
                            lat = 1
                    else:
                            lat = 0
                    if not fbw == None:
                            fbw = 1
                    else:
                            fbw = 0
                    if not bbw == None:
                            bbw = 1
                    else:
                            bbw = 0
                        
		    if (meastype == "lat"):
		    	if lat > 0:
				G.add_edge((ss, ds))
				pass
                    	pass
		    if (meastype == "bw"):
			bw = fbw + bbw
		    	if bw > 1:
				G.add_edge((ss, ds))
				pass
                    	pass
Sachin Goyal's avatar
Sachin Goyal committed
1047 1048 1049 1050 1051 1052
		    if (meastype == "both"):
			both = lat + fbw + bbw
		    	if both > 2:
				G.add_edge((ss, ds))
				pass
                    	pass
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
                    
                pass

	cliques = find_cliques(G)
	cliquesize = graph_clique_number(G,cliques)

        csites = dict({})
	for c in cliques:
		if len(c) == cliquesize:
			for s in c:
				csites[s] = usedsites[s]
			break
		pass
	
	# if size is 0, return the maximal clique
	if size == 0:
		size = cliquesize	

        # return the nodelist
        if cliquesize >= size:			
		tmp = csites.values()
	        retval = tmp[0:size]
        else:
            	retval = FlexlabFault(20,
                                      "Could not find %d fully-connected sites!" \
                                      % size)
        return retval
    
    pass
1082 1083 1084 1085 1086

import getopt

DEF_HOST = '0.0.0.0'
DEF_PORT = 3993
1087 1088
#DEF_ACLLIST = ['155.98.32.0/20','155.98.60.0/24']
DEF_ACLLIST = []
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107

def usage(progname):
    print "Usage: %s [-hpa <arg>]" % str(progname)
    print "  -h <addr>    Binds to this IP address (default: %s)" % DEF_HOST
    print "  -p <port>    Listens on this port (default: %d)" % DEF_PORT
    print "  -a <acllist> Comma-separated list of networks that we accept"
    print "               requests from (default: '%s')" % str(DEF_ACLLIST)
    pass

def main():
    try:
        opts,args = getopt.getopt(sys.argv[1:],"h:p:a:")
    except getopt.GetoptError:
        usage(sys.argv[0])
        sys.exit(2)

    (host,port,acllist) = (DEF_HOST,DEF_PORT,DEF_ACLLIST)

    for (o,v) in opts:
David Johnson's avatar
David Johnson committed
1108
        if o == '-h':
1109 1110
            host = v
            pass
David Johnson's avatar
David Johnson committed
1111
        elif o == '-p':
1112 1113
            port = int(v)
            pass
David Johnson's avatar
David Johnson committed
1114
        elif o == '-a':
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142
            acllist = v.split(',')
            pass
        pass

    # setup the db lib so as NOT to send mail to TBOPS
    libdb.debug = True
    libdb.__dbMailOnFail = True
    libdb.__dbFailMailAddr = "johnsond@emulab.net"

    print "Initializing XMLRPC server..."
    server = BasicXMLRPCServers.InetAclXMLRPCServer((host,port),acllist)

    fds = FlexlabDataServer(autorefresh=60)
    print "Populating flexmon cache..."
    fds.populatePairDataCache()

    print "Registering XMLRPC functions..."
    server.register_introspection_functions()
    server.register_class_functions(flexlab,initargs=(fds,))
    
    print "Accepting requests now."
    server.serve_forever()
    sys.exit(0)


if __name__ == '__main__':
    main()
    pass