def graphite(): params = {k: request.values.getlist(k) for k in request.values.keys()} try: response = fetch_remote( current_app.config['GRAPHITE_SERVER'], method=request.method, data=request.data, accept=request.headers.get('Accept'), params=params, timeout=current_app.config['GRAPHITE_TIMEOUT'] ) headers = {'Content-Type': response.headers.get('Content-Type')} cache_for = int(request.values.get('_cache', 0)) if cache_for > 0: headers['Cache-Control'] = 'private, max-age=' + str(cache_for) headers['Expires'] = http_date(datetime.now() + timedelta(seconds=cache_for)) return make_response(( response.content, response.status_code, headers )) except Exception as e: width = request.values.get('width', '400') height = request.values.get('height', '200') image = IMAGE_TPL.format( error=str(e), width=width, height=height ) return make_response(( image, 500, {'Content-Type': 'image/svg+xml'} ))
def server_get(server): obj = Server.query.filter_by(name=server).first() if not obj: return jsonify(errors={'name': "Not found"}), 404 headers = {} if obj.api_key: headers['X-API-Key'] = obj.api_key server = obj.to_dict() try: response = fetch_remote( obj.pdns_url + '/servers/localhost', method='GET', accept=request.headers.get('Accept'), headers=headers, ) s = response.json() s.update(server) s['id'] = s['_id'] server = s except StandardError: pass # make sure JS doesn't loop endlessy server['version'] = server.get('version', '') return jsonify(**server)
def graphite(): params = dict( (k, request.values.getlist(k)) for k in request.values.keys()) try: response = fetch_remote(current_app.config['GRAPHITE_SERVER'], method=request.method, data=request.data, accept=request.headers.get('Accept'), params=params, timeout=current_app.config['GRAPHITE_TIMEOUT']) headers = {'Content-Type': response.headers.get('Content-Type')} cache_for = int(request.values.get('_cache', 0)) if cache_for > 0: headers['Cache-Control'] = 'private, max-age=' + str(cache_for) headers['Expires'] = http_date(datetime.now() + timedelta(seconds=cache_for)) return make_response((response.content, response.status_code, headers)) except Exception as e: width = request.values.get('width', '400') height = request.values.get('height', '200') image_tpl = """<?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 {width} {height}" width="{width}" height="{height}"> <text x="10" y="20" fill="red" font-size="15"> {error} </text> </svg>""" image = image_tpl.format(**{ 'error': str(e), 'width': width, 'height': height }) return make_response((image, 500, {'Content-Type': 'image/svg+xml'}))
def server_control(server): server = db.session.query(Server).filter_by(name=server).first() if server is None: return jsonify(errors={'name': "Not found"}), 404 data = {'parameters': request.values.get('command', '')} remote_url = server.pdns_url remote_url += '/jsonstat?command=pdns-control' r = fetch_remote(remote_url, method=request.method, data=data) return forward_remote_response(r)
def forward_request(server, remote_url): server = db.session.query(Server).filter_by(name=server).first() if server is None: return jsonify(errors={'name':"Not found"}), 404 response = fetch_remote( server.pdns_url + remote_url, method=request.method, data=request.data, accept=request.headers.get('Accept') ) return forward_remote_response(response)
def server_control(server): server = db.session.query(Server).filter_by(name=server).first() if server is None: return jsonify(errors={'name':"Not found"}), 404 data = {'parameters': request.values.get('command', '')} remote_url = server.pdns_url remote_url += '/jsonstat?command=pdns-control' r = fetch_remote(remote_url, method=request.method, data=data) return forward_remote_response(r)
def forward_request(server, remote_url, params=None, to_manager=False): server = db.session.query(Server).filter_by(name=server).first() if server is None: return jsonify(errors={'name': "Not found"}), 404 url = server.manager_url if to_manager else server.pdns_url response = fetch_remote(url + remote_url, method=request.method, data=request.data, accept=request.headers.get('Accept'), params=params) return forward_remote_response(response)
def forward_request(server, remote_url, params=None, to_manager=False): server = db.session.query(Server).filter_by(name=server).first() if server is None: return jsonify(errors={'name': "Not found"}), 404 url = server.manager_url if to_manager else server.pdns_url response = fetch_remote( url + remote_url, method=request.method, data=request.data, accept=request.headers.get('Accept'), params=params ) return forward_remote_response(response)
def graphite(): params = {k: request.values.getlist(k) for k in request.values.keys()} try: response = fetch_remote( current_app.config['GRAPHITE_SERVER'], method=request.method, data=request.data, accept=request.headers.get('Accept'), params=params, timeout=current_app.config['GRAPHITE_TIMEOUT'] ) headers = {'Content-Type': response.headers.get('Content-Type')} cache_for = int(request.values.get('_cache', 0)) if cache_for > 0: headers['Cache-Control'] = 'private, max-age=' + str(cache_for) headers['Expires'] = http_date(datetime.now() + timedelta(seconds=cache_for)) return make_response(( response.content, response.status_code, headers )) except Exception as e: width = request.values.get('width', '400') height = request.values.get('height', '200') image_tpl = """<?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" viewBox="0 0 {width} {height}" width="{width}" height="{height}"> <text x="10" y="20" fill="red" font-size="15"> {error} </text> </svg>""" image = image_tpl.format(**{ 'error': str(e), 'width': width, 'height': height }) return make_response(( image, 500, {'Content-Type': 'image/svg+xml'} ))
def server_get(server): obj = Server.query.filter_by(name=server).first() if not obj: return jsonify(errors={'name':"Not found"}), 404 server = obj.to_dict() try: response = fetch_remote( obj.pdns_url + '/servers/localhost', method='GET', accept=request.headers.get('Accept') ) s = response.json() s.update(server) s['id'] = s['_id'] server = s except Exception as e: pass # make sure JS doesn't loop endlessy server['version'] = server.get('version', '') return jsonify(**server)