Created
June 18, 2018 09:42
-
-
Save sandlbn/53d1cd2903cd764d74fb6db12c134b9b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
"""Tutorial on using the InfluxDB client.""" | |
import argparse | |
from datetime import datetime | |
from influxdb import InfluxDBClient | |
def build_json(tags, value, current_time): | |
return [ | |
{ | |
"measurement": "disturbance", | |
"tags": tags, | |
"time": current_time, | |
"fields": { | |
"value": value | |
} | |
} | |
] | |
def main(value, dis_type, dis_amount): | |
"""Instantiate a connection to the InfluxDB.""" | |
user = 'admin' | |
password = 'admin' | |
dbname = 'network' | |
host = 'localhost' | |
port = 8086 | |
current_time = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ') | |
client = InfluxDBClient(host, port, user, password, dbname) | |
tags = {} | |
tags["dis_type"] = dis_type | |
tags["dis_amount"] = dis_amount | |
json_body = build_json(tags, value, current_time) | |
print("Write points: {0}".format(json_body)) | |
client.write_points(json_body) | |
def parse_args(): | |
"""Parse the args.""" | |
parser = argparse.ArgumentParser( | |
description='') | |
parser.add_argument('--dis_amount', type=str, required=True, | |
default="test", | |
help='Tag') | |
parser.add_argument('--value', type=int, required=True, | |
default=0, | |
help='Value') | |
parser.add_argument('--dis_type', type=str, required=True, | |
default="", | |
help='Distrurbance Type') | |
return parser.parse_args() | |
if __name__ == '__main__': | |
args = parse_args() | |
main(value=args.value, dis_type=args.dis_type, dis_amount=args.dis_amount) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment