def windows_data(start, end): interval = 1 # TODO api = HttpAPI(namespace="windows", apikey='test', url=settings.FLAMONGO_ENDPOINT) ret = api.retrieve(start_time=start, end_time=end, interval=interval) # XXX Begin hack acc = {} for x in ret: try: acc[x['timestamp']].append(x) except KeyError: acc[x['timestamp']] = [x] y = [] for k, values in acc.items(): count = len(values) y.append({'timestamp': k, 'count': count}) y.sort(key=lambda r: r['timestamp']) output = [] for r in y: lastIndex = y.index(r)-1 if lastIndex >= 0: last = y[lastIndex]['timestamp'] if r['timestamp']-last > 700: output.append({'timestamp': r['timestamp']-600, 'count': 0}) output.append({'timestamp': last+600, 'count': 0}) output.append(r); # XXX End hack return output
def json_linux_machines_data_both(request, ns, start, end): interval = 1 # TODO # Retrieve data. api = HttpAPI(namespace=ns, apikey='test', url=settings.FLAMONGO_ENDPOINT) output_linux_local = api.retrieve(start_time=start, end_time=end, interval=interval, attributes={'is_local': True}) output_linux_ssh = api.retrieve(start_time=start, end_time=end, interval=interval, attributes={'is_local': False}) for r in output_linux_local: r['hostname'] = r['hostname'].split('.')[0] for r in output_linux_ssh: r['hostname'] = r['hostname'].split('.')[0] output = {'machines': list(get_linux_machines()), 'data': { 'linuxLocal' : output_linux_local, 'linuxRemote' : output_linux_ssh } } return HttpResponse(json.dumps(output), mimetype='text/json')
def linux_data(start, end, local): interval = 600 api = HttpAPI(namespace='linux', apikey='test', url=settings.FLAMONGO_ENDPOINT) s = datetime.utcfromtimestamp(start) s = datetime(s.year, s.month, s.day, 0, 0) # Get the start of day. s = time.mktime(s.timetuple()) ret = api.retrieve(start_time=s, end_time=end, interval=interval, attributes={'is_local': local}) # Fetch the reboot events. reboots = api.retrieve(start_time=s, end_time=end, interval=interval, attributes={'event': 'reboot'}) # Merge then sort the results ret = list(ret) + list(reboots) ret.sort(key=lambda x: x['timestamp']) def get_n(x): # The nth interval. return int(x - start) // interval current_hosts = set() current_n = 0 counts = [] # for record in ret: if record['timestamp'] >= start: n = get_n(record['timestamp']) if n > current_n: for i in range(current_n, n): counts.append(len(current_hosts)) current_n = n if record['event'] == 'login': current_hosts.add(record['hostname']) else: if record['hostname'] in current_hosts: current_hosts.remove(record['hostname']) # Add the tail. for i in range(current_n, get_n(end)): counts.append(len(current_hosts)) output = [] def get_t(start=start, interval=interval): # Generate the times. x = start while True: yield start start += interval for c, ts in zip(counts, get_t()): output.append({'timestamp': ts, 'count': c}) return output
def json_windows_machines_data(request, ns, start, end): interval = 1 # TODO # Retrieve data. api = HttpAPI(namespace=ns, apikey='test', url=settings.FLAMONGO_ENDPOINT) ret = api.retrieve(start_time=start, end_time=end, interval=interval) output = {'machines': list(get_windows_machines()), 'data': ret} return HttpResponse(json.dumps(output), mimetype='text/json')
def f(): # create the api object. api = HttpAPI(namespace='ns', apikey='apikey', hostname='localhost', port=2000, timeout=20) # set the start and end range. Not implemented yet. start_time = time.time() - 100000 end_time = time.time() # Do a retrieve and display the graph. for _ in range(1): x = api.retrieve(start_time=start_time, end_time=end_time, interval=1, attributes={'type': 'E'}, fields=['amount']) print x # test_plot(x) print len(x)
def json_view_all(request, ns, start, end): interval = 1 # TODO # Retrieve data. api = HttpAPI(namespace=ns, apikey='test', url=settings.FLAMONGO_ENDPOINT) ret = api.retrieve(start_time=start, end_time=end, interval=interval) return HttpResponse(json.dumps(ret), mimetype='text/json')