将 RRD 数据导入 Python 数据结构

发布于 2024-10-14 16:25:17 字数 124 浏览 6 评论 0原文

有谁有将rrd数据导入python的好方法吗?到目前为止,我找到的唯一库只是命令行的包装器或提供将数据导入到 rrd 并绘制图表的功能。

我知道 rrd 的导出和转储选项,但我想知道是否有人已经在这里完成了繁重的工作。

Does anyone have a good method for importing rrd data into python? The only libraries I have found so far are just wrappers for the command line or provide importing data into rrd and graphing it.

I am aware of the export and dump options of rrd, but I am wondering if someone has already done the heavy lifting here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Smile简单爱 2024-10-21 16:25:17

这是我为获取 cacti rrd 数据而编写的脚本的摘录。它不太可能正是您想要的,但它可能会给您一个良好的开始。我的脚本的目的是将 Cacti 转变为数据仓库,因此我倾向于提取大量平均值、最大值或最小值数据。我还有一些用于抛出上限或下限范围峰值的标志,如果我想将“字节/秒”转换为更可用的值,例如“mb/小时”...

如果您想要精确的一对一数据复制,您可能需要稍微调整一下。

    value_dict = {}
    for file in files:
        if file[0] == '':
            continue
        file = file[0]
        value_dict[file] = {}
        starttime = 0
        endtime = 0
        cmd = '%s fetch %s %s -s %s -e %s 2>&1' % (options.rrdtool, file, options.cf, options.start, options.end)
        if options.verbose: print cmd
        output = os.popen(cmd).readlines()
        dsources = output[0].split()
        if dsources[0].startswith('ERROR'):
            if options.verbose:
                print output[0]
            continue
        if not options.source:
            source = 0
        else:
            try:
                source = dsources.index(options.source)
            except:
                print "Invalid data source, options are: %s" % (dsources)
                sys.exit(0)

        data = output[3:]
        for val in data:
            val = val.split()
            time = int(val[0][:-1])
            val = float(val[source+1])
            # make sure it's not invalid numerical data, and also an actual number
            ok = 1
            if options.lowerrange:
                if val < options.lowerrange: ok = 0
            if options.upperrange:
                if val > options.upperrange: ok = 0
            if ((options.toss and val != options.toss and val == val) or val == val) and ok:
                if starttime == 0:
                    # this should be accurate for up to six months in the past
                    if options.start < -87000:
                        starttime = time - 1800
                    else:
                        starttime = time - 300
                else:
                    starttime = endtime
                endtime = time
                filehash[file] = 1
                val = val * options.multiply 
                values.append(val)
                value_dict[file][time] = val
                seconds = seconds + (endtime - starttime)

Here's an excerpt from a script that I wrote to get cacti rrd data out. It's not likely to be exactly what you want, but it might give you a good start. The intent of my script is to turn Cacti into a data warehouse, so I tend to pull out a lot of averages, max, or min data. I also have some flags for tossing upper or lower range spikes, multiplying in case I want to turn "bytes/sec" into something more usable, like "mb/hour"...

If you want exact one-to-one data copy, you might need to tweak this a bit.

    value_dict = {}
    for file in files:
        if file[0] == '':
            continue
        file = file[0]
        value_dict[file] = {}
        starttime = 0
        endtime = 0
        cmd = '%s fetch %s %s -s %s -e %s 2>&1' % (options.rrdtool, file, options.cf, options.start, options.end)
        if options.verbose: print cmd
        output = os.popen(cmd).readlines()
        dsources = output[0].split()
        if dsources[0].startswith('ERROR'):
            if options.verbose:
                print output[0]
            continue
        if not options.source:
            source = 0
        else:
            try:
                source = dsources.index(options.source)
            except:
                print "Invalid data source, options are: %s" % (dsources)
                sys.exit(0)

        data = output[3:]
        for val in data:
            val = val.split()
            time = int(val[0][:-1])
            val = float(val[source+1])
            # make sure it's not invalid numerical data, and also an actual number
            ok = 1
            if options.lowerrange:
                if val < options.lowerrange: ok = 0
            if options.upperrange:
                if val > options.upperrange: ok = 0
            if ((options.toss and val != options.toss and val == val) or val == val) and ok:
                if starttime == 0:
                    # this should be accurate for up to six months in the past
                    if options.start < -87000:
                        starttime = time - 1800
                    else:
                        starttime = time - 300
                else:
                    starttime = endtime
                endtime = time
                filehash[file] = 1
                val = val * options.multiply 
                values.append(val)
                value_dict[file][time] = val
                seconds = seconds + (endtime - starttime)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文