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