3

我一直在尝试弄清楚如何在没有运气的情况下将 python dict 写入 InfluxDB。我试过使用 InfluxDBClient 以及通过 API 使用 Requests Post。

我有以下代码:

for server in  data['service_group_stat']['member_stat_list']:
    metrics = {}
    metrics['measurement'] = "LB01"
    metrics['tags'] = {}
    metrics['fields'] = {}
    metrics['tags']['SGNAME'] = name
    metrics['tags']['SRVNAME'] = server['server']
    metrics['fields']['CURCONNS'] = server['cur_conns']
    metrics['fields']['TOTCONNS'] = server['tot_conns']
    metrics['fields']['REQBYTES'] = server['req_bytes']
    metrics['fields']['REQPKTS'] = server['req_pkts']
    metrics['fields']['RESPBYTES'] = server['resp_bytes']
    metrics['fields']['RESPPKTS'] = server['resp_pkts']
    pprint(metrics)

这会给我以下输出:

{'fields': {'CURCONNS': 33,
            'REQBYTES': 3151292236,
            'REQPKTS': 21160834,
            'RESPBYTES': 66671993850,
            'RESPPKTS': 51709706,
            'TOTCONNS': 332177},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0006'}}
{'fields': {'CURCONNS': 39,
            'REQBYTES': 3387948728,
            'REQPKTS': 23103920,
            'RESPBYTES': 71703285665,
            'RESPPKTS': 55687510,
            'TOTCONNS': 369628},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0005'}}
{'fields': {'CURCONNS': 16,
            'REQBYTES': 3401134891,
            'REQPKTS': 24014042,
            'RESPBYTES': 70917802336,
            'RESPPKTS': 55029480,
            'TOTCONNS': 342360},
 'measurement': 'LB01',
 'tags': {'SGNAME': 'SG_ACCOUNT.BUSINESS.COM_443',
          'SRVNAME': u'WWW0004'}}

我尝试使用 requests.post 来利用 InfluxDB API:

url = "http://localhost:8086/write?db=metrics"

r = requests.post(url, metrics)

但是我似乎总是得到 400 响应:

reply: 'HTTP/1.1 400 Bad Request\r\n'

我也尝试过使用 InfluxDBClient:

from influxdb import InfluxDBClient

# Define InfluxDB Client Information

client = InfluxDBClient(host='127.0.0.1', port=8086, username='root', password='root', database='metrics')

for server in  data['service_group_stat']['member_stat_list']:
    metrics = {}
    metrics['measurement'] = "LB01"
    metrics['tags'] = {}
    metrics['fields'] = {}
    metrics['tags']['SGNAME'] = name
    metrics['tags']['SRVNAME'] = server['server']
    metrics['fields']['CURCONNS'] = server['cur_conns']
    metrics['fields']['TOTCONNS'] = server['tot_conns']
    metrics['fields']['REQBYTES'] = server['req_bytes']
    metrics['fields']['REQPKTS'] = server['req_pkts']
    metrics['fields']['RESPBYTES'] = server['resp_bytes']
    metrics['fields']['RESPPKTS'] = server['resp_pkts']

client.write_points(metrics)

最终给了我以下错误:

    client.write_points(metrics)
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 456, in write_points
    tags=tags, protocol=protocol)
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 506, in _write_points
    protocol=protocol
  File "/usr/lib/python2.7/site-packages/influxdb/client.py", line 292, in write
    data = make_lines(data, precision).encode('utf-8')
  File "/usr/lib/python2.7/site-packages/influxdb/line_protocol.py", line 126, in make_lines
    point.get('measurement', data.get('measurement'))
AttributeError: 'str' object has no attribute 'get'

关于我做错了什么有什么建议吗?

4

2 回答 2

6

使用 json 协议(默认)时,您必须将字典列表传递给 InfluxDBClient.write_points() 。
您可以检查make_lines() 方法的源代码,以确信它会迭代点。

答案:使用client.write_points([metrics])

于 2017-06-21T10:19:17.390 回答
0

上周我在实习项目中遇到了类似的问题。似乎python不喜欢这样的语法:

metrics['tags']['SGNAME'] = name

也许你可以尝试这样的事情..它对我有用。

for server in  data['service_group_stat']['member_stat_list']:
    metrics = {}
    metrics['measurement'] = "LB01"
    tags = {}
    fields = {}
    tags['SGNAME'] = name
    tags['SRVNAME'] = server['server']
    metrics['tags'] = tags
    fields['CURCONNS'] = server['cur_conns']
    fields['TOTCONNS'] = server['tot_conns']
    fields['REQBYTES'] = server['req_bytes']
    fields['REQPKTS'] = server['req_pkts']
    fields['RESPBYTES'] = server['resp_bytes']
    fields['RESPPKTS'] = server['resp_pkts']
    metrics['fields'] = fields
client.write_points(metrics)

希望它有所帮助

于 2017-05-12T10:18:46.723 回答