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