xmlrpcprotocol has been updated
[sfa.git] / sfa / client / sfascan.py
1 #!/usr/bin/python
2
3 import sys
4 import socket
5 import traceback
6 from urlparse import urlparse
7
8 import pygraphviz
9
10 from optparse import OptionParser
11
12 from sfa.client.sfi import Sfi
13 from sfa.util.sfalogging import logger
14 import sfa.util.xmlrpcprotocol as xmlrpcprotocol
15
16 def url_hostname_port (url):
17     if url.find("://")<0:
18         url="http://"+url
19     parsed_url=urlparse(url)
20     # 0(scheme) returns protocol
21     default_port='80'
22     if parsed_url[0]=='https': default_port='443'
23     # 1(netloc) returns the hostname+port part
24     parts=parsed_url[1].split(":")
25     # just a hostname
26     if len(parts)==1:
27         return (url,parts[0],default_port)
28     else:
29         return (url,parts[0],parts[1])
30
31 ###
32 class Interface:
33
34     def __init__ (self,url):
35         self._url=url
36         try:
37             (self._url,self.hostname,self.port)=url_hostname_port(url)
38             self.ip=socket.gethostbyname(self.hostname)
39             self.probed=False
40         except:
41 #            traceback.print_exc()
42             self.hostname="unknown"
43             self.ip='0.0.0.0'
44             self.port="???"
45             # don't really try it
46             self.probed=True
47             self._version={}
48
49     def url(self):
50         return self._url
51
52     # this is used as a key for creating graph nodes and to avoid duplicates
53     def uid (self):
54         return "%s:%s"%(self.ip,self.port)
55
56     # connect to server and trigger GetVersion
57     def get_version(self):
58         if self.probed:
59             return self._version
60         # dummy to meet Sfi's expectations for its 'options' field
61         class DummyOptions:
62             pass
63         options=DummyOptions()
64         options.verbose=False
65         options.timeout=30
66         try:
67             client=Sfi(options)
68             client.read_config()
69             key_file = client.get_key_file()
70             cert_file = client.get_cert_file(key_file)
71             url=self.url()
72             logger.info('issuing get version at %s'%url)
73             server=xmlrpcprotocol.get_server(url, key_file, cert_file, timeout=options.timeout, verbose=options.verbose)
74             self._version=server.GetVersion()
75         except:
76 #            traceback.print_exc()
77             self._version={}
78         self.probed=True
79         return self._version
80
81     @staticmethod
82     def multi_lines_label(*lines):
83         result='<<TABLE BORDER="0" CELLBORDER="0"><TR><TD>' + \
84             '</TD></TR><TR><TD>'.join(lines) + \
85             '</TD></TR></TABLE>>'
86 #        print 'multilines=',result
87         return result
88
89     # default is for when we can't determine the type of the service
90     # typically the server is down, or we can't authenticate, or it's too old code
91     shapes = {"registry": "diamond", "slicemgr":"ellipse", "aggregate":"box", 'default':'plaintext'}
92     abbrevs = {"registry": "REG", "slicemgr":"SA", "aggregate":"AM", 'default':'[unknown interface]'}
93
94     # return a dictionary that translates into the node's attr
95     def get_layout (self):
96         layout={}
97         ### retrieve cached GetVersion
98         version=self.get_version()
99         # set the href; xxx would make sense to try and 'guess' the web URL, not the API's one...
100         layout['href']=self.url()
101         ### set html-style label
102         ### see http://www.graphviz.org/doc/info/shapes.html#html
103         # if empty the service is unreachable
104         if not version:
105             label="offline"
106         else:
107             label=''
108             try: abbrev=Interface.abbrevs[version['interface']]
109             except: abbrev=Interface.abbrevs['default']
110             label += abbrev
111             if 'hrn' in version: label += " %s"%version['hrn']
112             else:                label += "[no hrn]"
113             if 'code_tag' in version: 
114                 label += " %s"%version['code_tag']
115             if 'testbed' in version:
116                 label += " (%s)"%version['testbed']
117         layout['label']=Interface.multi_lines_label(self.url(),label)
118         ### set shape
119         try: shape=Interface.shapes[version['interface']]
120         except: shape=Interface.shapes['default']
121         layout['shape']=shape
122         ### fill color to outline wrongly configured bodies
123         if 'geni_api' not in version and 'sfa' not in version:
124             layout['style']='filled'
125             layout['fillcolor']='gray'
126         return layout
127
128 class SfaScan:
129
130     # provide the entry points (a list of interfaces)
131     def __init__ (self, left_to_right=False, verbose=False):
132         self.verbose=verbose
133         self.left_to_right=left_to_right
134
135     def graph (self,entry_points):
136         graph=pygraphviz.AGraph(directed=True)
137         if self.left_to_right: 
138             graph.graph_attr['rankdir']='LR'
139         self.scan(entry_points,graph)
140         return graph
141     
142     # scan from the given interfaces as entry points
143     def scan(self,interfaces,graph):
144         if not isinstance(interfaces,list):
145             interfaces=[interfaces]
146
147         # remember node to interface mapping
148         node2interface={}
149         # add entry points right away using the interface uid's as a key
150         to_scan=interfaces
151         for i in interfaces: 
152             graph.add_node(i.uid())
153             node2interface[graph.get_node(i.uid())]=i
154         scanned=[]
155         # keep on looping until we reach a fixed point
156         # don't worry about abels and shapes that will get fixed later on
157         while to_scan:
158             for interface in to_scan:
159                 # performing xmlrpc call
160                 version=interface.get_version()
161                 if self.verbose:
162                     logger.info("GetVersion at interface %s"%interface.url())
163                     if not version:
164                         logger.info("<EMPTY GetVersion(); offline or cannot authenticate>")
165                     else: 
166                         for (k,v) in version.iteritems(): 
167                             if not isinstance(v,dict):
168                                 logger.info("\r\t%s:%s"%(k,v))
169                             else:
170                                 logger.info(k)
171                                 for (k1,v1) in v.iteritems():
172                                     logger.info("\r\t\t%s:%s"%(k1,v1))
173                 # 'geni_api' is expected if the call succeeded at all
174                 # 'peers' is needed as well as AMs typically don't have peers
175                 if 'geni_api' in version and 'peers' in version: 
176                     # proceed with neighbours
177                     for (next_name,next_url) in version['peers'].iteritems():
178                         next_interface=Interface(next_url)
179                         # locate or create node in graph
180                         try:
181                             # if found, we're good with this one
182                             next_node=graph.get_node(next_interface.uid())
183                         except:
184                             # otherwise, let's move on with it
185                             graph.add_node(next_interface.uid())
186                             next_node=graph.get_node(next_interface.uid())
187                             node2interface[next_node]=next_interface
188                             to_scan.append(next_interface)
189                         graph.add_edge(interface.uid(),next_interface.uid())
190                 scanned.append(interface)
191                 to_scan.remove(interface)
192             # we've scanned the whole graph, let's get the labels and shapes right
193             for node in graph.nodes():
194                 interface=node2interface.get(node,None)
195                 if interface:
196                     for (k,v) in interface.get_layout().iteritems():
197                         node.attr[k]=v
198                 else:
199                     logger.error("MISSED interface with node %s"%node)
200     
201
202 default_outfiles=['sfa.png','sfa.svg','sfa.dot']
203
204 def main():
205     usage="%prog [options] url-entry-point(s)"
206     parser=OptionParser(usage=usage)
207     parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
208                       help="output filenames (cumulative) - defaults are %r"%default_outfiles)
209     parser.add_option("-l","--left-to-right",action="store_true",dest="left_to_right",default=False,
210                       help="instead of top-to-bottom")
211     parser.add_option("-v","--verbose",action='store_true',dest='verbose',default=False,
212                       help="verbose")
213     (options,args)=parser.parse_args()
214     if not args:
215         parser.print_help()
216         sys.exit(1)
217     if not options.outfiles:
218         options.outfiles=default_outfiles
219     scanner=SfaScan(left_to_right=options.left_to_right, verbose=options.verbose)
220     entries = [ Interface(entry) for entry in args ]
221     g=scanner.graph(entries)
222     logger.info("creating layout")
223     g.layout(prog='dot')
224     for outfile in options.outfiles:
225         logger.info("drawing in %s"%outfile)
226         g.draw(outfile)
227     logger.info("done")
228
229 if __name__ == '__main__':
230     main()