|
| 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