really fixed the redundant logging issue this time.
[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         try:
66             client=Sfi(options)
67             client.read_config()
68             key_file = client.get_key_file()
69             cert_file = client.get_cert_file(key_file)
70             url=self.url()
71             logger.info('issuing get version at %s'%url)
72             server=xmlrpcprotocol.get_server(url, key_file, cert_file, options)
73             self._version=server.GetVersion()
74         except:
75 #            traceback.print_exc()
76             self._version={}
77         self.probed=True
78         return self._version
79
80     @staticmethod
81     def multi_lines_label(*lines):
82         result='<<TABLE BORDER="0" CELLBORDER="0"><TR><TD>' + \
83             '</TD></TR><TR><TD>'.join(lines) + \
84             '</TD></TR></TABLE>>'
85 #        print 'multilines=',result
86         return result
87
88     # default is for when we can't determine the type of the service
89     # typically the server is down, or we can't authenticate, or it's too old code
90     shapes = {"registry": "diamond", "slicemgr":"ellipse", "aggregate":"box", 'default':'plaintext'}
91     abbrevs = {"registry": "REG", "slicemgr":"SA", "aggregate":"AM", 'default':'[unknown interface]'}
92
93     # return a dictionary that translates into the node's attr
94     def get_layout (self):
95         layout={}
96         ### retrieve cached GetVersion
97         version=self.get_version()
98         # set the href; xxx would make sense to try and 'guess' the web URL, not the API's one...
99         layout['href']=self.url()
100         ### set html-style label
101         ### see http://www.graphviz.org/doc/info/shapes.html#html
102         # if empty the service is unreachable
103         if not version:
104             label="offline"
105         else:
106             label=''
107             try: abbrev=Interface.abbrevs[version['interface']]
108             except: abbrev=Interface.abbrevs['default']
109             label += abbrev
110             if 'hrn' in version: label += " %s"%version['hrn']
111             else:                label += "[no hrn]"
112             if 'code_tag' in version: 
113                 label += " %s"%version['code_tag']
114             if 'testbed' in version:
115                 label += " (%s)"%version['testbed']
116         layout['label']=Interface.multi_lines_label(self.url(),label)
117         ### set shape
118         try: shape=Interface.shapes[version['interface']]
119         except: shape=Interface.shapes['default']
120         layout['shape']=shape
121         ### fill color to outline wrongly configured bodies
122         if 'geni_api' not in version and 'sfa' not in version:
123             layout['style']='filled'
124             layout['fillcolor']='gray'
125         return layout
126
127 class SfaScan:
128
129     # provide the entry points (a list of interfaces)
130     def __init__ (self, left_to_right=False, verbose=False):
131         self.verbose=verbose
132         self.left_to_right=left_to_right
133
134     def graph (self,entry_points):
135         graph=pygraphviz.AGraph(directed=True)
136         if self.left_to_right: 
137             graph.graph_attr['rankdir']='LR'
138         self.scan(entry_points,graph)
139         return graph
140     
141     # scan from the given interfaces as entry points
142     def scan(self,interfaces,graph):
143         if not isinstance(interfaces,list):
144             interfaces=[interfaces]
145
146         # remember node to interface mapping
147         node2interface={}
148         # add entry points right away using the interface uid's as a key
149         to_scan=interfaces
150         for i in interfaces: 
151             graph.add_node(i.uid())
152             node2interface[graph.get_node(i.uid())]=i
153         scanned=[]
154         # keep on looping until we reach a fixed point
155         # don't worry about abels and shapes that will get fixed later on
156         while to_scan:
157             for interface in to_scan:
158                 # performing xmlrpc call
159                 version=interface.get_version()
160                 if self.verbose:
161                     logger.info("GetVersion at interface %s"%interface.url())
162                     if not version:
163                         logger.info("<EMPTY GetVersion(); offline or cannot authenticate>")
164                     else: 
165                         for (k,v) in version.iteritems(): 
166                             if not isinstance(v,dict):
167                                 logger.info("\r\t%s:%s"%(k,v))
168                             else:
169                                 logger.info(k)
170                                 for (k1,v1) in v.iteritems():
171                                     logger.info("\r\t\t%s:%s"%(k1,v1))
172                 # 'geni_api' is expected if the call succeeded at all
173                 # 'peers' is needed as well as AMs typically don't have peers
174                 if 'geni_api' in version and 'peers' in version: 
175                     # proceed with neighbours
176                     for (next_name,next_url) in version['peers'].iteritems():
177                         next_interface=Interface(next_url)
178                         # locate or create node in graph
179                         try:
180                             # if found, we're good with this one
181                             next_node=graph.get_node(next_interface.uid())
182                         except:
183                             # otherwise, let's move on with it
184                             graph.add_node(next_interface.uid())
185                             next_node=graph.get_node(next_interface.uid())
186                             node2interface[next_node]=next_interface
187                             to_scan.append(next_interface)
188                         graph.add_edge(interface.uid(),next_interface.uid())
189                 scanned.append(interface)
190                 to_scan.remove(interface)
191             # we've scanned the whole graph, let's get the labels and shapes right
192             for node in graph.nodes():
193                 interface=node2interface.get(node,None)
194                 if interface:
195                     for (k,v) in interface.get_layout().iteritems():
196                         node.attr[k]=v
197                 else:
198                     logger.error("MISSED interface with node %s"%node)
199     
200
201 default_outfiles=['sfa.png','sfa.svg','sfa.dot']
202
203 def main():
204     usage="%prog [options] url-entry-point(s)"
205     parser=OptionParser(usage=usage)
206     parser.add_option("-o","--output",action='append',dest='outfiles',default=[],
207                       help="output filenames (cumulative) - defaults are %r"%default_outfiles)
208     parser.add_option("-l","--left-to-right",action="store_true",dest="left_to_right",default=False,
209                       help="instead of top-to-bottom")
210     parser.add_option("-v","--verbose",action='store_true',dest='verbose',default=False,
211                       help="verbose")
212     (options,args)=parser.parse_args()
213     if not args:
214         parser.print_help()
215         sys.exit(1)
216     if not options.outfiles:
217         options.outfiles=default_outfiles
218     scanner=SfaScan(left_to_right=options.left_to_right, verbose=options.verbose)
219     entries = [ Interface(entry) for entry in args ]
220     g=scanner.graph(entries)
221     logger.info("creating layout")
222     g.layout(prog='dot')
223     for outfile in options.outfiles:
224         logger.info("drawing in %s"%outfile)
225         g.draw(outfile)
226     logger.info("done")
227
228 if __name__ == '__main__':
229     main()