Grafana基础:配置文件与说明

这篇文章以6.5.1版本的Grafana为例,对于Grafana配置文件的使用进行介绍。

事前准备

本文使用Docker方式准备Grafana环境,执行如下命令即可启动Grafana服务

执行命令:docker run -d -p 3000:3000 --name grafana grafana/grafana:6.5.1

配置文件说明

缺省配置文件

项目设定值
默认配置文件目录/usr/share/grafana/conf
默认配置文件名称defaults.ini
liumiaocn:config liumiao$ docker exec -it grafana ls /etc/grafana/grafana.ini
/etc/grafana/grafana.ini
liumiaocn:config liumiao$ 

注意:分号是ini配置文件的标准注释方式。

配置文件设定方式

  • 方式1: 使用--config指定配置文件
  • 方式2: 通过环境变量GF_PATHS_CONFIG指定配置文件

比如官方Grafana 镜像中这两种方式都设定为同一值,配置文件的目录信息如下所示:

项目设定值
配置文件目录/etc/grafana
配置文件名称grafana.ini
liumiaocn:config liumiao$ docker exec -it grafana sh
/usr/share/grafana $ env |grep PATHS_CONFIG
GF_PATHS_CONFIG=/etc/grafana/grafana.ini
/usr/share/grafana $ ps -ef |grep -v ps |grep -v -w sh
PID   USER     TIME  COMMAND1 grafana   0:09 grafana-server --homepath=/usr/share/grafana --config=/etc/grafana/grafana.ini --packaging=docker cfg:default.log.mode=console cfg:default.paths.data=/var/lib/grafana cfg:default.paths.logs=/var/log/grafana cfg:default.paths.plugins=/var/lib/grafana/plugins cfg:default.paths.provisioning=/etc/grafana/provisioning
/usr/share/grafana $ 

配置文件设定格式

配置文件的格式如下所示

[段名称]
键的名称 = 键的设定值
...

环境变量的使用方式

所有在配置文件设定的变量都可以通过环境变量来进行覆盖,相应的环境变量的名称规则如下所示:

环境变量映射规则:GF_段名称_键的名称

以Grafana启动时缺省的用户名和密码的设定为例,配置文件中可通过如下设定登录的用户名和密码为admin/admin.

[security]
# default admin user, created on startup
admin_user = admin# default admin password, can be changed before first start of grafana,  or in profile settings
admin_password = admin

在使用的时候,比如可以通过Dockerfile或者启动的docker run命令中指定如下环境变量来进行设定:

  • 设定管理用户名名称的环境变量:GF_SECURITY_ADMIN_USER

部分配置文件设定

缺省段设定

设定项缺省设定详细说明
app_modeproduction应用模式,可设定为production或者 development
instance_nameHOSTNAME环境变量值示例名称,缺省设定为HOSTNAME环境变量,如果其值为空则会取hostname命令的值

paths段设定

设定项缺省设定详细说明
data/var/lib/grafana主要设定Grafana保存临时文件、会话相关的目录信息,如果使用了sqlite3时,也会在此段中进行设定
temp_data_lifetime24h临时数据的存续时间,data目录中的数据在多长时间之后会被删除
logs/var/log/grafana存放Grafana日志信息的目录
plugins/var/lib/grafana/plugins插件目录,确认插件信息时Grafana会自动扫描和查询此目录
provisioningconf/provisioning存放Grafana配置文件信息的目录

server段设定

设定项缺省设定详细说明
protocolhttp支持http、https、h2和socket方式
http_addr-绑定的IP地址,为空时表示不做限制
http_port3000对外服务的端口号
domainlocalhost服务的域名
enforce_domainfalse如果报文头部的信息和域信息不匹配时重定向到正确域名以预防DNS重绑定攻击
root_urlhttp://localhost:3000服务的URL地址
serve_from_sub_pathfalse出于兼容性的原因缺省设定为false
router_loggingfalse是否记录Web的日志请求
static_root_pathpublic相对工作目录
enable_gzipfalse是否启用gzip
cert_file-证书文件(协议为https时的设定项)
cert_key-证书密钥(协议为https时的设定项)
socket-(协议为socket时的设定项)

database段设定

设定项缺省设定详细说明
typesqlite3支持的数据库类型包括mysql、postgres以及sqlite3
host127.0.0.1:3306机器名称和端口号,mysql和postgres需要的设定选项
namegrafana实例名称
userroot用户名称
password-用户密码,如果包含#或者;时,需要使用三个双引号将其引起来
url-数据库连接的URL设定,比如mysql://user:secret@host:port/database
ssl_modedisablepostgres的设定选项,可设定为disable、require或者verify-full
pathgrafana.dbsqlite3的设定选项,相对于data_path的相对路径
max_idle_conn2最大的空闲的连接数
max_open_conn0|最大连接数量,0表示不做限定
conn_max_lifetime14400连接的最大市场,单位为秒,缺省为14400秒(4个小时)
log_queries-设定为true时会记录sql调用和执行时间
cache_modeprivatesqlite3的设定选项,数据库连接的缓存模式设定,可设定为private或者shared

remote_cache段设定

设定项缺省设定详细说明
typedatabase缓存类型,可设定为database、redis或者memcached
connstr-缓存连接选项,设定为database时会使用Grafana的数据库,使用redis的设定示例为:addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false,使用memcache的设定示例为:127.0.0.1:11211

