Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

Commit d5d1249

Browse files
shantanoo-desaixginn8
authored andcommitted
Add Example for sending information to DB via UDP (#648)
Due to lack of documentation for UDP, this example provides basic usage of sending information points via UDP. The code structure followed is similar, if not same as other examples in the `examples` directory. Signed-off-by: Shantanoo <shantanoo.desai@gmail.com>
1 parent dc54e5e commit d5d1249

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

docs/source/examples.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,9 @@ Tutorials - SeriesHelper
2525

2626
.. literalinclude:: ../../examples/tutorial_serieshelper.py
2727
:language: python
28+
29+
Tutorials - UDP
30+
===============
31+
32+
.. literalinclude:: ../../examples/tutorial_udp.py
33+
:language: python

examples/tutorial_udp.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# -*- coding: utf-8 -*-
2+
"""Example for sending batch information to InfluxDB via UDP."""
3+
4+
"""
5+
INFO: In order to use UDP, one should enable the UDP service from the
6+
`influxdb.conf` under section
7+
[[udp]]
8+
enabled = true
9+
bind-address = ":8089" # port number for sending data via UDP
10+
database = "udp1" # name of database to be stored
11+
[[udp]]
12+
enabled = true
13+
bind-address = ":8090"
14+
database = "udp2"
15+
"""
16+
17+
18+
import argparse
19+
20+
from influxdb import InfluxDBClient
21+
22+
23+
def main(uport):
24+
"""Instantiate connection to the InfluxDB."""
25+
# NOTE: structure of the UDP packet is different than that of information
26+
# sent via HTTP
27+
json_body = {
28+
"tags": {
29+
"host": "server01",
30+
"region": "us-west"
31+
},
32+
"time": "2009-11-10T23:00:00Z",
33+
"points": [{
34+
"measurement": "cpu_load_short",
35+
"fields": {
36+
"value": 0.64
37+
}
38+
},
39+
{
40+
"measurement": "cpu_load_short",
41+
"fields": {
42+
"value": 0.67
43+
}
44+
}]
45+
}
46+
47+
# make `use_udp` True and add `udp_port` number from `influxdb.conf` file
48+
# no need to mention the database name since it is already configured
49+
client = InfluxDBClient(use_udp=True, udp_port=uport)
50+
51+
# Instead of `write_points` use `send_packet`
52+
client.send_packet(json_body)
53+
54+
55+
def parse_args():
56+
"""Parse the args."""
57+
parser = argparse.ArgumentParser(
58+
description='example code to play with InfluxDB along with UDP Port')
59+
parser.add_argument('--uport', type=int, required=True,
60+
help=' UDP port of InfluxDB')
61+
return parser.parse_args()
62+
63+
64+
if __name__ == '__main__':
65+
args = parse_args()
66+
main(uport=args.uport)

0 commit comments

Comments
 (0)