我一直在努力弄清楚如何在运气不好的情况下将python字典写入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'}}


我尝试过仅使用request.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'


关于我在做什么错的任何建议?

最佳答案

使用json协议(默认)时,您必须将字典列表传递给InfluxDBClient.write_points()。
您可以检查make_lines()方法的the source code以确保它遍历了点。

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

07-24 13:02