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')
def main(args): """Main entry point of this program. :param args: The command-line argument list :return: Exit code :rtype: int """ parser = argparse.ArgumentParser(prog='resmon.py', description='Monitor the system resource utilisation, periodically') top = parser group1 = top.add_argument_group() group1.add_argument('-n', metavar='name', help='Name of the resource monitoring session, artifacts will follow this' ' name', type=str) group1.add_argument('-p', metavar='period', help='The sampling period of the monitoring process. E.g. 10s, 1m, 2h.' ' Default is 1m', default='1m', type=str) group2 = top.add_argument_group() group2.add_argument('-l', help='List all the resmon databases found in the working directory and return.', action='count') arg = parser.parse_args(args) if arg.l: print(execute_list_dbs()) return 0 if not arg.n: logger.error('No name specified') return errno.EINVAL if arg.p: period = parse_timespec(arg.p) else: period = 60 # Default period is 1m monitor = start_monitor(arg.n, period) try: while 1: # Run until stopped # Wake up periodically and do something, like kicking a watchdog or something like that, not sure what yet. sleep(600) except KeyboardInterrupt: logger.info('User interrupted') return errno.EINTR finally: monitor.stop() logger.info('resmon terminated') return errno.EINVAL # If we are here we were interrupted by something other than KeyboardInterrupt
def main(args): """Main entry to program. :param args: Parsed command-line args. :return: Exit code. """ try: start_ts = (int(time.time()) / _step) * _step # TS should be aligned with the period # Step 1: Start the monitoring if (not os.path.exists(_base_name + 'cpu.rrd')) or (not os.path.exists(_base_name + 'mem.rrd')): fill_in_the_rrds(_base_name + 'cpu.rrd', _base_name + 'mem.rrd', start_ts - (24 * 3600), start_ts, _step) else: fill_in_the_rrds(_base_name + 'cpu.rrd', _base_name + 'mem.rrd', int(rrdutil.get_last_update(_base_name + 'cpu.rrd')) + _step, start_ts, _step) monitor = resmon_util.start_monitor(_base_name, _step) try: prev_ts = start_ts for loop in range(0, 360): # Run for an hour (360 loops of 10 seconds). # Step 2: Wait for some time (10s) for the monitor to gather data logger.info('Waiting for data, loop #{0}'.format(loop)) time.sleep(10) # Step 3: Observe the result last_cpu_data = rrdutil.fetch_last_data(monitor.get_cpu_rrd(), start_ts=prev_ts, resolution=1) last_mem_data = rrdutil.fetch_last_data(monitor.get_mem_rrd(), start_ts=prev_ts) # Print out the result print('\nLAST CPU data {0}, curr time = {1}'.format(last_cpu_data[0], int(time.time()))) print(rrdutil.format_cpu_stats(last_cpu_data)) print('\nLAST Memory data {0}'.format(last_mem_data[0])) print(rrdutil.format_mem_stats(last_mem_data)) prev_ts = (int(time.time()) / _step) * _step # TS should be aligned with the period avg_data = rrdutil.fetch_avg_data(monitor.get_cpu_rrd(), start_ts=start_ts, resolution=10) print('\nAVG CPU data {0}'.format(avg_data[0])) print(rrdutil.format_cpu_stats(avg_data)) avg_data = rrdutil.fetch_avg_data(monitor.get_mem_rrd(), start_ts=start_ts, resolution=10) print('\nAVG Memory data {0}'.format(avg_data[0])) print(rrdutil.format_mem_stats(avg_data)) finally: # Don't forget to stop the monitor when finish logger.info('Stopping {0}'.format(monitor)) monitor.stop() # Generate a graph end_graf = time.time() start_graf = end_graf - (2 * SECONDS_IN_HOUR) gen_graph('cpu.png', rrd_name=monitor.get_cpu_rrd(), title='CPU Utilisation', cf='AVERAGE', start_ts=start_graf, end_ts=end_graf, dataset=[('idle', '%Idle'), ('wait', '%Wait'), ('load', 'Load avg')]) gen_graph('mem.png', rrd_name=monitor.get_mem_rrd(), title='Memory Utilisation', cf='AVERAGE', start_ts=start_graf, end_ts=end_graf, dataset=[('memtotal', 'Total'), ('memfree', 'Free'), ('buffers', 'Buffers'), ('cached', 'Cached')]) except (TypeError, ValueError) as e: logger.warning('Encountered invalid monitor period {0}, at {1}'.format(e, format_tb(sys.exc_info()[2]))) except KeyboardInterrupt: logger.warning('User interruption at {0}'.format(format_tb(sys.exc_info()[2]))) except BaseException as e: logger.error('Encountered unexpected exception {0} at {1}'.format(repr(e), format_tb(sys.exc_info()[2])))