Skip to content

Instantly share code, notes, and snippets.

@otoolep
Last active December 20, 2016 01:07
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save otoolep/3d5741e680bf76021f77 to your computer and use it in GitHub Desktop.
Save otoolep/3d5741e680bf76021f77 to your computer and use it in GitHub Desktop.
Pump a sine way into a local influxdb cluster
#!/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
@otoolep
Copy link
Author

otoolep commented Jun 11, 2014

Check out the blog post at http://www.philipotoole.com/influxdb-and-grafana-howto for full details on how to use this program.

@jimmystewpot
Copy link

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.

influxdata/influxdb#2127

@nekoyokoshima
Copy link

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

@ThomDietrich
Copy link

r = requests.post("http://localhost:8086/write?db=" + DATABASE, data=v)

@ThomDietrich
Copy link

@otoolep
Copy link
Author

otoolep commented Dec 20, 2016

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