Influxdb - 測試腳本

1. 啟動influxdb相關服務并測試

創(chuàng)建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,監(jiān)控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,監(jiān)控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,監(jiān)控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)
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容