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_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 main(): # See ex_create.py # api = HttpAPI(namespace='ns', apikey='apikey', hostname='localhost', # port=2000, timeout=20) # print api.retrieve_last(attributes={'type': 'A'}, limit=2) # print api.retrieve_last(attributes={'type': 'A'}, limit=3) # print api.retrieve_last(attributes={'type': ['A', 'B']}, limit=5) api = HttpAPI(namespace='windows', apikey='apikey', hostname='129.10.112.144', port=2000, timeout=20) print api.retrieve_last(attributes={'hostname': 'bulbasaur'})
def json_linux_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, attributes={'is_local': True}) for r in ret: r['hostname'] = r['hostname'].split('.')[0] output = {'machines': list(get_linux_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) # For n (100) times insert some data. start = time.time() - 1000 for i in range(1000): # timestamp denotes when the event occured. print api.store(timestamp=start + i, **{ 'amount': 11000 + i, 'type': random.choice(['A', 'B']), # 'type': randint(0, 2) and 'A' or 'B', })
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 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 get_linux_current(): api = HttpAPI(namespace='linux', apikey='test', url=settings.FLAMONGO_ENDPOINT) newlist = [] for m in linmachines: np = m['point'] ret = api.retrieve_last( attributes={'hostname': '%s.ccs.neu.edu' % np['name'], 'is_local': True})[0] # XXX: temporary fix. timestamp = datetime.fromtimestamp(ret['timestamp'] - 3600 * 4) newlist.append({'hostname': np['name'], 'longitude': np['longitude'], 'latitude': np['latitude'], 'floor': np['floor'], # last event was a login (so it's in use) 'inuse': (ret['event'] == 'login'), 'timestamp': timestamp}) return newlist
def get_windows_current(): api = HttpAPI(namespace='windows', apikey='test', url=settings.FLAMONGO_ENDPOINT) now = time.time() # pad with an extra minute then = now - 660 newlist = [] for m in winmachines: np = m['point'] ret = api.retrieve_last(attributes={'hostname': np['name']})[0] # NOTE: The time below appears to be unreliable. # XXX: temporary fix. timestamp = datetime.fromtimestamp(ret['timestamp'] - 3600 * 4) newlist.append({'hostname': np['name'], 'longitude': np['longitude'], 'latitude': np['latitude'], 'floor': np['floor'], 'inuse': (ret['timestamp'] >= then), 'timestamp': timestamp}) return newlist
def store(self): api = HttpAPI(namespace=self.ns, apikey=self.apikey, url=self.url) for timestamp, data in self.parselines(): # A successful read last, pos = self.ld.get() if last: logging.info('Last update: %.6f' % last) if last is None or timestamp > last: for host, domain in data: # Upload data. logging.debug('store: hostname=%s domain=%s timestamp=%s', host.lower(), domain, timestamp) resp = api.store(hostname=host.lower(), domain=domain, timestamp=timestamp) if not resp['code'] == 201: raise Exception('Endpoint error.') # TODO get seek pos. self.ld.set(str(timestamp), pos)
class LinuxData(object): def __init__(self, ns, apikey, url): self.ns = ns self.apikey = apikey self.url = url self.api = HttpAPI(namespace=self.ns, apikey=self.apikey, url=self.url) def store(self, count, timestamp): #logging.debug('store: count=%s timestamp=%s', count, timestamp) resp = self.api.store(count=count,timestamp=float(timestamp)) if not resp['code'] == 201: raise Exception('Endpoint error.')
def __init__(self, ns, apikey, url): self.ns = ns self.apikey = apikey self.url = url self.api = HttpAPI(namespace=self.ns, apikey=self.apikey, url=self.url)
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')