Skip to content

Instantly share code, notes, and snippets.

@sudorandom
Created December 5, 2013 05:08
Show Gist options
  • Save sudorandom/7800455 to your computer and use it in GitHub Desktop.
Save sudorandom/7800455 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
"""
Generates graphs of VLANs and connected hardware/CCIs from data in the
SoftLayer API.
"""
import argparse
import time
try:
from pydot import Dot, Node, Edge
import SoftLayer
except ImportError:
exit("This script requires a couple python libraries to be installed:"
" pip install softlayer pydot")
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Generates graphs of VLANs and connected hardware/CCIs '
'from data in the SoftLayer API.')
parser.add_argument(
'-U', '--username',
default=None,
help='SoftLayer username.')
parser.add_argument(
'-K', '--api_key',
default=None,
help='SoftLayer Api_key.')
args = parser.parse_args()
print("Fetching from API")
before = time.time()
client = SoftLayer.Client(username=args.username, api_key=args.api_key)
masks = ['hardware',
'virtualGuests',
'virtualGuestCount',
'primaryRouter[hostname, datacenter]',
'dedicatedFirewallFlag',
'highAvailabilityFirewallFlag']
vlans = client['Account'].getNetworkVlans(mask=','.join(masks))
print("Fetching from API took %0.2f seconds" % (time.time() - before, ))
# Create a graph for each datacenter and one for all data.
print("Building graph data")
before = time.time()
datacenter_graphs = {}
all_graph = Dot(graph_type='digraph', rankdir='LR')
for vlan in vlans:
datacenter_id = vlan['primaryRouter']['datacenter']['id']
if datacenter_id not in datacenter_graphs:
datacenter_graphs[datacenter_id] = {
'graph': Dot(graph_type='digraph', rankdir='LR'),
'longName': vlan['primaryRouter']['datacenter']['longName'],
'name': vlan['primaryRouter']['datacenter']['name'],
}
graph = datacenter_graphs[datacenter_id]['graph']
firewall = False
if vlan['dedicatedFirewallFlag'] or \
vlan['highAvailabilityFirewallFlag']:
firewall = True
for g in [graph, all_graph]:
vlan_id = "vlan%s" % vlan['id']
if vlan['primaryRouter']['hostname'].startswith('bcr'):
g.add_node(Node(vlan_id,
style="filled",
fillcolor="#3B7A57",
fontcolor="white"))
else:
g.add_node(Node(vlan_id,
style="filled",
fillcolor="#8DB600",
fontcolor="white"))
if firewall:
firewall_id = "firewall%s" % vlan_id
g.add_node(Node(firewall_id,
style="filled",
fillcolor="#AF002A",
fontcolor="white"))
g.add_edge(Edge(firewall_id, vlan_id))
for hardware in vlan['hardware']:
g.add_node(Node(hardware['fullyQualifiedDomainName'],
style="filled",
fillcolor="#5D8AA8",
fontcolor="white"))
g.add_edge(Edge(vlan_id, hardware['fullyQualifiedDomainName']))
for vguest in vlan['virtualGuests']:
g.add_node(Node(vguest['fullyQualifiedDomainName'],
style="filled",
fillcolor="#72A0C1",
fontcolor="white"))
g.add_edge(Edge(vlan_id, vguest['fullyQualifiedDomainName']))
datacenter_graphs['all'] = {
'graph': all_graph,
'longName': 'All Datacenters',
'name': 'all',
}
print("Building graph data took %0.2f seconds" % (time.time() - before, ))
# Output graphs
print("Drawing graphs")
before = time.time()
for datacenter_key, d in datacenter_graphs.items():
filename = "graph_%s.png" % d['name']
d['graph'].write_png(filename)
print("Writing file: %s" % filename)
# plt.savefig(filename, dpi=args.dpi)
print("Drawing graphs took %0.2f seconds" % (time.time() - before, ))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment