我连接了 ThingsBoard 和 Telegraf 以可视化 CPU 使用率,但收到的数据是嵌套 JSON 格式。我尝试了不同的方法,但无法以扁平化的 JSON 格式获取数据。
使用 Telegraf 1.30.0 版本,数据以以下格式返回:
[
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":91.40585349348385,
"usage_iowait":0.1691761123329754,
"usage_irq":0、
"usage_nice":0.13534088986632262,
"usage_softirq":0.08458805616646366,
"usage_steal":0、
"usage_system":2.317712738961436,
"usage_user":5.887328709186236
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":91.94207836457213,
"usage_iowait":0.9028960817717189,
"usage_irq":0、
"usage_nice":0.30664395229985647,
"usage_softirq":0.11925042589437249,
"usage_steal":0、
"usage_system":1.8568994889266988,
"usage_user":4.872231686542196
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":91.22272957889072,
"usage_iowait":0.8963301200743917,
"usage_irq":0、
"usage_nice":0.35514967021814886,
"usage_softirq":0.05073566717402127,
"usage_steal":0、
"usage_system":2.130898021308797,
"usage_user":5.344156942329791
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":92.3648648648699,
"usage_iowait":0.4222972972973181,
"usage_irq":0、
"usage_nice":0.3209459459459579,
"usage_softirq":0.05067567567566809,
"usage_steal":0、
"usage_system":1.2837837837838315,
"usage_user":5.557432432432644
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":92.40399255626502,
"usage_iowait":0.16917611233301705,
"usage_irq":0、
"usage_nice":0.11842327863312155,
"usage_softirq":0.033835222466601006,
"usage_steal":0、
"usage_system":1.3364912874308539,
"usage_user":5.938081542887148
},
"name":"cpu4"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":92.9369918699103,
"usage_iowait":0.5928184281842386,
"usage_irq":0、
"usage_nice":0.16937669376690467,
"usage_softirq":0.016937669376688058,
"usage_steal":0、
"usage_system":1.1856368563684772,
"usage_user":5.09823848238449
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":93.07888040712137,
"usage_iowait":0.25445292620872184,
"usage_irq":0、
"usage_nice":0.15267175572520417,
"usage_softirq":0.06785411365564227,
"usage_steal":0、
"usage_system":1.5436810856660335,
"usage_user":4.902459711620043
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":92.3622845555872,
"usage_iowait":0.33795201081445,
"usage_irq":0、
"usage_nice":0.15207840486649526,
"usage_softirq":0.03379520108144139,
"usage_steal":0、
"usage_system":1.824940858397751,
"usage_user":5.288948969245875
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
},
{
"fields":{
"usage_guest":0、
"usage_guest_nice":0、
"usage_idle":92.21845893310595,
"usage_iowait":0.4678238780693211,
"usage_irq":0、
"usage_nice":0.21168501270109033,
"usage_softirq":0.05503810330228156,
"usage_steal":0、
"usage_system":1.682895850973822,
"usage_user":5.364098221845659
},
"name":"yavar"、
"tags":{}、
"timestamp":1716900420
}
]
但我需要从 Telegraf 到 ThingsBoard 的扁平化 JSON 格式数据。
可以使用 Telegraf 的
starlark
处理器来将嵌套的 JSON 数据扁平化为 ThingsBoard 可接受的格式。下面是一个配置示例:
# ... 其他 Telegraf 输入插件配置 ...
[[processors.starlark]]
source = '''
def apply(metric):
fields = metric.fields.copy()
for k, v in fields.items():
metric.fields[f"{metric.name}_{k}"] = v
metric.fields.pop("usage_guest", None)
metric.fields.pop("usage_guest_nice", None)
metric.fields.pop("usage_idle", None)
metric.fields.pop("usage_iowait", None)
metric.fields.pop("usage_irq", None)
metric.fields.pop("usage_nice", None)
metric.fields.pop("usage_softirq", None)
metric.fields.pop("usage_steal", None)
metric.fields.pop("usage_system", None)
metric.fields.pop("usage_user", None)
return metric
'''
# ... ThingsBoard 输出插件配置 ...
解释:
- [[processors.starlark]] : 这行定义了一个新的 Starlark 处理器。
- source : 包含要执行的 Starlark 脚本。
-
apply(metric)
: 这个函数接收一个
metric
对象,该对象代表 Telegraf 收集到的单个数据点。 -
fields = metric.fields.copy()
: 复制
fields
字典以避免修改原始数据。 -
for k, v in fields.items():
: 遍历
fields
字典中的每个键值对。 -
metric.fields[f"{metric.name}_{k}"] = v
: 使用
name
和原始键名创建一个新的扁平化键名,并将值赋给它。 - metric.fields.pop("key", None) : 删除原始的嵌套键。
-
return metric
: 返回修改后的
metric
对象。
使用此配置,Telegraf 将输出以下格式的扁平化 JSON 数据:
[
{
"name": "yavar",
"tags": {},
"timestamp": 1716900420,
"yavar_usage_guest": 0,
"yavar_usage_guest_nice": 0,
"yavar_usage_idle": 91.40585349348385,
"yavar_usage_iowait": 0.1691761123329754,
"yavar_usage_irq": 0,
"yavar_usage_nice": 0.13534088986632262,
"yavar_usage_softirq": 0.08458805616646366,
"yavar_usage_steal": 0,
"yavar_usage_system": 2.317712738961436,
"yavar_usage_user": 5.887328709186236
},
# ... 其他数据点 ...
]
请确保根据的实际需求调整
starlark
处理器中的代码,例如修改键名或添加其他逻辑。