def report_model(request): """Reports: Model.""" # DB Search collection = helpers.mongo_helper(netspot_settings.COLL_NETSPOT) models = collection.aggregate([{'$group' : {'_id': {'model': '$model', 're0_model': '$re0_model', 're1_model': '$re1_model'}, 'count':{'$sum':1}}}]) # Prepare data. Return dict result = defaultdict() for model in models: for sub_model in model['_id']: if result.get(model['_id'][sub_model]): result[model['_id'][sub_model]] += model['count'] else: result[model['_id'][sub_model]] = model['count'] rows = list() for model in result: rows.append([model, result[model]]) # Create report report = Report(name='Models', description='Number of different asset models.', headers=['Model', 'Count'], rows=rows) return render( request, 'report.htm', context=report.context )
def report_playbook_runs(request): """Report: Playbooks runs.""" # DB Search collection = helpers.mongo_helper(netspot_settings.COLL_PLAYBOOK_LOGS) playbooks = collection.aggregate([{'$group': {'_id': {'playbook':'$playbook'}, 'count': {'$sum': 1}}}]) rows = list() for playbook in playbooks: rows.append([playbook['_id']['playbook'], playbook['count']]) # Create report report = Report(name='Playbook runs', description='Number of times a given playbook have been run.', headers=['Playbook', 'Number of runs'], rows=rows) return render( request, 'report.htm', context=report.context )
def report_junos_version(request): """Report: JUNOS versions.""" # DB Search collection = helpers.mongo_helper(netspot_settings.COLL_NETSPOT) models = collection.aggregate([{'$group': {'_id': {'model':'$model', 'version': '$version'}, 'count': {'$sum': 1}}}]) rows = list() for model in models: rows.append([model['_id']['model'], model['_id']['version']]) # Create report report = Report(name='JUNOS Versions', description='Number of different JUNOS versions.', headers=['Model', 'Version(s)'], rows=rows) return render( request, 'report.htm', context=report.context )
def api_get_mac(request, ip_address): """Returns a list with MAC addresses for a given IP address. Args: ip_address: string, IP address """ if request.method == 'GET': collection = helpers.mongo_helper(netspot_settings.COLL_MACS) macs = collection.find({ 'macs.ip': { '$regex': ip_address } }).sort('asset') # Serialize response macs_response = set() for entry in macs: for mac in entry['macs']: if ip_address == mac['ip']: macs_response.add(mac['mac']) return Response(macs_response)
def macs(request): """Lists all assets.""" # Get the MongoDB collection collection = helpers.mongo_helper(netspot_settings.COLL_MACS) num_assets = collection.count() # Number of MACs try: num_macs = collection.aggregate([{ "$unwind": "$macs" }, { "$group": { "_id": "$macs" } }, { "$group": { "_id": "DistictCount", "count": { "$sum": 1 } } }]).next() num_macs = num_macs['count'] except StopIteration: num_macs = 0 return render( request, 'macs.htm', context={ 'filter': '', 'num_assets': num_assets, 'num_macs': num_macs }, )