def execute_graph_request(rrd_name, data_set, image_file, start_ts, end_ts, consolidation_func, width=640, height=480): """Execute the graph request. :param rrd_name: Name of the RRD against which the reosurce graph is desired :type rrd_name: str :param data_set: The data set whose trend is to be retrieved. :type data_set: list :param image_file: The name of the resulting graph image file :type image_file: str :param start_ts: The starting time of the graph :type start_ts: int :param end_ts: The end time of the graph :type end_ts: int :param consolidation_func: The consolidation function for the graph (typical: 'AVERAGE', 'LAST', ...) :type consolidation_func: str :param width: The width of the graph, in number of pixels :type width: int :param height: The height of the graph, in number of pixels :type height: int :return: None :rtype: None """ fields = rrdutil.fetch_last_data(rrd_name, end_ts=end_ts, start_ts=start_ts)[1] # Convert to the dataset format that gen_graph expects ds = [] for d in data_set: ds.append((d, _data_set_desc[d])) title = "CPU utilisation" if "mem.rrd" in rrd_name: title = "Memory utilisation" gen_graph( image_file, rrd_name=rrd_name, title=title, cf=consolidation_func, start_ts=start_ts, end_ts=end_ts, width=width, height=height, dataset=ds, )
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])))