首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Zabbix API -有没有办法请求减少特定时间范围内的“趋势”或“历史”记录

Zabbix API -有没有办法请求减少特定时间范围内的“趋势”或“历史”记录
EN

Stack Overflow用户
提问于 2019-04-04 21:39:16
回答 1查看 328关注 0票数 0

我从事一个项目已经有一段时间了,该项目需要将Zabbix的“趋势”和“历史”数据转换为各种类型的图表,例如折线图或饼图。

问题是可能有太多的数据(时间-值对),特别是在“历史”数据的情况下。当然,我不想将10,000+点发送到前端,因此我希望减少点的数量,以便它仍然代表该特定的时间范围。

当然,一种解决方法是在服务器端实现它,但如果不是必需的,我不想增加我的资源(CPU、网络等)的负担。

我在Zabbix API的文档中搜索了“历史”和“趋势”,但我没有找到我需要的东西。

我想知道是否有任何方法可以从Zabbix API请求减少特定时间段的‘历史’或‘趋势’点的数量,使其对于所有数据仍然具有代表性?

Zabbix API版本: 4.0

代码语言:javascript
运行
复制
from datetime import datetime
import math
import sys
import time

from pyzabbix import ZabbixAPI


def n_sized_chunks(lst, n):
    """Yield successive n-sized chunks from 'lst'."""
    for i in range(0, len(lst), n):
        yield lst[i:i+n]


# The hostname at which the Zabbix web interface is available
ZABBIX_SERVER = '<zabbix-server>'
MAX_POINTS = 300

zapi = ZabbixAPI(ZABBIX_SERVER)

# Login to the Zabbix API
zapi.login('<username>', '<password>')

item_id = '<item-id>'

# Create a time range
time_till = time.mktime(datetime.now().timetuple())
time_from = time_till - 60 * 60 * 24 * 7  # 1 week

# Query item's history (integer) data
history = zapi.history.get(itemids=[item_id],
                           time_from=time_from,
                           time_till=time_till,
                           output='extend',
                           )

length = len(history)
print(f"Before: {length}")  # ~10097

###################################################################
# Can Zabbix API do the followings (or something similar) for me? #
###################################################################
if length <= MAX_POINTS:
    sys.exit(0)
chunk_size = math.ceil(length / MAX_POINTS)
x = list(map(lambda point: float(point['clock']), history))
y = list(map(lambda point: float(point['value']), history))
x_chunks = list(n_sized_chunks(lst=x, n=chunk_size))
y_chunks = list(n_sized_chunks(lst=y, n=chunk_size))
history = []
for x, y in zip(x_chunks, y_chunks):
    history.append({'clock': (x[0]+x[-1])/2, 'value': sum(y)/len(y)})
######################################################################

print(f"After: {len(history)}") ## ~297
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-04 23:41:23

目前这是不可能的。你可能想在https://support.zabbix.com/browse/ZBXNEXT-656上投票。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55517420

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档