def server_schema(host): # Get the most recent data point points = mongo.db.points try: record = points.find(host=host).sort('date', -1).limit(1)[0] except IndexError: return jsonify(objects=[]) # Flatten its data into a list of attributes objects = list(flatten_dict(record.get('data'))) # Get just the attribute name and sort alphabetically objects = sorted(map(lambda x: x[0], objects)) return jsonify(objects=objects)
def inform(): """ Report on a node's current stats and save that data to mongodb """ points = mongo.db.points points.insert({ 'ip': request.json['ip'], 'host': request.json['host'], 'data': request.json.get('data', {}), 'date': datetime.datetime.utcnow(), }) return jsonify(success=True)
def server_metric(host, stat): # Figure out the date end = request.args.get('end', datetime.datetime.now()) if not isinstance(end, datetime.datetime): end = parser.parse(end) start = request.args.get('start', end - datetime.timedelta(hours=4)) if not isinstance(start, datetime.datetime): start = parser.parse(start) # Get data from the server points = mongo.db.points query = { 'host': host, 'date': { '$gt': start, "$lte": end, }, } fields = { 'data.%s' % stat: 1, 'date': 1 } # Parse the data into a dict of { date: value } results = [] bits = stat.split('.') for record in points.find(query, fields=fields): t = record['data'] for bit in bits: t = t.get(bit, {}) #Javascript needs UTC time in milliseconds timestamp = int(mktime(record['date'].timetuple())) * 1000 results.append((timestamp, t)) data = { 'host': host, 'property': stat, 'series': results, } return jsonify(objects=data)
def fetch_alerts(host): alerts = mongo.db.alerts.find(host=host) return jsonify(objects=alerts)
def server_list(): """ Get a list of all servers and their properties """ points = mongo.db.points return jsonify(objects=points.distinct('host'))