auth.anonymous段设定

设定项缺省设定详细说明
enabledfalse是否允许匿名登录
org_nameMain Org.无需认证的组织名称
org_roleViewer无需认证的角色信息

设定文件及说明示例

以官方的Grafana的配置文件为例,配置文件中对各个选项进行了说明,6.5.1版本的设定文件如下所示:

liumiaocn:config liumiao$ docker exec -it grafana cat /etc/grafana/grafana.ini
##################### Grafana Configuration Example #####################
#
# Everything has defaults so you only need to uncomment things you want to
# change# possible values : production, development
;app_mode = production# instance name, defaults to HOSTNAME environment variable value or hostname if HOSTNAME var is empty
;instance_name = ${HOSTNAME}#################################### Paths ####################################
[paths]
# Path to where grafana can store temp files, sessions, and the sqlite3 db (if that is used)
;data = /var/lib/grafana# Temporary files in `data` directory older than given duration will be removed
;temp_data_lifetime = 24h# Directory where grafana can store logs
;logs = /var/log/grafana# Directory where grafana will automatically scan and look for plugins
;plugins = /var/lib/grafana/plugins# folder that contains provisioning config files that grafana will apply on startup and while running.
;provisioning = conf/provisioning#################################### Server ####################################
[server]
# Protocol (http, https, h2, socket)
;protocol = http# The ip address to bind to, empty will bind to all interfaces
;http_addr =# The http port  to use
;http_port = 3000# The public facing domain name used to access grafana from a browser
;domain = localhost# Redirect to correct domain if host header does not match domain
# Prevents DNS rebinding attacks
;enforce_domain = false# The full public facing url you use in browser, used for redirects and emails
# If you use reverse proxy and sub path specify full url (with sub path)
;root_url = http://localhost:3000# Serve Grafana from subpath specified in `root_url` setting. By default it is set to `false` for compatibility reasons.
;serve_from_sub_path = false# Log web requests
;router_logging = false# the path relative working path
;static_root_path = public# enable gzip
;enable_gzip = false# https certs & key file
;cert_file =
;cert_key =# Unix socket path
;socket =#################################### Database ####################################
[database]
# You can configure the database connection by specifying type, host, name, user and password
# as separate properties or as on string using the url properties.# Either "mysql", "postgres" or "sqlite3", it's your choice
;type = sqlite3
;host = 127.0.0.1:3306
;name = grafana
;user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
;password =# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
;url =# For "postgres" only, either "disable", "require" or "verify-full"
;ssl_mode = disable# For "sqlite3" only, path relative to data_path setting
;path = grafana.db# Max idle conn setting default is 2
;max_idle_conn = 2# Max conn setting default is 0 (mean not set)
;max_open_conn =# Connection Max Lifetime default is 14400 (means 14400 seconds or 4 hours)
;conn_max_lifetime = 14400# Set to true to log the sql calls and execution times.
;log_queries =# For "sqlite3" only. cache mode setting used for connecting to the database. (private, shared)
;cache_mode = private#################################### Cache server #############################
[remote_cache]
# Either "redis", "memcached" or "database" default is "database"
;type = database# cache connectionstring options
# database: will use Grafana primary database.
# redis: config like redis server e.g. `addr=127.0.0.1:6379,pool_size=100,db=0,ssl=false`. Only addr is required. ssl may be 'true', 'false', or 'insecure'.
# memcache: 127.0.0.1:11211
;connstr =#################################### Data proxy ###########################
[dataproxy]# This enables data proxy logging, default is false
;logging = false# How long the data proxy should wait before timing out default is 30 (seconds)
;timeout = 30# If enabled and user is not anonymous, data proxy will add X-Grafana-User header with username into the request, default is false.
;send_user_header = false#################################### Analytics ####################################
[analytics]
# Server reporting, sends usage counters to stats.grafana.org every 24 hours.
# No ip addresses are being tracked, only simple counters to track
# running instances, dashboard and error counts. It is very helpful to us.
# Change this option to false to disable reporting.
;reporting_enabled = true# Set to false to disable all checks to https://grafana.net
# for new vesions (grafana itself and plugins), check is used
# in some UI views to notify that grafana or plugin update exists
# This option does not cause any auto updates, nor send any information
# only a GET request to http://grafana.com to get latest versions
;check_for_updates = true# Google Analytics universal tracking code, only enabled if you specify an id here
;google_analytics_ua_id =# Google Tag Manager ID, only enabled if you specify an id here
;google_tag_manager_id =#################################### Security ####################################
[security]
# disable creation of admin user on first start of grafana
;disable_initial_admin_creation = false# default admin user, created on startup
;admin_user = admin# default admin password, can be changed before first start of grafana,  or in profile settings
;admin_password = admin# used for signing
;secret_key = SW2YcwTIb9zpOOhoPsMm# disable gravatar profile images
;disable_gravatar = false# data source proxy whitelist (ip_or_domain:port separated by spaces)
;data_source_proxy_whitelist =# disable protection against brute force login attempts
;disable_brute_force_login_protection = false# set to true if you host Grafana behind HTTPS. default is false.
;cookie_secure = false# set cookie SameSite attribute. defaults to `lax`. can be set to "lax", "strict" and "none"
;cookie_samesite = lax# set to true if you want to allow browsers to render Grafana in a ,