-#!/usr/bin/env python
-
import sys, os.path
import pickle
import time
import traceback
from urlparse import urlparse
-import pygraphviz
+try:
+ import pygraphviz
+except:
+ print 'Warning, could not import pygraphviz, test mode only'
from optparse import OptionParser
+from sfa.client.return_value import ReturnValue
from sfa.client.sfi import Sfi
from sfa.util.sfalogging import logger, DEBUG
-import sfa.client.xmlrpcprotocol as xmlrpcprotocol
+from sfa.client.sfaserverproxy import SfaServerProxy
def url_hostname_port (url):
if url.find("://")<0:
### assuming everything is sequential, as simple as it gets
### { url -> (timestamp,version)}
class VersionCache:
+ # default expiration period is 1h
def __init__ (self, filename=None, expires=60*60):
# default is to store cache in the same dir as argv[0]
if filename is None:
return None
###
+# non-existing hostnames happen...
+# for better perfs we cache the result of gethostbyname too
class Interface:
- def __init__ (self,url,verbose=False):
+ def __init__ (self,url,mentioned_in=None,verbose=False):
self._url=url
self.verbose=verbose
+ cache=VersionCache()
+ key="interface:%s"%url
try:
(self._url,self.hostname,self.port)=url_hostname_port(url)
- self.ip=socket.gethostbyname(self.hostname)
- self.probed=False
+ # look for ip in the cache
+ tuple=cache.get(key)
+ if tuple:
+ (self.hostname, self.ip, self.port) = tuple
+ else:
+ self.ip=socket.gethostbyname(self.hostname)
except:
+ msg="can't resolve hostname %s\n\tfound in url %s"%(self.hostname,self._url)
+ if mentioned_in:
+ msg += "\n\t(mentioned at %s)"%mentioned_in
+ logger.warning (msg)
self.hostname="unknown"
self.ip='0.0.0.0'
self.port="???"
+
+ cache.set(key, (self.hostname, self.ip, self.port,) )
+ cache.save()
+ self.probed=False
+
+ # mark unknown interfaces as probed to avoid unnecessary attempts
+ if self.hostname=='unknown':
# don't really try it
self.probed=True
self._version={}
+
def url(self):
return self._url
logger.debug("searching in version cache %s"%self.url())
cached_version = VersionCache().get(self.url())
if cached_version is not None:
- logger.info("Retrieved version info from cache")
+ logger.info("Retrieved version info from cache %s"%self.url())
return cached_version
### otherwise let's do the hard work
# dummy to meet Sfi's expectations for its 'options' field
try:
client=Sfi(options)
client.read_config()
- key_file = client.get_key_file()
- cert_file = client.get_cert_file(key_file)
+ client.bootstrap()
+ key_file = client.private_key
+ cert_file = client.my_gid
+ logger.debug("using key %s & cert %s"%(key_file,cert_file))
url=self.url()
logger.info('issuing GetVersion at %s'%url)
# setting timeout here seems to get the call to fail - even though the response time is fast
- #server=xmlrpcprotocol.server_proxy(url, key_file, cert_file, verbose=self.verbose, timeout=options.timeout)
- server=xmlrpcprotocol.server_proxy(url, key_file, cert_file, verbose=self.verbose)
- self._version=server.GetVersion()
+ #server=SfaServerProxy(url, key_file, cert_file, verbose=self.verbose, timeout=options.timeout)
+ server=SfaServerProxy(url, key_file, cert_file, verbose=self.verbose)
+ self._version=ReturnValue.get_value(server.GetVersion())
except:
logger.log_exc("failed to get version")
self._version={}
try: shape=Interface.shapes[version['interface']]
except: shape=Interface.shapes['default']
layout['shape']=shape
- ### fill color to outline wrongly configured bodies
- if 'geni_api' not in version and 'sfa' not in version:
+ ### fill color to outline wrongly configured or unreachable bodies
+ # as of sfa-2.0 registry doesn't have 'sfa' not 'geni_api', but have peers
+ # slicemgr and aggregate have 'geni_api' and 'sfa'
+ if 'geni_api' not in version and 'peers' not in version:
layout['style']='filled'
layout['fillcolor']='gray'
return layout
-class SfaScan:
+class Scanner:
# provide the entry points (a list of interfaces)
def __init__ (self, left_to_right=False, verbose=False):
logger.debug(k)
for (k1,v1) in v.iteritems():
logger.debug("\r\t\t%s:%s"%(k1,v1))
- # 'geni_api' is expected if the call succeeded at all
- # 'peers' is needed as well as AMs typically don't have peers
- if 'geni_api' in version and 'peers' in version:
- # proceed with neighbours
+ # proceed with neighbours
+ if 'peers' in version:
for (next_name,next_url) in version['peers'].iteritems():
- next_interface=Interface(next_url)
+ next_interface=Interface(next_url,mentioned_in=interface.url())
# locate or create node in graph
try:
# if found, we're good with this one
logger.error("MISSED interface with node %s"%node)
-default_outfiles=['sfa.png','sfa.svg','sfa.dot']
+class SfaScan:
-def main():
- usage="%prog [options] url-entry-point(s)"
- parser=OptionParser(usage=usage)
- parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
- help="output filenames (cumulative) - defaults are %r"%default_outfiles)
- parser.add_option("-l","--left-to-right",action="store_true",dest="left_to_right",default=False,
- help="instead of top-to-bottom")
- parser.add_option("-v", "--verbose", action="count", dest="verbose", default=0,
- help="verbose - can be repeated for more verbosity")
- parser.add_option("-c", "--clean-cache",action='store_true',
- dest='clean_cache',default=False,
- help='clean/trash version cache and exit')
- parser.add_option("-s","--show-cache",action='store_true',
- dest='show_cache',default=False,
- help='show/display version cache')
-
- (options,args)=parser.parse_args()
- logger.enable_console()
- # apply current verbosity to logger
- logger.setLevelFromOptVerbose(options.verbose)
- # figure if we need to be verbose for these local classes that only have a bool flag
- bool_verbose=logger.getBoolVerboseFromOpt(options.verbose)
+ default_outfiles=['sfa.png','sfa.svg','sfa.dot']
- if options.show_cache:
- VersionCache().show()
- sys.exit(0)
- if options.clean_cache:
- VersionCache().clean()
- sys.exit(0)
- if not args:
- parser.print_help()
- sys.exit(1)
+ def main(self):
+ usage="%prog [options] url-entry-point(s)"
+ parser=OptionParser(usage=usage)
+ parser.add_option("-d", "--dir", dest="sfi_dir",
+ help="config & working directory - default is " + Sfi.default_sfi_dir(),
+ metavar="PATH", default=Sfi.default_sfi_dir())
+ parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
+ help="output filenames (cumulative) - defaults are %r"%SfaScan.default_outfiles)
+ parser.add_option("-l","--left-to-right",action="store_true",dest="left_to_right",default=False,
+ help="instead of top-to-bottom")
+ parser.add_option("-v", "--verbose", action="count", dest="verbose", default=0,
+ help="verbose - can be repeated for more verbosity")
+ parser.add_option("-c", "--clean-cache",action='store_true',
+ dest='clean_cache',default=False,
+ help='clean/trash version cache and exit')
+ parser.add_option("-s","--show-cache",action='store_true',
+ dest='show_cache',default=False,
+ help='show/display version cache')
- if not options.outfiles:
- options.outfiles=default_outfiles
- scanner=SfaScan(left_to_right=options.left_to_right, verbose=bool_verbose)
- entries = [ Interface(entry) for entry in args ]
- g=scanner.graph(entries)
- logger.info("creating layout")
- g.layout(prog='dot')
- for outfile in options.outfiles:
- logger.info("drawing in %s"%outfile)
- g.draw(outfile)
- logger.info("done")
+ (options,args)=parser.parse_args()
+ logger.enable_console()
+ # apply current verbosity to logger
+ logger.setLevelFromOptVerbose(options.verbose)
+ # figure if we need to be verbose for these local classes that only have a bool flag
+ bool_verbose=logger.getBoolVerboseFromOpt(options.verbose)
+
+ if options.show_cache:
+ VersionCache().show()
+ sys.exit(0)
+ if options.clean_cache:
+ VersionCache().clean()
+ sys.exit(0)
+ if not args:
+ parser.print_help()
+ sys.exit(1)
+
+ if not options.outfiles:
+ options.outfiles=SfaScan.default_outfiles
+ scanner=Scanner(left_to_right=options.left_to_right, verbose=bool_verbose)
+ entries = [ Interface(entry,mentioned_in="command line") for entry in args ]
+ try:
+ g=scanner.graph(entries)
+ logger.info("creating layout")
+ g.layout(prog='dot')
+ for outfile in options.outfiles:
+ logger.info("drawing in %s"%outfile)
+ g.draw(outfile)
+ logger.info("done")
+ # test mode when pygraphviz is not available
+ except:
+ entry=entries[0]
+ print "GetVersion at %s returned %s"%(entry.url(),entry.get_version())
-if __name__ == '__main__':
- main()