安装Grafana

警告
本文最后更新于 2022-07-01,文中内容可能已过时。

Grafana allows you to query, visualize, alert on and understand your metrics no matter where they are stored. Create, explore, and share dashboards with your team and foster a data-driven culture:

  • Visualizations: Fast and flexible client side graphs with a multitude of options. Panel plugins offer many different ways to visualize metrics and logs.
  • Dynamic Dashboards: Create dynamic & reusable dashboards with template variables that appear as dropdowns at the top of the dashboard.
  • Explore Metrics: Explore your data through ad-hoc queries and dynamic drilldown. Split view and compare different time ranges, queries and data sources side by side.
  • Explore Logs: Experience the magic of switching from metrics to logs with preserved label filters. Quickly search through all your logs or streaming them live.
  • Alerting: Visually define alert rules for your most important metrics. Grafana will continuously evaluate and send notifications to systems like Slack, PagerDuty, VictorOps, OpsGenie.
  • Mixed Data Sources: Mix different data sources in the same graph! You can specify a data source on a per-query basis. This works for even custom datasources.
1
2
3
4
wget https://dl.grafana.com/oss/release/grafana-6.5.2.linux-amd64.tar.gz

tar -zxf grafana-6.5.2.linux-amd64.tar.gz
mv grafana-6.5.2 /usr/local/grafana
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
cat > /usr/lib/systemd/system/grafana-server.service <<EOF
[Unit]
Description=Grafana
After=network.target
[Service]
Type=notify
ExecStart=/usr/local/grafana/bin/grafana-server -homepath /usr/local/grafana
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
1
2
systemctl enable grafana-server.service
systemctl start grafana-server.service

https://img.bwcxtech.com/img/20200928171740.png

点击 Add data source,选择Prometheus,在URL输入框键入http://localhost:9090,点击save & test,如果出现下图中的绿色提示,则表示配置有效,否则可能是地址或者端口等其他错误,需要自行修改。

https://img.bwcxtech.com/img/20200928171712.png

下载https://grafana.com/grafana/dashboards/9276或者https://grafana.com/grafana/dashboards/8919

导入模板

https://img.bwcxtech.com/img/20200928171600.png

https://img.bwcxtech.com/img/20200928171607.png

效果图

https://img.bwcxtech.com/img/20200928171633.png

添加Nginx配置,proxy_pass后面一定要有"/"(用以去掉/grafana/匹配本身)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
server {
    listen 80;
        server_name localhost;

    location /grafana/ {
        proxy_pass       http://localhost:3000/;
        proxy_buffering off;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header Host $host;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        access_log off;
    }
}

修改grafana配置(grafana.ini),需要去掉行前的";"

1
2
3
[server]
domain = 你的域名
root_url = %(protocol)s://%(domain)s/grafana/
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
docker run -d \
-p 3000:3000 \
-e TZ=Asia/Shanghai \
-e GF_DATABASE_TYPE=mysql \
-e GF_DATABASE_HOST=127.0.0.1:3306 \
-e GF_DATABASE_NAME=grafana \
-e GF_DATABASE_USER=root \
-e GF_DATABASE_PASSWORD=root \
-e GF_PLUGINS_ENABLE_ALPHA=true \
-e "GF_INSTALL_PLUGINS=grafana-piechart-panel,grafana-simple-json-datasource" \
grafana/grafana

Grafana具有默认和自定义配置文件。您可以通过修改自定义配置文件或使用环境变量来自定义 Grafana 实例。

不能使用环境变量添加新的配置设置。

1
GF_<SectionName>_<KeyName>

其中节点名称是括号内的文本。 所有内容都应该被替换为大写。 例如,如果您有以下配置设置:. - _

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#################################### 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 property.

# Either "mysql", "postgres" or "sqlite3", it's your choice
type = mysql
host = grafana-mysql:3306
name = grafana
user = root
# If the password contains # or ; you have to wrap it with triple quotes. Ex """#password;"""
password = 123456
# Use either URL or the previous fields to configure the database
# Example: mysql://user:secret@host:port/database
url = mysql://root:123456@grafana-mysql:3306/grafana

#################################### Server ##############################
[server]
# The public facing domain name used to access grafana from a browser
domain = monitor.example.com

#################################### Anonymous Auth ######################
[auth.anonymous]
# enable anonymous access
enabled = true

# specify organization name that should be used for unauthenticated users
org_name = Main Org.

# specify role for unauthenticated users
org_role = Viewer

您可以使用以下命令在 Linux 计算机上覆盖它们:

1
2
3
4
5
6
7
8
9
export GF_DATABASE_TYPE=mysql
export GF_DATABASE_HOST=grafana-mysql:3306
export GF_DATABASE_NAME=grafana
export GF_DATABASE_USER=root
export GF_DATABASE_PASSWORD=123456
export GF_SERVER_DOMAIN=monitor.example.com
export GF_AUTH_ANONYMOUS_enabled=true
export GF_AUTH_ANONYMOUS_ORG_NAME=Test.
export GF_AUTH_ANONYMOUS_ORG_ROLE=Viewer

ES Nginx Logs