https://grafana.com/docs/grafana/latest/installation/docker/
influxdbと連携させたいので、influxdb も一緒にインストール。
docker-compose.yml
version: "3" services: influxdb: image: influxdb:latest container_name: local-influxdb # grafanaからの接続用にコンテナ名を明示しておく hostname: local-influxdb ports: - "8086:8086" volumes: - ./influxdb/data:/var/lib/influxdb grafana: image: grafana/grafana:latest ports: - "3000:3000" volumes: - ./grafana/dashboards:/etc/grafana/provisioning/dashboards - ./grafana/datasources:/etc/grafana/provisioning/datasources depends_on: - influxdb environment: - GF_SERVER_ROOT_URL=http://localhost:3000 - GF_SECURITY_ADMIN_PASSWORD=admin
起動
docker-compose up -d Starting influxdb_grafana_influxdb_1 ... done Recreating influxdb_grafana_grafana_1 ... done
influxdbのコンテナに入ってDBを作成しておく
docker exec -it `docker ps | awk '/8086/ {print $1}'` /bin/bash # influx -execute "create database mydb1" # exit
python3 -m venv venv source venv/bin/activate pip install influxdb
create_sample_data.py
import random from datetime import datetime from influxdb import InfluxDBClient if __name__ == '__main__': records = [] client = InfluxDBClient('localhost', 8086, 'admin', 'admin', 'mydb1') ts = datetime.strptime("2019-12-01T00:00:00", "%Y-%m-%dT%H:%M:%S") for i in range(6*24): date_text = ts.strftime("%Y-%m-%dT%H:%M:%SZ") records.append({ "measurement": "cpu_load_sample", "tags": { "host": "server01", }, "time": date_text, "fields": { "value": random.random() } }) next_ts = ts.timestamp() + 60 * 10 ts = datetime.fromtimestamp(next_ts) client.write_points(records) result = client.query('select value from cpu_load_short;') print("Result: {0}".format(result))