Skip to content

Instantly share code, notes, and snippets.

@otoolep otoolep/sine.py
Last active Dec 20, 2016

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner 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

This comment has been minimized.

Copy link

jimmystewpot commented May 12, 2015

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

This comment has been minimized.

Copy link

nekoyokoshima commented Aug 18, 2015

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

This comment has been minimized.

Copy link

ThomDietrich commented Feb 25, 2016

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

This comment has been minimized.

@otoolep

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.