コード例 #1
0
ファイル: views.py プロジェクト: haihovu/dmiso
def fetch(request, name, res_type, consolidation):
    """Fetch the data from a specific RRD, in some time frame, with a particular resolution.

    :param request: The original request.
    :type request: HttpRequest
    :param name: The name of the target resmon database.
    :type name: str
    :param res_type: The type of data: cpu | memory
    :type res_type: str
    :param consolidation: The optional consolidation function used. If specified must be one of: average | last.
     The default is last.
    :type consolidation; str
    :return: The requested data set.
    """
    try:
        resolution = request.GET.get('resolution')
        start = request.GET.get('start')
        end = request.GET.get('end')
        if not resolution:
            resolution = '1s'  # Default to the fastest resolution

        if not end:
            end = time.time()  # Default to now

        if not start:
            start = end - (24 * 3600)  # Default to one day worth of data

        ts = time.time()
        data = None
        if consolidation == 'average':
            data = rrdutil.fetch_avg_data(name + res_type + '.rrd', start_ts=int(start), end_ts=int(end), resolution=parse_timespec(resolution))
        else:
            data = rrdutil.fetch_last_data(name + res_type + '.rrd', start_ts=int(start), end_ts=int(end), resolution=parse_timespec(resolution))

        resp = RestCmd('/resmon/fetch', _process_fetch_data(data))

        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.error('Encountered unexpected {0} at {1}'.format(e, traceback.format_tb(tb)))
        return HttpResponseServerError('Encountered unexpected error, please check service log for more details')
コード例 #2
0
ファイル: fetch.py プロジェクト: haihovu/pyrate
def main(args):
    """Main entry to the gen_graph command.

    :param args: Command line argument list. A list of strings.
    :type args: list
    :return: The return code for the command, 0 is success, anything else for failure
    :rtype: int
    """
    exit_code = 0  # Default to success
    parser = None
    try:
        # Parse the given argument list
        parser = argparse.ArgumentParser(prog='fetch', description='Fetch some resource utilisation data from a resmon DB')
        parser.add_argument('-s', metavar='start_time', help='The start time, default to 24h before the end time',
                            type=int)
        parser.add_argument('-e', metavar='end_time', help='The end time, default to now', type=int)
        parser.add_argument('-c', help='Consolidation function: ave | last. Default to ave', type=str,
                            choices=['ave', 'last'], default='ave')
        parser.add_argument('-r', help='Resolution of the requested data, e.g. 1s, 30s, 1m, ...', type=str, required=True)
        parser.add_argument('-p', metavar='period/timeframe', help='If a start time is not specified then this will '
                            'specify the period of time against which the graph will cover. E.g. 23s, 5m, 6h, 1d, 2w',
                            type=str, default='1d')
        parser.add_argument('-f', metavar='RRD_file', help='Name of the RRD file', type=str, required=True)

        arg = parser.parse_args(args=args)

        # Calculating the end timestamp
        end_ts = int(time.time())
        if arg.e:  # End time
            end_ts = int(arg.e)

        # Calculating the start timestamp
        start_ts = end_ts - (24 * 3600)  # Default start time is one day old data
        if arg.s:  # Start time
            start_ts = int(arg.s)
        elif arg.p:  # Period is meaningful only when start time is not specified
            start_ts = end_ts - parse_timespec(arg.p)

        ret = []
        # Calculating the consolidation function
        if arg.c == 'last':
            ret += fetch_last_data(arg.f, start_ts=start_ts, end_ts=end_ts, resolution=parse_timespec(arg.r))
        else:
            ret += fetch_avg_data(arg.f, start_ts=start_ts, end_ts=end_ts, resolution=parse_timespec(arg.r))

        if len(ret) > 2:
            curr_ts = ret[0][0]
            inc = int(ret[0][2])
            print(rrd_fields_to_csv('ts', ret[1]))
            for line in ret[2]:
                print(rrd_fields_to_csv(curr_ts, line))
                curr_ts += inc

    except KeyboardInterrupt:
        tb = sys.exc_info()[2]
        logger.warning('User interruption at {0}'.format(format_tb(tb)))
        exit_code = errno.EINTR

    except ValueError as e:
        tb = sys.exc_info()[2]
        logger.warning('Encountered {0} at {1}'.format(e, format_tb(tb)))
        exit_code = errno.EINVAL

    except Exception as e:
        tb = sys.exc_info()[2]
        logger.warning('Encountered unexpected {0} at {1}'.format(e, format_tb(tb)))
        exit_code = errno.EINVAL

    return exit_code