File: influxdb_18_example.py

package info (click to toggle)
python-influxdb-client 1.40.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,216 kB
  • sloc: python: 60,236; sh: 64; makefile: 53
file content (28 lines) | stat: -rw-r--r-- 873 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from influxdb_client import InfluxDBClient, Point

username = 'username'
password = 'password'

database = 'telegraf'
retention_policy = 'autogen'

bucket = f'{database}/{retention_policy}'

with InfluxDBClient(url='http://localhost:8086', token=f'{username}:{password}', org='-') as client:

    with client.write_api() as write_api:
        print('*** Write Points ***')

        point = Point("mem").tag("host", "host1").field("used_percent", 25.43234543)
        print(point.to_line_protocol())

        write_api.write(bucket=bucket, record=point)

    print('*** Query Points ***')

    query_api = client.query_api()
    query = f'from(bucket: \"{bucket}\") |> range(start: -1h)'
    tables = query_api.query(query)
    for record in tables[0].records:
        print(f'#{record.get_time()} #{record.get_measurement()}: #{record.get_field()} #{record.get_value()}')