Last active
December 20, 2016 01:07
-
-
Save otoolep/3d5741e680bf76021f77 to your computer and use it in GitHub Desktop.
Pump a sine way into a local influxdb cluster
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import json | |
import math | |
import requests | |
import sys | |
from time import sleep | |
DATABASE = 'test1' | |
STATUS_MOD = 10 | |
n = 0 | |
while True: | |
for d in range(0, 360): | |
v = [{'name': 'sin', 'columns': ['val'], 'points': [[math.sin(math.radians(d))]]}] | |
r = requests.post('http://localhost:8086/db/%s/series?u=root&p=root' % DATABASE, data=json.dumps(v)) | |
if r.status_code != 200: | |
print 'Failed to add point to influxdb -- aborting.' | |
sys.exit(1) | |
n += 1 | |
sleep(1) | |
if n % STATUS_MOD == 0: | |
print '%d points inserted.' % n |
This no longer works on 0.9.0.
It seems that the API endpoint is no longer available (I am getting a 404 error). Reading the 0.9.0 documentation it suggests that we now need to include the database name in the JSON. Is there a way to pass the database name via the URI rather than in the JSON payload?
Updated..
It is possible to append &db= to the URI to pass the database in. However the JSON still appears to require the database: k/v pair. There is currently an open issue to cover this.
I've taken the liberty to rewrite the request so that the script now works with v0.9
- json is deprecated with v0.9 so i've removed the json pump
- fixed data format in request for Line Protocol
- inlfuxdb now responds with a 204 not a 200
#!/usr/bin/python
import json
import math
import requests
import sys
from time import sleep
DATABASE = 'test1'
STATUS_MOD = 10
n = 0
while True:
for d in range(0, 360):
v = 'sin val=%s' % math.sin(math.radians(d))
r = requests.post("http://localhost:8086/write?db=test1", data=v)
if r.status_code != 204:
print 'Failed to add point to influxdb -- aborting.'
sys.exit(1)
n += 1
sleep(1)
if n % STATUS_MOD == 0:
print '%d points inserted.' % n
r = requests.post("http://localhost:8086/write?db=" + DATABASE, data=v)
Thanks all -- yes, this code is quite old now and InfluxDB's protocols have moved on.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Check out the blog post at http://www.philipotoole.com/influxdb-and-grafana-howto for full details on how to use this program.