xentaur

annotate xentaur.py @ 39:54b7c7ae50ec

brodge-bridge link visualisation
author igor
date Fri Sep 28 13:35:54 2007 +0300 (2007-09-28)
parents f71b298798c2
children 10444acb35f4
rev   line source
igor@0 1 #!/usr/bin/python
igor@0 2
igor@0 3
igor@2 4 import sys,os,time
igor@35 5
igor@35 6 xentaur_path=os.environ['HOME']+"/xentaur"
igor@35 7
igor@0 8 sys.path.append('/etc/xen')
igor@35 9 sys.path.append(xentaur_path)
igor@11 10
nata@36 11 network='netw'
nata@36 12 domain='dyn3'
igor@35 13 from xendomain import *
igor@35 14
igor@29 15 bridges_turned_down=[]
igor@29 16
igor@0 17 from IPython.Shell import IPShellEmbed
igor@0 18
igor@2 19
igor@2 20 screenrc=os.environ['HOME']+"/.screenrc_xentaur"
igor@2 21
igor@10 22 def run(program, *args):
igor@10 23 pid = os.fork()
igor@10 24 if not pid:
igor@10 25 os.execvp(program, (program,) + args)
igor@10 26 return os.wait()[0]
igor@10 27
igor@10 28 def run_command(line):
igor@10 29 #cmds=line.split()
igor@10 30 #run(cmds[0],*cmds[1:])
igor@10 31 run("/bin/sh", "-c", line)
igor@10 32
igor@23 33 def run_command_return_stdout(line):
igor@23 34 p = os.popen(line)
igor@23 35 output = p.read()
igor@23 36 p.close()
igor@23 37 return output
igor@23 38
igor@0 39 def create_bridges_script():
igor@38 40 unbound_bridges=set(bridges)-set(real_bridges)
igor@4 41 create_unbound_bridges="\n".join(map(lambda x: "sudo /usr/sbin/brctl show | awk '{print $1}' | grep -q "+x+" || sudo /usr/sbin/brctl addbr "+x, unbound_bridges))
igor@4 42 create_unbound_bridges+="\n"+"\n".join(map(lambda x: "sudo /bin/ip link set "+x+" up", unbound_bridges))
igor@0 43
igor@0 44 print """#!/bin/sh
igor@0 45 # create unbound bridges
igor@0 46 %(create_unbound_bridges)s
igor@0 47 """ % {'create_unbound_bridges' : create_unbound_bridges}
igor@0 48
igor@0 49
igor@0 50 def create_domains_script():
igor@35 51 for domain in domains:
igor@38 52 if not domain in real_nodes:
igor@38 53 print "sudo /usr/sbin/xm create "+xentaur_path+"/xendomain.py "+" domain="+domain+" network="+network+" && sleep 1 && sudo /usr/sbin/xm sched-credit -d $(sudo /usr/sbin/xm list | grep "+domain+" | awk '{print $2}') -c 10 && sleep 1"
igor@0 54
igor@0 55 def destroy_domains_script():
igor@35 56 for domain in domains:
igor@38 57 if not domain in real_nodes:
igor@38 58 print "sudo /usr/sbin/xm shutdown "+domain
igor@0 59
igor@0 60 def create_screens_script():
igor@7 61 N=1
igor@0 62 screens=[]
igor@0 63 for domain in domains:
igor@11 64 screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do sudo xm console "+domain+" ; echo Retrying in 5 seconds...; sleep 5; done'")
igor@0 65 N+=1
igor@0 66 screenlist="\n".join(screens)
igor@0 67
igor@11 68 #
igor@11 69 # Previous terminal acccess commands:
igor@11 70 # ip="192.168.80."+str(200+N)
igor@11 71 # screens.append("screen -t "+domain+" "+str(N)+" sh -c 'while true; do ssh root@"+ip+" ; done'")
igor@11 72 #
igor@11 73
igor@4 74 #hardstatus string "\%{gk}\%c \%{yk}\%M\%d \%{wk}\%?\%-Lw\%?\%{bw}\%n*\%f\%t\%?(\%u)\%?\%{wk}\%?\%+Lw\%?"
igor@10 75
igor@11 76 f=open(screenrc, "w");
igor@10 77 f.write("""
igor@2 78 hardstatus on
igor@2 79 hardstatus alwayslastline
igor@2 80
igor@0 81 screen -t console 0 bash
igor@10 82 %s
igor@10 83 """ % (screenlist))
igor@10 84 f.close()
igor@0 85
igor@0 86 def graph_node(node):
igor@0 87 i=0
igor@0 88 domain_type={}
igor@0 89 for domain in domains:
igor@0 90 domain_type[domain]=domain_types[i]
igor@0 91 i+=1
nata@31 92 return node+" [label=\" "+node+"\",shapefile=\"shapes/all/"+domain_type[node]+".png\",fontcolor=black,fontsize=16]"
igor@0 93
igor@29 94 def graph_bridge(bridge):
igor@29 95 if bridge in hidden_bridges:
igor@29 96 return ""
igor@38 97 if bridge in real_bridges:
igor@38 98 return "%s [shape=none,shapefile=\"shapes/all/real_switch.png\"]" % (bridge)
igor@38 99 elif bridge in bridges_turned_down:
igor@29 100 return "%s [shape=none,shapefile=\"shapes/all/switch_turned_down.png\"]" % (bridge)
igor@29 101 else:
igor@29 102 return "%s [shape=none,shapefile=\"shapes/all/switch.png\"]" % (bridge)
igor@29 103
igor@29 104
igor@0 105 def graph():
igor@0 106 nodelist=""
igor@0 107 bridgelist=""
igor@0 108 linklist=""
igor@0 109 physicallist=""
igor@0 110 networklist=""
igor@0 111
igor@0 112 nodelist=";\n ".join(map(graph_node,nodes))
igor@0 113 if nodelist: nodelist += ";"
igor@0 114
igor@29 115 bridgelist=";\n ".join(map(graph_bridge,bridges))
igor@0 116 if bridgelist: bridgelist += ";"
igor@0 117
igor@0 118 links=[]
igor@0 119 for host, bridges_raw in vbridges_table.iteritems():
igor@0 120 i=0
igor@0 121 for this_bridge in bridges_raw:
igor@27 122 if this_bridge in hidden_bridges or not this_bridge:
igor@0 123 continue
igor@28 124 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@28 125 links.append(host+" -- "+this_bridge+" [taillabel=\"fa"+str(bridges_raw.index(this_bridge))+"/0\"]")
igor@27 126 i+=1
igor@0 127
igor@0 128 for link in temporary_links:
igor@28 129 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",color=blue,len=10,w=5,weight=5]")
igor@0 130
igor@27 131 for link in broken_links:
igor@28 132 links.append(link[0]+" -- "+link[2]+" [taillabel=\"fa"+str(link[1])+"/0\",style=dashed]")
igor@27 133
igor@39 134
igor@39 135 # bridge-bridge links
igor@39 136 for host, bridges_raw in bridge_bridge_table.iteritems():
igor@39 137 i=0
igor@39 138 for this_bridge in bridges_raw:
igor@39 139 if this_bridge in hidden_bridges or not this_bridge:
igor@39 140 continue
igor@39 141 if not [ host, bridges_raw.index(this_bridge), this_bridge ] in temporary_links:
igor@39 142 links.append(host+" -- "+this_bridge)
igor@39 143 i+=1
igor@39 144
igor@39 145
igor@39 146
igor@0 147 linklist=";\n ".join(links)
igor@0 148
igor@0 149 graph_dot = {
igor@0 150 'nodelist' : nodelist,
igor@0 151 'bridgelist' : bridgelist,
igor@0 152 'linklist' : linklist,
igor@0 153 'physicallist' : physicallist,
igor@0 154 'networklist' : networklist,
igor@0 155 }
igor@0 156
igor@10 157 f = open("xenomips.dot", "w");
igor@10 158 f.write ("""
igor@0 159 graph G {
igor@0 160 edge [len=1.25];
igor@0 161 splines=true;
igor@0 162 // nodes
igor@0 163
igor@0 164 node [shape=plaintext,color=white,shapefile="shapes/cisco.bmp/router.png"];
igor@0 165 %(nodelist)s
igor@0 166
igor@0 167 // bridges
igor@0 168
igor@6 169 node [shape=none,shapefile="shapes/all/switch.png"];
igor@0 170 %(bridgelist)s
igor@0 171
igor@0 172 // physical
igor@0 173
igor@0 174 node [shape=rectangle,color=blue];
igor@0 175 %(physicallist)s
igor@0 176
igor@0 177 // networks (not bridges, not physical)
igor@0 178 node [shape=rectangle,color=green];
igor@0 179 %(networklist)s
igor@0 180
igor@0 181 // links (between nodes and bridges)
igor@0 182 %(linklist)s
igor@0 183
igor@0 184 };
igor@10 185 """ % graph_dot)
igor@10 186 f.close()
igor@10 187 run_command("neato -Tpng -o xenomips.png xenomips.dot ")
igor@0 188
igor@27 189 def autoredraw():
igor@27 190 graph()
igor@27 191
igor@0 192 def start_all():
igor@0 193 create_bridges_script()
igor@0 194 create_screens_script()
igor@0 195 create_domains_script()
igor@0 196 graph()
igor@0 197 print """
igor@0 198 cat <<NOTE_FOR_USER
igor@0 199 # To view virtual network map, run:
igor@0 200 gqview xenomips.png
igor@0 201 # To attach to VM consoles, run:
igor@0 202 screen -c screenrc
igor@0 203 NOTE_FOR_USER
igor@0 204 """
igor@0 205
igor@0 206 def shell():
nata@31 207 autoredraw()
igor@0 208 ipshell = IPShellEmbed()
igor@0 209 ipshell()
igor@0 210
igor@0 211 def stop_all():
igor@4 212 destroy_domains_script()
igor@0 213
igor@0 214 def show_usage():
igor@0 215 print """Usage:
igor@0 216 xentaur {start|stop|start-bridges|start-domains|stop-domains|screen|graph}
igor@0 217 """
igor@0 218
igor@33 219 def save():
igor@33 220 print "network =", xen_config_name
igor@33 221 print "domains =", domains
igor@33 222 print "domain_types =", domain_types
igor@33 223 print "bridges =", bridges
igor@33 224 print "vbridges_table =", vbridges_table
igor@33 225 print "hidden_bridges =", hidden_bridges
igor@33 226 print "broken_links =", broken_links
igor@33 227 print "temporary_links =", temporary_links
igor@33 228 print "bridges_turned_down =", bridges_turned_down
igor@33 229
igor@33 230 #-----------------------------------------------------------------------
igor@33 231 # CLASSES
igor@33 232
igor@33 233 class Bridge:
igor@33 234 def __init__ (self,name):
igor@33 235 self.name=name
igor@33 236 def up(self):
igor@33 237 bridge_up(self.name)
igor@33 238 def down(self):
igor@33 239 bridge_down(self.name)
igor@33 240 def show(self):
igor@33 241 show_bridge(self.name)
igor@33 242 def dump_start(self,filter=""):
igor@33 243 dump_start(self.name,filter)
igor@33 244
igor@33 245
igor@38 246 class Domain:
igor@38 247 def __init__ (self,name):
igor@38 248 self.name=name
igor@38 249 def start(self):
igor@38 250 return ""
igor@38 251 def stop(self):
igor@38 252 return ""
igor@38 253 def start_commandline(self):
igor@38 254 return ""
igor@38 255 def graphviz(self):
igor@38 256 return ""
igor@38 257 def get_domain_id(self):
igor@38 258 return get_domain_id(self.name)
igor@38 259
igor@23 260 #-----------------------------------------------------------------------
igor@23 261 # DOMAINS
igor@23 262
igor@23 263 def get_domain_id(domain):
igor@23 264 return run_command_return_stdout("sudo xm list | awk '{if ($1 == \"'%s'\") print $2}'" % domain).rstrip("\n")
igor@23 265
igor@22 266
igor@22 267 #-----------------------------------------------------------------------
igor@22 268 # BRIDGES and IFACES
igor@22 269
igor@22 270 def bridge_down(bridge):
igor@22 271 """
igor@22 272 Turn the bridge <bridge> down
igor@22 273 """
igor@38 274 if bridge in real_bridges:
igor@38 275 print "Bridge %s is a real bridge" % (bridge)
igor@38 276 return -1
igor@29 277 if bridge in bridges_turned_down:
igor@29 278 print "Bridge %s is turned down already" % (bridge)
igor@29 279 else:
igor@29 280 bridges_turned_down.append(bridge)
igor@29 281 run_command("sudo ip link set %s down" % bridge)
igor@29 282 autoredraw()
igor@22 283
igor@22 284 def bridge_up(bridge):
igor@22 285 """
igor@22 286 Turn the bridge <bridge> up
igor@22 287 """
igor@38 288 if bridge in real_bridges:
igor@38 289 print "Bridge %s is a real bridge" % (bridge)
igor@38 290 return -1
igor@29 291 if not (bridge in bridges_turned_down):
igor@29 292 print "Bridge %s is turned up already" % (bridge)
igor@29 293 else:
igor@29 294 bridges_turned_down.remove(bridge)
igor@29 295 run_command("sudo ip link set %s up" % bridge)
igor@29 296 autoredraw()
igor@22 297
igor@22 298 def show_bridge(bridge):
igor@22 299 """
igor@22 300 Show the state of the bridge <bridge>
igor@22 301 """
igor@38 302 if bridge in real_bridges:
igor@38 303 print "Bridge %s is a real bridge" % (bridge)
igor@38 304 return -1
igor@22 305 run_command("sudo ip link show %s" % bridge)
igor@22 306
igor@23 307
igor@23 308 def int_disconnect(domain, int_number):
igor@23 309 """
igor@23 310 Disconnect the interface with the number <int_number>
igor@23 311 of the domain <domain> from the bridge to which
igor@23 312 it is connected
igor@23 313 """
igor@23 314 dom_id=get_domain_id(domain)
igor@23 315 bridge=vbridges_table[domain][int_number]
igor@23 316 if not bridge:
igor@23 317 print "Interface %s of the %s domain is not connected" % (int_number, domain)
igor@23 318 return 1
igor@23 319 run_command("sudo brctl delif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 320 vbridges_table[domain][int_number]=''
igor@28 321 if [ domain, int_number, bridge ] in temporary_links:
igor@28 322 temporary_links.remove([ domain, int_number, bridge ])
igor@27 323 else:
igor@28 324 broken_links.append([ domain, int_number, bridge ])
igor@27 325 autoredraw()
igor@23 326
igor@23 327 def int_connect(domain, int_number, bridge):
igor@23 328 """
igor@23 329 Connect the interface with the number <int_number>
igor@24 330 of the domain <domain> to the bridge <bridge>
igor@23 331 """
igor@38 332 if bridge in real_bridges:
igor@38 333 print "Bridge %s is a real bridge" % (bridge)
igor@38 334 return -1
igor@38 335
igor@23 336 dom_id=get_domain_id(domain)
igor@23 337 if vbridges_table[domain][int_number]:
igor@23 338 print "Interface %s of the %s domain is connected already to the %s bridge" % (int_number, domain, vbridges_table[domain][int_number])
igor@23 339 return 1
igor@23 340 run_command("sudo brctl addif %s vif%s.%s" % (bridge, dom_id, int_number))
igor@23 341 vbridges_table[domain][int_number]=bridge
igor@28 342 if [ domain, int_number, bridge ] in broken_links:
igor@28 343 broken_links.remove([ domain, int_number, bridge ])
igor@27 344 else:
igor@28 345 temporary_links.append([ domain, int_number, bridge ])
igor@27 346 autoredraw()
igor@23 347
igor@24 348 def int_reconnect(domain, int_number, bridge):
igor@24 349 """
igor@24 350 Reconnect the interface with the number <int_number>
igor@24 351 of the domain <domain> from the bridge to which
igor@24 352 it is connected to the bridge <bridge>
igor@24 353 """
igor@38 354 if bridge in real_bridges:
igor@38 355 print "Bridge %s is a real bridge" % (bridge)
igor@38 356 return -1
igor@38 357
igor@24 358 int_disconnect(domain, int_number)
igor@24 359 int_connect(domain, int_number, bridge)
igor@24 360
igor@24 361 def show_int(domain, int_number):
igor@25 362 """
igor@25 363 Show information about the interface <int_nuber>
igor@25 364 of the domain <domain>
igor@25 365 """
igor@26 366 return vbridges_table[domain][int_number]
igor@24 367
igor@28 368
igor@28 369 def dump_start(bridge, filter=""):
igor@38 370 if bridge in real_bridges:
igor@38 371 print "Bridge %s is a real bridge" % (bridge)
igor@38 372 return -1
igor@32 373 try:
igor@32 374 print "Writing dump... (press Ctrl-C to stop)"
igor@32 375 run_command("sudo tcpdump -w xentaur.dump -i %s %s > /dev/null 2>&1 " % (bridge,filter))
igor@32 376 except:
igor@32 377 print "Done.\n Dump is written to xentaur.dump"
igor@28 378 return 0
igor@28 379
igor@28 380 def dump_stop():
igor@28 381 return 0
igor@33 382
igor@33 383
igor@33 384 #-----------------------------------------------------------------------
igor@33 385 # CONFIGURATION TEMPLATES
igor@33 386
igor@33 387
igor@33 388 def configure_ip_addresses(doms=domains):
igor@33 389 return 0
igor@33 390
igor@33 391 def configure_ospf(doms=domains):
igor@33 392 return 0
igor@33 393
igor@33 394 def configure_save(doms=domains):
igor@33 395 write_to(doms,"\nwr\n")
igor@33 396
igor@0 397 #-----------------------------------------------------------------------
igor@0 398
igor@0 399
igor@0 400 def add_domain(name,type):
igor@0 401 domains.append(name)
igor@0 402 domain_types.append(type)
igor@0 403
igor@0 404 def brake_link(domain,bridge):
igor@0 405 broken_links.append([domain,bridge])
igor@0 406
igor@4 407 wt_timeout=0.5
igor@8 408 def write_to(screen,string,return_to_screen=""):
igor@2 409 """
igor@2 410 write_to(screen,string):
igor@2 411
igor@8 412 Type *string* to the specified screen(s).
igor@8 413 Screen may be specified with the number *screen*,
igor@8 414 with array of numbers,
igor@8 415 with array of names.
igor@2 416
igor@2 417 """
igor@5 418 screen_numbers=[] # number of the screens to write to
igor@5 419 if type(screen) == list:
igor@5 420 screen_numbers=map(lambda x: domains.index(x)+1, screen)
igor@5 421 elif type(screen) == int:
igor@5 422 screen_numbers=[screen]
igor@5 423 else:
igor@5 424 screen_numbers=[domains.index(screen)+1]
igor@5 425
igor@5 426 for screen_number in screen_numbers:
igor@5 427 run_command("screen -X select "+str(screen_number))
igor@5 428 time.sleep(wt_timeout)
igor@5 429 for line in string.splitlines():
igor@5 430 f=open('/tmp/xentaurbuf', 'w')
igor@5 431 f.write(line+"\n")
igor@5 432 f.close()
igor@5 433 run_command("screen -X readreg p /tmp/xentaurbuf")
igor@5 434 time.sleep(wt_timeout)
igor@5 435 run_command("nohup screen -X paste p >& /dev/null")
igor@5 436 time.sleep(wt_timeout)
igor@5 437
igor@8 438 if return_to_screen != "":
igor@8 439 run_command("screen -X select %s" % (return_to_screen))
igor@8 440 time.sleep(wt_timeout)
igor@0 441
igor@0 442 #-----------------------------------------------------------------------
igor@0 443
igor@5 444 cisco_fa01_up="""
igor@5 445 ena
igor@5 446 conf t
igor@5 447 int fa0/0
igor@5 448 no shutdown
igor@5 449 exit
igor@5 450 int fa1/0
igor@5 451 no shutdown
igor@5 452 exit
igor@5 453 exit
igor@5 454 exit
igor@5 455 """
igor@5 456
igor@5 457 cisco_set_ip_on_int="""
igor@5 458 interface fa%s/0
igor@5 459 no ip address
igor@5 460 ip address %s 255.255.255.0
igor@5 461 exit
igor@5 462 """
igor@5 463
igor@33 464
igor@0 465
igor@0 466 nodes=domains
igor@0 467
igor@0 468 if len(sys.argv) > 1:
igor@0 469 if sys.argv[1] == 'start':
igor@0 470 start_all()
igor@0 471 if sys.argv[1] == 'stop':
igor@0 472 stop_all()
igor@0 473 if sys.argv[1] == 'start-bridges':
igor@3 474 create_bridges_script()
igor@0 475 if sys.argv[1] == 'start-domains':
igor@3 476 create_domains_script()
igor@0 477 if sys.argv[1] == 'stop-domains':
igor@3 478 destroy_domains_script()
igor@0 479 elif sys.argv[1] == 'screen':
igor@3 480 create_screens_script()
igor@0 481 elif sys.argv[1] == 'graph':
igor@0 482 graph()
igor@0 483 elif sys.argv[1] == 'shell':
igor@0 484 shell()
igor@0 485
igor@0 486 else:
igor@0 487 show_usage()
igor@0 488 sys.exit(1)
igor@0 489
igor@0 490 sys.exit(0)
igor@0 491
igor@0 492