From e133e3ffb333c5a5361b54f2341aefb7b494af75 Mon Sep 17 00:00:00 2001 From: Shantanoo Date: Thu, 4 Oct 2018 19:10:03 +0200 Subject: [PATCH 1/4] Add Example for sending information to DB via UDP 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. Locally tested with: InfluxDB v1.6.1 on Windows10 and influxdb-python v5.0.0 and should not break anything Signed-off-by: Shantanoo --- examples/tutorial_udp.py | 64 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 examples/tutorial_udp.py diff --git a/examples/tutorial_udp.py b/examples/tutorial_udp.py new file mode 100644 index 00000000..7cf49f4f --- /dev/null +++ b/examples/tutorial_udp.py @@ -0,0 +1,64 @@ +# -*- coding: utf-8 -*- +"""Example for sending batch information to InfluxDB via UDP""" + +""" +INFO: In order to use UDP, one should enable the UDP service from the +`influxdb.conf` under section + [[udp]] + enabled = true + bind-address = ":8089" # port number for sending data via UDP + database = "example-udp" # name of database to be stored +""" + + +import argparse + +from influxdb import InfluxDBClient + +def main(host='localhost', port=8086): + """Instantiate connection to the InfluxDB.""" + + # NOTE: structure of the UDP packet is different than that of information + # sent via HTTP + json_body = { + "tags": { + "host": "server01", + "region": "us-west" + }, + "time": "2009-11-10T23:00:00Z", + "points": [{ + "measurement": "cpu_load_short", + "fields": { + "value": 0.64 + } + }, + { + "measurement": "cpu_load_short", + "fields": { + "value": 0.67 + } + } + ] + } + + # make `use_udp` True and add `udp_port` number from `influxdb.conf` file + # no need to mention the database name since it is already configured + client = InfluxDBClient(host, port, use_udp=True, udp_port=8089) + + # Instead of `write_points` use `send_packet` + client.send_packet(json_body) + +def parse_args(): + """Parse the args.""" + parser = argparse.ArgumentParser( + description='example code to play with InfluxDB') + parser.add_argument('--host', type=str, required=False, + default='localhost', + help='hostname of InfluxDB http API') + parser.add_argument('--port', type=int, required=False, default=8086, + help='port of InfluxDB http API') + return parser.parse_args() + +if __name__ == '__main__': + args = parse_args() + main(host=args.host, port=args.port) From 830364068cb737548b9b16b75dacc94438ccfb2a Mon Sep 17 00:00:00 2001 From: Shantanoo Date: Sun, 31 Mar 2019 13:30:58 -0700 Subject: [PATCH 2/4] Add Documentation for UDP Example - This commit adds the `tutorial_udp.py`'s Documentation to the existing documentation sourcecode. Signed-off-by: Shantanoo --- docs/source/examples.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/source/examples.rst b/docs/source/examples.rst index 2c85fbda..fdda62a9 100644 --- a/docs/source/examples.rst +++ b/docs/source/examples.rst @@ -25,3 +25,9 @@ Tutorials - SeriesHelper .. literalinclude:: ../../examples/tutorial_serieshelper.py :language: python + +Tutorials - UDP +=============== + +.. literalinclude:: ../../examples/tutorial_udp.py + :language: python From 0af9b5d126e0fda062340e07a6fe677b73fd1f76 Mon Sep 17 00:00:00 2001 From: Shantanoo Date: Sun, 31 Mar 2019 13:59:55 -0700 Subject: [PATCH 3/4] Adapt example to proper pydocstyle - Travis Build 1500.37 failed for: `Python3.6 and TOX_ENV=pep257` - Improve Docstring within code to overcome build error Signed-off-by: Shantanoo --- examples/tutorial_udp.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/examples/tutorial_udp.py b/examples/tutorial_udp.py index 7cf49f4f..ecfc9380 100644 --- a/examples/tutorial_udp.py +++ b/examples/tutorial_udp.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -"""Example for sending batch information to InfluxDB via UDP""" +"""Example for sending batch information to InfluxDB via UDP.""" """ INFO: In order to use UDP, one should enable the UDP service from the @@ -17,7 +17,6 @@ def main(host='localhost', port=8086): """Instantiate connection to the InfluxDB.""" - # NOTE: structure of the UDP packet is different than that of information # sent via HTTP json_body = { From c7425c0ac7ecb565a8d585ce509ba40dc8df5fbf Mon Sep 17 00:00:00 2001 From: Shantanoo Date: Wed, 10 Apr 2019 13:11:21 +0200 Subject: [PATCH 4/4] Improve `tutorial_udp.py` example - Instead of providing arguments of HTTP InfluxDB, provide UDP Port as argument - Improve the Comments to distinguish between different UDP Ports (e.g. 8089(default) saves to `udp1` and 8090 saves to `udp2` databases respectively Signed-off-by: Shantanoo --- examples/tutorial_udp.py | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/examples/tutorial_udp.py b/examples/tutorial_udp.py index 7cf49f4f..851e753f 100644 --- a/examples/tutorial_udp.py +++ b/examples/tutorial_udp.py @@ -7,7 +7,11 @@ [[udp]] enabled = true bind-address = ":8089" # port number for sending data via UDP - database = "example-udp" # name of database to be stored + database = "udp1" # name of database to be stored + [[udp]] + enabled = true + bind-address = ":8090" + database = "udp2" """ @@ -15,7 +19,8 @@ from influxdb import InfluxDBClient -def main(host='localhost', port=8086): + +def main(uport): """Instantiate connection to the InfluxDB.""" # NOTE: structure of the UDP packet is different than that of information @@ -29,36 +34,34 @@ def main(host='localhost', port=8086): "points": [{ "measurement": "cpu_load_short", "fields": { - "value": 0.64 + "value": 0.64 } }, - { - "measurement": "cpu_load_short", - "fields": { - "value": 0.67 - } - } - ] + { + "measurement": "cpu_load_short", + "fields": { + "value": 0.67 + } + }] } # make `use_udp` True and add `udp_port` number from `influxdb.conf` file # no need to mention the database name since it is already configured - client = InfluxDBClient(host, port, use_udp=True, udp_port=8089) + client = InfluxDBClient(use_udp=True, udp_port=uport) # Instead of `write_points` use `send_packet` client.send_packet(json_body) + def parse_args(): """Parse the args.""" parser = argparse.ArgumentParser( - description='example code to play with InfluxDB') - parser.add_argument('--host', type=str, required=False, - default='localhost', - help='hostname of InfluxDB http API') - parser.add_argument('--port', type=int, required=False, default=8086, - help='port of InfluxDB http API') + description='example code to play with InfluxDB along with UDP Port') + parser.add_argument('--uport', type=int, required=True, + help=' UDP port of InfluxDB') return parser.parse_args() + if __name__ == '__main__': args = parse_args() - main(host=args.host, port=args.port) + main(uport=args.uport)