InfluxDB

Influxdb - 测试脚本

2019-09-17  本文已影响0人  红薯爱帅

1. 启动influxdb相关服务并测试

创建network

$ docker network create influxdb

启动influxdb和chronograf

$ docker run --name influxdb \
    --net influxdb \
    -p 8083:8083 -p 8086:8086 -d influxdb
$ docker run -p 8888:8888 \
    --net influxdb \
    --name chronograf -d \
    chronograf --influxdb-url=http://influxdb:8086

启动grafana,须在UI中配置influxdb数据源

$ docker run -d --name=grafana \
      --net=influxdb \
      -p 3000:3000 grafana/grafana

启动telegraf,监控mysql and redis, and supporting statsd service

$ docker run --name some-redis --net influxdb -d redis
$ docker run --name some-mysql --net influxdb -e MYSQL_ROOT_PASSWORD=123456 -d mysql

$ docker run --rm telegraf telegraf config > telegraf.mysql.redis.statsd.conf
# 编辑conf,添加mysql和redis地址

$ docker run -d --name telegraf.statsd \
      --net influxdb \
      -p 8125:8125/udp \
      -v $PWD/telegraf.mysql.redis.statsd.conf:/etc/telegraf/telegraf.conf:ro \
      -d telegraf

# 测试statsd
$ for i in {1..50}; do echo $i;echo "foo:1|c" | nc -u -w0 127.0.0.1 8125; done

启动telegraf,监控system各项指标

$ docker run -d --name telegraf.system \
      --net influxdb \
      -e HOST_PROC=/host/proc \
      -v /proc:/host/proc:ro \
      -v $PWD/telegraf.conf:/etc/telegraf/telegraf.conf:ro \
      -d telegraf

启动telegraf,监控docker各项指标

$ docker run -d --name=telegraf.docker \
      --net=influxdb \
      -v /var/run/docker.sock:/var/run/docker.sock \
      -v $PWD/telegraf.docker.conf:/etc/telegraf/telegraf.conf:ro \
      telegraf

2. 通过python操作influxdb

# -*- coding: utf-8 -*-
"""Tutorial on using the InfluxDB client."""

import argparse

from influxdb import InfluxDBClient


def main(host='localhost', port=8086):
    """Instantiate a connection to the InfluxDB."""
    user = 'root'
    password = 'root'
    dbname = 'example'
    dbuser = 'smly'
    dbuser_password = 'my_secret_password'
    query = 'select value from cpu_load_short;'
    json_body = [
        {
            "measurement": "cpu_load_short",
            "tags": {
                "host": "server01",
                "region": "us-west"
            },
            "time": "2009-11-10T23:00:00Z",
            "fields": {
                "Float_value": 0.64,
                "Int_value": 3,
                "String_value": "Text",
                "Bool_value": True
            }
        }
    ]

    client = InfluxDBClient(host, port, user, password, dbname)

    print("Create database: " + dbname)
    client.create_database(dbname)

    print("Create a retention policy")
    client.create_retention_policy('awesome_policy', '3d', 3, default=True)

    print("Switch user: " + dbuser)
    client.switch_user(dbuser, dbuser_password)

    print("Write points: {0}".format(json_body))
    client.write_points(json_body)

    print("Querying data: " + query)
    result = client.query(query)

    print("Result: {0}".format(result))

    print("Switch user: " + user)
    client.switch_user(user, password)

    print("Drop database: " + dbname)
    client.drop_database(dbname)


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)
上一篇下一篇

猜你喜欢

热点阅读