count clients, instead of nodes

This commit is contained in:
Nils Schneider 2014-08-17 18:40:15 +02:00
parent 663539c206
commit 2dfd11189d
4 changed files with 18 additions and 121 deletions

View file

@ -31,9 +31,6 @@ parser.add_argument('-a', '--aliases',
parser.add_argument('-m', '--mesh', action='append', parser.add_argument('-m', '--mesh', action='append',
help='batman mesh interface') help='batman mesh interface')
parser.add_argument('-o', '--obscure', action='store_true',
help='obscure client macs')
parser.add_argument('-A', '--alfred', action='store_true', parser.add_argument('-A', '--alfred', action='store_true',
help='retrieve aliases from alfred') help='retrieve aliases from alfred')
@ -66,11 +63,6 @@ if options['alfred']:
af = alfred() af = alfred()
db.import_aliases(af.aliases()) db.import_aliases(af.aliases())
db.count_clients()
if options['obscure']:
db.obscure_clients()
db.load_state("state.json") db.load_state("state.json")
# remove nodes that have been offline for more than 30 days # remove nodes that have been offline for more than 30 days

View file

@ -13,7 +13,6 @@ class D3MapBuilder:
nodes = self._db.get_nodes() nodes = self._db.get_nodes()
output['nodes'] = [{'name': x.name, 'id': x.id, output['nodes'] = [{'name': x.name, 'id': x.id,
'macs': ', '.join(x.macs),
'geo': [float(x) for x in x.gps.split(" ")] if x.gps else None, 'geo': [float(x) for x in x.gps.split(" ")] if x.gps else None,
'firmware': x.firmware, 'firmware': x.firmware,
'flags': x.flags, 'flags': x.flags,

View file

@ -7,7 +7,6 @@ class Node():
self.flags = dict({ self.flags = dict({
"online": False, "online": False,
"gateway": False, "gateway": False,
"client": False
}) })
self.gps = None self.gps = None
self.firmware = None self.firmware = None

129
nodedb.py
View file

@ -28,9 +28,6 @@ class NodeDB:
obj = [] obj = []
for node in self._nodes: for node in self._nodes:
if node.flags['client']:
continue
obj.append({ 'id': node.id obj.append({ 'id': node.id
, 'name': node.name , 'name': node.name
, 'lastseen': node.lastseen , 'lastseen': node.lastseen
@ -102,8 +99,11 @@ class NodeDB:
node.add_mac(x['secondary']) node.add_mac(x['secondary'])
for x in vis_data: for x in vis_data:
if 'router' in x: if 'router' in x:
# TTs will be processed later
if x['label'] == "TT":
continue
try: try:
node = self.maybe_node_by_mac((x['router'], )) node = self.maybe_node_by_mac((x['router'], ))
except: except:
@ -115,16 +115,6 @@ class NodeDB:
node.add_mac(x['router']) node.add_mac(x['router'])
self._nodes.append(node) self._nodes.append(node)
# If it's a TT link and the MAC is very similar
# consider this MAC as one of the routers
# MACs
if 'gateway' in x and x['label'] == "TT":
if is_similar(x['router'], x['gateway']):
node.add_mac(x['gateway'])
# skip processing as regular link
continue
try: try:
if 'neighbor' in x: if 'neighbor' in x:
try: try:
@ -140,15 +130,15 @@ class NodeDB:
node = Node() node = Node()
node.lastseen = self.time node.lastseen = self.time
node.flags['online'] = True node.flags['online'] = True
if x['label'] == 'TT':
node.flags['client'] = True
node.add_mac(x['neighbor']) node.add_mac(x['neighbor'])
self._nodes.append(node) self._nodes.append(node)
for x in vis_data: for x in vis_data:
if 'router' in x: if 'router' in x:
# TTs will be processed later
if x['label'] == "TT":
continue
try: try:
if 'gateway' in x: if 'gateway' in x:
x['neighbor'] = x['gateway'] x['neighbor'] = x['gateway']
@ -172,13 +162,9 @@ class NodeDB:
link.quality = x['label'] link.quality = x['label']
link.id = "-".join(sorted((link.source.interface, link.target.interface))) link.id = "-".join(sorted((link.source.interface, link.target.interface)))
if x['label'] == "TT":
link.type = "client"
self._links.append(link) self._links.append(link)
for x in vis_data: for x in vis_data:
if 'primary' in x: if 'primary' in x:
try: try:
node = self.maybe_node_by_mac((x['primary'], )) node = self.maybe_node_by_mac((x['primary'], ))
@ -187,6 +173,16 @@ class NodeDB:
node.id = x['primary'] node.id = x['primary']
for x in vis_data:
if 'router' in x and x['label'] == 'TT':
try:
node = self.maybe_node_by_mac((x['router'], ))
node.add_mac(x['gateway'])
if not is_similar(x['router'], x['gateway']):
node.clientcount += 1
except:
pass
def reduce_links(self): def reduce_links(self):
tmp_links = defaultdict(list) tmp_links = defaultdict(list)
@ -256,9 +252,6 @@ class NodeDB:
while changes > 0: while changes > 0:
changes = 0 changes = 0
for link in self._links: for link in self._links:
if link.type == "client":
continue
source_interface = self._nodes[link.source.id].interfaces[link.source.interface] source_interface = self._nodes[link.source.id].interfaces[link.source.interface]
target_interface = self._nodes[link.target.id].interfaces[link.target.interface] target_interface = self._nodes[link.target.id].interfaces[link.target.interface]
if source_interface.vpn or target_interface.vpn: if source_interface.vpn or target_interface.vpn:
@ -269,92 +262,6 @@ class NodeDB:
link.type = "vpn" link.type = "vpn"
def count_clients(self):
for link in self._links:
try:
a = self.maybe_node_by_id(link.source.interface)
b = self.maybe_node_by_id(link.target.interface)
if a.flags['client']:
client = a
node = b
elif b.flags['client']:
client = b
node = a
else:
continue
node.clientcount += 1
except:
pass
def obscure_clients(self):
globalIdCounter = 0
nodeCounters = {}
clientIds = {}
for node in self._nodes:
if node.flags['client']:
node.macs = set()
clientIds[node.id] = None
for link in self._links:
ids = link.source.interface
idt = link.target.interface
try:
node_source = self.maybe_node_by_fuzzy_mac(ids)
node_target = self.maybe_node_by_id(idt)
if not node_source.flags['client'] and not node_target.flags['client']:
# if none of the nodes associated with this link are clients,
# we do not want to obscure
continue
if ids in clientIds and idt in clientIds:
# This is for corner cases, when a client
# is linked to another client.
clientIds[ids] = str(globalIdCounter)
ids = str(globalIdCounter)
globalIdCounter += 1
clientIds[idt] = str(globalIdCounter)
idt = str(globalIdCounter)
globalIdCounter += 1
elif ids in clientIds:
newId = generateId(idt)
clientIds[ids] = newId
ids = newId
link.source.interface = ids;
node_source.id = ids;
elif idt in clientIds:
newId = generateId(ids,nodeCounters)
clientIds[idt] = newId
idt = newId
link.target.interface = idt;
node_target.id = idt;
link.id = ids + "-" + idt
except KeyError:
pass
# extends node id by incremented node counter
def generateId(nodeId,nodeCounters):
if nodeId in nodeCounters:
n = nodeCounters[nodeId]
nodeCounters[nodeId] = n + 1
else:
nodeCounters[nodeId] = 1
n = 0
return nodeId + "_" + str(n)
# compares two MACs and decides whether they are # compares two MACs and decides whether they are
# similar and could be from the same node # similar and could be from the same node
def is_similar(a, b): def is_similar(a, b):