def graph(request, name): """Handle the graph request. :param request: The original resmon GRAPH request. :type request: HttpRequest :param name: The name of the resmon RRD whose graph is desired. :type name: str :return: The response to the graph request. :rtype: HttpResponse """ try: period = request.GET.get('period') width = request.GET.get('width') height = request.GET.get('height') dataset = request.GET.get('dataset') # Space delimited data set end_ts = int(time.time()) start_ts = end_ts - parse_timespec(period) if not dataset: raise HttpResponseBadRequest('No dataset given') resp = RestCmd('resmon/graph', '') img = tempfile.mkstemp(suffix=".png", prefix="resmongraph") try: close(img[0]) execute_graph_request(name, dataset.split(), img[1], start_ts, end_ts, 'AVERAGE', width, height) # Now read in the image file and prepare for the response. with open(img[1], 'r') as f: img_size = os.path.getsize(img[1]) if img_size <= 2095104: # Read maximum 2Mb resp.data = f.read() if 'xml' in request.META.get('HTTP_ACCEPT'): return HttpResponse(ElementTree.tostring(resp.export_xml()), content_type='application/xml') return HttpResponse(resp.export_json(), content_type='application/json') else: logger.warning('Image file size {0} exceeded the maximum limit of 2Mb, please retry with smaller dimensions.' .format(img_size)) return HttpResponseServerError('Image file too large') finally: os.remove(img[1]) # Clean up the temp file. except Exception as e: tb = sys.exc_info()[2] logger.error('Encountered unexpected {0} at {1}'.format(e, traceback.format_tb(tb))) return HttpResponseServerError('Unexpected internal server error')
def resmon_on_off(request, action, name): """Activate/deactivate a specific resmon, create a new resmon DB if necessary. :param request: The original HTTP request. :type request: HttpRequest :param action: The action to take, must be one of: 'on' or 'off'. :type action: str :param name: The name of the monitor against which to apply the action. :type name: str :return: The response to this request. :rtype: HttpResponse """ global _task_repository global _lock try: resp = RestCmd('resmon', '') if action == 'on': period = request.GET.get('period') with _lock: if name in _task_repository: logger.warning('Monitor {0} already running, restarting.'.format(name)) _task_repository.pop(name).stop() logger.info('Staring monitor {0} at intervals {1}.'.format(name, period)) _task_repository[name] = start_monitor(name, parse_timespec(period), ResmonListener()) if 'xml' in request.META.get('HTTP_ACCEPT'): return HttpResponse(ElementTree.tostring(resp.export_xml()), content_type='application/xml') return HttpResponse(resp.export_json(), content_type='application/json') with _lock: if name in _task_repository: task = _task_repository.pop(name) logger.info('Stopping {0}'.format(name)) task.stop() else: logger.warning('{0} not in resmon repository. Attempt to stop is ignored.'.format(name)) if 'xml' in request.META.get('HTTP_ACCEPT'): return HttpResponse(ElementTree.tostring(resp.export_xml()), content_type='application/xml') return HttpResponse(resp.export_json(), content_type='application/json') except Exception as e: tb = sys.exc_info()[2] logger.warning('Encountered unexpected {0} at {1}'.format(e, traceback.format_tb(tb))) return HttpResponseServerError('Encountered unexpected internal server error')